-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Product reviews API #8772
Changes from 1 commit
0f6966a
c660fa9
fb337a6
0e2ccc9
0151d20
9785c0e
4aa26f9
de75e27
ca4a90a
3c1048f
bc07098
5e41ae6
e998b59
dd37af3
0228602
d571b9c
3933ed2
3cf247a
859021c
ca795cb
78d2c96
aca53a4
27086bd
e386f37
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
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> | ||
*/ | ||
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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing denied access test There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
{ | ||
$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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
{ | ||
$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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My bad, there should be |
||
* | ||
* @return string | ||
*/ | ||
private function getReviewUrl(ProductInterface $product, ProductReview $productReview) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
{ | ||
return sprintf('%s%s', $this->getReviewListUrl($product), $productReview->getId()); | ||
} | ||
} |
There was a problem hiding this comment.
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