[API] Implement managing payment state feature

This commit is contained in:
Grzegorz Sadowski 2020-03-11 12:52:32 +01:00 committed by Mateusz Zalewski
parent d305957233
commit 0d25d47ee7
No known key found for this signature in database
GPG key ID: 9BECA0BB71612E52
10 changed files with 141 additions and 6 deletions

View file

@ -222,6 +222,7 @@
"spec\\Sylius\\Bundle\\AddressingBundle\\": "src/Sylius/Bundle/AddressingBundle/spec/",
"spec\\Sylius\\Bundle\\AdminApiBundle\\": "src/Sylius/Bundle/AdminApiBundle/spec/",
"spec\\Sylius\\Bundle\\AdminBundle\\": "src/Sylius/Bundle/AdminBundle/spec/",
"spec\\Sylius\\Bundle\\ApiBundle\\": "src/Sylius/Bundle/ApiBundle/spec/",
"spec\\Sylius\\Bundle\\AttributeBundle\\": "src/Sylius/Bundle/AttributeBundle/spec/",
"spec\\Sylius\\Bundle\\ChannelBundle\\": "src/Sylius/Bundle/ChannelBundle/spec/",
"spec\\Sylius\\Bundle\\CoreBundle\\": "src/Sylius/Bundle/CoreBundle/spec/",

View file

@ -9,11 +9,10 @@ Feature: Completing a payment from its list
And the store ships everywhere for free
And the store has a product "Apple"
And the store allows paying with "Cash on Delivery"
And there is a customer "donald@duck.com" that placed an order "#00000001" in channel "United States"
And there is a "New" "#00000001" order with "Apple" product
And there is an "#00000001" order with "Apple" product
And I am logged in as an administrator
@ui
@ui @api
Scenario: Completing a payment from payments index
When I browse payments
And I complete the payment of order "#00000001"

View file

@ -20,6 +20,7 @@ suites:
AddressingBundle: { namespace: Sylius\Bundle\AddressingBundle, psr4_prefix: Sylius\Bundle\AddressingBundle, spec_path: src/Sylius/Bundle/AddressingBundle, src_path: src/Sylius/Bundle/AddressingBundle }
AdminBundle: { namespace: Sylius\Bundle\AdminBundle, psr4_prefix: Sylius\Bundle\AdminBundle, spec_path: src/Sylius/Bundle/AdminBundle, src_path: src/Sylius/Bundle/AdminBundle }
AdminApiBundle: { namespace: Sylius\Bundle\AdminApiBundle, psr4_prefix: Sylius\Bundle\AdminApiBundle, spec_path: src/Sylius/Bundle/AdminApiBundle, src_path: src/Sylius/Bundle/AdminApiBundle }
ApiBundle: { namespace: Sylius\Bundle\ApiBundle, psr4_prefix: Sylius\Bundle\ApiBundle, spec_path: src/Sylius/Bundle/ApiBundle, src_path: src/Sylius/Bundle/ApiBundle }
AttributeBundle: { namespace: Sylius\Bundle\AttributeBundle, psr4_prefix: Sylius\Bundle\AttributeBundle, spec_path: src/Sylius/Bundle/AttributeBundle, src_path: src/Sylius/Bundle/AttributeBundle }
ChannelBundle: { namespace: Sylius\Bundle\ChannelBundle, psr4_prefix: Sylius\Bundle\ChannelBundle, spec_path: src/Sylius/Bundle/ChannelBundle, src_path: src/Sylius/Bundle/ChannelBundle }
CoreBundle: { namespace: Sylius\Bundle\CoreBundle, psr4_prefix: Sylius\Bundle\CoreBundle, spec_path: src/Sylius/Bundle/CoreBundle, src_path: src/Sylius/Bundle/CoreBundle }

View file

@ -17,6 +17,8 @@ use Behat\Behat\Context\Context;
use Sylius\Behat\Client\ApiClientInterface;
use Sylius\Component\Core\Formatter\StringInflector;
use Sylius\Component\Core\Model\CustomerInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Payment\PaymentTransitions;
use Webmozart\Assert\Assert;
final class ManagingPaymentsContext implements Context
@ -38,6 +40,21 @@ final class ManagingPaymentsContext implements Context
$this->client->index('payments');
}
/**
* @When I complete the payment of order :order
*/
public function iCompleteThePaymentOfOrder(OrderInterface $order): void
{
$payment = $order->getLastPayment();
Assert::notNull($payment);
$this->client->applyTransition(
'payments',
(string) $payment->getId(),
PaymentTransitions::TRANSITION_COMPLETE
);
}
/**
* @Then I should see a single payment in the list
* @Then I should see :count payments in the list
@ -77,4 +94,24 @@ final class ManagingPaymentsContext implements Context
$this->client->hasItemOnPositionWithValue($position - 1, 'order', sprintf('/new-api/orders/%s', $orderNumber))
);
}
/**
* @Then I should be notified that the payment has been completed
*/
public function iShouldBeNotifiedThatThePaymentHasBeenCompleted(): void
{
Assert::true($this->client->isUpdateSuccessful());
}
/**
* @Then I should see the payment of order :order as :paymentState
*/
public function iShouldSeeThePaymentOfOrderAs(OrderInterface $order, string $paymentState): void
{
$payment = $order->getLastPayment();
Assert::notNull($payment);
$this->client->show('payments', (string) $payment->getId());
Assert::true($this->client->responseHasValue('state', StringInflector::nameToLowercaseCode($paymentState)));
}
}

