From 52f74c320cc6bd495e9b353d261cb17fbcded0f5 Mon Sep 17 00:00:00 2001 From: AlexP Date: Thu, 16 May 2024 16:27:47 +0500 Subject: [PATCH] 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 @@ +