From 52f74c320cc6bd495e9b353d261cb17fbcded0f5 Mon Sep 17 00:00:00 2001 From: AlexP Date: Thu, 16 May 2024 16:27:47 +0500 Subject: [PATCH 1/3] STA-962 | create news test --- app/composer.json | 5 +- app/config/bundles.php | 2 + .../packages/dama_doctrine_test_bundle.yaml | 5 + app/config/services.yaml | 6 +- app/phpunit.xml.dist | 2 + app/symfony.lock | 24 +++ .../Controller/DetailNewsHandleTest.php | 162 ++++++++++++++++++ .../NewsTests/DataFixtures/NewsFixture.php | 80 +++++++++ .../Controller/RestaurantsControllerTests.php | 8 + 9 files changed, 292 insertions(+), 2 deletions(-) create mode 100644 app/config/packages/dama_doctrine_test_bundle.yaml create mode 100644 app/tests/NewsTests/Controller/DetailNewsHandleTest.php create mode 100644 app/tests/NewsTests/DataFixtures/NewsFixture.php create mode 100644 app/tests/RestaurantTests/Controller/RestaurantsControllerTests.php diff --git a/app/composer.json b/app/composer.json index be399e9..c4803f2 100644 --- a/app/composer.json +++ b/app/composer.json @@ -13,6 +13,7 @@ "doctrine/orm": "^3.1", "phpdocumentor/reflection-docblock": "^5.3", "phpstan/phpdoc-parser": "^1.28", + "ramsey/collection": "^2.0", "symfony/asset": "6.4.*", "symfony/asset-mapper": "6.4.*", "symfony/console": "6.4.*", @@ -95,7 +96,9 @@ } }, "require-dev": { - "phpunit/phpunit": "^9.5", + "dama/doctrine-test-bundle": "^8.0", + "doctrine/doctrine-fixtures-bundle": "^3.6", + "phpunit/phpunit": "^9.6", "symfony/browser-kit": "6.4.*", "symfony/css-selector": "6.4.*", "symfony/debug-bundle": "6.4.*", diff --git a/app/config/bundles.php b/app/config/bundles.php index 4e3a560..c06e13d 100644 --- a/app/config/bundles.php +++ b/app/config/bundles.php @@ -13,4 +13,6 @@ return [ Symfony\Bundle\SecurityBundle\SecurityBundle::class => ['all' => true], Symfony\Bundle\MonologBundle\MonologBundle::class => ['all' => true], Symfony\Bundle\MakerBundle\MakerBundle::class => ['dev' => true], + DAMA\DoctrineTestBundle\DAMADoctrineTestBundle::class => ['test' => true], + Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle::class => ['dev' => true, 'test' => true], ]; diff --git a/app/config/packages/dama_doctrine_test_bundle.yaml b/app/config/packages/dama_doctrine_test_bundle.yaml new file mode 100644 index 0000000..3482cba --- /dev/null +++ b/app/config/packages/dama_doctrine_test_bundle.yaml @@ -0,0 +1,5 @@ +when@test: + dama_doctrine_test: + enable_static_connection: true + enable_static_meta_data_cache: true + enable_static_query_cache: true diff --git a/app/config/services.yaml b/app/config/services.yaml index 55758d4..0c9cd8c 100644 --- a/app/config/services.yaml +++ b/app/config/services.yaml @@ -21,4 +21,8 @@ services: resource: '../src/News/' App\Shared\: - resource: '../src/Shared/' \ No newline at end of file + resource: '../src/Shared/' + + App\Tests\NewsTests\DataFixtures\: + resource: '../tests/NewsTests/DataFixtures' + tags: [ 'doctrine.fixture.orm' ] \ No newline at end of file diff --git a/app/phpunit.xml.dist b/app/phpunit.xml.dist index 6c4bfed..05a53a7 100644 --- a/app/phpunit.xml.dist +++ b/app/phpunit.xml.dist @@ -34,5 +34,7 @@ + + diff --git a/app/symfony.lock b/app/symfony.lock index 9b33bba..9069fc1 100644 --- a/app/symfony.lock +++ b/app/symfony.lock @@ -1,4 +1,16 @@ { + "dama/doctrine-test-bundle": { + "version": "8.0", + "recipe": { + "repo": "github.com/symfony/recipes-contrib", + "branch": "main", + "version": "7.2", + "ref": "896306d79d4ee143af9eadf9b09fd34a8c391b70" + }, + "files": [ + "config/packages/dama_doctrine_test_bundle.yaml" + ] + }, "doctrine/doctrine-bundle": { "version": "2.12", "recipe": { @@ -13,6 +25,18 @@ "src/Repository/.gitignore" ] }, + "doctrine/doctrine-fixtures-bundle": { + "version": "3.6", + "recipe": { + "repo": "github.com/symfony/recipes", + "branch": "main", + "version": "3.0", + "ref": "1f5514cfa15b947298df4d771e694e578d4c204d" + }, + "files": [ + "src/DataFixtures/AppFixtures.php" + ] + }, "doctrine/doctrine-migrations-bundle": { "version": "3.3", "recipe": { diff --git a/app/tests/NewsTests/Controller/DetailNewsHandleTest.php b/app/tests/NewsTests/Controller/DetailNewsHandleTest.php new file mode 100644 index 0000000..9a9b78a --- /dev/null +++ b/app/tests/NewsTests/Controller/DetailNewsHandleTest.php @@ -0,0 +1,162 @@ +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/"', + $data, + 'Полученная детальная новость инккоректена' + ); + } + + public function testCorruptUuid(): void + { + $client = static::createClient(); + $client->request('GET', '/api/v1/news/00000000-0000-0000-0000-000000000005'); + $data = $client->getResponse()->getContent(); + + self::assertResponseStatusCodeSame(422); + } + + public function testNonExistedUuid(): void + { + $client = static::createClient(); + $client->request('GET', '/api/v1/news/looks-like-not-a-UUId'); + $data = $client->getResponse()->getContent(); + + self::assertResponseStatusCodeSame(404); + } + + + // Тест news + public function testNullRequest(): void + { + $client = static::createClient(); + $client->request('GET', '/api/v1/news/'); + $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":"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, + 'Полученный список новостей инккоректен при пустом реквесте' + ); + + /** Проверка фильтров */ + self::assertStringContainsString( + '"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, + 'Пагинация инкорректна при пустом реквесте' + ); + } + + public function testNegativePagination(): void + { + $client = static::createClient(); + $client->request('GET', '/api/v1/news/?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/news/?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/?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",'. + '"description":null,"image":null}]', + $data, + 'Полученный список новостей инккоректен при правильном фильтре' + ); + } + + public function testCorruptFilter(): void + { + $client = static::createClient(); + $client->request('GET', '/api/v1/news/?news_category=looks-like-not-a-UUId'); + $data = $client->getResponse()->getContent(); + + self::assertResponseStatusCodeSame(422); + } + + public function +} diff --git a/app/tests/NewsTests/DataFixtures/NewsFixture.php b/app/tests/NewsTests/DataFixtures/NewsFixture.php new file mode 100644 index 0000000..53e6344 --- /dev/null +++ b/app/tests/NewsTests/DataFixtures/NewsFixture.php @@ -0,0 +1,80 @@ +setId('00000000-0000-0000-0000-000000000000'); + $newsCategory1->setName('newsCategory1'); + $newsCategory1->setCode('newsCategory_1'); + $manager->persist($newsCategory1); + + $newsCategory2 = new NewsCategories(); + $newsCategory2->setId('00000000-0000-0000-0000-000000000001'); + $newsCategory2->setName('newsCategory2'); + $newsCategory2->setCode('newsCategory_2'); + $manager->persist($newsCategory2); + + $photo = new File(); + $photo->setId('00000000-0000-0000-0000-000000000000'); + $photo->setName('file1'); + $photo->setDescription('fileDescription'); + $photo->setSize(1024); + $photo->setType('png'); + $photo->setUrl('/somewhere/'); + $manager->persist($photo); + + $newsType = new NewsType(); + $newsType->setId('00000000-0000-0000-0000-000000000000'); + $newsType->setName('newsType1'); + $newsType->setCode('newsType_1'); + $manager->persist($newsType); + + $news = new News(); + $news->setId('00000000-0000-0000-0000-000000000000'); + $news->setName('news1'); + $news->setCode('news_1'); + $news->setSort(1); + $news->setActive(true); + $news->setCreatedAt(new DateTimeImmutable()); + $news->setUpdateAt(new DateTimeImmutable()); + $news->setPreviewImage($photo); + $news->setDetailImage($photo); + $news->setPreviewText('Preview text'); + $news->setDetailText('Detail text'); + $news->setMainPageRender(true); + $news->setType($newsType); + $news->addCategory($newsCategory1); + $manager->persist($news); + + $news = new News(); + $news->setId('00000000-0000-0000-0000-000000000001'); + $news->setName('news2'); + $news->setCode('news_2'); + $news->setSort(2); + $news->setActive(true); + $news->setCreatedAt(new DateTimeImmutable()); + $news->setUpdateAt(new DateTimeImmutable()); + $news->setPreviewImage(null); + $news->setDetailImage(null); + $news->setPreviewText(null); + $news->setDetailText(null); + $news->setMainPageRender(false); + $news->SetType(null); + $news->addCategory($newsCategory2); + $manager->persist($news); + + $manager->flush(); + } +} diff --git a/app/tests/RestaurantTests/Controller/RestaurantsControllerTests.php b/app/tests/RestaurantTests/Controller/RestaurantsControllerTests.php new file mode 100644 index 0000000..48366ff --- /dev/null +++ b/app/tests/RestaurantTests/Controller/RestaurantsControllerTests.php @@ -0,0 +1,8 @@ + Date: Fri, 17 May 2024 11:14:54 +0500 Subject: [PATCH 2/3] 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 From 73614b757a19f24d881cdb06747c3612939fc988 Mon Sep 17 00:00:00 2001 From: AlexP Date: Mon, 20 May 2024 15:32:59 +0500 Subject: [PATCH 3/3] STA-962 | fixes --- .../Controller/NewsControllerTests.php | 158 +++++++++++++----- .../Controller/RestaurantsControllerTests.php | 128 +++++++++++++- 2 files changed, 237 insertions(+), 49 deletions(-) diff --git a/app/tests/NewsTests/Controller/NewsControllerTests.php b/app/tests/NewsTests/Controller/NewsControllerTests.php index df1b1b1..09c41ca 100644 --- a/app/tests/NewsTests/Controller/NewsControllerTests.php +++ b/app/tests/NewsTests/Controller/NewsControllerTests.php @@ -2,7 +2,18 @@ namespace App\Tests\NewsTests\Controller; +use App\News\Collection\NewsCategoryCollection; +use App\News\Dto\NewsCategoryDto; +use App\News\Dto\NewsDetailElementDto; +use App\News\Dto\NewsFilterVariantsDto; +use App\News\Dto\NewsListDto; +use App\News\Dto\NewsListingElementDto; +use App\Shared\Collection\ListingCollection; +use App\Shared\Dto\FileDto; +use App\Shared\Dto\PaginationDto; +use DateTimeImmutable; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; +use Symfony\Component\HttpFoundation\JsonResponse; class NewsControllerTests extends WebTestCase { @@ -14,14 +25,30 @@ class NewsControllerTests extends WebTestCase '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":"17.05.2024","text":"Detail text","image":{"id":"00000000-0000-0000-0000-000000000000","name":"file1","description":"fileDescription","size":1024,"type":"png","url":"\/somewhere\/"', - $data, + self::assertSame( + (new JsonResponse( + new NewsDetailElementDto( + '00000000-0000-0000-0000-000000000000', + 'news1', + 'Preview text', + (new DateTimeImmutable())->format('d.m.Y'), + 'Detail text', + new FileDto( + '00000000-0000-0000-0000-000000000000', + 'file1', + 'fileDescription', + 1024, + 'png', + '/somewhere/' + ), + ) + ))->getContent(), + $client->getResponse()->getContent(), 'Полученная детальная новость инккоректена' ); } @@ -33,7 +60,6 @@ class NewsControllerTests extends WebTestCase 'GET', '/api/v1/news/00000000-0000-0000-0000-000000000005' ); - $data = $client->getResponse()->getContent(); self::assertResponseStatusCodeSame(404); } @@ -52,33 +78,62 @@ class NewsControllerTests extends WebTestCase { $client = static::createClient(); $client->request('GET', '/api/v1/news/'); - $data = $client->getResponse()->getContent(); self::assertResponseIsSuccessful(); - /** Проверка пагинации */ - self::assertStringContainsString( - '"pagination":{"currentPage":1,"pages":1,"pageSize":12}', - $data, - 'Пагинация инкорректна при пустом реквесте' + $testResponse = new JsonResponse( + new NewsListDto( + new PaginationDto(1, 1, 12), + new ListingCollection( + [ + new NewsListingElementDto( + '00000000-0000-0000-0000-000000000000', + 'news1', + (new DateTimeImmutable())->format('d.m.Y'), + 'api/v1/news/00000000-0000-0000-0000-000000000000', + 'Preview text', + new FileDto( + '00000000-0000-0000-0000-000000000000', + 'file1', + 'fileDescription', + 1024, + 'png', + '/somewhere/' + ), + ), + new NewsListingElementDto( + '00000000-0000-0000-0000-000000000001', + 'news2', + (new DateTimeImmutable())->format('d.m.Y'), + 'api/v1/news/00000000-0000-0000-0000-000000000001', + null, + null, + ), + ], + ), + new NewsFilterVariantsDto( + new NewsCategoryCollection( + [ + new NewsCategoryDto( + '00000000-0000-0000-0000-000000000000', + 'newsCategory1', + 'newsCategory_1', + ), + new NewsCategoryDto( + '00000000-0000-0000-0000-000000000001', + 'newsCategory2', + 'newsCategory_2', + ), + ] + ), + ), + ), ); - /** Проверка получемых новостей */ - self::assertStringContainsString( - '"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",' . - '"name":"newsCategory2","code":"newsCategory_2"}]}', - $data, - 'Пагинация инкорректна при пустом реквесте' + self::assertSame( + $testResponse->getContent(), + $client->getResponse()->getContent(), + 'Полученное тело респонза неверно' ); } @@ -92,9 +147,9 @@ class NewsControllerTests extends WebTestCase /** Проверка пагинации */ self::assertStringContainsString( - '"pagination":{"currentPage":1,"pages":1,"pageSize":12}', + (new JsonResponse(new PaginationDto(1, 1, 12)))->getContent(), $data, - 'Пагинация инкорректна при негативных значениях' + 'Пагинация инкоректна' ); } @@ -108,9 +163,9 @@ class NewsControllerTests extends WebTestCase /** Проверка пагинации */ self::assertStringContainsString( - '"pagination":{"currentPage":1,"pages":1,"pageSize":12}', + (new JsonResponse(new PaginationDto(1, 1, 12)))->getContent(), $data, - 'Пагинация инкорректна при позитивных значениях' + 'Пагинация инкоректна' ); } @@ -125,19 +180,38 @@ class NewsControllerTests extends WebTestCase 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",' . - '"description":null,"image":null}]', + (new JsonResponse( + new ListingCollection( + [ + new NewsListingElementDto( + '00000000-0000-0000-0000-000000000000', + 'news1', + (new DateTimeImmutable())->format('d.m.Y'), + 'api/v1/news/00000000-0000-0000-0000-000000000000', + 'Preview text', + new FileDto( + '00000000-0000-0000-0000-000000000000', + 'file1', + 'fileDescription', + 1024, + 'png', + '/somewhere/', + ), + ), + new NewsListingElementDto( + '00000000-0000-0000-0000-000000000001', + 'news2', + (new DateTimeImmutable())->format('d.m.Y'), + 'api/v1/news/00000000-0000-0000-0000-000000000001', + null, + null, + ), + ] + ) + ))->getContent(), $data, 'Полученный список новостей инккоректен при правильном фильтре' ); diff --git a/app/tests/RestaurantTests/Controller/RestaurantsControllerTests.php b/app/tests/RestaurantTests/Controller/RestaurantsControllerTests.php index 8e020e7..6374fb4 100644 --- a/app/tests/RestaurantTests/Controller/RestaurantsControllerTests.php +++ b/app/tests/RestaurantTests/Controller/RestaurantsControllerTests.php @@ -2,7 +2,22 @@ namespace App\Tests\RestaurantTests\Controller; +use App\Restaurants\Collection\KitchenCollection; +use App\Restaurants\Collection\RestaurantTypeCollection; +use App\Restaurants\Collection\TagCollection; +use App\Restaurants\Dto\KitchenTypeDto; +use App\Restaurants\Dto\RestaurantDetailElementDto; +use App\Restaurants\Dto\RestaurantFilterVariantsDto; +use App\Restaurants\Dto\RestaurantListingElementDto; +use App\Restaurants\Dto\RestaurantTypeDto; +use App\Restaurants\Dto\TagDto; +use App\Shared\Collection\FileCollection; +use App\Shared\Collection\ListingCollection; +use App\Shared\Collection\StringCollection; +use App\Shared\Dto\FileDto; +use App\Shared\Dto\PaginationDto; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; +use Symfony\Component\HttpFoundation\JsonResponse; class RestaurantsControllerTests extends WebTestCase { @@ -18,8 +33,56 @@ class RestaurantsControllerTests extends WebTestCase 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":[]}', + self::assertSame( + (new JsonResponse( + new RestaurantDetailElementDto( + '00000000-0000-0000-0000-000000000000', + 'Restaurant1', + 'restaurant_1', + '0.0.0.0', + new RestaurantTypeDto( + '00000000-0000-0000-0000-000000000001', + 'Type2', + 'type_2', + ), + 'check1', + 'check1Info', + new KitchenCollection( + [ + new KitchenTypeDto( + '00000000-0000-0000-0000-000000000000', + 'Kitchen1', + 'kitchen_1' + ), + ] + ), + new StringCollection(['phone1' => '+7000000000']), + new StringCollection(['email' => 'base@gmail.com']), + new StringCollection(['address' => 'somewhere']), + new TagCollection( + [ + new TagDto( + 'setTags1', + new StringCollection(['tag1', 'tag2', 'tag3']) + ), + new TagDto( + 'setTags2', + new StringCollection(['tag1', 'tag2', 'tag3']) + ), + ] + ), + 'www.restaurant.ru', + new FileDto( + '00000000-0000-0000-0000-000000000001', + 'image1', + 'description', + 512, + 'jpg', + '/here/' + ), + new FileCollection([]) + ) + ))->getContent(), $data, 'Полученная детальная новость инккоректена' ); @@ -55,21 +118,72 @@ class RestaurantsControllerTests extends WebTestCase /** Проверка пагинации */ self::assertStringContainsString( - '"pagination":{"currentPage":1,"pages":1,"pageSize":12}', + (new JsonResponse(new PaginationDto(1, 1, 12),))->getContent(), $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', + (new JsonResponse( + new ListingCollection( + [ + new RestaurantListingElementDto( + '00000000-0000-0000-0000-000000000000', + 'Restaurant1', + 'restaurant_1', + new RestaurantTypeDto( + '00000000-0000-0000-0000-000000000001', + 'Type2', + 'type_2', + ), + 'api/v1/restaurants/00000000-0000-0000-0000-000000000000', + 'check1', + new FileDto( + '00000000-0000-0000-0000-000000000001', + 'image1', + 'description', + 512, + 'jpg', + '/here/' + ), + ), + ] + ) + ))->getContent(), $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"}]}', + (new JsonResponse( + new RestaurantFilterVariantsDto( + new RestaurantTypeCollection( + [ + new RestaurantTypeDto( + '00000000-0000-0000-0000-000000000000', + 'Type1', + 'type_1', + ), + new RestaurantTypeDto( + '00000000-0000-0000-0000-000000000001', + 'Type2', + 'type_2', + ), + ] + ), + new KitchenCollection( + [ + new KitchenTypeDto( + '00000000-0000-0000-0000-000000000000', + 'Kitchen1', + 'kitchen_1', + ) + ] + ) + ) + ))->getContent(), $data, 'Пагинация инкорректна при пустом реквесте' ); @@ -85,7 +199,7 @@ class RestaurantsControllerTests extends WebTestCase /** Проверка пагинации */ self::assertStringContainsString( - '"pagination":{"currentPage":1,"pages":1,"pageSize":12}', + (new JsonResponse(new PaginationDto(1, 1, 12),))->getContent(), $data, 'Пагинация инкорректна при негативных значениях' ); @@ -101,7 +215,7 @@ class RestaurantsControllerTests extends WebTestCase /** Проверка пагинации */ self::assertStringContainsString( - '"pagination":{"currentPage":1,"pages":1,"pageSize":12}', + (new JsonResponse(new PaginationDto(1, 1, 12),))->getContent(), $data, 'Пагинация инкорректна при позитивных значениях' ); -- GitLab