From 6d72efa695b4ed0030a84ef87f69d9772d318b03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= Date: Wed, 7 Oct 2015 02:44:03 +0100 Subject: [PATCH] Finish the job feature --- app/config/config_test.yml | 2 +- behat.yml | 8 +- composer.json | 4 +- composer.lock | 2 +- features/api/job.feature | 316 ++++++++++++++++-- features/api/mandate.feature | 9 +- features/api/user.feature | 2 +- features/bootstrap/Context/ApiContext.php | 2 +- features/bootstrap/Context/FrontContext.php | 2 +- features/bootstrap/Context/JsonContext.php | 98 ++++-- features/bootstrap/Context/JsonLdContext.php | 12 +- .../fixtures/ORM/job/collection-enabled.yml | 12 + .../fixtures/ORM/job/collection-order.yml | 12 + features/fixtures/ORM/job/collection.yml | 35 ++ features/fixtures/ORM/job/mandate2005.yml | 4 + features/fixtures/ORM/job/president.yml | 25 ++ features/fixtures/ORM/job/user-president.yml | 10 + src/ApiBundle/Entity/Job.php | 2 + 18 files changed, 481 insertions(+), 76 deletions(-) create mode 100644 features/fixtures/ORM/job/collection-enabled.yml create mode 100644 features/fixtures/ORM/job/collection-order.yml create mode 100644 features/fixtures/ORM/job/collection.yml create mode 100644 features/fixtures/ORM/job/mandate2005.yml create mode 100644 features/fixtures/ORM/job/president.yml create mode 100644 features/fixtures/ORM/job/user-president.yml diff --git a/app/config/config_test.yml b/app/config/config_test.yml index 847a687fe..82c3d27c5 100644 --- a/app/config/config_test.yml +++ b/app/config/config_test.yml @@ -11,7 +11,7 @@ framework: # If later a different database name is needed for tests, should be done here doctrine: dbal: - dbname: %database_name% + dbname: %database_name%_test # Doctrine Extensions stof_doctrine_extensions: diff --git a/behat.yml b/behat.yml index 085a1e8fa..53d9f0110 100644 --- a/behat.yml +++ b/behat.yml @@ -11,21 +11,21 @@ default: contexts: - Behat\MinkExtension\Context\MinkContext - Fidry\AliceBundleExtension\Context\Doctrine\AliceORMContext - - Incipio\Test\Behat\Context\ApiContext: + - Behat\Context\ApiContext: doctrine: @doctrine jwtManager: @lexik_jwt_authentication.jwt_manager userManager: @fos_user.user_manager encoderFactory: @security.encoder_factory - - Incipio\Test\Behat\Context\JsonContext: + - Behat\Context\JsonContext: evaluationMode: javascript - - Incipio\Test\Behat\Context\JsonLdContext: + - Behat\Context\JsonLdContext: evaluationMode: javascript front_features: paths: [ %paths.base%/features/front ] contexts: - Fidry\AliceBundleExtension\Context\Doctrine\AliceORMContext - - Incipio\Test\Behat\Context\FrontContext + - Behat\Context\FrontContext extensions: Behat\MinkExtension: diff --git a/composer.json b/composer.json index f406833a2..b11c55184 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,7 @@ "autoload-dev": { "psr-4": { "": "tests/", - "Incipio\\Test\\Behat\\": "features/bootstrap" + "Behat\\": "features/bootstrap" } }, "require": { @@ -58,7 +58,7 @@ "style:check": "php bin/php-cs-fixer fix -n --dry-run --diff -v --ansi src && php bin/php-cs-fixer fix -n --dry-run --diff -v --ansi features ", "style:fix": "php bin/php-cs-fixer fix -v --diff --ansi src && php bin/php-cs-fixer fix -v --diff --ansi features", "test": "composer test:db && composer test:phpunit && composer test:behat && composer test:security", - "test:db": "php app/console d:s:v --ansi", + "test:db": "php app/console d:s:v --ansi --env=test", "test:security": "php bin/security-checker security:check --ansi", "test:phpunit": "php bin/phpunit -c phpunit_travis.xml --testsuite App --colors=always", "test:phpunit:api": "php bin/phpunit -c phpunit_travis.xml --testsuite ApiBundle --colors=always", diff --git a/composer.lock b/composer.lock index 99bdb9829..3acb2cfae 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "630e82e8486c555d60cde60fea1d1b04", + "hash": "c0a95bf86c332f36017df07470d7314f", "content-hash": "b48e499ce314adb348564f2752e80920", "packages": [ { diff --git a/features/api/job.feature b/features/api/job.feature index bdd717e99..905e00076 100644 --- a/features/api/job.feature +++ b/features/api/job.feature @@ -1,38 +1,306 @@ -@job @ignore +@job Feature: Jobs management - The job of a user is the job he has occupied for a given mandate. - A job must belong to a mandate. - At each new mandate, the jobs available are the job enabled. - A job may be left empty. - New jobs are created for the current mandate. + The job of a user is the job he has occupied for a given mandate + A job may belong to a mandate + At each new mandate, the jobs available are the job enabled + A job may be left empty + New jobs are created for the current mandate Background: + Given the database is empty + Given the fixtures file "authentication.yml" is loaded Given I authenticate myself as admin - Scenario: It should be possible to get all the jobs. + @crud + Scenario: It should be possible to get all the jobs + Given the fixtures file "job/collection.yml" is loaded When I send a GET request to "/api/jobs" - Then I should get a paged collection with the context "/api/contexts/Job" - And the JSON node "hydra:totalItems" should be greater than 55 + Then the response status code should be 200 + And the response should be in JSON-LD + And I should get a paged collection with the context "/api/contexts/Job" + And the JSON node "hydra:totalItems" should be greater than 11 - #TODO + @crud + Scenario: It should be possible to get a specific job + Given the fixtures file "job/president.yml" is loaded + When I send a GET request to "/api/jobs/1" + Then the response status code should be 200 + And the response should be in JSON-LD + And the JSON response should have the following nodes: + | node | value | type  | + | @context | /api/contexts/Job | | + | @id | /api/jobs/1 | | + | @type | Job | | + | abbreviation | PR |   | + | enabled | true | bool  | + | mandate | | object | + | mandate->@id | /api/mandates/1 | | + | mandate->@type | Mandate | | + | mandate->endAt | | | + | mandate->name | Mandate 2005/2006 | | + | mandate->startAt | | | + | title | President | | + | users | | array | + | users[0] | /api/users/4 | | + And the JSON node "users" should have 1 element -# Scenario: It should be possible to get all the enabled jobs. - #TODO + @crud + Scenario: It should be possible to create a new job + Given the fixtures file "job/mandate2005.yml" is loaded + And the fixtures file "job/user-president.yml" is loaded + When I send a POST request to "/api/jobs" with body: + """ + { + "title": "President", + "abbreviation": "PR", + "enabled": false, + "mandate": "/api/mandates/1", + "users": ["/api/users/1"] + } + """ + Then the response status code should be 201 + And the JSON response should have the following nodes: + | node | value | type | + | @context | /api/contexts/Job | | + | @id | /api/jobs/1 | | + | @type | Job | | + | abbreviation | PR | | + | enabled | false | bool | + | mandate | | object | + | mandate->@id | /api/mandates/1 | | + | mandate->@type | Mandate | | + | mandate->endAt | | | + | mandate->name | Mandate 2005/2006 | | + | mandate->startAt | | | + | title | President | | + | users | | array | + | users[0] | /api/users/1 | | + And the JSON node "users" should have 1 element -# Scenario: It should be possible to order jobs by ID, title or abbreviation. - #TODO + When I send a GET request to "/api/jobs/1" + Then the response status code should be 200 + And the response should be in JSON-LD + And the JSON response should have the following nodes: + | node | value | type | + | @context | /api/contexts/Job | | + | @id | /api/jobs/1 | | + | @type | Job | | + | abbreviation | PR |   | + | enabled | false | bool | + | mandate | | object | + | mandate->@id | /api/mandates/1 | | + | mandate->@type | Mandate | | + | mandate->endAt | | | + | mandate->name | Mandate 2005/2006 | | + | mandate->startAt | | | + | title | President | | + | users | | array | + | users[0] | /api/users/1 | | + And the JSON node "users" should have 1 element -# Scenario: It should be possible to find a job by its ID or title. - #TODO + @crud + Scenario: Data send for creating a job should be validated + When I send a POST request to "/api/jobs" with body: + """ + { + } + """ + Then the response status code should be 400 + And the response should be in JSON-LD + And the JSON node "violations" should have 1 element + And the JSON response should have the following nodes: + | node | value | type  | + | @context | /api/contexts/ConstraintViolationList | | + | @type | ConstraintViolationList | | + | hydra:title | |   | + | hydra:description | | array | + | violations | | array | + | violations[0] | | object | + | violations[0]->propertyPath | title | | + | violations[0]->message | Cette valeur ne doit pas être vide. | | -# Scenario: It shoud be possible to find jobs by their abbreviation (an abbreviation may have several jobs). - #TODO + When I send a GET request to "/api/jobs" + Then the response status code should be 200 + And the response should be in JSON-LD + And I should get a paged collection with the context "/api/contexts/Job" + And the JSON node "hydra:totalItems" should be greater than 0 + + + @crud + Scenario: It should be possible to update a job + Given the fixtures file "job/president.yml" is loaded + When I send a PUT request to "/api/jobs/1" with body: + """ + { + "abbreviation": "Yo", + "title": "Yolo" + } + """ + Then the response status code should be 200 + And the response should be in JSON-LD + And the JSON response should have the following nodes: + | node | value | type | + | @context | /api/contexts/Job | | + | @id | /api/jobs/1 | | + | @type | Job | | + | abbreviation | Yo | | + | enabled | true | bool | + | mandate | | object | + | mandate->@id | /api/mandates/1 | | + | mandate->@type | Mandate | | + | mandate->endAt | | | + | mandate->name | Mandate 2005/2006 | | + | mandate->startAt | | | + | title | Yolo | | + | users | | array | + | users[0] | /api/users/4 | | + And the JSON node "users" should have 1 element + + When I send a GET request to "/api/jobs/1" + Then the response status code should be 200 + And the response should be in JSON-LD + And the JSON response should have the following nodes: + | node | value | type | + | @context | /api/contexts/Job | | + | @id | /api/jobs/1 | | + | @type | Job | | + | abbreviation | Yo | | + | enabled | true | bool | + | mandate | | object | + | mandate->@id | /api/mandates/1 | | + | mandate->@type | Mandate | | + | mandate->endAt | | | + | mandate->name | Mandate 2005/2006 | | + | mandate->startAt | | | + | title | Yolo | | + | users | | array | + | users[0] | /api/users/4 | | + And the JSON node "users" should have 1 element + + When I send a GET request to "/api/mandates/1" + Then the JSON node "jobs[0]" should be equal to "/api/jobs/1" + + When I send a GET request to "/api/users/4" + Then the JSON node "jobs[0]->@id" should be equal to "/api/jobs/1" + Then the JSON node "jobs[0]->abbreviation" should be equal to "Yo" + Then the JSON node "jobs[0]->title" should be equal to "Yolo" + + @crud + Scenario: It should be possible to delete a job. + Given the fixtures file "job/president.yml" is loaded + When I send a GET request to "/api/jobs/1" + Then the JSON node "mandate->@id" should be equal to "/api/mandates/1" + Then the JSON node "users[0]" should be equal to "/api/users/4" + When I send a GET request to "/api/mandates/1" + Then the JSON node "jobs[0]" should be equal to "/api/jobs/1" + When I send a GET request to "/api/users/4" + Then the JSON node "jobs[0]->@id" should be equal to "/api/jobs/1" + + When I send a DELETE request to "/api/jobs/1" + Then the response status code should be 204 + And the JSON should be equal to: + """ + """ + + When I send a GET request to "/api/mandates/1" + Then the JSON node "jobs" should have 0 element + + When I send a GET request to "/api/users/4" + Then the JSON node "jobs" should have 0 element + + @filter + Scenario: It should be possible to get all the enabled jobs. + Given the fixtures file "job/collection-enabled.yml" is loaded + When I send a GET request to "/api/jobs?filter[where][enabled]=1" + Then the response status code should be 200 + And the response should be in JSON-LD + And I should get a paged collection with the context "/api/contexts/Job" + And the JSON node "hydra:totalItems" should be equal to 2 + And the JSON node "hydra:member[0]->enabled" should be equal to true + And the JSON node "hydra:member[1]->enabled" should be equal to true + + @filter + Scenario: It should be possible to order jobs by ID, title or abbreviation. + Given the fixtures file "job/collection-order.yml" is loaded + When I send a GET request to "/api/jobs?filter[order][id]=asc" + Then the response status code should be 200 + And the response should be in JSON-LD + And I should get a paged collection with the context "/api/contexts/Job" + And the JSON node "hydra:totalItems" should be equal to 3 + And the JSON node "hydra:member[0]->@id" should be equal to "/api/jobs/1" + And the JSON node "hydra:member[1]->@id" should be equal to "/api/jobs/2" + And the JSON node "hydra:member[2]->@id" should be equal to "/api/jobs/3" + + When I send a GET request to "/api/jobs?filter[order][id]=desc" + Then the response status code should be 200 + And the response should be in JSON-LD + And I should get a paged collection with the context "/api/contexts/Job" + And the JSON node "hydra:totalItems" should be equal to 3 + And the JSON node "hydra:member[0]->@id" should be equal to "/api/jobs/3" + And the JSON node "hydra:member[1]->@id" should be equal to "/api/jobs/2" + And the JSON node "hydra:member[2]->@id" should be equal to "/api/jobs/1" + + When I send a GET request to "/api/jobs?filter[order][title]=asc" + Then the response status code should be 200 + And the response should be in JSON-LD + And I should get a paged collection with the context "/api/contexts/Job" + And the JSON node "hydra:totalItems" should be equal to 3 + And the JSON node "hydra:member[0]->@id" should be equal to "/api/jobs/1" + And the JSON node "hydra:member[1]->@id" should be equal to "/api/jobs/2" + And the JSON node "hydra:member[2]->@id" should be equal to "/api/jobs/3" + + When I send a GET request to "/api/jobs?filter[order][title]=desc" + Then the response status code should be 200 + And the response should be in JSON-LD + And I should get a paged collection with the context "/api/contexts/Job" + And the JSON node "hydra:totalItems" should be equal to 3 + And the JSON node "hydra:member[0]->@id" should be equal to "/api/jobs/3" + And the JSON node "hydra:member[1]->@id" should be equal to "/api/jobs/2" + And the JSON node "hydra:member[2]->@id" should be equal to "/api/jobs/1" + + When I send a GET request to "/api/jobs?filter[order][abbreviation]=asc" + Then the response status code should be 200 + And the response should be in JSON-LD + And I should get a paged collection with the context "/api/contexts/Job" + And the JSON node "hydra:totalItems" should be equal to 3 + And the JSON node "hydra:member[0]->@id" should be equal to "/api/jobs/1" + And the JSON node "hydra:member[1]->@id" should be equal to "/api/jobs/2" + And the JSON node "hydra:member[2]->@id" should be equal to "/api/jobs/3" + + When I send a GET request to "/api/jobs?filter[order][abbreviation]=desc" + Then the response status code should be 200 + And the response should be in JSON-LD + And I should get a paged collection with the context "/api/contexts/Job" + And the JSON node "hydra:totalItems" should be equal to 3 + And the JSON node "hydra:member[0]->@id" should be equal to "/api/jobs/3" + And the JSON node "hydra:member[1]->@id" should be equal to "/api/jobs/2" + And the JSON node "hydra:member[2]->@id" should be equal to "/api/jobs/1" + + @filter + Scenario: It should be possible to find a job by its ID or title. + Given the fixtures file "job/collection.yml" is loaded + When I send a GET request to "/api/jobs?filter[where][id]=/api/jobs/1" + Then the response status code should be 200 + And the response should be in JSON + And I should get a paged collection with the context "/api/contexts/Job" + And the JSON node "hydra:totalItems" should be equal to 1 + And the JSON node "hydra:member[0]->@id" should be equal to "/api/jobs/1" -# Scenario: When creating a new job, it must have at least one mandate. By default is for the ongoing mandate. - #TODO + When I send a GET request to "/api/jobs?filter[where][title]=President" + Then the response status code should be 200 + And the response should be in JSON + And I should get a paged collection with the context "/api/contexts/Job" + And the JSON node "hydra:totalItems" should be equal to 1 + And the JSON node "hydra:member[0]->@id" should be equal to "/api/jobs/1" -# Scenario: It should be possible to update a job. - #TODO + @filter + Scenario: It shoud be possible to find jobs by their abbreviation (an abbreviation may have several jobs). + Given the fixtures file "job/collection.yml" is loaded + When I send a GET request to "/api/jobs?filter[where][abbreviation]=PR" + Then the response status code should be 200 + And the response should be in JSON + And I should get a paged collection with the context "/api/contexts/Job" + And the JSON node "hydra:totalItems" should be equal to 2 + And the JSON node "hydra:member[0]->@id" should be equal to "/api/jobs/1" + And the JSON node "hydra:member[1]->@id" should be equal to "/api/jobs/2" -# Scenario: It should be possible to delete a job. - #TODO diff --git a/features/api/mandate.feature b/features/api/mandate.feature index 75b550bf4..7ad6fe9e2 100644 --- a/features/api/mandate.feature +++ b/features/api/mandate.feature @@ -18,12 +18,11 @@ Feature: Mandates management Then the response status code should be 200 And I should get a paged collection with the context "/api/contexts/Mandate" - Scenario: Get a resource When I send a GET request to "/api/mandates/1" Then the response status code should be 200 And the JSON node "jobs" should have 2 element - Then the JSON response should should have the following nodes: + Then the JSON response should have the following nodes: | node | value | type  | | @context | /api/contexts/Mandate | | | @id | /api/mandates/1 | | @@ -48,7 +47,7 @@ Feature: Mandates management """ Then the response status code should be 201 And the JSON node "jobs" should have 0 element - Then the JSON response should should have the following nodes: + Then the JSON response should have the following nodes: | node | value | type  | | @context | /api/contexts/Mandate | | | @id | /api/mandates/13 | | @@ -62,7 +61,7 @@ Feature: Mandates management When I send a GET request to "/api/mandates/13" Then the response status code should be 200 And the JSON node "jobs" should have 0 element - Then the JSON response should should have the following nodes: + Then the JSON response should have the following nodes: | node | value | type  | | @context | /api/contexts/Mandate | | | @id | /api/mandates/1 | | @@ -93,7 +92,7 @@ Feature: Mandates management """ Then the response status code should be 400 And the JSON node "violations" should have 2 element - Then the JSON response should should have the following nodes: + Then the JSON response should have the following nodes: | node | value | type  | | @context | /api/contexts/ConstraintViolationList | | | @type | ConstraintViolationList | | diff --git a/features/api/user.feature b/features/api/user.feature index f1d5f8988..8a01be97c 100644 --- a/features/api/user.feature +++ b/features/api/user.feature @@ -18,7 +18,7 @@ Feature: User management And the JSON node "jobs" should have 1 element And the JSON node "types" should have 1 element And the JSON node "roles" should have 2 elements - Then the JSON response should should have the following nodes: + Then the JSON response should have the following nodes: | node | value | type  | | @context | /api/contexts/User | | | @id | /api/users/1 | | diff --git a/features/bootstrap/Context/ApiContext.php b/features/bootstrap/Context/ApiContext.php index a55958252..1ca0b542b 100644 --- a/features/bootstrap/Context/ApiContext.php +++ b/features/bootstrap/Context/ApiContext.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Incipio\Test\Behat\Context; +namespace Behat\Context; use ApiBundle\Entity\User; use Behat\Behat\Context\Context; diff --git a/features/bootstrap/Context/FrontContext.php b/features/bootstrap/Context/FrontContext.php index 0b57e2625..a068051f0 100644 --- a/features/bootstrap/Context/FrontContext.php +++ b/features/bootstrap/Context/FrontContext.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Incipio\Test\Behat\Context; +namespace Behat\Context; use Behat\Behat\Context\Context; use Behat\Gherkin\Node\TableNode; diff --git a/features/bootstrap/Context/JsonContext.php b/features/bootstrap/Context/JsonContext.php index 684fe8d76..f0855cb30 100644 --- a/features/bootstrap/Context/JsonContext.php +++ b/features/bootstrap/Context/JsonContext.php @@ -9,12 +9,12 @@ * file that was distributed with this source code. */ -namespace Incipio\Test\Behat\Context; +namespace Behat\Context; use Behat\Behat\Context\Context; use Behat\Gherkin\Node\TableNode; use PHPUnit_Framework_Assert as PHPUnit; -use Sanpi\Behatch\Context\JsonContext as SampiJsonContext; +use Sanpi\Behatch\Context\JsonContext as BehatchJsonContext; use Sanpi\Behatch\HttpCall\HttpCallResultPool; use Sanpi\Behatch\Json\Json; use Sanpi\Behatch\Json\JsonInspector; @@ -22,7 +22,7 @@ /** * @author Théo FIDRY */ -class JsonContext extends SampiJsonContext implements Context +class JsonContext extends BehatchJsonContext implements Context { /** * @var JsonInspector @@ -118,7 +118,10 @@ public function theJsonNodeShouldBeNull($node) $json = $this->getJson(); $actual = $this->inspector->evaluate($json, $node); - PHPUnit::assertNull($actual); + PHPUnit::assertNull( + $actual, + sprintf('Expected node %s to be not null, got %s instead.', $node, json_encode($actual)) + ); } /** @@ -131,7 +134,10 @@ public function theJsonNodeShouldNotBeNull($node) $json = $this->getJson(); $actual = $this->inspector->evaluate($json, $node); - PHPUnit::assertNotNull($actual); + PHPUnit::assertNotNull( + $actual, + sprintf('Expected node %s to be not null, got %s instead.', $node, $actual) + ); } /** @@ -148,7 +154,7 @@ public function theJsonNodeShouldBeAString($node) } /** - * @Then the JSON response should should have the following nodes: + * @Then the JSON response should have the following nodes: * * @param TableNode $table */ @@ -157,12 +163,13 @@ public function theJSONResponseIsComposedOf(TableNode $table) $count = 0; foreach ($table->getColumnsHash() as $row) { ++$count; - $value = $row['value']; + $expectedValue = $row['value']; // Check for null value - // The `~` is used to specify null value unless the type is explicitely set to string - if ('~' === $value - && (false === isset($row['type']) || 'string' !== $row['type']) + // The `~` is used to specify null value (like in YAML) unless the type is explicitly set to string + // in which case will be processed as the string '~'. + if ('~' === $expectedValue + && (false === array_key_exists('type', $row) || 'string' !== $row['type']) ) { $this->theJsonNodeShouldBeNull($row['node']); @@ -170,30 +177,30 @@ public function theJSONResponseIsComposedOf(TableNode $table) } // Default type is set to string - if (false === isset($row['type'])) { - $row['type'] = 'string'; + $expectedValueType = 'string'; + if (array_key_exists('type', $row)) { + // Trim the expected value type of all spaces before using its value + $_expectedValueType = str_replace(' ', '', $row['type']); + if (false === empty($_expectedValueType)) { + $expectedValueType = $_expectedValueType; + } + unset($_expectedValueType); } - if ('bool' === $row['type'] || 'boolean' === $row['type']) { - if ('false' === $value) { - $value = false; - } else { - // if 'true' === $value will be true - $value = (bool) $value; - } - PHPUnit::assertEquals($value, $this->inspector->evaluate($this->getJson(), $row['node'])); + $expectedValue = $this->normalizeValue($row['value'], $expectedValueType); + if (true === is_bool($expectedValue) || true === is_int($expectedValue)) { + PHPUnit::assertEquals($expectedValue, $this->inspector->evaluate($this->getJson(), $row['node'])); continue; - } elseif ('int' === $row['type'] || 'integer' === $row['type']) { - $value = (int) $value; - PHPUnit::assertEquals($value, $this->inspector->evaluate($this->getJson(), $row['node'])); + } - continue; - } elseif ('array' === $row['type']) { + if ('array' === $expectedValueType) { $this->theJsonNodeShouldBeAnArray($row['node']); continue; - } elseif ('object' === $row['type']) { + } + + if ('object' === $expectedValueType) { $this->theJsonNodeShouldBeAnObject($row['node']); continue; @@ -201,13 +208,19 @@ public function theJSONResponseIsComposedOf(TableNode $table) // If want to compare to an empty string, the value must be `""` in the table // Otherwise an empty string means no check on the value - if ('""' === $value) { + if ('""' === $expectedValue) { $this->theJsonNodeShouldBeEqualTo($row['node'], ''); - } elseif ('' === $value) { + + continue; + } + + if ('' === $expectedValue) { $this->theJsonNodeShouldExist($row['node']); - } else { - $this->theJsonNodeShouldBeEqualTo($row['node'], $value); + + continue; } + + $this->theJsonNodeShouldBeEqualTo($row['node'], $expectedValue); } $nbrOfNodes = $this->getNumberOfNodes($this->getJson()->getContent()); @@ -246,4 +259,29 @@ private function getJson() { return new Json($this->getSession()->getPage()->getContent()); } + + /** + * Helper to cast the string value to its real value depending of its type. + * + * @param string $value + * @param string $type May be 'bool', 'boolean', 'int', 'integer' + * + * @return string|bool|int + */ + private function normalizeValue($value, $type) + { + if ('bool' === $type || 'boolean' === $type) { + if ('false' === $value) { + return false; + } + + return (bool) $value; + } + + if ('int' === $type || 'integer' === $type) { + return (int) $value; + } + + return $value; + } } diff --git a/features/bootstrap/Context/JsonLdContext.php b/features/bootstrap/Context/JsonLdContext.php index 8f7661238..3856a33da 100644 --- a/features/bootstrap/Context/JsonLdContext.php +++ b/features/bootstrap/Context/JsonLdContext.php @@ -9,9 +9,10 @@ * file that was distributed with this source code. */ -namespace Incipio\Test\Behat\Context; +namespace Behat\Context; use Behat\Behat\Context\Context; +use PHPUnit_Framework_Assert as PHPUnit; use Sanpi\Behatch\Context\RestContext; use Sanpi\Behatch\HttpCall\HttpCallResultPool; @@ -42,12 +43,11 @@ public function __construct($evaluationMode = 'javascript', HttpCallResultPool $ */ public function jsonLdResponse() { - $isJson = json_decode($this->getSession()->getDriver()->getContent()); - if (!$isJson) { - throw new \Exception('Expected response content to be JSON.'); - } - $this->theHeaderShouldBeEqualTo('content-type', 'application/ld+json'); + PHPUnit::assertNotNull( + json_decode($this->getSession()->getDriver()->getContent()), + 'Expected response content to be JSON.' + ); } /** diff --git a/features/fixtures/ORM/job/collection-enabled.yml b/features/fixtures/ORM/job/collection-enabled.yml new file mode 100644 index 000000000..becb1ca90 --- /dev/null +++ b/features/fixtures/ORM/job/collection-enabled.yml @@ -0,0 +1,12 @@ +ApiBundle\Entity\Job: + job (template): + title: + abbreviation: + #user: Should be kept unset since because is set in the user entity + #mandate: Should be kept unset since because is set in the mandate entity + + job__{0..1} (extends job): + enabled: true + + job__{10..11} (extends job): + enabled: false diff --git a/features/fixtures/ORM/job/collection-order.yml b/features/fixtures/ORM/job/collection-order.yml new file mode 100644 index 000000000..bb3109b34 --- /dev/null +++ b/features/fixtures/ORM/job/collection-order.yml @@ -0,0 +1,12 @@ +ApiBundle\Entity\Job: + job_0: + title: AAA + abbreviation: BBB + + job_1: + title: AAb + abbreviation: BBc + + job_2: + title: YYY + abbreviation: ZZZ diff --git a/features/fixtures/ORM/job/collection.yml b/features/fixtures/ORM/job/collection.yml new file mode 100644 index 000000000..628f5c9a0 --- /dev/null +++ b/features/fixtures/ORM/job/collection.yml @@ -0,0 +1,35 @@ +ApiBundle\Entity\Job: + job (template): + title: + abbreviation: + enabled: + #user: Should be kept unset since because is set in the user entity + #mandate: Should be kept unset since because is set in the mandate entity + + job_president (extends job): + title: President + abbreviation: PR + enabled: true + + job_pr (extends job): + abbreviation: PR + + job__{1..9} (extends job): {} + +ApiBundle\Entity\Mandate: + mandate_2005: + startAt: + endAt: + jobs (unique): x @job* + +ApiBundle\Entity\User: + user_president: + username: president.tendiserp + fullname: Président TENDISERP + email: president.tendiserp@incipio.fr + roles: [ ROLE_ADMIN ] + plainPassword: guest + enabled: true + jobs: [ @job_president ] + studentConvention: ~ + types: diff --git a/features/fixtures/ORM/job/mandate2005.yml b/features/fixtures/ORM/job/mandate2005.yml new file mode 100644 index 000000000..d124f8b33 --- /dev/null +++ b/features/fixtures/ORM/job/mandate2005.yml @@ -0,0 +1,4 @@ +ApiBundle\Entity\Mandate: + mandate_2005: + startAt: + endAt: diff --git a/features/fixtures/ORM/job/president.yml b/features/fixtures/ORM/job/president.yml new file mode 100644 index 000000000..ff3d9666e --- /dev/null +++ b/features/fixtures/ORM/job/president.yml @@ -0,0 +1,25 @@ +ApiBundle\Entity\Job: + job_president: + title: President + abbreviation: PR + enabled: true + #user: Should be kept unset since because is set in the user entity + #mandate: Should be kept unset since because is set in the mandate entity + +ApiBundle\Entity\Mandate: + mandate_2005: + startAt: + endAt: + jobs (unique): [ @job_president ] + +ApiBundle\Entity\User: + user_president: + username: president.tendiserp + fullname: Président TENDISERP + email: president.tendiserp@incipio.fr + roles: [ ROLE_ADMIN ] + plainPassword: guest + enabled: true + jobs: [ @job_president ] + studentConvention: ~ + types: diff --git a/features/fixtures/ORM/job/user-president.yml b/features/fixtures/ORM/job/user-president.yml new file mode 100644 index 000000000..92c0a2ba4 --- /dev/null +++ b/features/fixtures/ORM/job/user-president.yml @@ -0,0 +1,10 @@ +ApiBundle\Entity\User: + user_president: + username: president.tendiserp + fullname: Président TENDISERP + email: president.tendiserp@incipio.fr + roles: [ ROLE_ADMIN ] + plainPassword: guest + enabled: true + studentConvention: ~ + types: diff --git a/src/ApiBundle/Entity/Job.php b/src/ApiBundle/Entity/Job.php index 62001004c..c2693180a 100644 --- a/src/ApiBundle/Entity/Job.php +++ b/src/ApiBundle/Entity/Job.php @@ -54,6 +54,7 @@ class Job * @ORM\Column(type="boolean") * @Assert\Type("bool") * @Assert\NotNull + * @Groups({"job", "user"}) */ private $enabled = true; @@ -73,6 +74,7 @@ class Job * @ORM\Column(type="string", length=255) * @Assert\Type("string") * @Assert\Length(min=2, max=100) + * @Assert\NotBlank * @Groups({"job", "user"}) */ private $title;