[API] Checkout API payment step tests

This commit is contained in:
Mateusz Zalewski 2016-08-26 12:24:50 +02:00
parent 98985b8d5c
commit 0abe891734
4 changed files with 232 additions and 0 deletions

View file

@ -0,0 +1,189 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sylius\Tests\Controller;
use Lakion\ApiTestCase\JsonApiTestCase;
use Symfony\Component\HttpFoundation\Response;
/**
* @author Mateusz Zalewski <mateusz.zalewski@lakion.com>
*/
class CheckoutPaymentApiTest extends JsonApiTestCase
{
/**
* @var array
*/
private static $authorizedHeaderWithContentType = [
'HTTP_Authorization' => 'Bearer SampleTokenNjZkNjY2MDEwMTAzMDkxMGE0OTlhYzU3NzYyMTE0ZGQ3ODcyMDAwM2EwMDZjNDI5NDlhMDdlMQ',
'CONTENT_TYPE' => 'application/json',
];
/**
* @test
*/
public function it_denies_order_payment_selection_for_non_authenticated_user()
{
$this->client->request('PUT', '/api/checkouts/select-payment/1');
$response = $this->client->getResponse();
$this->assertResponse($response, 'authentication/access_denied_response', Response::HTTP_UNAUTHORIZED);
}
/**
* @test
*/
public function it_does_not_allow_to_select_payment_for_unexisting_order()
{
$this->loadFixturesFromFile('authentication/api_administrator.yml');
$this->client->request('PUT', '/api/checkouts/select-payment/1', [], [], static::$authorizedHeaderWithContentType);
$response = $this->client->getResponse();
$this->assertResponseCode($response, Response::HTTP_NOT_FOUND);
}
/**
* @test
*/
public function it_does_not_allow_to_select_payment_for_order_that_is_not_addressed_and_has_no_shipping_method_selected()
{
$this->loadFixturesFromFile('authentication/api_administrator.yml');
$orders = $this->loadFixturesFromFile('resources/checkout.yml');
$orderId = $orders['order1']->getId();
$this->addressOrder($orderId);
$url = sprintf('/api/checkouts/select-payment/%d', $orderId);
$this->client->request('PUT', $url, [], [], static::$authorizedHeaderWithContentType);
$response = $this->client->getResponse();
$this->assertResponse($response, 'checkout/payment_invalid_order_state', Response::HTTP_INTERNAL_SERVER_ERROR);
}
/**
* @test
*/
public function it_does_not_allow_to_select_unexisting_payment_method()
{
$this->loadFixturesFromFile('authentication/api_administrator.yml');
$checkoutData = $this->loadFixturesFromFile('resources/checkout.yml');
$orderId = $checkoutData['order1']->getId();
$this->addressOrder($orderId);
$this->selectOrderShippingMethod($orderId, $checkoutData['ups']->getId());
$data =
<<<EOT
{
"payments": [
{
"method": 0
}
]
}
EOT;
$url = sprintf('/api/checkouts/select-payment/%d', $orderId);
$this->client->request('PUT', $url, [], [], static::$authorizedHeaderWithContentType, $data);
$response = $this->client->getResponse();
$this->assertResponse($response, 'checkout/payment_validation_failed', Response::HTTP_BAD_REQUEST);
}
/**
* @test
*/
public function it_allows_to_select_payment_method_for_order()
{
$this->loadFixturesFromFile('authentication/api_administrator.yml');
$checkoutData = $this->loadFixturesFromFile('resources/checkout.yml');
$orderId = $checkoutData['order1']->getId();
$this->addressOrder($orderId);
$this->selectOrderShippingMethod($orderId, $checkoutData['ups']->getId());
$data =
<<<EOT
{
"payments": [
{
"method": {$checkoutData['cash_on_delivery']->getId()}
}
]
}
EOT;
$url = sprintf('/api/checkouts/select-payment/%d', $orderId);
$this->client->request('PUT', $url, [], [], static::$authorizedHeaderWithContentType, $data);
$response = $this->client->getResponse();
$this->assertResponseCode($response, Response::HTTP_NO_CONTENT);
}
/**
* @param int $orderId
*/
private function addressOrder($orderId)
{
$this->loadFixturesFromFile('resources/countries.yml');
$customers = $this->loadFixturesFromFile('resources/customers.yml');
$data =
<<<EOT
{
"shippingAddress": {
"firstName": "Hieronim",
"lastName": "Bosch",
"street": "Surrealism St.",
"countryCode": "NL",
"city": "s-Hertogenbosch",
"postcode": "99-999"
},
"billingAddress": {
"firstName": "Vincent",
"lastName": "van Gogh",
"street": "Post-Impressionism St.",
"countryCode": "NL",
"city": "Groot Zundert",
"postcode": "88-888"
},
"differentBillingAddress": true
}
EOT;
$url = sprintf('/api/checkouts/addressing/%d/%d', $orderId, $customers['customer_Oliver']->getId());
$this->client->request('PUT', $url, [], [], static::$authorizedHeaderWithContentType, $data);
}
/**
* @param int $orderId
* @param int $shippingMethodId
*/
private function selectOrderShippingMethod($orderId, $shippingMethodId)
{
$data =
<<<EOT
{
"shipments": [
{
"method": {$shippingMethodId}
}
]
}
EOT;
$url = sprintf('/api/checkouts/select-shipping/%d', $orderId);
$this->client->request('PUT', $url, [], [], static::$authorizedHeaderWithContentType, $data);
}
}

View file

@ -28,6 +28,7 @@ Sylius\Component\Core\Model\Channel:
taxCalculationStrategy: "order_items_based"
currencies: [@currency]
shippingMethods: [@ups]
paymentMethods: [@cash_on_delivery]
Sylius\Component\Core\Model\Product:
product1:
@ -82,6 +83,20 @@ Sylius\Component\Shipping\Model\ShippingMethodTranslation:
locale: 'en_US'
translatable: @ups
Sylius\Component\Payment\Model\PaymentMethod:
cash_on_delivery:
code: cod
enabled: true
gateway: 'offline'
currentLocale: en_US
currentTranslation: @cash_on_delivery_translation1
Sylius\Component\Payment\Model\PaymentMethodTranslation:
cash_on_delivery_translation1:
name: 'Cach on delivery'
locale: 'en_US'
translatable: @cash_on_delivery
Sylius\Component\Currency\Model\Currency:
currency:
code: "EUR"

View file

@ -0,0 +1,4 @@
{
"code": 500,
"message": "Transition select_payment cannot be applied on state addressed of object Sylius\\Component\\Core\\Model\\Order with graph sylius_order_checkout"
}

View file

@ -0,0 +1,24 @@
{
"code": 400,
"message": "Validation Failed",
"errors": {
"children": {
"payments": {
"children": [
{
"children": {
"method": {
"errors": [
"This value is not valid."
],
"children": [
{}
]
}
}
}
]
}
}
}
}