Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Product reviews API #8772

Merged
merged 24 commits into from
Jan 5, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
0f6966a
[Product Reviews API] - add API for product reviews
paulstoicareea Oct 4, 2017
c660fa9
[Documentation][API] - add product reviews api docs
paulstoicareea Oct 4, 2017
fb337a6
[Product Reviews API]: accept/reject endpoints with state machine
paulstoicareea Oct 5, 2017
0e2ccc9
[Unit Testing]: ProductReviewApi Controller
paulstoicareea Oct 5, 2017
0151d20
[Documentation]: add in toctree and map Product Reviews API
paulstoicareea Oct 5, 2017
9785c0e
[Documentation]: minor changes
paulstoicareea Oct 9, 2017
4aa26f9
[Product Reviews API] - minor changes
paulstoicareea Oct 9, 2017
de75e27
[Documentation]: fix malformed tables Product reviews API
paulstoicareea Oct 9, 2017
ca4a90a
[Product Reviews API] - minor changes for tests
paulstoicareea Oct 9, 2017
3c1048f
[Product Reviews API] - add API for product reviews
paulstoicareea Oct 4, 2017
bc07098
[Documentation][API] - add product reviews api docs
paulstoicareea Oct 4, 2017
5e41ae6
[Product Reviews API]: accept/reject endpoints with state machine
paulstoicareea Oct 5, 2017
e998b59
[Unit Testing]: ProductReviewApi Controller
paulstoicareea Oct 5, 2017
dd37af3
[Documentation]: add in toctree and map Product Reviews API
paulstoicareea Oct 5, 2017
0228602
[Documentation]: minor changes
paulstoicareea Oct 9, 2017
d571b9c
[Product Reviews API] - minor changes
paulstoicareea Oct 9, 2017
3933ed2
[Documentation]: fix malformed tables Product reviews API
paulstoicareea Oct 9, 2017
3cf247a
[Product Reviews API] - minor changes for tests
paulstoicareea Oct 9, 2017
859021c
[Documentation]: texts improvments Product reviews API
paulstoicareea Nov 17, 2017
ca795cb
Product review API syntax improvments
paulstoicareea Nov 17, 2017
78d2c96
Product Review API tests: review creation real inputs
paulstoicareea Nov 18, 2017
aca53a4
[Product Reviews API] - add API for product reviews
paulstoicareea Oct 4, 2017
27086bd
fix product reviews api test methods names, expected response
paulstoicareea Dec 5, 2017
e386f37
product reviews api test remove author, fix indentation
paulstoicareea Jan 4, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[Unit Testing]: ProductReviewApi Controller
  • Loading branch information
paulstoicareea committed Nov 17, 2017
commit e998b59df2c0d9d9443a0c00a3627a7fb22ff779
359 changes: 359 additions & 0 deletions tests/Controller/ProductReviewApiTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,359 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Tests\Controller;

use Lakion\ApiTestCase\JsonApiTestCase;
use Sylius\Component\Core\Model\ProductInterface;
use Sylius\Component\Core\Model\ProductReview;
use Symfony\Component\HttpFoundation\Response;

