diff --git a/.env.test b/.env.test new file mode 100644 index 0000000..d048686 --- /dev/null +++ b/.env.test @@ -0,0 +1,5 @@ +# define your env variables for the test env here +KERNEL_CLASS='App\Kernel' +APP_SECRET='$ecretf0rt3st' +SYMFONY_DEPRECATIONS_HELPER=999999 +PANTHER_APP_ENV=panther diff --git a/.gitignore b/.gitignore index 416a4d1..c3b28d9 100755 --- a/.gitignore +++ b/.gitignore @@ -29,3 +29,9 @@ public/polyfills-es2018.js public/polyfills-es5.js public/scripts.js public/styles.css + +###> symfony/phpunit-bridge ### +.phpunit +.phpunit.result.cache +/phpunit.xml +###< symfony/phpunit-bridge ### diff --git a/bin/phpunit b/bin/phpunit new file mode 100755 index 0000000..4d1ed05 --- /dev/null +++ b/bin/phpunit @@ -0,0 +1,13 @@ +#!/usr/bin/env php +=3", + "symfony/monolog-bundle": "^3.2", + "symfony/phpunit-bridge": "^3.4 || ^4.1 || ^5.0", + "theofidry/alice-data-fixtures": "^1.0.1" + }, + "suggest": { + "doctrine/dbal": "Required when using the fixture loading functionality with an ORM and SQLite", + "doctrine/doctrine-fixtures-bundle": "Required when using the fixture loading functionality", + "doctrine/orm": "Required when using the fixture loading functionality with an ORM and SQLite", + "hautelook/alice-bundle": "Required when using loadFixtureFiles functionality with custom providers", + "theofidry/alice-data-fixtures": "Required when using loadFixtureFiles functionality" + }, + "type": "symfony-bundle", + "extra": { + "branch-alias": { + "dev-master": "1.x-dev" + } + }, + "autoload": { + "psr-4": { + "Liip\\TestFixturesBundle\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Liip AG", + "homepage": "http://www.liip.ch/" + }, + { + "name": "Community contributions", + "homepage": "https://github.com/liip/LiipTestFixturesBundle/contributors" + } + ], + "description": "This bundles enables efficient loading of Doctrine fixtures in functional test-cases for Symfony applications", + "keywords": [ + "fixtures", + "symfony", + "testing" + ], + "time": "2020-04-27T10:24:37+00:00" + }, + { + "name": "symfony/browser-kit", + "version": "v4.3.11", + "source": { + "type": "git", + "url": "https://github.com/symfony/browser-kit.git", + "reference": "66d301ce3458b522e3b1f2a76ecfccd1834dcf90" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/browser-kit/zipball/66d301ce3458b522e3b1f2a76ecfccd1834dcf90", + "reference": "66d301ce3458b522e3b1f2a76ecfccd1834dcf90", + "shasum": "" + }, + "require": { + "php": "^7.1.3", + "symfony/dom-crawler": "~3.4|~4.0" + }, + "require-dev": { + "symfony/css-selector": "~3.4|~4.0", + "symfony/http-client": "^4.3", + "symfony/mime": "^4.3", + "symfony/process": "~3.4|~4.0" + }, + "suggest": { + "symfony/process": "" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "4.3-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Component\\BrowserKit\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony BrowserKit Component", + "homepage": "https://symfony.com", + "time": "2020-01-04T12:24:57+00:00" + }, + { + "name": "symfony/dom-crawler", + "version": "v4.3.11", + "source": { + "type": "git", + "url": "https://github.com/symfony/dom-crawler.git", + "reference": "ccf895f6f3ed9430f53ae1ce34566e9bb6c58446" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/dom-crawler/zipball/ccf895f6f3ed9430f53ae1ce34566e9bb6c58446", + "reference": "ccf895f6f3ed9430f53ae1ce34566e9bb6c58446", + "shasum": "" + }, + "require": { + "php": "^7.1.3", + "symfony/polyfill-ctype": "~1.8", + "symfony/polyfill-mbstring": "~1.0" + }, + "conflict": { + "masterminds/html5": "<2.6" + }, + "require-dev": { + "masterminds/html5": "^2.6", + "symfony/css-selector": "~3.4|~4.0" + }, + "suggest": { + "symfony/css-selector": "" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "4.3-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Component\\DomCrawler\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony DomCrawler Component", + "homepage": "https://symfony.com", + "time": "2020-01-04T12:24:57+00:00" + }, + { + "name": "symfony/phpunit-bridge", + "version": "v5.0.8", + "source": { + "type": "git", + "url": "https://github.com/symfony/phpunit-bridge.git", + "reference": "00b8da18a52fa842b7a39613fb0a63720a354e74" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/phpunit-bridge/zipball/00b8da18a52fa842b7a39613fb0a63720a354e74", + "reference": "00b8da18a52fa842b7a39613fb0a63720a354e74", + "shasum": "" + }, + "require": { + "php": ">=5.5.9" + }, + "conflict": { + "phpunit/phpunit": "<4.8.35|<5.4.3,>=5.0|<6.4,>=6.0|9.1.2" + }, + "suggest": { + "symfony/error-handler": "For tracking deprecated interfaces usages at runtime with DebugClassLoader" + }, + "bin": [ + "bin/simple-phpunit" + ], + "type": "symfony-bridge", + "extra": { + "branch-alias": { + "dev-master": "5.0-dev" + }, + "thanks": { + "name": "phpunit/phpunit", + "url": "https://github.com/sebastianbergmann/phpunit" + } + }, + "autoload": { + "files": [ + "bootstrap.php" + ], + "psr-4": { + "Symfony\\Bridge\\PhpUnit\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony PHPUnit Bridge", + "homepage": "https://symfony.com", + "time": "2020-04-28T17:58:55+00:00" + }, { "name": "symfony/process", "version": "v4.3.10", @@ -6617,7 +6873,11 @@ "php": "^7.1.3", "ext-ctype": "*", "ext-iconv": "*", - "ext-json": "*" + "ext-json": "*", + "ext-pdo_mysql": "*" }, - "platform-dev": [] + "platform-dev": { + "ext-mbstring": "*", + "ext-pdo_sqlite": "*" + } } diff --git a/config/bundles.php b/config/bundles.php index fb980ad..b003748 100755 --- a/config/bundles.php +++ b/config/bundles.php @@ -14,4 +14,5 @@ return [ Nelmio\CorsBundle\NelmioCorsBundle::class => ['all' => true], Symfony\Bundle\TwigBundle\TwigBundle::class => ['all' => true], Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle::class => ['all' => true], + Liip\TestFixturesBundle\LiipTestFixturesBundle::class => ['test' => true], ]; diff --git a/config/packages/test/doctrine.yaml b/config/packages/test/doctrine.yaml new file mode 100755 index 0000000..a9b9904 --- /dev/null +++ b/config/packages/test/doctrine.yaml @@ -0,0 +1,5 @@ +doctrine: + dbal: + driver: pdo_sqlite + path: "%kernel.cache_dir%/test.db" + url: null \ No newline at end of file diff --git a/config/packages/test/framework.yaml b/config/packages/test/framework.yaml index d051c84..d5b0918 100755 --- a/config/packages/test/framework.yaml +++ b/config/packages/test/framework.yaml @@ -2,3 +2,6 @@ framework: test: true session: storage_id: session.storage.mock_file +liip_test_fixtures: + cache_db: + sqlite: liip_test_fixtures.services_database_backup.sqlite \ No newline at end of file diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..d81f0c3 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,33 @@ + + + + + + + + + + + + + + + tests + + + + + + src + + + + + + + diff --git a/symfony.lock b/symfony.lock index e9a8466..7d09126 100755 --- a/symfony.lock +++ b/symfony.lock @@ -170,6 +170,9 @@ "config/packages/prod/jms_serializer.yaml" ] }, + "liip/test-fixtures-bundle": { + "version": "1.8.0" + }, "nelmio/api-doc-bundle": { "version": "3.0", "recipe": { @@ -240,6 +243,9 @@ "swiftmailer/swiftmailer": { "version": "v6.2.3" }, + "symfony/browser-kit": { + "version": "v4.3.11" + }, "symfony/cache": { "version": "v4.3.5" }, @@ -271,6 +277,9 @@ "symfony/doctrine-bridge": { "version": "v4.3.5" }, + "symfony/dom-crawler": { + "version": "v4.3.11" + }, "symfony/dotenv": { "version": "v4.3.5" }, @@ -365,6 +374,21 @@ "symfony/orm-pack": { "version": "v1.0.7" }, + "symfony/phpunit-bridge": { + "version": "4.3", + "recipe": { + "repo": "github.com/symfony/recipes", + "branch": "master", + "version": "4.3", + "ref": "6d0e35f749d5f4bfe1f011762875275cd3f9874f" + }, + "files": [ + ".env.test", + "bin/phpunit", + "phpunit.xml.dist", + "tests/bootstrap.php" + ] + }, "symfony/polyfill-intl-icu": { "version": "v1.12.0" }, diff --git a/tests/Functional/PollControllerTest.php b/tests/Functional/PollControllerTest.php new file mode 100644 index 0000000..577a192 --- /dev/null +++ b/tests/Functional/PollControllerTest.php @@ -0,0 +1,130 @@ +loadFixtures(array( + 'App\DataFixtures\AppPollFixtures' + )); + + $client->request('GET', '/api/v1/poll/'); + + $response = $client->getResponse(); + $this->assertEquals(200, $response->getStatusCode()); + $body = $response->getContent(); + $json = json_decode($body, true); + $this->assertEquals(4, count($json['poll'])); + } + + // Test getting one poll (we should try to check all poll property) + public function testGetOnePoll() { + $client = static::createClient(); + + $this->loadFixtures(array( + 'App\DataFixtures\AppPollFixtures' + )); + + $client->request('GET', '/api/v1/poll/1'); + + $response = $client->getResponse(); + $this->assertEquals(200, $response->getStatusCode()); + $body = $response->getContent(); + $json = json_decode($body, true); + $this->assertEquals("citron ou orange", $json['poll']['title']); + } + + // Test Put some data on a poll + public function testPutOnePoll() { + $client = static::createClient(); + + $this->loadFixtures(array( + 'App\DataFixtures\AppPollFixtures' + )); + + $client->request('PUT', '/api/v1/poll/1/notTheToken', [ + 'body' => "{ + \"id\": 1, + \"title\": \"Fromage ou dessert ? \", + \"description\": \"Votre plat préféré\", + \"creation_date\": \"2048-04-25T16:19:48+02:00\", + \"expiracy_date\": \"2048-04-25T16:19:48+02:00\", + \"kind\": \"text\", + \"allowed_answers\": [ + \"yes\" + ], + \"modification_policy\": \"nobody\", + \"mail_on_vote\": true, + \"choices\": [ + { + \"id\": 1, + \"name\": \"fromage\" + }, + { + \"id\": 2, + \"name\": \"dessert\" + } + ], + \"default_expiracy_days_from_now\": 365 + }" + ]); + $response = $client->getResponse(); + $this->assertEquals(403, $response->getStatusCode()); + + //Same request but with admin key + $client->request('PUT', '/api/v1/poll/1/5eb5ade73ec4f', [ + 'body' => "{ + \"id\": 1, + \"title\": \"Fromage ou dessert ? \", + \"description\": \"Votre plat préféré\", + \"creation_date\": \"2048-04-25T16:19:48+02:00\", + \"expiracy_date\": \"2048-04-25T16:19:48+02:00\", + \"kind\": \"text\", + \"allowed_answers\": [ + \"yes\" + ], + \"modification_policy\": \"nobody\", + \"mail_on_vote\": true, + \"choices\": [ + { + \"id\": 1, + \"name\": \"fromage\" + }, + { + \"id\": 2, + \"name\": \"dessert\" + } + ], + \"default_expiracy_days_from_now\": 365 + }" + ]); + + $response = $client->getResponse(); + $this->assertEquals(200, $response->getStatusCode()); + + //Checking the result of modification + $client->request('GET', '/api/v1/poll/1'); + + $response = $client->getResponse(); + $this->assertEquals(200, $response->getStatusCode()); + $body = $response->getContent(); + $json = json_decode($body, true); + $this->assertEquals("Fromage ou dessert ?", $json['poll']['title']); + + } + + // Test Post new poll + public function testPostOnePoll() { + //TODO + + } + +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 0000000..469dcce --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,11 @@ +bootEnv(dirname(__DIR__).'/.env'); +}