From ba7e4b6f293b449fdf0b209c8c001f485577d31f Mon Sep 17 00:00:00 2001 From: Wojdylak Date: Thu, 9 May 2024 21:12:20 +0200 Subject: [PATCH] [API][Address] Enable unit tests --- tests/Api/Admin/AddressesTest.php | 8 +- .../ORM/address_with_customer.yaml | 8 +- .../address/get_address_log_entries.json | 11 +- ...address_response.json => get_address.json} | 0 ...esses_response.json => get_addresses.json} | 4 +- ...n => post_address_with_province_code.json} | 0 ...n => post_address_with_province_name.json} | 0 ...son => post_address_without_province.json} | 0 ...address_response.json => put_address.json} | 0 tests/Api/Shop/AddressesTest.php | 118 ++++++++++++++---- 10 files changed, 111 insertions(+), 38 deletions(-) rename tests/Api/Responses/shop/address/{get_address_response.json => get_address.json} (100%) rename tests/Api/Responses/shop/address/{get_addresses_response.json => get_addresses.json} (94%) rename tests/Api/Responses/shop/address/{post_address_with_province_code_response.json => post_address_with_province_code.json} (100%) rename tests/Api/Responses/shop/address/{post_address_with_province_name_response.json => post_address_with_province_name.json} (100%) rename tests/Api/Responses/shop/address/{post_address_without_province_response.json => post_address_without_province.json} (100%) rename tests/Api/Responses/shop/address/{put_address_response.json => put_address.json} (100%) diff --git a/tests/Api/Admin/AddressesTest.php b/tests/Api/Admin/AddressesTest.php index 7b49958570..3ff2ec073e 100644 --- a/tests/Api/Admin/AddressesTest.php +++ b/tests/Api/Admin/AddressesTest.php @@ -25,7 +25,7 @@ final class AddressesTest extends JsonApiTestCase $fixtures = $this->loadFixturesFromFiles(['authentication/api_administrator.yaml', 'address_with_customer.yaml']); /** @var AddressInterface $address */ - $address = $fixtures['address']; + $address = $fixtures['address_tony']; $this->client->request( method: 'GET', @@ -46,7 +46,7 @@ final class AddressesTest extends JsonApiTestCase $fixtures = $this->loadFixturesFromFiles(['authentication/api_administrator.yaml', 'address_with_customer.yaml']); /** @var AddressInterface $address */ - $address = $fixtures['address']; + $address = $fixtures['address_tony']; $this->client->request( method: 'PUT', @@ -78,7 +78,7 @@ final class AddressesTest extends JsonApiTestCase $fixtures = $this->loadFixturesFromFiles(['authentication/api_administrator.yaml', 'address_with_customer.yaml']); /** @var AddressInterface $address */ - $address = $fixtures['address']; + $address = $fixtures['address_tony']; $this->client->request( method: 'PUT', @@ -120,7 +120,7 @@ final class AddressesTest extends JsonApiTestCase $fixtures = $this->loadFixturesFromFiles(['authentication/api_administrator.yaml', 'address_with_customer.yaml']); /** @var AddressInterface $address */ - $address = $fixtures['address']; + $address = $fixtures['address_tony']; $this->client->request( method: 'GET', diff --git a/tests/Api/DataFixtures/ORM/address_with_customer.yaml b/tests/Api/DataFixtures/ORM/address_with_customer.yaml index 98afb1e267..bafb2c0642 100644 --- a/tests/Api/DataFixtures/ORM/address_with_customer.yaml +++ b/tests/Api/DataFixtures/ORM/address_with_customer.yaml @@ -1,8 +1,8 @@ Sylius\Component\Core\Model\Address: - address: + address_{tony, oliver}: firstName: "John" lastName: "Doe" - customer: '@customer_tony' + customer: "@customer_" company: "CocaCola" street: "Green Avenue" countryCode: "US" @@ -11,7 +11,7 @@ Sylius\Component\Core\Model\Address: phoneNumber: "123456789" Sylius\Component\Core\Model\ShopUser: - shop_user_{tony}: + shop_user_{tony, oliver}: plainPassword: "sylius" roles: [ROLE_USER] enabled: true @@ -20,7 +20,7 @@ Sylius\Component\Core\Model\ShopUser: usernameCanonical: "\\@example.com" Sylius\Component\Core\Model\Customer: - customer_{tony}: + customer_{tony, oliver}: firstName: "" lastName: "Jones" email: "\\@example.com" diff --git a/tests/Api/Responses/admin/address/get_address_log_entries.json b/tests/Api/Responses/admin/address/get_address_log_entries.json index 514045e35e..31bcedb37a 100644 --- a/tests/Api/Responses/admin/address/get_address_log_entries.json +++ b/tests/Api/Responses/admin/address/get_address_log_entries.json @@ -1,9 +1,11 @@ { - "@context": "\/api\/v2\/contexts\/Address", - "@id": "\/api\/v2\/shop\/addresses", + "@context": "\/api\/v2\/contexts\/AddressLogEntry", + "@id": "\/api\/v2\/admin\/addresses\/@integer@\/log-entries", "@type": "hydra:Collection", + "hydra:totalItems": 1, "hydra:member": [ { + "@id": "\/api\/v2\/admin\/address-log-entries\/@integer@", "@type": "AddressLogEntry", "action": "create", "version": 1, @@ -19,8 +21,7 @@ "provinceCode": null, "provinceName": null }, - "loggedAt": @date@ + "loggedAt": "@date@" } - ], - "hydra:totalItems": 1 + ] } diff --git a/tests/Api/Responses/shop/address/get_address_response.json b/tests/Api/Responses/shop/address/get_address.json similarity index 100% rename from tests/Api/Responses/shop/address/get_address_response.json rename to tests/Api/Responses/shop/address/get_address.json diff --git a/tests/Api/Responses/shop/address/get_addresses_response.json b/tests/Api/Responses/shop/address/get_addresses.json similarity index 94% rename from tests/Api/Responses/shop/address/get_addresses_response.json rename to tests/Api/Responses/shop/address/get_addresses.json index f3687bb755..1333e5e68b 100644 --- a/tests/Api/Responses/shop/address/get_addresses_response.json +++ b/tests/Api/Responses/shop/address/get_addresses.json @@ -2,6 +2,7 @@ "@context": "\/api\/v2\/contexts\/Address", "@id": "\/api\/v2\/shop\/addresses", "@type": "hydra:Collection", + "hydra:totalItems": 1, "hydra:member": [ { "@id": "\/api\/v2\/shop\/addresses\/@integer@", @@ -18,6 +19,5 @@ "city": "New York", "postcode": "00000" } - ], - "hydra:totalItems": 1 + ] } diff --git a/tests/Api/Responses/shop/address/post_address_with_province_code_response.json b/tests/Api/Responses/shop/address/post_address_with_province_code.json similarity index 100% rename from tests/Api/Responses/shop/address/post_address_with_province_code_response.json rename to tests/Api/Responses/shop/address/post_address_with_province_code.json diff --git a/tests/Api/Responses/shop/address/post_address_with_province_name_response.json b/tests/Api/Responses/shop/address/post_address_with_province_name.json similarity index 100% rename from tests/Api/Responses/shop/address/post_address_with_province_name_response.json rename to tests/Api/Responses/shop/address/post_address_with_province_name.json diff --git a/tests/Api/Responses/shop/address/post_address_without_province_response.json b/tests/Api/Responses/shop/address/post_address_without_province.json similarity index 100% rename from tests/Api/Responses/shop/address/post_address_without_province_response.json rename to tests/Api/Responses/shop/address/post_address_without_province.json diff --git a/tests/Api/Responses/shop/address/put_address_response.json b/tests/Api/Responses/shop/address/put_address.json similarity index 100% rename from tests/Api/Responses/shop/address/put_address_response.json rename to tests/Api/Responses/shop/address/put_address.json diff --git a/tests/Api/Shop/AddressesTest.php b/tests/Api/Shop/AddressesTest.php index 6ef289dae5..dbc3387295 100644 --- a/tests/Api/Shop/AddressesTest.php +++ b/tests/Api/Shop/AddressesTest.php @@ -28,12 +28,11 @@ final class AddressesTest extends JsonApiTestCase /** @test */ public function it_denies_access_to_get_addresses_for_not_authenticated_user(): void { - $this->loadFixturesFromFiles(['authentication/customer.yaml']); + $this->loadFixturesFromFiles(['authentication/shop_user.yaml']); $this->client->request(method: 'GET', uri: '/api/v2/shop/addresses', server: self::CONTENT_TYPE_HEADER); - $response = $this->client->getResponse(); - $this->assertSame(Response::HTTP_UNAUTHORIZED, $response->getStatusCode()); + $this->assertResponseCode($this->client->getResponse(), Response::HTTP_UNAUTHORIZED); } /** @test */ @@ -49,8 +48,7 @@ final class AddressesTest extends JsonApiTestCase $this->assertResponse( $this->client->getResponse(), - 'shop/address/get_addresses_response', - Response::HTTP_OK, + 'shop/address/get_addresses', ); } @@ -61,7 +59,7 @@ final class AddressesTest extends JsonApiTestCase /** @var CustomerInterface $customer */ $customer = $fixtures['customer_tony']; /** @var AddressInterface $address */ - $address = $fixtures['address']; + $address = $fixtures['address_tony']; $header = array_merge($this->logInShopUser($customer->getEmailCanonical()), self::CONTENT_TYPE_HEADER); @@ -69,15 +67,30 @@ final class AddressesTest extends JsonApiTestCase $this->assertResponse( $this->client->getResponse(), - 'shop/address/get_address_response', - Response::HTTP_OK, + 'shop/address/get_address', ); } + /** @test */ + public function it_does_not_get_an_address_of_another_customer(): void + { + $fixtures = $this->loadFixturesFromFiles(['address_with_customer.yaml']); + /** @var CustomerInterface $customer */ + $customer = $fixtures['customer_tony']; + /** @var AddressInterface $address */ + $address = $fixtures['address_oliver']; + + $header = array_merge($this->logInShopUser($customer->getEmailCanonical()), self::CONTENT_TYPE_HEADER); + + $this->client->request(method: 'GET', uri: '/api/v2/shop/addresses/' . $address->getId(), server: $header); + + $this->assertResponseCode($this->client->getResponse(), Response::HTTP_NOT_FOUND); + } + /** @test */ public function it_denies_access_to_create_an_address_for_not_authenticated_user(): void { - $fixtures = $this->loadFixturesFromFiles(['authentication/customer.yaml', 'country.yaml']); + $fixtures = $this->loadFixturesFromFiles(['authentication/shop_user.yaml', 'country.yaml']); /** @var CountryInterface $country */ $country = $fixtures['country_DE']; @@ -90,14 +103,13 @@ final class AddressesTest extends JsonApiTestCase content: json_encode($bodyRequest, \JSON_THROW_ON_ERROR), ); - $response = $this->client->getResponse(); - $this->assertSame(Response::HTTP_UNAUTHORIZED, $response->getStatusCode()); + $this->assertResponseCode($this->client->getResponse(), Response::HTTP_UNAUTHORIZED); } /** @test */ public function it_creates_a_new_address_with_country_and_province_code(): void { - $fixtures = $this->loadFixturesFromFiles(['authentication/customer.yaml', 'country.yaml']); + $fixtures = $this->loadFixturesFromFiles(['authentication/shop_user.yaml', 'country.yaml']); /** @var CustomerInterface $customer */ $customer = $fixtures['customer_oliver']; /** @var CountryInterface $country */ @@ -118,7 +130,7 @@ final class AddressesTest extends JsonApiTestCase $this->assertResponse( $this->client->getResponse(), - 'shop/address/post_address_with_province_code_response', + 'shop/address/post_address_with_province_code', Response::HTTP_CREATED, ); } @@ -126,7 +138,7 @@ final class AddressesTest extends JsonApiTestCase /** @test */ public function it_creates_a_new_address_with_country_and_province_code_when_the_country_code_is_set_after_province_code_in_body(): void { - $fixtures = $this->loadFixturesFromFiles(['authentication/customer.yaml', 'country.yaml']); + $fixtures = $this->loadFixturesFromFiles(['authentication/shop_user.yaml', 'country.yaml']); /** @var CustomerInterface $customer */ $customer = $fixtures['customer_oliver']; /** @var CountryInterface $country */ @@ -155,7 +167,7 @@ final class AddressesTest extends JsonApiTestCase $this->assertResponse( $this->client->getResponse(), - 'shop/address/post_address_with_province_code_response', + 'shop/address/post_address_with_province_code', Response::HTTP_CREATED, ); } @@ -163,7 +175,7 @@ final class AddressesTest extends JsonApiTestCase /** @test */ public function it_creates_a_new_address_with_country_and_province_name(): void { - $fixtures = $this->loadFixturesFromFiles(['authentication/customer.yaml', 'country.yaml']); + $fixtures = $this->loadFixturesFromFiles(['authentication/shop_user.yaml', 'country.yaml']); /** @var CustomerInterface $customer */ $customer = $fixtures['customer_oliver']; /** @var CountryInterface $country */ @@ -182,7 +194,7 @@ final class AddressesTest extends JsonApiTestCase $this->assertResponse( $this->client->getResponse(), - 'shop/address/post_address_with_province_name_response', + 'shop/address/post_address_with_province_name', Response::HTTP_CREATED, ); } @@ -190,7 +202,7 @@ final class AddressesTest extends JsonApiTestCase /** @test */ public function it_creates_a_new_address_with_country_without_province_data(): void { - $fixtures = $this->loadFixturesFromFiles(['authentication/customer.yaml', 'country.yaml']); + $fixtures = $this->loadFixturesFromFiles(['authentication/shop_user.yaml', 'country.yaml']); /** @var CustomerInterface $customer */ $customer = $fixtures['customer_oliver']; /** @var CountryInterface $country */ @@ -209,7 +221,7 @@ final class AddressesTest extends JsonApiTestCase $this->assertResponse( $this->client->getResponse(), - 'shop/address/post_address_without_province_response', + 'shop/address/post_address_without_province', Response::HTTP_CREATED, ); } @@ -217,7 +229,7 @@ final class AddressesTest extends JsonApiTestCase /** @test */ public function it_does_not_create_a_new_address_with_invalid_data(): void { - $fixtures = $this->loadFixturesFromFiles(['authentication/customer.yaml', 'country.yaml']); + $fixtures = $this->loadFixturesFromFiles(['authentication/shop_user.yaml', 'country.yaml']); /** @var CustomerInterface $customer */ $customer = $fixtures['customer_oliver']; @@ -263,7 +275,7 @@ final class AddressesTest extends JsonApiTestCase /** @var CustomerInterface $customer */ $customer = $fixtures['customer_tony']; /** @var AddressInterface $address */ - $address = $fixtures['address']; + $address = $fixtures['address_tony']; $header = array_merge($this->logInShopUser($customer->getEmailCanonical()), self::CONTENT_TYPE_HEADER); @@ -286,11 +298,71 @@ final class AddressesTest extends JsonApiTestCase $this->assertResponse( $this->client->getResponse(), - 'shop/address/put_address_response', - Response::HTTP_OK, + 'shop/address/put_address', ); } + /** @test */ + public function it_does_not_update_an_address_of_another_customer(): void + { + $fixtures = $this->loadFixturesFromFiles(['address_with_customer.yaml']); + /** @var CustomerInterface $customer */ + $customer = $fixtures['customer_tony']; + /** @var AddressInterface $address */ + $address = $fixtures['address_oliver']; + + $header = array_merge($this->logInShopUser($customer->getEmailCanonical()), self::CONTENT_TYPE_HEADER); + + $this->client->request( + method: 'PUT', + uri: '/api/v2/shop/addresses/' . $address->getId(), + server: $header, + content: json_encode([ + 'firstName' => 'Tony', + ], \JSON_THROW_ON_ERROR), + ); + + $this->assertResponseCode($this->client->getResponse(), Response::HTTP_NOT_FOUND); + } + + /** @test */ + public function it_deletes_an_address(): void + { + $fixtures = $this->loadFixturesFromFiles(['address_with_customer.yaml']); + /** @var CustomerInterface $customer */ + $customer = $fixtures['customer_tony']; + /** @var AddressInterface $address */ + $address = $fixtures['address_tony']; + + $headers = array_merge($this->logInShopUser($customer->getEmailCanonical()), self::CONTENT_TYPE_HEADER); + + $this->requestDelete( + uri: '/api/v2/shop/addresses/' . $address->getId(), + headers: $headers, + ); + + $this->assertResponseCode($this->client->getResponse(), Response::HTTP_NO_CONTENT); + } + + /** @test */ + public function it_does_not_delete_an_address_of_another_customer(): void + { + $fixtures = $this->loadFixturesFromFiles(['address_with_customer.yaml']); + /** @var CustomerInterface $customer */ + $customer = $fixtures['customer_tony']; + /** @var AddressInterface $address */ + $address = $fixtures['address_oliver']; + + $headers = array_merge($this->logInShopUser($customer->getEmailCanonical()), self::CONTENT_TYPE_HEADER); + + $this->requestDelete( + uri: '/api/v2/shop/addresses/' . $address->getId(), + headers: $headers, + ); + + $this->assertResponseCode($this->client->getResponse(), Response::HTTP_NOT_FOUND); + } + private function createBodyRequest( string $countryCode, ?string $provinceCode = null,