diff --git a/src/Sylius/Bundle/ApiBundle/Resources/config/routing/zone.yml b/src/Sylius/Bundle/ApiBundle/Resources/config/routing/zone.yml index bf40ba910c..0dd389e787 100644 --- a/src/Sylius/Bundle/ApiBundle/Resources/config/routing/zone.yml +++ b/src/Sylius/Bundle/ApiBundle/Resources/config/routing/zone.yml @@ -21,6 +21,12 @@ sylius_api_zone_create: arguments: type: $type +sylius_api_zone_update: + path: /{id} + methods: [PUT, PATCH] + defaults: + _controller: sylius.controller.zone:updateAction + sylius_api_zone_delete: path: /{id} methods: [DELETE] diff --git a/tests/Controller/ZoneApiTest.php b/tests/Controller/ZoneApiTest.php index 25113b4e77..dd8f1ce764 100644 --- a/tests/Controller/ZoneApiTest.php +++ b/tests/Controller/ZoneApiTest.php @@ -151,4 +151,125 @@ EOT; $response = $this->client->getResponse(); $this->assertResponse($response, 'zone/show_response', Response::HTTP_OK); } + + /** + * @test + */ + public function it_denies_zone_full_update_for_not_authenticated_users() + { + $this->client->request('PUT', '/api/zones/1'); + + $response = $this->client->getResponse(); + $this->assertResponse($response, 'authentication/access_denied_response', Response::HTTP_UNAUTHORIZED); + } + + /** + * @test + */ + public function it_returns_not_found_response_when_requesting_full_update_of_a_zone_which_does_not_exist() + { + $this->loadFixturesFromFile('authentication/api_administrator.yml'); + + $this->client->request('PUT', '/api/zones/-1', [], [], static::$authorizedHeaderWithAccept); + + $response = $this->client->getResponse(); + $this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND); + } + + /** + * @test + */ + public function it_does_not_allow_to_update_zone_fully_without_specifying_required_data() + { + $this->loadFixturesFromFile('authentication/api_administrator.yml'); + $zones = $this->loadFixturesFromFile('resources/zones.yml'); + + $this->client->request('PUT', '/api/zones/'.$zones['zone_eu']->getId(), [], [], static::$authorizedHeaderWithContentType); + + $response = $this->client->getResponse(); + $this->assertResponse($response, 'zone/update_validation_fail_response', Response::HTTP_BAD_REQUEST); + } + + /** + * @test + */ + public function it_allows_to_update_zone_fully() + { + $this->loadFixturesFromFile('authentication/api_administrator.yml'); + $this->loadFixturesFromFile('resources/countries.yml'); + $zones = $this->loadFixturesFromFile('resources/zones.yml'); + + $data = +<<client->request('PUT', '/api/zones/'.$zones['zone_eu']->getId(), [], [], static::$authorizedHeaderWithContentType, $data); + + $response = $this->client->getResponse(); + $this->assertResponseCode($response, Response::HTTP_NO_CONTENT); + + $this->client->request('GET', '/api/zones/'.$zones['zone_eu']->getId(), [], [], static::$authorizedHeaderWithAccept); + + $response = $this->client->getResponse(); + $this->assertResponse($response, 'zone/update_response', Response::HTTP_OK); + } + + /** + * @test + */ + public function it_denies_zone_partial_update_for_not_authenticated_users() + { + $this->client->request('PATCH', '/api/zones/1'); + + $response = $this->client->getResponse(); + $this->assertResponse($response, 'authentication/access_denied_response', Response::HTTP_UNAUTHORIZED); + } + + /** + * @test + */ + public function it_returns_not_found_response_when_requesting_partial_update_of_a_zone_which_does_not_exist() + { + $this->loadFixturesFromFile('authentication/api_administrator.yml'); + + $this->client->request('PATCH', '/api/zones/-1', [], [], static::$authorizedHeaderWithAccept); + + $response = $this->client->getResponse(); + $this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND); + } + + /** + * @test + */ + public function it_allows_to_update_zone_partially() + { + $this->loadFixturesFromFile('authentication/api_administrator.yml'); + $this->loadFixturesFromFile('resources/countries.yml'); + $zones = $this->loadFixturesFromFile('resources/zones.yml'); + + $data = +<<client->request('PATCH', '/api/zones/'.$zones['zone_eu']->getId(), [], [], static::$authorizedHeaderWithContentType, $data); + + $response = $this->client->getResponse(); + $this->assertResponseCode($response, Response::HTTP_NO_CONTENT); + + $this->client->request('GET', '/api/zones/'.$zones['zone_eu']->getId(), [], [], static::$authorizedHeaderWithAccept); + + $response = $this->client->getResponse(); + $this->assertResponse($response, 'zone/update_response', Response::HTTP_OK); + } } diff --git a/tests/DataFixtures/ORM/resources/countries.yml b/tests/DataFixtures/ORM/resources/countries.yml index 3b5386f6a4..29fd2e3e8c 100644 --- a/tests/DataFixtures/ORM/resources/countries.yml +++ b/tests/DataFixtures/ORM/resources/countries.yml @@ -3,6 +3,8 @@ Sylius\Component\Addressing\Model\Country: code: NL country_BE: code: BE + country_PL: + code: PL Sylius\Component\Addressing\Model\Province: province_BE_limburg: diff --git a/tests/Responses/Expected/zone/update_response.json b/tests/Responses/Expected/zone/update_response.json new file mode 100644 index 0000000000..1e2a7d67ac --- /dev/null +++ b/tests/Responses/Expected/zone/update_response.json @@ -0,0 +1,11 @@ +{ + "id": @integer@, + "code": "EU", + "name": "European Union +", + "type": "country", + "_links": { + "self": { + "href": @string@ + } + } +} diff --git a/tests/Responses/Expected/zone/update_validation_fail_response.json b/tests/Responses/Expected/zone/update_validation_fail_response.json new file mode 100644 index 0000000000..04f0185310 --- /dev/null +++ b/tests/Responses/Expected/zone/update_validation_fail_response.json @@ -0,0 +1,20 @@ +{ + "code": 400, + "message": "Validation Failed", + "errors": { + "errors": [ + "Please add at least 1 zone member." + ], + "children": { + "name": { + "errors": [ + "Please enter zone name." + ] + }, + "type": {}, + "members": {}, + "scope": {}, + "code": {} + } + } +}