diff --git a/app/composer.json b/app/composer.json index be399e99a066e779e3015ba3f581ad72061505b3..c4803f222e0f1cd68e54ee56f74e3f2f24780886 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 4e3a56077fb806de5799fe7e82171145e3ce4ff5..c06e13d1d3cccf9cf4bb41d503718a398f82ca41 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 0000000000000000000000000000000000000000..3482cbae1cfdc54115f59e840b88092c2844265a --- /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 851a66275ae74e085d470571afbe8687d1c939ac..b0fb5a8ccc1713ed8fcc23c99d952f1809841c13 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/phpunit.xml.dist b/app/phpunit.xml.dist index 6c4bfed7f5ebd8bedc033fa4ac9656205d1e0c7e..05a53a7a1a2ab2e86ac78d65853d48272c68f6b3 100644 --- a/app/phpunit.xml.dist +++ b/app/phpunit.xml.dist @@ -34,5 +34,7 @@ + + diff --git a/app/src/Shared/Abstraction/AbstractController.php b/app/src/Shared/Abstraction/AbstractController.php new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/app/symfony.lock b/app/symfony.lock index 9b33bba30d57e7e8c65bf7da822187f080ec6fd1..9069fc1b67e3ff2a0db4e6705256995e2d21f274 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/DataFixtures/NewsFixture.php b/app/tests/DataFixtures/NewsFixture.php new file mode 100644 index 0000000000000000000000000000000000000000..729ca1b8db50dc2a3608e778cfdbad454c94652f --- /dev/null +++ b/app/tests/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/DataFixtures/RestaurantFixture.php b/app/tests/DataFixtures/RestaurantFixture.php new file mode 100644 index 0000000000000000000000000000000000000000..8ca0aeebf97eb3580120c4f9ee1bfd79e6f4e8e6 --- /dev/null +++ b/app/tests/DataFixtures/RestaurantFixture.php @@ -0,0 +1,76 @@ +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/NewsControllerTests.php b/app/tests/NewsTests/Controller/NewsControllerTests.php new file mode 100644 index 0000000000000000000000000000000000000000..09c41ca4da1847fe90d54bc7027d31b69a88887a --- /dev/null +++ b/app/tests/NewsTests/Controller/NewsControllerTests.php @@ -0,0 +1,251 @@ +request( + 'GET', + '/api/v1/news/00000000-0000-0000-0000-000000000000' + ); + + + self::assertResponseIsSuccessful(); + + /** Проверка получемых новостей */ + 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(), + 'Полученная детальная новость инккоректена' + ); + } + + public function testNonExistedUuid(): void + { + $client = static::createClient(); + $client->request( + 'GET', + '/api/v1/news/00000000-0000-0000-0000-000000000005' + ); + + self::assertResponseStatusCodeSame(404); + } + + public function testCorruptUuid(): void + { + $client = static::createClient(); + $client->request('GET', '/api/v1/news/looks-like-not-a-UUId'); + + self::assertResponseStatusCodeSame(400); + } + + + // Тест news + public function testNullRequest(): void + { + $client = static::createClient(); + $client->request('GET', '/api/v1/news/'); + + self::assertResponseIsSuccessful(); + + $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::assertSame( + $testResponse->getContent(), + $client->getResponse()->getContent(), + 'Полученное тело респонза неверно' + ); + } + + 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( + (new JsonResponse(new PaginationDto(1, 1, 12)))->getContent(), + $data, + 'Пагинация инкоректна' + ); + } + + public function testPageOverListCountPagination(): void + { + $client = static::createClient(); + $client->request('GET', '/api/v1/news/?page=14'); + $data = $client->getResponse()->getContent(); + + self::assertResponseIsSuccessful(); + + /** Проверка пагинации */ + self::assertStringContainsString( + (new JsonResponse(new PaginationDto(1, 1, 12)))->getContent(), + $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( + (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, + 'Полученный список новостей инккоректен при правильном фильтре' + ); + } + + 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(200); + } + + + // mainNews + public function testMainNews() + { + $client = static::createClient(); + $client->request('GET', '/api/v1/news/mainNews'); + $data = $client->getResponse()->getContent(); + + self::assertResponseIsSuccessful(); + } + + 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 new file mode 100644 index 0000000000000000000000000000000000000000..6374fb4e918b45a14f363517650eda104b8f9bb0 --- /dev/null +++ b/app/tests/RestaurantTests/Controller/RestaurantsControllerTests.php @@ -0,0 +1,245 @@ +request( + 'GET', + '/api/v1/restaurants/00000000-0000-0000-0000-000000000000' + ); + $data = $client->getResponse()->getContent(); + + self::assertResponseIsSuccessful(); + + 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, + 'Полученная детальная новость инккоректена' + ); + } + + 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( + (new JsonResponse(new PaginationDto(1, 1, 12),))->getContent(), + $data, + 'Пагинация инкорректна при пустом реквесте' + ); + + /** Проверка получемых новостей */ + self::assertStringContainsString( + (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( + (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, + 'Пагинация инкорректна при пустом реквесте' + ); + } + + 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( + (new JsonResponse(new PaginationDto(1, 1, 12),))->getContent(), + $data, + 'Пагинация инкорректна при негативных значениях' + ); + } + + public function testPageOverListCountPagination(): void + { + $client = static::createClient(); + $client->request('GET', '/api/v1/restaurants/?page=14'); + $data = $client->getResponse()->getContent(); + + self::assertResponseIsSuccessful(); + + /** Проверка пагинации */ + self::assertStringContainsString( + (new JsonResponse(new PaginationDto(1, 1, 12),))->getContent(), + $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