/**
* @author Paul Stoica <paul.stoica18@gmail.com>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are not adding author doc blocks anymore. Relevant PR #8882

*/
final class ProductReviewApiTest extends JsonApiTestCase
{
/**
* @var array
*/
private static $authorizedHeaderWithContentType = [
'HTTP_Authorization' => 'Bearer SampleTokenNjZkNjY2MDEwMTAzMDkxMGE0OTlhYzU3NzYyMTE0ZGQ3ODcyMDAwM2EwMDZjNDI5NDlhMDdlMQ',
'CONTENT_TYPE' => 'application/json',
];

/**
* @var array
*/
private static $authorizedHeaderWithAccept = [
'HTTP_Authorization' => 'Bearer SampleTokenNjZkNjY2MDEwMTAzMDkxMGE0OTlhYzU3NzYyMTE0ZGQ3ODcyMDAwM2EwMDZjNDI5NDlhMDdlMQ',
'ACCEPT' => 'application/json',
];

/**
* @test
*/
public function it_does_not_allow_to_show_product_review_list_when_access_is_denied()
{
$productReviewsData = $this->loadFixturesFromFile('resources/product_reviews.yml');

/** @var ProductInterface $product */
$product = $productReviewsData['product1'];

$this->client->request('GET', $this->getReviewListUrl($product));
$response = $this->client->getResponse();

$this->assertResponse($response, 'authentication/access_denied_response', Response::HTTP_UNAUTHORIZED);
}

/**
* @test
*/
public function it_does_not_allow_to_show_product_review_when_it_does_not_exist()
{
$productReviewsData = $this->loadFixturesFromFile('resources/product_reviews.yml');
$this->loadFixturesFromFile('authentication/api_administrator.yml');

/** @var ProductInterface $product */
$product = $productReviewsData['product1'];

$this->client->request('GET', $this->getReviewListUrl($product) . '0', [], [], static::$authorizedHeaderWithAccept);
$response = $this->client->getResponse();

$this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND);
}

/**
* @test
*/
public function it_allows_showing_product_review()
{
$this->loadFixturesFromFile('authentication/api_administrator.yml');
$productReviewsData = $this->loadFixturesFromFile('resources/product_reviews.yml');

/** @var ProductInterface $product */
$product = $productReviewsData['product1'];

/** @var ProductReview $productReview */
$productReview = $productReviewsData['productReview1'];

$this->client->request('GET', $this->getReviewUrl($product, $productReview), [], [], static::$authorizedHeaderWithAccept);
$response = $this->client->getResponse();

$this->assertResponse($response, 'product_review/show_response', Response::HTTP_OK);
}

/**
* @test
*/
public function it_allows_indexing_product_reviews()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing denied access test

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lchrusciel there is a test method: "it_does_not_allows_showing_product_review_list_when_access_is_denied", or what do you mean?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just missed it

{
$this->loadFixturesFromFile('authentication/api_administrator.yml');
$productReviewsData = $this->loadFixturesFromFile('resources/product_reviews.yml');

/** @var ProductInterface $product */
$product = $productReviewsData['product1'];

$this->client->request('GET', $this->getReviewListUrl($product), [], [], static::$authorizedHeaderWithAccept);
$response = $this->client->getResponse();

$this->assertResponse($response, 'product_review/index_response', Response::HTTP_OK);
}

/**
* @test
*/
public function it_allows_create_product_review()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it_allows_creating_new_product_review

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @Zales0123,

For these methods names I checked also tests/Controller/ProductVariantApiTest.php witch have added the same method name's logic: "it_allows_create_product_variant"; it means that for tests/Controller/ProductVariantApiTest.php is also incorect? if not, what is the difference between ProductReviewApiTest.php and ProductVariantApiTest.php, why for one is correct and for the other one, not?

Thank you.
Paul.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the name of the methods the most important requirement is language correctness :) So even if somewhere else method names are named incorrectly (from the grammatical point of view) we should on writing new ones as perfect as possible.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, then I will rewrite, these methods name, with the good ones, for tests/Controller/ProductReviewApiTest.php.

{
$this->loadFixturesFromFile('authentication/api_administrator.yml');
$productReviewsData = $this->loadFixturesFromFile('resources/product_reviews.yml');

/** @var ProductInterface $product */
$product = $productReviewsData['product1'];

$data =
<<<EOT
{
"title": "J_REVIEW",
"rating": "3",
"comment": "J_REVIEW_COMMENT",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't be afraid of using some more natural exemplary data, these are of course not wrong, but less natural to read. Tests are good place to place easter eggs in code (look at our Behat scenarios and I'll see what I mean 😄 )

"author": {
"email": "j@example.com"
}
}
EOT;

