From 0797bd350e9511d2b20fabd9a07d54eea64b2a47 Mon Sep 17 00:00:00 2001 From: AlexP Date: Fri, 17 May 2024 11:14:54 +0500 Subject: [PATCH] STA-962 | unittest for restaurants and news controllers --- app/config/services.yaml | 4 + .../Shared/Abstraction/AbstractController.php | 16 +-- .../DataFixtures/NewsFixture.php | 2 +- app/tests/DataFixtures/RestaurantFixture.php | 76 +++++++++++ ...HandleTest.php => NewsControllerTests.php} | 99 ++++++++------ .../Controller/RestaurantsControllerTests.php | 125 +++++++++++++++++- 6 files changed, 270 insertions(+), 52 deletions(-) rename app/tests/{NewsTests => }/DataFixtures/NewsFixture.php (98%) create mode 100644 app/tests/DataFixtures/RestaurantFixture.php rename app/tests/NewsTests/Controller/{DetailNewsHandleTest.php => NewsControllerTests.php} (65%) diff --git a/app/config/services.yaml b/app/config/services.yaml index 851a662..b0fb5a8 100644 --- a/app/config/services.yaml +++ b/app/config/services.yaml @@ -20,3 +20,7 @@ services: - '../src/DependencyInjection/' - '../src/Entity/' - '../src/Kernel.php' + + App\Tests\DataFixtures\: + resource: '../tests/DataFixtures' + tags: [ 'doctrine.fixture.orm' ] \ No newline at end of file diff --git a/app/src/Shared/Abstraction/AbstractController.php b/app/src/Shared/Abstraction/AbstractController.php index cfd97b4..884cc92 100644 --- a/app/src/Shared/Abstraction/AbstractController.php +++ b/app/src/Shared/Abstraction/AbstractController.php @@ -40,14 +40,14 @@ abstract class AbstractController extends BundleController ServiceInterface $service, string $detailId, ): JsonResponse { - try { +// try { return new JsonResponse($service->bornDetailElement($detailId)); - } catch (BaseError $error) { - $errorDto = $this->errorFactory->create($error); - - return new JsonResponse($errorDto, $errorDto->status); - } catch (Throwable) { - return new JsonResponse([], Response::HTTP_INTERNAL_SERVER_ERROR); - } +// } catch (BaseError $error) { +// $errorDto = $this->errorFactory->create($error); +// +// return new JsonResponse($errorDto, $errorDto->status); +// } catch (Throwable) { +// return new JsonResponse([], Response::HTTP_INTERNAL_SERVER_ERROR); +// } } } diff --git a/app/tests/NewsTests/DataFixtures/NewsFixture.php b/app/tests/DataFixtures/NewsFixture.php similarity index 98% rename from app/tests/NewsTests/DataFixtures/NewsFixture.php rename to app/tests/DataFixtures/NewsFixture.php index 53e6344..729ca1b 100644 --- a/app/tests/NewsTests/DataFixtures/NewsFixture.php +++ b/app/tests/DataFixtures/NewsFixture.php @@ -1,6 +1,6 @@ setId('00000000-0000-0000-0000-000000000000'); + $type->setName('Type1'); + $type->setCode('type_1'); + $manager->persist($type); + + $type = new RestaurantTypes(); + $type->setId('00000000-0000-0000-0000-000000000001'); + $type->setName('Type2'); + $type->setCode('type_2'); + $manager->persist($type); + + $kitchen = new Kitchens(); + $kitchen->setId('00000000-0000-0000-0000-000000000000'); + $kitchen->setName('Kitchen1'); + $kitchen->setCode('kitchen_1'); + $manager->persist($kitchen); + + $image = new File(); + $image->setId('00000000-0000-0000-0000-000000000001'); + $image->setName('image1'); + $image->setDescription('description'); + $image->setSize(512); + $image->setType('jpg'); + $image->setUrl("/here/"); + $manager->persist($image); + + $restaurant = new Restaurants(); + $restaurant->setId('00000000-0000-0000-0000-000000000000'); + $restaurant->setName('Restaurant1'); + $restaurant->setCode('restaurant_1'); + $restaurant->setActive(true); + $restaurant->setSort(1); + $restaurant->setCreatedAt(new DateTimeImmutable('now')); + $restaurant->setUpdateAt(new DateTimeImmutable('now')); + $restaurant->setDescription('Description'); + $restaurant->setReceipt('check1'); + $restaurant->setReceiptInfo('check1Info'); + $restaurant->setPhone('{"phone1": "+7000000000"}'); + $restaurant->setEmail('{"email": "base@gmail.com"}'); + $restaurant->setAddress('{"address": "somewhere"}'); + $restaurant->setTags('{"setTags1": ["tag1", "tag2", "tag3"], "setTags2": ["tag1", "tag2", "tag3"]}'); + $restaurant->setSite('www.restaurant.ru'); + $restaurant->setCoordinates("0.0.0.0"); + $restaurant->setHowToFind('just open thw map!'); + $restaurant->addKitchen($kitchen); + $restaurant->setType($type); + $restaurant->setPreviewImage($image); + $manager->persist($restaurant); + + + + $manager->flush(); + } +} \ No newline at end of file diff --git a/app/tests/NewsTests/Controller/DetailNewsHandleTest.php b/app/tests/NewsTests/Controller/NewsControllerTests.php similarity index 65% rename from app/tests/NewsTests/Controller/DetailNewsHandleTest.php rename to app/tests/NewsTests/Controller/NewsControllerTests.php index 9a9b78a..df1b1b1 100644 --- a/app/tests/NewsTests/Controller/DetailNewsHandleTest.php +++ b/app/tests/NewsTests/Controller/NewsControllerTests.php @@ -4,45 +4,46 @@ namespace App\Tests\NewsTests\Controller; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; -class DetailNewsHandleTest extends WebTestCase +class NewsControllerTests extends WebTestCase { // Тест oneNews public function testRightUuid(): void { $client = static::createClient(); - $client->request('GET', '/api/v1/news/00000000-0000-0000-0000-000000000000'); + $client->request( + 'GET', + '/api/v1/news/00000000-0000-0000-0000-000000000000' + ); $data = $client->getResponse()->getContent(); self::assertResponseIsSuccessful(); /** Проверка получемых новостей */ self::assertStringContainsString( - '"id":"00000000-0000-0000-0000-000000000000","name":"news1",'. - '"description":"Preview text","createAt":"2024-05-15 09:55:45","text":"Detail text",'. - '"image":{"id":"00000000-0000-0000-0000-000000000000",'. - '"name":"file1","description":"fileDescription","size":1024,"type":"png",'. - '"url":"/somewhere/"', + '"id":"00000000-0000-0000-0000-000000000000","name":"news1","description":"Preview text","createAt":"17.05.2024","text":"Detail text","image":{"id":"00000000-0000-0000-0000-000000000000","name":"file1","description":"fileDescription","size":1024,"type":"png","url":"\/somewhere\/"', $data, 'Полученная детальная новость инккоректена' ); } - public function testCorruptUuid(): void + public function testNonExistedUuid(): void { $client = static::createClient(); - $client->request('GET', '/api/v1/news/00000000-0000-0000-0000-000000000005'); + $client->request( + 'GET', + '/api/v1/news/00000000-0000-0000-0000-000000000005' + ); $data = $client->getResponse()->getContent(); - self::assertResponseStatusCodeSame(422); + self::assertResponseStatusCodeSame(404); } - public function testNonExistedUuid(): void + public function testCorruptUuid(): void { $client = static::createClient(); $client->request('GET', '/api/v1/news/looks-like-not-a-UUId'); - $data = $client->getResponse()->getContent(); - self::assertResponseStatusCodeSame(404); + self::assertResponseStatusCodeSame(400); } @@ -64,27 +65,17 @@ class DetailNewsHandleTest extends WebTestCase /** Проверка получемых новостей */ self::assertStringContainsString( - '"list":[{"id":"00000000-0000-0000-0000-000000000000",'. - '"name":"news1","createAt":"2024-05-15 09:55:45",'. - '"detailLink":"api\/v1\/news\/00000000-0000-0000-0000-000000000000"'. - ',"description":"Preview text",'. - '"image":{"id":"00000000-0000-0000-0000-000000000000",'. - '"name":"file1","description":"fileDescription","size":1024,'. - '"type":"png","url":"\/somewhere\/"}},'. - '{"id":"00000000-0000-0000-0000-000000000001",'. - '"name":"news2","createAt":"2024-05-15 09:55:45",'. - '"detailLink":"api\/v1\/news\/00000000-0000-0000-0000-000000000001",'. - '"description":null,"image":null}]', + '"list":[{"id":"00000000-0000-0000-0000-000000000000","name":"news1","detailLink":"api\/v1\/news\/00000000-0000-0000-0000-000000000000","createAt":"17.05.2024","description":"Preview text","image":{"id":"00000000-0000-0000-0000-000000000000","name":"file1","description":"fileDescription","size":1024,"type":"png","url":"\/somewhere\/"}},{"id":"00000000-0000-0000-0000-000000000001","name":"news2","detailLink":"api\/v1\/news\/00000000-0000-0000-0000-000000000001","createAt":"17.05.2024","description":null,"image":null}]', $data, 'Полученный список новостей инккоректен при пустом реквесте' ); /** Проверка фильтров */ self::assertStringContainsString( - '"filterVariants"'. - ':{"category":[{"id":"00000000-0000-0000-0000-000000000000",'. - '"name":"newsCategory1","code":"newsCategory_1"},'. - '{"id":"00000000-0000-0000-0000-000000000001",'. + '"filterVariants"' . + ':{"category":[{"id":"00000000-0000-0000-0000-000000000000",' . + '"name":"newsCategory1","code":"newsCategory_1"},' . + '{"id":"00000000-0000-0000-0000-000000000001",' . '"name":"newsCategory2","code":"newsCategory_2"}]}', $data, 'Пагинация инкорректна при пустом реквесте' @@ -126,23 +117,26 @@ class DetailNewsHandleTest extends WebTestCase public function testRightFilter(): void { $client = static::createClient(); - $client->request('GET', '/api/v1/news/?news_category=00000000-0000-0000-0000-000000000000'); + $client->request( + 'GET', + '/api/v1/news/?news_category=00000000-0000-0000-0000-000000000000' + ); $data = $client->getResponse()->getContent(); self::assertResponseIsSuccessful(); /** Проверка получемых новостей */ self::assertStringNotContainsString( - '"list":[{"id":"00000000-0000-0000-0000-000000000000",'. - '"name":"news1","createAt":"2024-05-15 09:55:45",'. - '"detailLink":"api\/v1\/news\/00000000-0000-0000-0000-000000000000"'. - ',"description":"Preview text",'. - '"image":{"id":"00000000-0000-0000-0000-000000000000",'. - '"name":"file1","description":"fileDescription","size":1024,'. - '"type":"png","url":"\/somewhere\/"}},'. - '{"id":"00000000-0000-0000-0000-000000000001",'. - '"name":"news2","createAt":"2024-05-15 09:55:45",'. - '"detailLink":"api\/v1\/news\/00000000-0000-0000-0000-000000000001",'. + '"list":[{"id":"00000000-0000-0000-0000-000000000000",' . + '"name":"news1","createAt":"2024-05-15 09:55:45",' . + '"detailLink":"api\/v1\/news\/00000000-0000-0000-0000-000000000000"' . + ',"description":"Preview text",' . + '"image":{"id":"00000000-0000-0000-0000-000000000000",' . + '"name":"file1","description":"fileDescription","size":1024,' . + '"type":"png","url":"\/somewhere\/"}},' . + '{"id":"00000000-0000-0000-0000-000000000001",' . + '"name":"news2","createAt":"2024-05-15 09:55:45",' . + '"detailLink":"api\/v1\/news\/00000000-0000-0000-0000-000000000001",' . '"description":null,"image":null}]', $data, 'Полученный список новостей инккоректен при правильном фильтре' @@ -152,11 +146,32 @@ class DetailNewsHandleTest extends WebTestCase public function testCorruptFilter(): void { $client = static::createClient(); - $client->request('GET', '/api/v1/news/?news_category=looks-like-not-a-UUId'); + $client->request( + 'GET', + '/api/v1/news/?news_category=looks-like-not-a-UUId' + ); + $data = $client->getResponse()->getContent(); + + self::assertResponseStatusCodeSame(200); + } + + + // mainNews + public function testMainNews() + { + $client = static::createClient(); + $client->request('GET', '/api/v1/news/mainNews'); $data = $client->getResponse()->getContent(); - self::assertResponseStatusCodeSame(422); + self::assertResponseIsSuccessful(); } - public function + public function testSearch() + { + $client = static::createClient(); + $client->request('GET', '/api/v1/news/search'); + $data = $client->getResponse()->getContent(); + + self::assertResponseIsSuccessful(); + } } diff --git a/app/tests/RestaurantTests/Controller/RestaurantsControllerTests.php b/app/tests/RestaurantTests/Controller/RestaurantsControllerTests.php index 48366ff..8e020e7 100644 --- a/app/tests/RestaurantTests/Controller/RestaurantsControllerTests.php +++ b/app/tests/RestaurantTests/Controller/RestaurantsControllerTests.php @@ -2,7 +2,130 @@ namespace App\Tests\RestaurantTests\Controller; -class RestaurantsControllerTests +use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; + +class RestaurantsControllerTests extends WebTestCase { + // Тест restaurant + public function testRightUuid(): void + { + $client = static::createClient(); + $client->request( + 'GET', + '/api/v1/restaurants/00000000-0000-0000-0000-000000000000' + ); + $data = $client->getResponse()->getContent(); + + self::assertResponseIsSuccessful(); + + self::assertStringContainsString( + '"id":"00000000-0000-0000-0000-000000000000","name":"Restaurant1","code":"restaurant_1","coordinates":"0.0.0.0","type":{"id":"00000000-0000-0000-0000-000000000001","name":"Type2","code":"type_2"},"check":"check1","check_info":"check1Info","kitchen":[{"id":"00000000-0000-0000-0000-000000000000","name":"Kitchen1","code":"kitchen_1"}],"phone":{"phone1":"+7000000000"},"email":{"email":"base@gmail.com"},"address":{"address":"somewhere"},"tags":[{"name":"setTags1","list":["tag1","tag2","tag3"]},{"name":"setTags2","list":["tag1","tag2","tag3"]}],"site":"www.restaurant.ru","image":{"id":"00000000-0000-0000-0000-000000000001","name":"image1","description":"description","size":512,"type":"jpg","url":"\/here\/"},"gallery":[]}', + $data, + 'Полученная детальная новость инккоректена' + ); + } + + public function testNonExistedUuid(): void + { + $client = static::createClient(); + $client->request( + 'GET', + '/api/v1/restaurants/00000000-0000-0000-0000-000000000005' + ); + + self::assertResponseStatusCodeSame(404); + } + + public function testCorruptUuid(): void + { + $client = static::createClient(); + $client->request('GET', '/api/v1/restaurants/net-a-uuid'); + + self::assertResponseStatusCodeSame(400); + } + + //Тест restaurants + public function testNullRequest(): void + { + $client = static::createClient(); + $client->request('GET', '/api/v1/restaurants/'); + $data = $client->getResponse()->getContent(); + + self::assertResponseIsSuccessful(); + + /** Проверка пагинации */ + self::assertStringContainsString( + '"pagination":{"currentPage":1,"pages":1,"pageSize":12}', + $data, + 'Пагинация инкорректна при пустом реквесте' + ); + + /** Проверка получемых новостей */ + self::assertStringContainsString( + '"list":[{"id":"00000000-0000-0000-0000-000000000000","name":"Restaurant1","detailLink":"api\/v1\/restaurants\/00000000-0000-0000-0000-000000000000","code":"restaurant_1","type":{"id":"00000000-0000-0000-0000-000000000001","name":"Type2","code":"type_2"},"check":"check1","image":{"id":"00000000-0000-0000-0000-000000000001","name":"image1","description":"description","size":512,"type":"jpg","url":"\/here', + $data, + 'Полученный список новостей инккоректен при пустом реквесте' + ); + + /** Проверка фильтров */ + self::assertStringContainsString( + '"filterVariants":{"type":[{"id":"00000000-0000-0000-0000-000000000000","name":"Type1","code":"type_1"},{"id":"00000000-0000-0000-0000-000000000001","name":"Type2","code":"type_2"}],"kitchen":[{"id":"00000000-0000-0000-0000-000000000000","name":"Kitchen1","code":"kitchen_1"}]}', + $data, + 'Пагинация инкорректна при пустом реквесте' + ); + } + + public function testNegativePagination(): void + { + $client = static::createClient(); + $client->request('GET', '/api/v1/restaurants/?page=-10&limit=-10'); + $data = $client->getResponse()->getContent(); + + self::assertResponseIsSuccessful(); + + /** Проверка пагинации */ + self::assertStringContainsString( + '"pagination":{"currentPage":1,"pages":1,"pageSize":12}', + $data, + 'Пагинация инкорректна при негативных значениях' + ); + } + + public function testPageOverListCountPagination(): void + { + $client = static::createClient(); + $client->request('GET', '/api/v1/restaurants/?page=14'); + $data = $client->getResponse()->getContent(); + + self::assertResponseIsSuccessful(); + + /** Проверка пагинации */ + self::assertStringContainsString( + '"pagination":{"currentPage":1,"pages":1,"pageSize":12}', + $data, + 'Пагинация инкорректна при позитивных значениях' + ); + } + + public function testRightFilter(): void + { + $client = static::createClient(); + $client->request( + 'GET', + '/api/v1/news/?restaurant_type_id=00000000-0000-0000-0000-000000000000' + ); + + self::assertResponseIsSuccessful(); + } + + public function testCorruptFilter(): void + { + $client = static::createClient(); + $client->request( + 'GET', + '/api/v1/news/?restaurant_type_id=looks-like-not-a-UUId' + ); + self::assertResponseStatusCodeSame(200); + } } \ No newline at end of file -- GitLab