View file

@ -7,6 +7,7 @@ default:
contexts:
- sylius.behat.context.hook.doctrine_orm
- sylius.behat.context.transform.channel
- sylius.behat.context.transform.customer
- sylius.behat.context.transform.order
- sylius.behat.context.transform.product

View file

@ -0,0 +1,42 @@
<?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.
*/
declare(strict_types=1);
namespace Sylius\Bundle\ApiBundle\Applicator;
use SM\Factory\FactoryInterface as StateMachineFactoryInterface;
use Sylius\Component\Payment\Model\PaymentInterface;
use Sylius\Component\Payment\PaymentTransitions;
final class PaymentStateMachineTransitionApplicator
{
/** @var StateMachineFactoryInterface $stateMachineFactory */
private $stateMachineFactory;
public function __construct(StateMachineFactoryInterface $stateMachineFactory)
{
$this->stateMachineFactory = $stateMachineFactory;
}
public function complete(PaymentInterface $data): PaymentInterface
{
$this->applyTransition($data, PaymentTransitions::TRANSITION_COMPLETE);
return $data;
}
private function applyTransition(PaymentInterface $payment, string $transition): void
{
$stateMachine = $this->stateMachineFactory->get($payment, PaymentTransitions::GRAPH);
$stateMachine->apply($transition);
}
}

View file

@ -30,10 +30,21 @@
<itemOperations>
<itemOperation name="get" />
<itemOperation name="complete_payment">
<attribute name="method">PATCH</attribute>
<attribute name="path">/payments/{id}/complete</attribute>
<attribute name="controller">sylius.api.payment_state_machine_transition_applicator:complete</attribute>
</itemOperation>
</itemOperations>
<property name="id" identifier="true" writable="false" />
<property name="createdAt" writable="false" />
<property name="updatedAt" writable="false" />
<property name="method" writable="false" />
<property name="currencyCode" writable="false" />
<property name="amount" writable="false" />
<property name="state" writable="false" />
<property name="details" writable="false" />
<property name="order" writable="false" />
</resource>
</resources>

View file

@ -33,12 +33,12 @@
<itemOperation name="accept_product_review">
<attribute name="method">PATCH</attribute>
<attribute name="path">/product_reviews/{id}/accept</attribute>
<attribute name="controller">sylius.api.state_machine_transition_applicator:accept</attribute>
<attribute name="controller">sylius.api.product_review_state_machine_transition_applicator:accept</attribute>
</itemOperation>
<itemOperation name="reject_product_review">
<attribute name="method">PATCH</attribute>
<attribute name="path">/product_reviews/{id}/reject</attribute>
<attribute name="controller">sylius.api.state_machine_transition_applicator:reject</attribute>
<attribute name="controller">sylius.api.product_review_state_machine_transition_applicator:reject</attribute>
</itemOperation>
</itemOperations>
<property name="createdAt" writable="false" />

View file

@ -17,7 +17,11 @@
</imports>
<services>
<service id="sylius.api.state_machine_transition_applicator" class="Sylius\Bundle\ApiBundle\Applicator\ProductReviewStateMachineTransitionApplicator" public="true">
<service id="sylius.api.payment_state_machine_transition_applicator" class="Sylius\Bundle\ApiBundle\Applicator\PaymentStateMachineTransitionApplicator" public="true">
<argument id="sm.factory" type="service" />
</service>
<service id="sylius.api.product_review_state_machine_transition_applicator" class="Sylius\Bundle\ApiBundle\Applicator\ProductReviewStateMachineTransitionApplicator" public="true">
<argument id="sm.factory" type="service" />
</service>
</services>

View file

@ -0,0 +1,39 @@
<?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.
*/
declare(strict_types=1);
namespace spec\Sylius\Bundle\ApiBundle\Applicator;
use PhpSpec\ObjectBehavior;
use SM\Factory\FactoryInterface as StateMachineFactoryInterface;
use SM\StateMachine\StateMachine;
use Sylius\Component\Core\Model\PaymentInterface;
use Sylius\Component\Payment\PaymentTransitions;
final class PaymentStateMachineTransitionApplicatorSpec extends ObjectBehavior
{
function let(StateMachineFactoryInterface $stateMachineFactory)
{
$this->beConstructedWith($stateMachineFactory);
}
function it_completes_payment(
StateMachineFactoryInterface $stateMachineFactory,
PaymentInterface $payment,
StateMachine $stateMachine
): void {
$stateMachineFactory->get($payment, PaymentTransitions::GRAPH)->willReturn($stateMachine);
$stateMachine->apply(PaymentTransitions::TRANSITION_COMPLETE)->shouldBeCalled();
$this->complete($payment);
}
}