$this->client->request('POST', $this->getReviewListUrl($product), [], [], static::$authorizedHeaderWithContentType, $data);
$response = $this->client->getResponse();

$this->assertResponse($response, 'product_review/create_response', Response::HTTP_CREATED);
}

/**
* @test
*/
public function it_does_not_allow_to_create_product_review_without_required_fields()
{
$this->loadFixturesFromFile('authentication/api_administrator.yml');
$productReviewsData = $this->loadFixturesFromFile('resources/product_reviews.yml');

/** @var ProductInterface $product */
$product = $productReviewsData['product1'];

$this->client->request('POST', $this->getReviewListUrl($product), [], [], static::$authorizedHeaderWithContentType, []);
$response = $this->client->getResponse();

$this->assertResponse($response, 'product_review/create_validation_fail_response', Response::HTTP_BAD_REQUEST);
}

/**
* @test
*/
public function it_does_not_allow_delete_product_review_if_it_does_not_exist()
{
$this->loadFixturesFromFile('authentication/api_administrator.yml');
$productReviewsData = $this->loadFixturesFromFile('resources/product_reviews.yml');

/** @var ProductInterface $product */
$product = $productReviewsData['product1'];

$this->client->request('DELETE', $this->getReviewListUrl($product) . '0', [], [], static::$authorizedHeaderWithAccept);
$response = $this->client->getResponse();

$this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND);
}

/**
* @test
*/
public function it_allows_delete_product_review()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it_allows_deleting_... here and below.

{
$this->loadFixturesFromFile('authentication/api_administrator.yml');
$productReviewsData = $this->loadFixturesFromFile('resources/product_reviews.yml');

/** @var ProductInterface $product */
$product = $productReviewsData['product1'];

/** @var ProductReview $productReview */
$productReview = $productReviewsData['productReview1'];

$this->client->request('DELETE', $this->getReviewUrl($product, $productReview), [], [], static::$authorizedHeaderWithContentType, []);

$response = $this->client->getResponse();
$this->assertResponseCode($response, Response::HTTP_NO_CONTENT);

/** @var ProductInterface $product */
$product = $productReviewsData['product1'];

$this->client->request('GET', $this->getReviewUrl($product, $productReview), [], [], static::$authorizedHeaderWithAccept);
$response = $this->client->getResponse();

$this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND);
}

/**
* @test
*/
public function it_allows_updating_information_about_product_review()
{
$this->loadFixturesFromFile('authentication/api_administrator.yml');
$productReviewsData = $this->loadFixturesFromFile('resources/product_reviews.yml');

/** @var ProductInterface $product */
$product = $productReviewsData['product1'];

/** @var ProductReview $productReview */
$productReview = $productReviewsData['productReview1'];

$data =
<<<EOT
{
"title": "NEW_REVIEW_TITLE",
"rating": "1",
"comment": "NEW_REVIEW_COMMENT"
}
EOT;
$this->client->request('PUT', $this->getReviewUrl($product, $productReview), [], [], static::$authorizedHeaderWithContentType, $data);
$response = $this->client->getResponse();

$this->assertResponseCode($response, Response::HTTP_NO_CONTENT);
}

/**
* @test
*/
public function it_allows_updating_partial_information_about_product_review()
{
$this->loadFixturesFromFile('authentication/api_administrator.yml');
$productReviewsData = $this->loadFixturesFromFile('resources/product_reviews.yml');
$this->loadFixturesFromFile('resources/locales.yml');

/** @var ProductInterface $product */
$product = $productReviewsData['product1'];

/** @var ProductReview $productReview */
$productReview = $productReviewsData['productReview1'];

$data =
<<<EOT
{
"comment": "A_NEW_REVIEW_COMMENT"
}
EOT;

$this->client->request('PATCH', $this->getReviewUrl($product, $productReview), [], [], static::$authorizedHeaderWithContentType, $data);
$response = $this->client->getResponse();

$this->assertResponseCode($response, Response::HTTP_NO_CONTENT);
}

