From 5c4e67ffadd6010ddad287612390d7514f4805c4 Mon Sep 17 00:00:00 2001 From: mamazu Date: Fri, 1 Feb 2019 16:34:59 +0100 Subject: [PATCH 1/6] Added validation for the add product view --- .../AddProductReviewByCodeRequest.xml | 34 +++++++++++++++++++ .../AddProductReviewBySlugRequest.xml | 34 +++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 src/Resources/config/validation/AddProductReviewByCodeRequest.xml create mode 100644 src/Resources/config/validation/AddProductReviewBySlugRequest.xml diff --git a/src/Resources/config/validation/AddProductReviewByCodeRequest.xml b/src/Resources/config/validation/AddProductReviewByCodeRequest.xml new file mode 100644 index 000000000..9604333a1 --- /dev/null +++ b/src/Resources/config/validation/AddProductReviewByCodeRequest.xml @@ -0,0 +1,34 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Resources/config/validation/AddProductReviewBySlugRequest.xml b/src/Resources/config/validation/AddProductReviewBySlugRequest.xml new file mode 100644 index 000000000..2da5fc905 --- /dev/null +++ b/src/Resources/config/validation/AddProductReviewBySlugRequest.xml @@ -0,0 +1,34 @@ + + + + + + + + + + + + + + + + + + + + + + + From 227fbf8e394de9ccd4a9d4c418ba82acd031a6ff Mon Sep 17 00:00:00 2001 From: mamazu Date: Mon, 4 Feb 2019 10:48:53 +0100 Subject: [PATCH 2/6] Added validation for product review --- .../AddProductReviewByCodeRequest.xml | 6 +- .../AddProductReviewBySlugRequest.xml | 6 +- .../Controller/AddProductReviewByCodeTest.php | 67 +++++++++++++++++++ .../Controller/AddProductReviewBySlugTest.php | 65 ++++++++++++++++++ .../reviews/add_review_failed_email.json | 9 +++ .../reviews/add_review_failed_rating.json | 9 +++ 6 files changed, 156 insertions(+), 6 deletions(-) create mode 100644 tests/Controller/AddProductReviewByCodeTest.php create mode 100644 tests/Controller/AddProductReviewBySlugTest.php create mode 100644 tests/Responses/Expected/reviews/add_review_failed_email.json create mode 100644 tests/Responses/Expected/reviews/add_review_failed_rating.json diff --git a/src/Resources/config/validation/AddProductReviewByCodeRequest.xml b/src/Resources/config/validation/AddProductReviewByCodeRequest.xml index 9604333a1..cc46d8825 100644 --- a/src/Resources/config/validation/AddProductReviewByCodeRequest.xml +++ b/src/Resources/config/validation/AddProductReviewByCodeRequest.xml @@ -11,15 +11,15 @@ --> - - + - + diff --git a/src/Resources/config/validation/AddProductReviewBySlugRequest.xml b/src/Resources/config/validation/AddProductReviewBySlugRequest.xml index 2da5fc905..0f1c136d9 100644 --- a/src/Resources/config/validation/AddProductReviewBySlugRequest.xml +++ b/src/Resources/config/validation/AddProductReviewBySlugRequest.xml @@ -11,15 +11,15 @@ --> - - + - + diff --git a/tests/Controller/AddProductReviewByCodeTest.php b/tests/Controller/AddProductReviewByCodeTest.php new file mode 100644 index 000000000..4f43a38f0 --- /dev/null +++ b/tests/Controller/AddProductReviewByCodeTest.php @@ -0,0 +1,67 @@ + 'application/json', 'ACCEPT' => 'application/json']; + + /** + * @test + */ + public function fails_to_add_review_because_rating_is_out_of_bounds() + { + $this->loadFixturesFromFiles(['channel.yml', 'shop.yml']); + + $data = +<<client->request('POST', '/shop-api/WEB_GB/products/LOGAN_MUG_CODE/reviews', [], [], self::$acceptAndContentTypeHeader, $data); + + $response = $this->client->getResponse(); + $this->assertResponse($response, 'reviews/add_review_failed_rating', Response::HTTP_BAD_REQUEST); + } + + /** + * @test + */ + public function fails_to_add_review_because_email_is_not_valid() + { + $this->loadFixturesFromFiles(['channel.yml', 'shop.yml']); + + $data = +<<client->request('POST', '/shop-api/WEB_GB/products/LOGAN_MUG_CODE/reviews', [], [], self::$acceptAndContentTypeHeader, $data); + + $response = $this->client->getResponse(); + $this->assertResponse($response, 'reviews/add_review_failed_email', Response::HTTP_BAD_REQUEST); + } +} diff --git a/tests/Controller/AddProductReviewBySlugTest.php b/tests/Controller/AddProductReviewBySlugTest.php new file mode 100644 index 000000000..e6f3e5a80 --- /dev/null +++ b/tests/Controller/AddProductReviewBySlugTest.php @@ -0,0 +1,65 @@ + 'application/json', 'ACCEPT' => 'application/json']; + + /** + * @test + */ + public function fails_to_add_review_because_rating_is_out_of_bounds() + { + $this->loadFixturesFromFiles(['channel.yml', 'shop.yml']); + + $data = +<<client->request('POST', '/shop-api/WEB_GB/product-reviews-by-slug/mug', [], [], self::$acceptAndContentTypeHeader, $data); + + $response = $this->client->getResponse(); + $this->assertResponse($response, 'reviews/add_review_failed_rating', Response::HTTP_BAD_REQUEST); + } + + /** + * @test + */ + public function fails_to_add_review_because_rating_email_is_not_valid() + { + $this->loadFixturesFromFiles(['channel.yml', 'shop.yml']); + + $data = +<<client->request('POST', '/shop-api/WEB_GB/product-reviews-by-slug/mug', [], [], self::$acceptAndContentTypeHeader, $data); + + $response = $this->client->getResponse(); + $this->assertResponse($response, 'reviews/add_review_failed_email', Response::HTTP_BAD_REQUEST); + } +} diff --git a/tests/Responses/Expected/reviews/add_review_failed_email.json b/tests/Responses/Expected/reviews/add_review_failed_email.json new file mode 100644 index 000000000..22e74fc2a --- /dev/null +++ b/tests/Responses/Expected/reviews/add_review_failed_email.json @@ -0,0 +1,9 @@ +{ + "code": 400, + "message": "Validation failed", + "errors": { + "email": [ + "This valid is not a valid email address." + ] + } +} diff --git a/tests/Responses/Expected/reviews/add_review_failed_rating.json b/tests/Responses/Expected/reviews/add_review_failed_rating.json new file mode 100644 index 000000000..cad1309f3 --- /dev/null +++ b/tests/Responses/Expected/reviews/add_review_failed_rating.json @@ -0,0 +1,9 @@ +{ + "code": 400, + "message": "Validation failed", + "errors": { + "rating": [ + "This value should be 5 or less." + ] + } +} From 243f349969a5fd8c7b8b7f201f75fbbaf5a5f0e1 Mon Sep 17 00:00:00 2001 From: mamazu Date: Mon, 4 Feb 2019 22:57:29 +0100 Subject: [PATCH 3/6] Fixed tests --- tests/Controller/AddProductReviewByCodeTest.php | 8 +------- tests/Controller/AddProductReviewBySlugTest.php | 8 +------- tests/DependencyInjection/ConfigurationTest.php | 2 +- 3 files changed, 3 insertions(+), 15 deletions(-) diff --git a/tests/Controller/AddProductReviewByCodeTest.php b/tests/Controller/AddProductReviewByCodeTest.php index 4f43a38f0..e5e779464 100644 --- a/tests/Controller/AddProductReviewByCodeTest.php +++ b/tests/Controller/AddProductReviewByCodeTest.php @@ -4,14 +4,8 @@ namespace Tests\Sylius\ShopApiPlugin\Controller\Product; -use PHPUnit\Framework\Assert; -use Sylius\Component\Core\Model\AddressInterface; -use Sylius\Component\Core\Model\Customer; -use Sylius\Component\Core\Repository\AddressRepositoryInterface; -use Sylius\Component\Core\Repository\CustomerRepositoryInterface; use Symfony\Component\HttpFoundation\Response; -use Tests\Sylius\ShopApiPlugin\Controller\Utils\JsonApiTestCase; -use Tests\Sylius\ShopApiPlugin\Controller\Utils\ShopUserLoginTrait; +use Tests\Sylius\ShopApiPlugin\Controller\JsonApiTestCase; final class AddProductReviewByCodeTest extends JsonApiTestCase { diff --git a/tests/Controller/AddProductReviewBySlugTest.php b/tests/Controller/AddProductReviewBySlugTest.php index e6f3e5a80..53b143880 100644 --- a/tests/Controller/AddProductReviewBySlugTest.php +++ b/tests/Controller/AddProductReviewBySlugTest.php @@ -4,14 +4,8 @@ namespace Tests\Sylius\ShopApiPlugin\Controller\Product; -use PHPUnit\Framework\Assert; -use Sylius\Component\Core\Model\AddressInterface; -use Sylius\Component\Core\Model\Customer; -use Sylius\Component\Core\Repository\AddressRepositoryInterface; -use Sylius\Component\Core\Repository\CustomerRepositoryInterface; use Symfony\Component\HttpFoundation\Response; -use Tests\Sylius\ShopApiPlugin\Controller\Utils\JsonApiTestCase; -use Tests\Sylius\ShopApiPlugin\Controller\Utils\ShopUserLoginTrait; +use Tests\Sylius\ShopApiPlugin\Controller\JsonApiTestCase; final class AddProductReviewBySlugTest extends JsonApiTestCase { diff --git a/tests/DependencyInjection/ConfigurationTest.php b/tests/DependencyInjection/ConfigurationTest.php index dc4b7ca7f..fe7bbaf31 100644 --- a/tests/DependencyInjection/ConfigurationTest.php +++ b/tests/DependencyInjection/ConfigurationTest.php @@ -63,7 +63,7 @@ public function it_has_view_classes(): void 'page_links' => View\PageLinksView::class, 'payment' => View\PaymentView::class, 'payment_method' => View\PaymentMethodView::class, - 'placed_order' => View\PlacedOrderView::class, + 'placed_order' => View\PlacedOrderView::class, 'price' => View\PriceView::class, 'product' => View\ProductView::class, 'product_attribute_value' => View\ProductAttributeValueView::class, From 7641fb61cca27a4ab3dad417aaefd528ab58391c Mon Sep 17 00:00:00 2001 From: mamazu Date: Thu, 7 Feb 2019 20:37:12 +0100 Subject: [PATCH 4/6] Fixed typos --- tests/Controller/AddProductReviewByCodeTest.php | 2 +- tests/Controller/AddProductReviewBySlugTest.php | 2 +- tests/Responses/Expected/reviews/add_review_failed_email.json | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Controller/AddProductReviewByCodeTest.php b/tests/Controller/AddProductReviewByCodeTest.php index e5e779464..9d6e4f001 100644 --- a/tests/Controller/AddProductReviewByCodeTest.php +++ b/tests/Controller/AddProductReviewByCodeTest.php @@ -46,7 +46,7 @@ public function fails_to_add_review_because_email_is_not_valid() << Date: Fri, 8 Feb 2019 16:21:27 +0100 Subject: [PATCH 5/6] Moved the tests to the correct controller --- .../Controller/AddProductReviewByCodeTest.php | 61 ------------------- .../Controller/AddProductReviewBySlugTest.php | 59 ------------------ .../ProductAddReviewByCodeApiTest.php | 50 ++++++++++++++- .../ProductAddReviewBySlugApiTest.php | 46 ++++++++++++++ 4 files changed, 95 insertions(+), 121 deletions(-) delete mode 100644 tests/Controller/AddProductReviewByCodeTest.php delete mode 100644 tests/Controller/AddProductReviewBySlugTest.php diff --git a/tests/Controller/AddProductReviewByCodeTest.php b/tests/Controller/AddProductReviewByCodeTest.php deleted file mode 100644 index 9d6e4f001..000000000 --- a/tests/Controller/AddProductReviewByCodeTest.php +++ /dev/null @@ -1,61 +0,0 @@ - 'application/json', 'ACCEPT' => 'application/json']; - - /** - * @test - */ - public function fails_to_add_review_because_rating_is_out_of_bounds() - { - $this->loadFixturesFromFiles(['channel.yml', 'shop.yml']); - - $data = -<<client->request('POST', '/shop-api/WEB_GB/products/LOGAN_MUG_CODE/reviews', [], [], self::$acceptAndContentTypeHeader, $data); - - $response = $this->client->getResponse(); - $this->assertResponse($response, 'reviews/add_review_failed_rating', Response::HTTP_BAD_REQUEST); - } - - /** - * @test - */ - public function fails_to_add_review_because_email_is_not_valid() - { - $this->loadFixturesFromFiles(['channel.yml', 'shop.yml']); - - $data = -<<client->request('POST', '/shop-api/WEB_GB/products/LOGAN_MUG_CODE/reviews', [], [], self::$acceptAndContentTypeHeader, $data); - - $response = $this->client->getResponse(); - $this->assertResponse($response, 'reviews/add_review_failed_email', Response::HTTP_BAD_REQUEST); - } -} diff --git a/tests/Controller/AddProductReviewBySlugTest.php b/tests/Controller/AddProductReviewBySlugTest.php deleted file mode 100644 index 2f28cc3ef..000000000 --- a/tests/Controller/AddProductReviewBySlugTest.php +++ /dev/null @@ -1,59 +0,0 @@ - 'application/json', 'ACCEPT' => 'application/json']; - - /** - * @test - */ - public function fails_to_add_review_because_rating_is_out_of_bounds() - { - $this->loadFixturesFromFiles(['channel.yml', 'shop.yml']); - - $data = -<<client->request('POST', '/shop-api/WEB_GB/product-reviews-by-slug/mug', [], [], self::$acceptAndContentTypeHeader, $data); - - $response = $this->client->getResponse(); - $this->assertResponse($response, 'reviews/add_review_failed_rating', Response::HTTP_BAD_REQUEST); - } - - /** - * @test - */ - public function fails_to_add_review_because_rating_email_is_not_valid() - { - $this->loadFixturesFromFiles(['channel.yml', 'shop.yml']); - - $data = -<<client->request('POST', '/shop-api/WEB_GB/product-reviews-by-slug/mug', [], [], self::$acceptAndContentTypeHeader, $data); - - $response = $this->client->getResponse(); - $this->assertResponse($response, 'reviews/add_review_failed_email', Response::HTTP_BAD_REQUEST); - } -} diff --git a/tests/Controller/ProductAddReviewByCodeApiTest.php b/tests/Controller/ProductAddReviewByCodeApiTest.php index 29176a95e..408947229 100644 --- a/tests/Controller/ProductAddReviewByCodeApiTest.php +++ b/tests/Controller/ProductAddReviewByCodeApiTest.php @@ -18,7 +18,7 @@ public function it_adds_review_to_product() $this->loadFixturesFromFiles(['shop.yml']); $data = - <<assertResponse($response, 'channel_has_not_been_found_response', Response::HTTP_NOT_FOUND); } + + /** + * @test + */ + public function it_does_not_allow_to_add_review_when_rating_is_out_of_bounds() + { + $this->loadFixturesFromFiles(['channel.yml', 'shop.yml']); + + $data = +<<client->request('POST', '/shop-api/WEB_GB/products/LOGAN_MUG_CODE/reviews', [], [], self::$acceptAndContentTypeHeader, $data); + + $response = $this->client->getResponse(); + $this->assertResponse($response, 'reviews/add_review_failed_rating', Response::HTTP_BAD_REQUEST); + } + + /** + * @test + */ + public function it_does_not_allow_to_add_review_when_email_is_not_valid() + { + $this->loadFixturesFromFiles(['channel.yml', 'shop.yml']); + + $data = +<<client->request('POST', '/shop-api/WEB_GB/products/LOGAN_MUG_CODE/reviews', [], [], self::$acceptAndContentTypeHeader, $data); + + $response = $this->client->getResponse(); + $this->assertResponse($response, 'reviews/add_review_failed_email', Response::HTTP_BAD_REQUEST); + } } diff --git a/tests/Controller/ProductAddReviewBySlugApiTest.php b/tests/Controller/ProductAddReviewBySlugApiTest.php index 0492ad1fb..221c04b93 100644 --- a/tests/Controller/ProductAddReviewBySlugApiTest.php +++ b/tests/Controller/ProductAddReviewBySlugApiTest.php @@ -75,4 +75,50 @@ public function it_does_not_allow_to_add_product_review_by_slug_in_non_existent_ $this->assertResponse($response, 'channel_has_not_been_found_response', Response::HTTP_NOT_FOUND); } + + /** + * @test + */ + public function it_does_not_allow_to_add_review_when_rating_is_out_of_bounds() + { + $this->loadFixturesFromFiles(['channel.yml', 'shop.yml']); + + $data = +<<client->request('POST', '/shop-api/WEB_GB/product-reviews-by-slug/mug', [], [], self::$acceptAndContentTypeHeader, $data); + + $response = $this->client->getResponse(); + $this->assertResponse($response, 'reviews/add_review_failed_rating', Response::HTTP_BAD_REQUEST); + } + + /** + * @test + */ + public function it_does_not_allow_to_add_review_when_rating_email_is_not_valid() + { + $this->loadFixturesFromFiles(['channel.yml', 'shop.yml']); + + $data = +<<client->request('POST', '/shop-api/WEB_GB/product-reviews-by-slug/mug', [], [], self::$acceptAndContentTypeHeader, $data); + + $response = $this->client->getResponse(); + $this->assertResponse($response, 'reviews/add_review_failed_email', Response::HTTP_BAD_REQUEST); + } } From dc54ce566f87edb49cf712c73af89942928d8000 Mon Sep 17 00:00:00 2001 From: mamazu Date: Tue, 12 Feb 2019 16:00:44 +0100 Subject: [PATCH 6/6] Codestyle --- tests/DependencyInjection/ConfigurationTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/DependencyInjection/ConfigurationTest.php b/tests/DependencyInjection/ConfigurationTest.php index fe7bbaf31..1d451ea7d 100644 --- a/tests/DependencyInjection/ConfigurationTest.php +++ b/tests/DependencyInjection/ConfigurationTest.php @@ -63,7 +63,7 @@ public function it_has_view_classes(): void 'page_links' => View\PageLinksView::class, 'payment' => View\PaymentView::class, 'payment_method' => View\PaymentMethodView::class, - 'placed_order' => View\PlacedOrderView::class, + 'placed_order' => View\PlacedOrderView::class, 'price' => View\PriceView::class, 'product' => View\ProductView::class, 'product_attribute_value' => View\ProductAttributeValueView::class,