diff --git a/src/Sylius/Bundle/ApiBundle/Resources/config/api_resources/CustomerGroup.xml b/src/Sylius/Bundle/ApiBundle/Resources/config/api_resources/CustomerGroup.xml index 96e13c2576..3bd3c35438 100644 --- a/src/Sylius/Bundle/ApiBundle/Resources/config/api_resources/CustomerGroup.xml +++ b/src/Sylius/Bundle/ApiBundle/Resources/config/api_resources/CustomerGroup.xml @@ -32,6 +32,9 @@ admin:customer_group:create + + admin:customer_group:read + @@ -48,6 +51,9 @@ admin:customer_group:update + + admin:customer_group:read + diff --git a/src/Sylius/Bundle/ApiBundle/Resources/config/serialization/CustomerGroup.xml b/src/Sylius/Bundle/ApiBundle/Resources/config/serialization/CustomerGroup.xml index d5a30f1f8b..5bc40c866c 100644 --- a/src/Sylius/Bundle/ApiBundle/Resources/config/serialization/CustomerGroup.xml +++ b/src/Sylius/Bundle/ApiBundle/Resources/config/serialization/CustomerGroup.xml @@ -16,9 +16,6 @@ xsi:schemaLocation="http://symfony.com/schema/dic/serializer-mapping https://symfony.com/schema/dic/serializer-mapping/serializer-mapping-1.0.xsd" > - - admin:customer_group:read - admin:customer_group:read admin:customer_group:create diff --git a/tests/Api/Admin/CustomerGroupsTest.php b/tests/Api/Admin/CustomerGroupsTest.php new file mode 100644 index 0000000000..4a071bc013 --- /dev/null +++ b/tests/Api/Admin/CustomerGroupsTest.php @@ -0,0 +1,114 @@ +loadFixturesFromFiles(['authentication/api_administrator.yaml', 'customer_group.yaml']); + $header = array_merge($this->logInAdminUser('api@example.com'), self::CONTENT_TYPE_HEADER); + + /** @var CustomerGroupInterface $group */ + $group = $fixtures['group_vip']; + + $this->client->request( + method: 'GET', + uri: sprintf('/api/v2/admin/customer-groups/%s', $group->getCode()), + server: $header, + ); + + $this->assertResponse( + $this->client->getResponse(), + 'admin/customer_group/get_customer_group_response', + Response::HTTP_OK, + ); + } + + /** @test */ + public function it_gets_customer_groups(): void + { + $this->loadFixturesFromFiles(['authentication/api_administrator.yaml', 'customer_group.yaml']); + $header = array_merge($this->logInAdminUser('api@example.com'), self::CONTENT_TYPE_HEADER); + + $this->client->request( + method: 'GET', + uri: '/api/v2/admin/customer-groups', + server: $header, + ); + + $this->assertResponse( + $this->client->getResponse(), + 'admin/customer_group/get_customer_groups_response', + Response::HTTP_OK, + ); + } + + /** @test */ + public function it_creates_a_customer_group(): void + { + $this->loadFixturesFromFiles(['authentication/api_administrator.yaml']); + $header = array_merge($this->logInAdminUser('api@example.com'), self::CONTENT_TYPE_HEADER); + + $this->client->request( + method: 'POST', + uri: '/api/v2/admin/customer-groups', + server: $header, + content: json_encode([ + 'name' => 'Special', + 'code' => 'special', + ], JSON_THROW_ON_ERROR), + ); + + $this->assertResponse( + $this->client->getResponse(), + 'admin/customer_group/post_customer_group_response', + Response::HTTP_CREATED, + ); + } + + /** @test */ + public function it_updates_an_existing_customer_group(): void + { + $fixtures = $this->loadFixturesFromFiles(['authentication/api_administrator.yaml', 'customer_group.yaml']); + + /** @var CustomerGroupInterface $group */ + $group = $fixtures['group_vip']; + + $header = array_merge($this->logInAdminUser('api@example.com'), self::CONTENT_TYPE_HEADER); + + $this->client->request( + method: 'PUT', + uri: '/api/v2/admin/customer-groups/' . $group->getCode(), + server: $header, + content: json_encode([ + 'name' => 'Very Important People', + ], JSON_THROW_ON_ERROR), + ); + + $this->assertResponse( + $this->client->getResponse(), + 'admin/customer_group/put_customer_group_response', + Response::HTTP_OK, + ); + } +} diff --git a/tests/Api/DataFixtures/ORM/customer_group.yaml b/tests/Api/DataFixtures/ORM/customer_group.yaml new file mode 100644 index 0000000000..97ea0d3ed3 --- /dev/null +++ b/tests/Api/DataFixtures/ORM/customer_group.yaml @@ -0,0 +1,7 @@ +Sylius\Component\Customer\Model\CustomerGroup: + group_premium: + code: 'premium' + name: 'Premium' + group_vip: + code: 'vip' + name: 'VIP' diff --git a/tests/Api/Responses/Expected/admin/customer_group/get_customer_group_response.json b/tests/Api/Responses/Expected/admin/customer_group/get_customer_group_response.json new file mode 100644 index 0000000000..01ff8994c4 --- /dev/null +++ b/tests/Api/Responses/Expected/admin/customer_group/get_customer_group_response.json @@ -0,0 +1,7 @@ +{ + "@context": "\/api\/v2\/contexts\/CustomerGroup", + "@id": "\/api\/v2\/admin\/customer-groups\/vip", + "@type": "CustomerGroup", + "code": "vip", + "name": "VIP" +} diff --git a/tests/Api/Responses/Expected/admin/customer_group/get_customer_groups_response.json b/tests/Api/Responses/Expected/admin/customer_group/get_customer_groups_response.json new file mode 100644 index 0000000000..387c9caf8e --- /dev/null +++ b/tests/Api/Responses/Expected/admin/customer_group/get_customer_groups_response.json @@ -0,0 +1,20 @@ +{ + "@context": "\/api\/v2\/contexts\/CustomerGroup", + "@id": "\/api\/v2\/admin\/customer-groups", + "@type": "hydra:Collection", + "hydra:member": [ + { + "@id": "\/api\/v2\/admin\/customer-groups\/premium", + "@type": "CustomerGroup", + "code": "premium", + "name": "Premium" + }, + { + "@id": "\/api\/v2\/admin\/customer-groups\/vip", + "@type": "CustomerGroup", + "code": "vip", + "name": "VIP" + } + ], + "hydra:totalItems": 2 +} diff --git a/tests/Api/Responses/Expected/admin/customer_group/post_customer_group_response.json b/tests/Api/Responses/Expected/admin/customer_group/post_customer_group_response.json new file mode 100644 index 0000000000..3206ed4885 --- /dev/null +++ b/tests/Api/Responses/Expected/admin/customer_group/post_customer_group_response.json @@ -0,0 +1,7 @@ +{ + "@context": "\/api\/v2\/contexts\/CustomerGroup", + "@id": "\/api\/v2\/admin\/customer-groups\/special", + "@type": "CustomerGroup", + "code": "special", + "name": "Special" +} diff --git a/tests/Api/Responses/Expected/admin/customer_group/put_customer_group_response.json b/tests/Api/Responses/Expected/admin/customer_group/put_customer_group_response.json new file mode 100644 index 0000000000..fd00865537 --- /dev/null +++ b/tests/Api/Responses/Expected/admin/customer_group/put_customer_group_response.json @@ -0,0 +1,7 @@ +{ + "@context": "\/api\/v2\/contexts\/CustomerGroup", + "@id": "\/api\/v2\/admin\/customer-groups\/vip", + "@type": "CustomerGroup", + "code": "vip", + "name": "Very Important People" +}