add contract tests

This commit is contained in:
Kamil Grygierzec 2024-05-29 08:17:23 +02:00
parent fc60e58574
commit bb3cc9dd5b
No known key found for this signature in database
GPG key ID: 7F54EE42DAD4B9E9
2 changed files with 148 additions and 19 deletions

View file

@ -0,0 +1,7 @@
{
"@context": "\/api\/v2\/contexts\/OrderItem",
"@id": "\/api\/v2\/shop\/orders\/nAWw2jewpA\/items",
"@type": "hydra:Collection",
"hydra:member": [],
"hydra:totalItems": 0
}

View file

@ -78,13 +78,33 @@ final class OrdersTest extends JsonApiTestCase
$this->addItemToCart('MUG_BLUE', 3, $tokenValue);
$this->client->request('GET', '/api/v2/shop/orders/nAWw2jewpA/items', [], [], self::CONTENT_TYPE_HEADER);
$response = $this->client->getResponse();
$this->assertResponse($response, 'shop/get_order_items_response', Response::HTTP_OK);
}
/** @test */
public function it_returns_nothing_if_visitor_tries_to_get_the_order_items_of_logged_in_user(): void
{
$this->loadFixturesFromFiles([
'channel.yaml',
'cart.yaml',
'country.yaml',
'authentication/customer.yaml',
'shipping_method.yaml',
'payment_method.yaml',
]);
$tokenValue = 'nAWw2jewpA';
$this->placeOrder($tokenValue, 'oliver@doe.com');
$this->client->request('GET', '/api/v2/shop/orders/nAWw2jewpA/items', [], [], self::CONTENT_TYPE_HEADER);
$response = $this->client->getResponse();
$this->assertResponse($response, 'shop/get_empty_order_items_response', Response::HTTP_OK);
}
/** @test */
public function it_gets_order_adjustments(): void
{
@ -100,6 +120,75 @@ final class OrdersTest extends JsonApiTestCase
$this->assertResponse($response, 'shop/get_order_adjustments_response', Response::HTTP_OK);
}
/** @test */
public function it_prevents_visitor_from_trying_to_get_the_order_adjustments_of_logged_in_user(): void
{
$this->loadFixturesFromFiles([
'channel.yaml',
'cart.yaml',
'country.yaml',
'authentication/customer.yaml',
'shipping_method.yaml',
'payment_method.yaml',
]);
$tokenValue = 'nAWw2jewpA';
$this->placeOrder($tokenValue, 'oliver@doe.com');
$this->client->request('GET', '/api/v2/shop/orders/nAWw2jewpA/adjustments', [], [], self::CONTENT_TYPE_HEADER);
$response = $this->client->getResponse();
$this->assertResponseCode($response, Response::HTTP_UNAUTHORIZED);
}
/** @test */
public function it_prevents_visitor_from_trying_to_get_the_order_item_adjustments_of_logged_in_user(): void
{
$this->loadFixturesFromFiles([
'channel.yaml',
'cart.yaml',
'country.yaml',
'authentication/customer.yaml',
'shipping_method.yaml',
'payment_method.yaml',
]);
$tokenValue = $this->pickUpCart();
$order = $this->createOrderWithOrderItemAdjustments($tokenValue, 'oliver@doe.com');
$this->client->request('GET', '/api/v2/shop/orders/nAWw2jewpA/items/'.$order->getItems()->first()->getId().'/adjustments', [], [], self::CONTENT_TYPE_HEADER);
$response = $this->client->getResponse();
$this->assertResponseCode($response, Response::HTTP_UNAUTHORIZED);
}
public function it_returns_nothing_if_logged_in_shop_user_tries_to_get_the_order_item_adjustments_of_another_user(): void
{
$this->loadFixturesFromFiles([
'channel.yaml',
'cart.yaml',
'country.yaml',
'authentication/customer.yaml',
'shipping_method.yaml',
'payment_method.yaml',
]);
$loginData = $this->logInShopUser('dave@doe.com');
$authorizationHeader = self::$kernel->getContainer()->getParameter('sylius.api.authorization_header');
$header['HTTP_' . $authorizationHeader] = 'Bearer ' . $loginData;
$header = array_merge($header, self::CONTENT_TYPE_HEADER);
$tokenValue = $this->pickUpCart();
$order = $this->createOrderWithOrderItemAdjustments($tokenValue, 'oliver@doe.com');
$this->client->request('GET', '/api/v2/shop/orders/nAWw2jewpA/items/'.$order->getItems()->first()->getId().'/adjustments', [], [], $header);
$response = $this->client->getResponse();
$this->assertResponseCode($response, Response::HTTP_UNAUTHORIZED);
}
/** @test */
public function it_gets_order_item_adjustments(): void
{
@ -107,22 +196,7 @@ final class OrdersTest extends JsonApiTestCase
$tokenValue = $this->pickUpCart();
$this->addItemToCart('MUG_BLUE', 3, $tokenValue);
/** @var OrderInterface $order */
$order = $this->get('sylius.repository.order')->findCartByTokenValue($tokenValue);
$orderItem = $order->getItems()->first();
/** @var AdjustmentInterface $adjustment */
$adjustment = $this->get('sylius.factory.adjustment')->createNew();
$adjustment->setType(AdjustmentInterface::ORDER_ITEM_PROMOTION_ADJUSTMENT);
$adjustment->setAmount(200);
$adjustment->setNeutral(false);
$adjustment->setLabel('Test Promotion Adjustment');
$orderItem->addAdjustment($adjustment);
$this->get('sylius.manager.order')->flush();
$order = $this->createOrderWithOrderItemAdjustments($tokenValue);
$this->client->request('GET', '/api/v2/shop/orders/nAWw2jewpA/items/'.$order->getItems()->first()->getId().'/adjustments', [], [], self::CONTENT_TYPE_HEADER);
$response = $this->client->getResponse();
@ -365,6 +439,30 @@ final class OrdersTest extends JsonApiTestCase
$this->assertResponseCode($this->client->getResponse(), Response::HTTP_NOT_FOUND);
}
/** @test */
public function it_returns_nothing_if_visitor_tries_to_get_the_order_of_logged_in_user(): void
{
$this->loadFixturesFromFiles([
'channel.yaml',
'cart.yaml',
'country.yaml',
'authentication/customer.yaml',
'shipping_method.yaml',
'payment_method.yaml',
]);
$tokenValue = 'nAWw2jewpA';
$this->placeOrder($tokenValue, 'oliver@doe.com');
$this->client->request(
'GET',
sprintf('/api/v2/shop/orders/%s', $tokenValue),
server: self::CONTENT_TYPE_HEADER,
);
$this->assertResponseCode($this->client->getResponse(), Response::HTTP_NOT_FOUND);
}
protected function setUp(): void
{
parent::setUp();
@ -392,7 +490,7 @@ final class OrdersTest extends JsonApiTestCase
$this->commandBus->dispatch($addItemToCartCommand);
}
private function updateCartWithAddress(string $tokenValue): void
private function updateCartWithAddress(string $tokenValue, string $email = 'sylius@example.com'): void
{
$address = new Address();
$address->setFirstName('John');
@ -402,9 +500,33 @@ final class OrdersTest extends JsonApiTestCase
$address->setCountryCode('US');
$address->setPostcode('90000');
$updateCartCommand = new UpdateCart(email: 'sylius@example.com', billingAddress: $address);
$updateCartCommand = new UpdateCart(email: $email, billingAddress: $address);
$updateCartCommand->setOrderTokenValue($tokenValue);
$this->commandBus->dispatch($updateCartCommand);
}
private function createOrderWithOrderItemAdjustments(string $tokenValue, string $email = 'sylius@example.com'): OrderInterface
{
$this->addItemToCart('MUG_BLUE', 3, $tokenValue);
/** @var OrderInterface $order */
$order = $this->get('sylius.repository.order')->findCartByTokenValue($tokenValue);
$orderItem = $order->getItems()->first();
/** @var AdjustmentInterface $adjustment */
$adjustment = $this->get('sylius.factory.adjustment')->createNew();
$adjustment->setType(AdjustmentInterface::ORDER_ITEM_PROMOTION_ADJUSTMENT);
$adjustment->setAmount(200);
$adjustment->setNeutral(false);
$adjustment->setLabel('Test Promotion Adjustment');
$this->updateCartWithAddress($tokenValue, $email);
$orderItem->addAdjustment($adjustment);
$this->get('sylius.manager.order')->flush();
return $order;
}
}