/**
* @test
*/
public function it_allows_accept_product_review()
{
$this->loadFixturesFromFile('authentication/api_administrator.yml');
$productReviewsData = $this->loadFixturesFromFile('resources/product_reviews.yml');

/** @var ProductInterface $product */
$product = $productReviewsData['product1'];

/** @var ProductReview $productReview */
$productReview = $productReviewsData['productReview1'];

$this->client->request('PATCH', $this->getReviewUrl($product, $productReview) . '/accept', [], [], static::$authorizedHeaderWithAccept);
$response = $this->client->getResponse();

$this->assertResponse($response, 'product_review/accept_response', Response::HTTP_OK);
}

/**
* @test
*/
public function it_does_not_allows_accept_product_review_if_it_has_not_new_status()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it_does_not_allows_accepting_... and so on.

{
$this->loadFixturesFromFile('authentication/api_administrator.yml');
$productReviewsData = $this->loadFixturesFromFile('resources/product_reviews.yml');

/** @var ProductInterface $product */
$product = $productReviewsData['product1'];

/** @var ProductReview $productReview */
$productReview = $productReviewsData['productReview2'];

$this->client->request('POST', $this->getReviewUrl($product, $productReview) . '/accept', [], [], static::$authorizedHeaderWithAccept);
$response = $this->client->getResponse();

$this->assertResponse($response, 'product_review/change_status_fail_response', Response::HTTP_BAD_REQUEST);
}

/**
* @test
*/
public function it_allows_reject_product_review()
{
$this->loadFixturesFromFile('authentication/api_administrator.yml');
$productReviewsData = $this->loadFixturesFromFile('resources/product_reviews.yml');

/** @var ProductInterface $product */
$product = $productReviewsData['product1'];

/** @var ProductReview $productReview */
$productReview = $productReviewsData['productReview1'];

$this->client->request('PATCH', $this->getReviewUrl($product, $productReview) . '/reject', [], [], static::$authorizedHeaderWithAccept);
$response = $this->client->getResponse();

$this->assertResponse($response, 'product_review/reject_response', Response::HTTP_OK);
}

/**
* @test
*/
public function it_does_not_allows_reject_product_review_if_it_has_not_new_status()
{
$this->loadFixturesFromFile('authentication/api_administrator.yml');
$productReviewsData = $this->loadFixturesFromFile('resources/product_reviews.yml');

/** @var ProductInterface $product */
$product = $productReviewsData['product1'];

/** @var ProductReview $productReview */
$productReview = $productReviewsData['productReview3'];

$this->client->request('POST', $this->getReviewUrl($product, $productReview) . '/accept', [], [], static::$authorizedHeaderWithAccept);
$response = $this->client->getResponse();

$this->assertResponse($response, 'product_review/change_status_fail_response', Response::HTTP_BAD_REQUEST);
}

/**
* @param ProductInterface $product
*
* @return string
*/
private function getReviewListUrl(ProductInterface $product)
{
return sprintf('/api/v1/products/%s/reviews/', $product->getCode());
}

/**
* @param ProductInterface $product
* @param ProductReview $productReview
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ProductReviewInterface

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi,
I tried to find the "ProductReviewInterface" but it does not exists, only "ReviewInterface".
And because this class refers to the Product Review Api, I thought will be a good fit to use "ProductReview" model. Or I should create a interface for ProductReview model?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad, there should be ReviewInterface instead of ProductReview 😃

*
* @return string
*/
private function getReviewUrl(ProductInterface $product, ProductReview $productReview)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private function getReviewUrl(ProductInterface $product, ProductReviewInterface $productReview): string

{
return sprintf('%s%s', $this->getReviewListUrl($product), $productReview->getId());
}
}
Loading