From 09e626608b610cb8b7a3bfbb2fb470fa3490186e Mon Sep 17 00:00:00 2001 From: Rafikooo Date: Thu, 16 Nov 2023 21:26:22 +0100 Subject: [PATCH] [Unit] Add tests for shipping address of placed order --- tests/Api/Admin/CustomersTest.php | 2 +- tests/Api/Admin/OrdersTest.php | 195 ++++++++++++++++-- .../Api/DataFixtures/ORM/customer_order.yaml | 15 +- .../Api/DataFixtures/ORM/order/customer.yaml | 7 + tests/Api/DataFixtures/ORM/order/order.yaml | 38 ++++ ...ing_address_of_placed_order_response.json} | 16 +- ...ping_address_of_placed_order_response.json | 17 ++ .../gets_orders_for_customer_response.json | 15 +- ...ling_address_of_placed_order_response.json | 17 ++ ...ping_address_of_placed_order_response.json | 17 ++ 10 files changed, 313 insertions(+), 26 deletions(-) create mode 100644 tests/Api/DataFixtures/ORM/order/customer.yaml create mode 100644 tests/Api/DataFixtures/ORM/order/order.yaml rename tests/Api/Responses/Expected/admin/order/{get_order_billing_data_response.json => get_billing_address_of_placed_order_response.json} (55%) create mode 100644 tests/Api/Responses/Expected/admin/order/get_shipping_address_of_placed_order_response.json create mode 100644 tests/Api/Responses/Expected/admin/order/put_billing_address_of_placed_order_response.json create mode 100644 tests/Api/Responses/Expected/admin/order/put_shipping_address_of_placed_order_response.json diff --git a/tests/Api/Admin/CustomersTest.php b/tests/Api/Admin/CustomersTest.php index 63a92e6d72..614ec6bf5d 100644 --- a/tests/Api/Admin/CustomersTest.php +++ b/tests/Api/Admin/CustomersTest.php @@ -11,7 +11,7 @@ declare(strict_types=1); -namespace Api\Admin; +namespace Sylius\Tests\Api\Admin; use Sylius\Component\Core\Model\CustomerInterface; use Sylius\Component\Customer\Model\CustomerGroupInterface; diff --git a/tests/Api/Admin/OrdersTest.php b/tests/Api/Admin/OrdersTest.php index 7d0e7c7c7a..243b364b35 100644 --- a/tests/Api/Admin/OrdersTest.php +++ b/tests/Api/Admin/OrdersTest.php @@ -72,15 +72,49 @@ final class OrdersTest extends JsonApiTestCase } /** @test */ - public function it_updates_an_order_billing_address(): void + public function it_gets_a_billing_address_of_placed_order(): void { $fixtures = $this->loadFixturesFromFiles([ 'authentication/api_administrator.yaml', 'channel.yaml', - 'customer.yaml', - 'customer_order.yaml', + 'order/order.yaml', + 'order/customer.yaml', ]); + $headers = $this + ->headerBuilder() + ->withJsonLdAccept() + ->withAdminUserAuthorization('api@example.com') + ->build() + ; + + /** @var AddressInterface $billingAddress */ + $billingAddress = $fixtures['billing_address']; + + $this->client->request( + method: 'GET', + uri: '/api/v2/admin/addresses/' . $billingAddress->getId(), + server: $headers, + ); + + $this->assertResponse( + $this->client->getResponse(), + 'admin/order/get_billing_address_of_placed_order_response', + Response::HTTP_OK, + ); + } + + /** @test */ + public function it_updates_a_billing_address_of_placed_order(): void + { + $fixtures = $this->loadFixturesFromFiles([ + 'authentication/api_administrator.yaml', + 'channel.yaml', + 'order/customer.yaml', + 'order/order.yaml', + ]); + + $headers = $this ->headerBuilder() ->withJsonLdAccept() @@ -97,32 +131,34 @@ final class OrdersTest extends JsonApiTestCase uri: '/api/v2/admin/addresses/' . $billingAddress->getId(), server: $headers, content: json_encode([ - 'firstName' => 'Adam', - 'lastName' => 'Handley', - 'company'=> 'FMŻ', - 'street' => 'Kościuszki 21', - 'countryCode' => 'FR', - 'city' => 'Bordeaux', - 'postcode' => '99-999', - 'phoneNumber' => '911213969', + 'firstName' => 'Updated: Adam', + 'lastName' => 'Updated: Handley', + 'company'=> 'Updated: FMŻ', + 'street' => 'Updated: Kościuszki 21', + 'countryCode' => 'Updated: FR', + 'city' => 'Updated: Bordeaux', + 'postcode' => 'Updated: 99-999', + 'phoneNumber' => 'Updated: 911213969', + 'provinceCode' => 'Updated: PL-WP', + 'provinceName' => 'Updated: wielkopolskie' ]), ); $this->assertResponse( $this->client->getResponse(), - 'admin/order/get_order_billing_data_response', + 'admin/order/put_billing_address_of_placed_order_response', Response::HTTP_OK, ); } /** @test */ - public function it_does_not_allow_to_update_customer_of_already_placed_order(): void + public function it_prevents_customer_update_in_billing_address_of_placed_order(): void { $fixtures = $this->loadFixturesFromFiles([ 'authentication/api_administrator.yaml', 'channel.yaml', - 'customer.yaml', - 'customer_order.yaml', + 'order/order.yaml', + 'order/customer.yaml', ]); $headers = $this @@ -164,4 +200,133 @@ final class OrdersTest extends JsonApiTestCase json_decode($this->client->getResponse()->getContent())->customer ); } + + /** @test */ + public function it_gets_a_shipping_address_of_placed_order(): void + { + $fixtures = $this->loadFixturesFromFiles([ + 'authentication/api_administrator.yaml', + 'channel.yaml', + 'order/customer.yaml', + 'order/order.yaml', + ]); + + $headers = $this + ->headerBuilder() + ->withJsonLdAccept() + ->withAdminUserAuthorization('api@example.com') + ->build() + ; + + /** @var AddressInterface $shippingAddress */ + $shippingAddress = $fixtures['shipping_address']; + + $this->client->request( + method: 'GET', + uri: '/api/v2/admin/addresses/' . $shippingAddress->getId(), + server: $headers, + ); + + $this->assertResponse( + $this->client->getResponse(), + 'admin/order/get_shipping_address_of_placed_order_response', + Response::HTTP_OK, + ); + } + + /** @test */ + public function it_updated_a_shipping_address_of_placed_order(): void + { + $fixtures = $this->loadFixturesFromFiles([ + 'authentication/api_administrator.yaml', + 'channel.yaml', + 'order/customer.yaml', + 'order/order.yaml', + ]); + + $headers = $this + ->headerBuilder() + ->withJsonLdAccept() + ->withJsonLdContentType() + ->withAdminUserAuthorization('api@example.com') + ->build() + ; + + /** @var AddressInterface $shippingAddress */ + $shippingAddress = $fixtures['shipping_address']; + + $this->client->request( + method: 'PUT', + uri: '/api/v2/admin/addresses/' . $shippingAddress->getId(), + server: $headers, + content: json_encode([ + 'firstName' => 'Updated: Julia', + 'lastName' => 'Updated: Kowalska', + 'company' => 'Updated: Błysk', + 'street' => 'Updated: Marszałkowska 10', + 'countryCode' => 'Updated: PL', + 'city' => 'Updated: Warszawa', + 'postcode' => 'Updated: 00-001', + 'phoneNumber' => 'Updated: 48222333444', + 'provinceCode' => 'Updated: PL-MA', + 'provinceName' => 'Updated: mazowieckie' + ]), + ); + + $this->assertResponse( + $this->client->getResponse(), + 'admin/order/put_shipping_address_of_placed_order_response', + Response::HTTP_OK, + ); + } + + /** @test */ + public function it_prevents_customer_update_in_shipping_address_of_placed_order(): void + { + $fixtures = $this->loadFixturesFromFiles([ + 'authentication/api_administrator.yaml', + 'channel.yaml', + 'order/customer.yaml', + 'order/order.yaml', + ]); + + $headers = $this + ->headerBuilder() + ->withJsonLdAccept() + ->withJsonLdContentType() + ->withAdminUserAuthorization('api@example.com') + ->build() + ; + + /** @var AddressInterface $shippingAddress */ + $shippingAddress = $fixtures['shipping_address']; + + /** @var CustomerInterface $customerTony */ + $customerTony = $fixtures['customer_tony']; + + /** @var CustomerInterface $customerDave */ + $customerDave = $fixtures['customer_dave']; + + $this->client->request( + method: 'PUT', + uri: '/api/v2/admin/addresses/' . $shippingAddress->getId(), + server: $headers, + content: json_encode([ + 'customer' => '/api/v2/admin/customers/' . $customerDave->getId(), + ]), + ); + + $this->assertResponseCode($this->client->getResponse(), Response::HTTP_OK); + + $this->client->request( + method: 'GET', + uri: '/api/v2/admin/addresses/' . $shippingAddress->getId(), + server: $headers, + ); + + $this->assertSame( + '/api/v2/admin/customers/' . $customerTony->getId(), + json_decode($this->client->getResponse()->getContent())->customer + ); + } } diff --git a/tests/Api/DataFixtures/ORM/customer_order.yaml b/tests/Api/DataFixtures/ORM/customer_order.yaml index 1a6bed6737..75ee89a359 100644 --- a/tests/Api/DataFixtures/ORM/customer_order.yaml +++ b/tests/Api/DataFixtures/ORM/customer_order.yaml @@ -7,13 +7,26 @@ Sylius\Component\Core\Model\Order: tokenValue: "token" customer: "@customer_tony" billingAddress: "@billing_address" + shippingAddress: "@shipping_address" Sylius\Component\Core\Model\Address: billing_address: customer: "@customer_tony" firstName: "Andrzej" lastName: "Legs" - company: "Polmotors" + company: "Polmotors office" + street: "Moniuszki 16/20" + countryCode: "PL" + city: "Pabianice" + postcode: "31-999" + phoneNumber: "111999222" + provinceCode: "PL-PL" + provinceName: "kujawsko-pomorskie" + shipping_address: + customer: "@customer_tony" + firstName: "Andrzej" + lastName: "Legs" + company: "Polmotors manufactory" street: "Moniuszki 16/20" countryCode: "PL" city: "Pabianice" diff --git a/tests/Api/DataFixtures/ORM/order/customer.yaml b/tests/Api/DataFixtures/ORM/order/customer.yaml new file mode 100644 index 0000000000..99b05bf8e2 --- /dev/null +++ b/tests/Api/DataFixtures/ORM/order/customer.yaml @@ -0,0 +1,7 @@ +Sylius\Component\Core\Model\Customer: + customer_{tony, dave}: + firstName: "" + lastName: "Doe" + email: "\\@doe.com" + emailCanonical: "\\@doe.com" + birthday: "<(new \\DateTime())>" diff --git a/tests/Api/DataFixtures/ORM/order/order.yaml b/tests/Api/DataFixtures/ORM/order/order.yaml new file mode 100644 index 0000000000..c8056d96ba --- /dev/null +++ b/tests/Api/DataFixtures/ORM/order/order.yaml @@ -0,0 +1,38 @@ +Sylius\Component\Core\Model\Order: + order: + channel: "@channel_web" + currencyCode: "USD" + localeCode: "en_US" + state: "new" + tokenValue: "token" + customer: "@customer_tony" + billingAddress: "@billing_address" + shippingAddress: "@shipping_address" + +Sylius\Component\Core\Model\Address: + billing_address: + customer: "@customer_tony" + firstName: "Andrzej" + lastName: "Legs" + company: "Polmotors office" + street: "Moniuszki 16/20" + countryCode: "PL" + city: "Pabianice" + postcode: "31-999" + phoneNumber: "111999222" + provinceCode: "PL-PL" + provinceName: "kujawsko-pomorskie" + + shipping_address: + customer: "@customer_tony" + firstName: "Andrzej" + lastName: "Legs" + company: "Polmotors manufactory" + street: "Moniuszki 16/20" + countryCode: "PL" + city: "Pabianice" + postcode: "31-999" + phoneNumber: "111999222" + provinceCode: "PL-PL" + provinceName: "kujawsko-pomorskie" + diff --git a/tests/Api/Responses/Expected/admin/order/get_order_billing_data_response.json b/tests/Api/Responses/Expected/admin/order/get_billing_address_of_placed_order_response.json similarity index 55% rename from tests/Api/Responses/Expected/admin/order/get_order_billing_data_response.json rename to tests/Api/Responses/Expected/admin/order/get_billing_address_of_placed_order_response.json index 7d28303d99..a6d283aebb 100644 --- a/tests/Api/Responses/Expected/admin/order/get_order_billing_data_response.json +++ b/tests/Api/Responses/Expected/admin/order/get_billing_address_of_placed_order_response.json @@ -4,14 +4,14 @@ "@type": "Address", "customer": "\/api\/v2\/admin\/customers\/@integer@", "id": "@integer@", - "firstName": "Adam", - "lastName": "Handley", - "company": "FMŻ", - "street": "Kościuszki 21", - "countryCode": "FR", - "city": "Bordeaux", - "postcode": "99-999", - "phoneNumber": "911213969", + "firstName": "Andrzej", + "lastName": "Legs", + "company": "Polmotors office", + "street": "Moniuszki 16/20", + "countryCode": "PL", + "city": "Pabianice", + "postcode": "31-999", + "phoneNumber": "111999222", "provinceCode": "PL-PL", "provinceName": "kujawsko-pomorskie" } diff --git a/tests/Api/Responses/Expected/admin/order/get_shipping_address_of_placed_order_response.json b/tests/Api/Responses/Expected/admin/order/get_shipping_address_of_placed_order_response.json new file mode 100644 index 0000000000..cc5a84ef59 --- /dev/null +++ b/tests/Api/Responses/Expected/admin/order/get_shipping_address_of_placed_order_response.json @@ -0,0 +1,17 @@ +{ + "@id": "\/api\/v2\/admin\/addresses\/@integer@", + "@context": "\/api\/v2\/contexts\/Address", + "@type": "Address", + "customer": "\/api\/v2\/admin\/customers\/@integer@", + "id": "@integer@", + "firstName": "Andrzej", + "lastName": "Legs", + "company": "Polmotors manufactory", + "street": "Moniuszki 16/20", + "countryCode": "PL", + "city": "Pabianice", + "postcode": "31-999", + "phoneNumber": "111999222", + "provinceCode": "PL-PL", + "provinceName": "kujawsko-pomorskie" +} diff --git a/tests/Api/Responses/Expected/admin/order/gets_orders_for_customer_response.json b/tests/Api/Responses/Expected/admin/order/gets_orders_for_customer_response.json index 1527e9e146..0ed81fc820 100644 --- a/tests/Api/Responses/Expected/admin/order/gets_orders_for_customer_response.json +++ b/tests/Api/Responses/Expected/admin/order/gets_orders_for_customer_response.json @@ -14,7 +14,20 @@ "firstName": "Andrzej", "lastName": "Legs", "phoneNumber": "111999222", - "company": "Polmotors", + "company": "Polmotors office", + "countryCode": "PL", + "provinceName": "kujawsko-pomorskie", + "street": "Moniuszki 16\/20", + "city": "Pabianice", + "postcode": "31-999" + }, + "shippingAddress": { + "@id": "\/api\/v2\/admin\/addresses\/@integer@", + "@type": "Address", + "firstName": "Andrzej", + "lastName": "Legs", + "phoneNumber": "111999222", + "company": "Polmotors manufactory", "countryCode": "PL", "provinceName": "kujawsko-pomorskie", "street": "Moniuszki 16\/20", diff --git a/tests/Api/Responses/Expected/admin/order/put_billing_address_of_placed_order_response.json b/tests/Api/Responses/Expected/admin/order/put_billing_address_of_placed_order_response.json new file mode 100644 index 0000000000..335831edc5 --- /dev/null +++ b/tests/Api/Responses/Expected/admin/order/put_billing_address_of_placed_order_response.json @@ -0,0 +1,17 @@ +{ + "@id": "\/api\/v2\/admin\/addresses\/@integer@", + "@context": "\/api\/v2\/contexts\/Address", + "@type": "Address", + "customer": "\/api\/v2\/admin\/customers\/@integer@", + "id": "@integer@", + "firstName": "Updated: Adam", + "lastName": "Updated: Handley", + "company": "Updated: FMŻ", + "street": "Updated: Kościuszki 21", + "countryCode": "Updated: FR", + "city": "Updated: Bordeaux", + "postcode": "Updated: 99-999", + "phoneNumber": "Updated: 911213969", + "provinceCode": "Updated: PL-WP", + "provinceName": "Updated: wielkopolskie" +} diff --git a/tests/Api/Responses/Expected/admin/order/put_shipping_address_of_placed_order_response.json b/tests/Api/Responses/Expected/admin/order/put_shipping_address_of_placed_order_response.json new file mode 100644 index 0000000000..3b79ef5179 --- /dev/null +++ b/tests/Api/Responses/Expected/admin/order/put_shipping_address_of_placed_order_response.json @@ -0,0 +1,17 @@ +{ + "@id": "\/api\/v2\/admin\/addresses\/@integer@", + "@context": "\/api\/v2\/contexts\/Address", + "@type": "Address", + "customer": "\/api\/v2\/admin\/customers\/@integer@", + "id": "@integer@", + "firstName": "Updated: Julia", + "lastName": "Updated: Kowalska", + "company": "Updated: Błysk", + "street": "Updated: Marszałkowska 10", + "countryCode": "Updated: PL", + "city": "Updated: Warszawa", + "postcode": "Updated: 00-001", + "phoneNumber": "Updated: 48222333444", + "provinceCode": "Updated: PL-MA", + "provinceName": "Updated: mazowieckie" +}