From ca0f3355a7fe62e1edf5c3e9e92c4bc5c1dbf2aa Mon Sep 17 00:00:00 2001 From: firdaous elhalafi Date: Thu, 4 Jan 2024 19:53:38 +0100 Subject: [PATCH 1/2] Creation d'une bd app_test + test des 3 routes des graphes --- api/README.md | 33 ++++ api/composer.json | 1 + api/composer.lock | 173 ++++++++++++++++++++- api/config/bundles.php | 1 + api/src/Controller/SaleController.php | 21 +-- api/src/DataFixtures/AppFixtures.php | 31 ++++ api/symfony.lock | 12 ++ api/tests/Api/SalesTest.php | 212 ++++++++++++++++++++++++++ 8 files changed, 463 insertions(+), 21 deletions(-) create mode 100644 api/src/DataFixtures/AppFixtures.php create mode 100644 api/tests/Api/SalesTest.php diff --git a/api/README.md b/api/README.md index 4c45cca..084f886 100644 --- a/api/README.md +++ b/api/README.md @@ -38,3 +38,36 @@ Indexation de la Colonne de Date, car toutes les routes font des opérations de create index date_index on sale (date); ```` +## Clear cache + +```Shell +sudo docker compose exec php bin/console cache:clear +``` + +## Run tests + +Create a test database: +```shell +sudo docker compose exec php bin/console doctrine:database:create --env=test +``` + +Migration: +```shell +sudo docker compose exec php bin/console doctrine:schema:create --env=test +sudo docker compose exec php bin/console doctrine:migrations:migrate --env=test +``` + +Add fixtures for test: +```shell +sudo docker compose exec php bin/console doctrine:fixtures:load --env=test +``` + +Run tests: +```Shell +sudo docker compose exec php bin/phpunit tests/Api/SalesTest.php +``` + +Drop test database: +```shell +sudo docker compose exec php bin/console doctrine:database:drop --force --env=test +``` diff --git a/api/composer.json b/api/composer.json index 272b903..aa6ac7b 100644 --- a/api/composer.json +++ b/api/composer.json @@ -35,6 +35,7 @@ }, "require-dev": { "api-platform/schema-generator": "^5.0", + "doctrine/doctrine-fixtures-bundle": "^3.5", "symfony/browser-kit": "6.4.*", "symfony/css-selector": "6.4.*", "symfony/debug-bundle": "6.4.*", diff --git a/api/composer.lock b/api/composer.lock index 3baddaf..57184b9 100644 --- a/api/composer.lock +++ b/api/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "830b9da35a09ffc162e5f9fda1fbaf62", + "content-hash": "5d692385fe1a4c436fd48a555e3ba65c", "packages": [ { "name": "api-platform/core", @@ -6739,6 +6739,177 @@ ], "time": "2022-02-25T21:32:43+00:00" }, + { + "name": "doctrine/data-fixtures", + "version": "1.7.0", + "source": { + "type": "git", + "url": "https://github.com/doctrine/data-fixtures.git", + "reference": "bbcb74f2ac6dbe81a14b3c3687d7623490a0448f" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/data-fixtures/zipball/bbcb74f2ac6dbe81a14b3c3687d7623490a0448f", + "reference": "bbcb74f2ac6dbe81a14b3c3687d7623490a0448f", + "shasum": "" + }, + "require": { + "doctrine/deprecations": "^0.5.3 || ^1.0", + "doctrine/persistence": "^2.0|^3.0", + "php": "^7.4 || ^8.0" + }, + "conflict": { + "doctrine/dbal": "<3.5 || >=5", + "doctrine/orm": "<2.14 || >=4", + "doctrine/phpcr-odm": "<1.3.0" + }, + "require-dev": { + "doctrine/annotations": "^1.12 || ^2", + "doctrine/coding-standard": "^12", + "doctrine/dbal": "^3.5 || ^4", + "doctrine/mongodb-odm": "^1.3.0 || ^2.0.0", + "doctrine/orm": "^2.14 || ^3", + "ext-sqlite3": "*", + "phpstan/phpstan": "^1.10", + "phpunit/phpunit": "^9.6.13 || ^10.4.2", + "symfony/cache": "^5.4 || ^6.3 || ^7", + "symfony/var-exporter": "^5.4 || ^6.3 || ^7", + "vimeo/psalm": "^5.9" + }, + "suggest": { + "alcaeus/mongo-php-adapter": "For using MongoDB ODM 1.3 with PHP 7 (deprecated)", + "doctrine/mongodb-odm": "For loading MongoDB ODM fixtures", + "doctrine/orm": "For loading ORM fixtures", + "doctrine/phpcr-odm": "For loading PHPCR ODM fixtures" + }, + "type": "library", + "autoload": { + "psr-4": { + "Doctrine\\Common\\DataFixtures\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jonathan Wage", + "email": "jonwage@gmail.com" + } + ], + "description": "Data Fixtures for all Doctrine Object Managers", + "homepage": "https://www.doctrine-project.org", + "keywords": [ + "database" + ], + "support": { + "issues": "https://github.com/doctrine/data-fixtures/issues", + "source": "https://github.com/doctrine/data-fixtures/tree/1.7.0" + }, + "funding": [ + { + "url": "https://www.doctrine-project.org/sponsorship.html", + "type": "custom" + }, + { + "url": "https://www.patreon.com/phpdoctrine", + "type": "patreon" + }, + { + "url": "https://tidelift.com/funding/github/packagist/doctrine%2Fdata-fixtures", + "type": "tidelift" + } + ], + "time": "2023-11-24T11:18:31+00:00" + }, + { + "name": "doctrine/doctrine-fixtures-bundle", + "version": "3.5.1", + "source": { + "type": "git", + "url": "https://github.com/doctrine/DoctrineFixturesBundle.git", + "reference": "c808a0c85c38c8ee265cc8405b456c1d2b38567d" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/DoctrineFixturesBundle/zipball/c808a0c85c38c8ee265cc8405b456c1d2b38567d", + "reference": "c808a0c85c38c8ee265cc8405b456c1d2b38567d", + "shasum": "" + }, + "require": { + "doctrine/data-fixtures": "^1.3", + "doctrine/doctrine-bundle": "^2.2", + "doctrine/orm": "^2.14.0 || ^3.0", + "doctrine/persistence": "^2.4|^3.0", + "php": "^7.4 || ^8.0", + "symfony/config": "^5.4|^6.0|^7.0", + "symfony/console": "^5.4|^6.0|^7.0", + "symfony/dependency-injection": "^5.4|^6.0|^7.0", + "symfony/deprecation-contracts": "^2.1|^3", + "symfony/doctrine-bridge": "^5.4|^6.0|^7.0", + "symfony/http-kernel": "^5.4|^6.0|^7.0" + }, + "conflict": { + "doctrine/dbal": "< 3" + }, + "require-dev": { + "doctrine/coding-standard": "^12", + "phpstan/phpstan": "^1.10.39", + "phpunit/phpunit": "^9.6.13", + "symfony/phpunit-bridge": "^6.3.6", + "vimeo/psalm": "^5.15" + }, + "type": "symfony-bundle", + "autoload": { + "psr-4": { + "Doctrine\\Bundle\\FixturesBundle\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Doctrine Project", + "homepage": "https://www.doctrine-project.org" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony DoctrineFixturesBundle", + "homepage": "https://www.doctrine-project.org", + "keywords": [ + "Fixture", + "persistence" + ], + "support": { + "issues": "https://github.com/doctrine/DoctrineFixturesBundle/issues", + "source": "https://github.com/doctrine/DoctrineFixturesBundle/tree/3.5.1" + }, + "funding": [ + { + "url": "https://www.doctrine-project.org/sponsorship.html", + "type": "custom" + }, + { + "url": "https://www.patreon.com/phpdoctrine", + "type": "patreon" + }, + { + "url": "https://tidelift.com/funding/github/packagist/doctrine%2Fdoctrine-fixtures-bundle", + "type": "tidelift" + } + ], + "time": "2023-11-19T12:48:54+00:00" + }, { "name": "friendsofphp/php-cs-fixer", "version": "v3.40.2", diff --git a/api/config/bundles.php b/api/config/bundles.php index e33ab8c..7c3f102 100644 --- a/api/config/bundles.php +++ b/api/config/bundles.php @@ -13,4 +13,5 @@ return [ Doctrine\Bundle\MigrationsBundle\DoctrineMigrationsBundle::class => ['all' => true], Symfony\Bundle\MonologBundle\MonologBundle::class => ['all' => true], Symfony\Bundle\DebugBundle\DebugBundle::class => ['dev' => true, 'test' => true], + Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle::class => ['dev' => true, 'test' => true], ]; diff --git a/api/src/Controller/SaleController.php b/api/src/Controller/SaleController.php index 2e14a2d..5a5a223 100644 --- a/api/src/Controller/SaleController.php +++ b/api/src/Controller/SaleController.php @@ -38,27 +38,8 @@ class SaleController extends AbstractController 'endDate' => '\d{4}-\d{2}-\d{2}', // YYYY-MM-DD 'granularity' => 'day|month|year', // day, month, ou year ])] - public function getChartData( - string $startDate, - string $endDate, - string $granularity - ): JsonResponse + public function getChartData(string $startDate, string $endDate, string $granularity): JsonResponse { - $validator = Validation::createValidator(); - $violations = $validator->validate([ - 'startDate' => $startDate, - 'endDate' => $endDate, - 'granularity' => $granularity, - ], new Assert\Collection([ - 'startDate' => new Assert\Date(), - 'endDate' => new Assert\Date(), - 'granularity' => new Assert\Choice(choices: [self::GRANULARITY_DAY, self::GRANULARITY_MONTH, self::GRANULARITY_YEAR]), - ])); - - if (count($violations) > 0) { - return new JsonResponse(['error' => 'Invalid input'], Response::HTTP_BAD_REQUEST); - } - $input = new BarChartInput(); $input->start = $startDate; $input->end = $endDate; diff --git a/api/src/DataFixtures/AppFixtures.php b/api/src/DataFixtures/AppFixtures.php new file mode 100644 index 0000000..6afaf5e --- /dev/null +++ b/api/src/DataFixtures/AppFixtures.php @@ -0,0 +1,31 @@ +setDate(new \DateTimeImmutable('2020-01-01')); + $sale->setAmount(100000); + $sale->setRegion('Auvergne-Rhône-Alpes'); + $sale->setSurface(100); + + $manager->persist($sale); + + $sale = new Sale(); + $sale->setDate(new \DateTimeImmutable('2021-02-01')); + $sale->setAmount(200000); + $sale->setRegion('Normandie'); + $sale->setSurface(200); + + $manager->persist($sale); + + $manager->flush(); + } +} diff --git a/api/symfony.lock b/api/symfony.lock index 3dea125..978ca0d 100644 --- a/api/symfony.lock +++ b/api/symfony.lock @@ -57,6 +57,18 @@ "doctrine/doctrine-cache-bundle": { "version": "1.3.5" }, + "doctrine/doctrine-fixtures-bundle": { + "version": "3.5", + "recipe": { + "repo": "github.com/symfony/recipes", + "branch": "main", + "version": "3.0", + "ref": "1f5514cfa15b947298df4d771e694e578d4c204d" + }, + "files": [ + "src/DataFixtures/AppFixtures.php" + ] + }, "doctrine/doctrine-migrations-bundle": { "version": "3.2", "recipe": { diff --git a/api/tests/Api/SalesTest.php b/api/tests/Api/SalesTest.php new file mode 100644 index 0000000..a8b1131 --- /dev/null +++ b/api/tests/Api/SalesTest.php @@ -0,0 +1,212 @@ +request('GET', '/api/timeseries'); + + $this->assertResponseStatusCodeSame(200); + + // Décoder le contenu JSON + $data = json_decode($client->getContent(), true); + + # print_r($client->getContent()); + + $this->assertJsonContains([ + [ + 'month' => 1, + 'year' => 2020, + 'averagePrice' => 1000, + ], + [ + 'month' => 2, + 'year' => 2021, + 'averagePrice' => 1000, + ], + ]); + } + + public function testGetBarChartDataMonth(): void + { + $client = static::createClient()->request('GET', '/api/bar-chart/2018-06-01/2019-12-31/month'); + + #print_r($client->getContent()); + $this->assertResponseStatusCodeSame(200); + $this->assertJsonContains([]); + } + + public function testGetBarChartDataMonth2(): void + { + $client = static::createClient()->request('GET', '/api/bar-chart/2020-01-01/2020-12-31/month'); + + #print_r($client->getContent()); + $this->assertResponseStatusCodeSame(200); + $this->assertJsonContains([ + [ + 'date' => '2020-01', + 'occurrences' => 1 + ] + ]); + + } + + public function testGetBarChartDataMonth3(): void + { + $client = static::createClient()->request('GET', '/api/bar-chart/2020-01-01/2022-12-31/month'); + + #print_r($client->getContent()); + $this->assertResponseStatusCodeSame(200); + $this->assertJsonContains([ + [ + 'date' => '2020-01', + 'occurrences' => 1 + ], + [ + 'date' => '2021-02', + 'occurrences' => 1 + ], + ]); + + } + + public function testGetBarChartDataYear(): void + { + $client = static::createClient()->request('GET', '/api/bar-chart/2018-06-01/2019-12-31/year'); + + #print_r($client->getContent()); + $this->assertResponseStatusCodeSame(200); + $this->assertJsonContains([]); + + } + + public function testGetBarChartDataYear2(): void + { + $client = static::createClient()->request('GET', '/api/bar-chart/2020-01-01/2020-12-31/year'); + + #print_r($client->getContent()); + $this->assertResponseStatusCodeSame(200); + $this->assertJsonContains([ + [ + 'date' => '2020', + 'occurrences' => 1 + ] + ]); + + } + + public function testGetBarChartDataYear3(): void + { + $client = static::createClient()->request('GET', '/api/bar-chart/2020-01-01/2022-12-31/year'); + + #print_r($client->getContent()); + $this->assertResponseStatusCodeSame(200); + $this->assertJsonContains([ + [ + 'date' => '2020', + 'occurrences' => 1 + ], + [ + 'date' => '2021', + 'occurrences' => 1 + ], + ]); + + } + + public function testGetBarChartDataDay(): void + { + $client = static::createClient()->request('GET', '/api/bar-chart/2018-06-01/2019-12-31/day'); + + #print_r($client->getContent()); + $this->assertResponseStatusCodeSame(200); + $this->assertJsonContains([]); + + } + + public function testGetBarChartDataDay2(): void + { + $client = static::createClient()->request('GET', '/api/bar-chart/2020-01-01/2020-12-31/day'); + + #print_r($client->getContent()); + $this->assertResponseStatusCodeSame(200); + $this->assertJsonContains([ + [ + 'date' => '2020-01-01', + 'occurrences' => 1 + ] + ]); + + } + + public function testGetBarChartDataDay3(): void + { + $client = static::createClient()->request('GET', '/api/bar-chart/2020-01-01/2022-12-31/day'); + + #print_r($client->getContent()); + $this->assertResponseStatusCodeSame(200); + $this->assertJsonContains([ + [ + 'date' => '2020-01-01', + 'occurrences' => 1 + ], + [ + 'date' => '2021-02-01', + 'occurrences' => 1 + ], + ]); + + } + + public function testGetDonutChartData(): void + { + $client = static::createClient()->request('GET', '/api/donut-chart/2022'); + + $this->assertResponseStatusCodeSame(200); + + #print_r($client->getContent()); + $this->assertJsonContains([]); + } + + public function testGetDonutChartData2(): void + { + $client = static::createClient()->request('GET', '/api/donut-chart/2021'); + + $this->assertResponseStatusCodeSame(200); + + #print_r($client->getContent()); + $this->assertJsonContains([ + [ + 'region' => 'Normandie', + 'occurrences' => 1 + ] + ]); + } + + public function testGetDonutChartData3(): void + { + $client = static::createClient()->request('GET', '/api/donut-chart/2020'); + + $this->assertResponseStatusCodeSame(200); + + #print_r($client->getContent()); + $this->assertJsonContains([ + [ + 'region' => 'Auvergne-Rhône-Alpes', + 'occurrences' => 1 + ] + ]); + } + + public function testGetDonutChartData4(): void + { + $client = static::createClient()->request('GET', '/api/donut-chart/2016'); + + $this->assertResponseStatusCodeSame(404); + + } +} -- GitLab From ea558321483f7a08bc2d6a7917ec9505228ced83 Mon Sep 17 00:00:00 2001 From: firdaous elhalafi Date: Thu, 4 Jan 2024 19:54:59 +0100 Subject: [PATCH 2/2] supression de GreetingsTest --- api/tests/Api/GreetingsTest.php | 27 --------------------------- 1 file changed, 27 deletions(-) delete mode 100644 api/tests/Api/GreetingsTest.php diff --git a/api/tests/Api/GreetingsTest.php b/api/tests/Api/GreetingsTest.php deleted file mode 100644 index acff072..0000000 --- a/api/tests/Api/GreetingsTest.php +++ /dev/null @@ -1,27 +0,0 @@ -request('POST', '/greetings', [ - 'json' => [ - 'name' => 'Kévin', - ], - 'headers' => [ - 'Content-Type' => 'application/ld+json', - ], - ]); - - $this->assertResponseStatusCodeSame(201); - $this->assertJsonContains([ - '@context' => '/contexts/Greeting', - '@type' => 'Greeting', - 'name' => 'Kévin', - ]); - } -} -- GitLab