Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Symfony workflow support #62

Merged
merged 11 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 4 additions & 12 deletions behat.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,22 @@ default:
snippets: false

extensions:
DMore\ChromeExtension\Behat\ServiceContainer\ChromeExtension: ~
Robertfausk\Behat\PantherExtension: ~

FriendsOfBehat\MinkDebugExtension:
directory: etc/build
clean_start: false
clean_start: true
screenshot: true

Behat\MinkExtension:
files_path: "%paths.base%/vendor/sylius/sylius/src/Sylius/Behat/Resources/fixtures/"
base_url: "https://127.0.0.1:8080/"
default_session: symfony
javascript_session: panther
javascript_session: javascript_chrome
sessions:
symfony:
symfony: ~
chromedriver:
chrome:
api_url: http://127.0.0.1:9222
validate_certificate: false
chrome_headless_second_session:
chrome:
api_url: http://127.0.0.1:9222
validate_certificate: false
panther:
javascript_chrome:
panther:
options:
webServerDir: "%paths.base%/tests/Application/public"
Expand All @@ -49,6 +40,7 @@ default:
acceptInsecureCerts: true
unexpectedAlertBehaviour: accept
show_auto: false
browser_name: 'behat_browser'

FriendsOfBehat\SymfonyExtension:
bootstrap: tests/Application/config/bootstrap.php
Expand Down
2 changes: 0 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
"ext-json": "*",
"behat/behat": "^3.7",
"dbrekelmans/bdi": "^1.2",
"dmore/behat-chrome-extension": "^1.4",
"dmore/chrome-mink-driver": "^2.8",
"friends-of-behat/mink": "^1.9",
"friends-of-behat/mink-browserkit-driver": "^1.4",
"friends-of-behat/mink-debug-extension": "^2.0",
Expand Down
14 changes: 5 additions & 9 deletions ecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,15 @@
use PhpCsFixer\Fixer\ControlStructure\TrailingCommaInMultilineFixer;
use Symplify\EasyCodingStandard\Config\ECSConfig;

return static function (ECSConfig $ecsConfig): void {
$ecsConfig->import(__DIR__ . '/vendor/sylius-labs/coding-standard/ecs.php');
return static function (ECSConfig $config): void {
$config->import(__DIR__ . '/vendor/sylius-labs/coding-standard/ecs.php');

$ecsConfig->paths([
$config->paths([
'src',
'tests/Behat',
]);

$services = $ecsConfig->services();

// PHP 7 compatibility
$services
->set(TrailingCommaInMultilineFixer::class)
->call('configure', [['elements' => ['arrays']]])
;
$config->ruleWithConfiguration(TrailingCommaInMultilineFixer::class, ['elements' => ['arrays']]);

};
35 changes: 23 additions & 12 deletions spec/Extension/UpdatePaymentStateExtensionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace spec\FluxSE\SyliusPayumStripePlugin\Extension;

use Exception;
use FluxSE\SyliusPayumStripePlugin\Abstraction\StateMachine\StateMachineInterface;
use Payum\Core\Extension\Context;
use Payum\Core\Extension\ExtensionInterface;
use Payum\Core\GatewayInterface;
Expand All @@ -15,20 +16,18 @@
use Payum\Core\Storage\IdentityInterface;
use Payum\Core\Storage\StorageInterface;
use PhpSpec\ObjectBehavior;
use SM\Factory\FactoryInterface;
use Sylius\Bundle\PayumBundle\Factory\GetStatusFactoryInterface;
use Sylius\Component\Core\Model\PaymentInterface;
use Sylius\Component\Payment\PaymentTransitions;
use Sylius\Component\Resource\StateMachine\StateMachineInterface;

final class UpdatePaymentStateExtensionSpec extends ObjectBehavior
{
public function let(
FactoryInterface $factory,
StateMachineInterface $stateMachine,
StorageInterface $storage,
GetStatusFactoryInterface $getStatusRequestFactory
): void {
$this->beConstructedWith($factory, $storage, $getStatusRequestFactory);
$this->beConstructedWith($stateMachine, $storage, $getStatusRequestFactory);
}

public function it_is_initializable(): void
Expand Down Expand Up @@ -96,7 +95,6 @@ public function it_OnPostExecute_apply_a_transition(
GetStatusInterface $status,
GetStatusFactoryInterface $getStatusRequestFactory,
GatewayInterface $gateway,
FactoryInterface $factory,
StateMachineInterface $stateMachine
): void {
$context->getException()->willReturn(null);
Expand All @@ -114,9 +112,16 @@ public function it_OnPostExecute_apply_a_transition(
$payment->getState()->willReturn(PaymentInterface::STATE_NEW);
$status->getValue()->willReturn(PaymentInterface::STATE_COMPLETED);

$factory->get($payment, PaymentTransitions::GRAPH)->willReturn($stateMachine);
$stateMachine->getTransitionToState(PaymentInterface::STATE_COMPLETED)->willReturn('complete');
$stateMachine->apply('complete')->shouldBeCalled();
$stateMachine->getTransitionToState(
$payment,
PaymentTransitions::GRAPH,
PaymentInterface::STATE_COMPLETED
)->willReturn('complete');
$stateMachine->apply(
$payment,
PaymentTransitions::GRAPH,
'complete'
)->shouldBeCalled();

$this->onPostExecute($context);
}
Expand All @@ -131,7 +136,6 @@ public function it_OnPostExecute_apply_a_transition_without_a_Sylius_PaymentInte
GetStatusInterface $status,
GetStatusFactoryInterface $getStatusRequestFactory,
GatewayInterface $gateway,
FactoryInterface $factory,
StateMachineInterface $stateMachine
): void {
$context->getException()->willReturn(null);
Expand All @@ -152,9 +156,16 @@ public function it_OnPostExecute_apply_a_transition_without_a_Sylius_PaymentInte
$previousPayment->getState()->willReturn(PaymentInterface::STATE_NEW);
$status->getValue()->willReturn(PaymentInterface::STATE_COMPLETED);

$factory->get($previousPayment, PaymentTransitions::GRAPH)->willReturn($stateMachine);
$stateMachine->getTransitionToState(PaymentInterface::STATE_COMPLETED)->willReturn('complete');
$stateMachine->apply('complete')->shouldBeCalled();
$stateMachine->getTransitionToState(
$previousPayment,
PaymentTransitions::GRAPH,
PaymentInterface::STATE_COMPLETED
)->willReturn('complete');
$stateMachine->apply(
$previousPayment,
PaymentTransitions::GRAPH,
'complete'
)->shouldBeCalled();

$this->onPreExecute($previousContext);

Expand Down
23 changes: 5 additions & 18 deletions spec/StateMachine/CancelOrderProcessorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,50 +19,37 @@ public function let(MessageBusInterface $commandBus): void {

public function it_is_invokable_when_it_is_new(
PaymentInterface $payment,
TransitionEvent $event,
MessageBusInterface $commandBus,
): void {
$event->getState()->willReturn(PaymentInterface::STATE_NEW);

$payment->getId()->willReturn(1);

$command = new CancelPayment(1);
$commandBus->dispatch($command)->willReturn(new Envelope($command));

$this->__invoke($payment, $event);
$this->__invoke($payment, PaymentInterface::STATE_NEW);
}

public function it_is_invokable_when_is_authorized(
PaymentInterface $payment,
TransitionEvent $event,
MessageBusInterface $commandBus
): void {

$event->getState()->willReturn(PaymentInterface::STATE_AUTHORIZED);

$payment->getId()->willReturn(1);

$command = new CancelPayment(1);
$commandBus->dispatch($command)->willReturn(new Envelope($command));

$this->__invoke($payment, $event);
$this->__invoke($payment, PaymentInterface::STATE_AUTHORIZED);
}

public function it_do_nothing_when_it_is_completed(
PaymentInterface $payment,
TransitionEvent $event
PaymentInterface $payment
): void {
$event->getState()->willReturn(PaymentInterface::STATE_COMPLETED);

$this->__invoke($payment, $event);
$this->__invoke($payment, PaymentInterface::STATE_COMPLETED);
}

public function it_do_nothing_when_it_is_refunded(
PaymentInterface $payment,
TransitionEvent $event
): void {
$event->getState()->willReturn(PaymentInterface::STATE_REFUNDED);

$this->__invoke($payment, $event);
$this->__invoke($payment, PaymentInterface::STATE_REFUNDED);
}
}
19 changes: 5 additions & 14 deletions spec/StateMachine/CaptureAuthorizedOrderProcessorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,25 @@ public function let(MessageBusInterface $commandBus): void {

public function it_is_invokable(
PaymentInterface $payment,
TransitionEvent $event,
MessageBusInterface $commandBus
): void {
$event->getState()->willReturn(PaymentInterface::STATE_AUTHORIZED);

$payment->getId()->willReturn(1);

$command = new CaptureAuthorizedPayment(1);
$commandBus->dispatch($command)->willReturn(new Envelope($command));

$this->__invoke($payment, $event);
$this->__invoke($payment, PaymentInterface::STATE_AUTHORIZED);
}

public function it_do_nothing_when_it_is_completed(
PaymentInterface $payment,
TransitionEvent $event
PaymentInterface $payment
): void {
$event->getState()->willReturn(PaymentInterface::STATE_COMPLETED);

$this->__invoke($payment, $event);
$this->__invoke($payment, PaymentInterface::STATE_COMPLETED);
}

public function it_do_nothing_when_it_is_refunded(
PaymentInterface $payment,
TransitionEvent $event
PaymentInterface $payment
): void {
$event->getState()->willReturn(PaymentInterface::STATE_REFUNDED);

$this->__invoke($payment, $event);
$this->__invoke($payment, PaymentInterface::STATE_REFUNDED);
}
}
19 changes: 10 additions & 9 deletions spec/StateMachine/RefundOrderProcessorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,31 @@
final class RefundOrderProcessorSpec extends ObjectBehavior
{
public function let(MessageBusInterface $commandBus): void {
$this->beConstructedWith($commandBus);
$this->beConstructedWith($commandBus, false);
}

public function it_is_invokable(
PaymentInterface $payment,
TransitionEvent $event,
MessageBusInterface $commandBus
): void {
$event->getState()->willReturn(PaymentInterface::STATE_COMPLETED);

$payment->getId()->willReturn(1);

$command = new RefundPayment(1);
$commandBus->dispatch($command)->willReturn(new Envelope($command));

$this->__invoke($payment, $event);
$this->__invoke($payment, PaymentInterface::STATE_COMPLETED);
}

public function it_do_nothing_when_it_is_authorized(
public function it_do_nothing_when_it_is_disabled(
PaymentInterface $payment,
TransitionEvent $event
MessageBusInterface $commandBus
): void {
$event->getState()->willReturn(PaymentInterface::STATE_AUTHORIZED);

$this->__invoke($payment, $event);
$this->beConstructedWith($commandBus, true);

$command = new RefundPayment(1);
$commandBus->dispatch($command)->shouldNotBeCalled();

$this->__invoke($payment, PaymentInterface::STATE_COMPLETED);
}
}
27 changes: 27 additions & 0 deletions src/Abstraction/StateMachine/CompositeStateMachine.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace FluxSE\SyliusPayumStripePlugin\Abstraction\StateMachine;

use Sylius\Abstraction\StateMachine\StateMachineInterface as SyliusAbstractionStateMachineInterface;

final class CompositeStateMachine implements StateMachineInterface
{
private SyliusAbstractionStateMachineInterface $stateMachine;

public function __construct(SyliusAbstractionStateMachineInterface $stateMachine)
{
$this->stateMachine = $stateMachine;
}

public function getTransitionToState(object $subject, string $graphName, string $toState): ?string
{
return $this->stateMachine->getTransitionToState($subject, $graphName, $toState);
}

public function apply(object $subject, string $graphName, string $transition, array $context = []): void
{
$this->stateMachine->apply($subject, $graphName, $transition, $context);
}
}
12 changes: 12 additions & 0 deletions src/Abstraction/StateMachine/StateMachineInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace FluxSE\SyliusPayumStripePlugin\Abstraction\StateMachine;

interface StateMachineInterface
{
public function getTransitionToState(object $subject, string $graphName, string $toState): ?string;

public function apply(object $subject, string $graphName, string $transition, array $context = []): void;
}
34 changes: 34 additions & 0 deletions src/Abstraction/StateMachine/WinzouStateMachine.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace FluxSE\SyliusPayumStripePlugin\Abstraction\StateMachine;

use SM\Factory\FactoryInterface;
use Sylius\Component\Resource\StateMachine\StateMachine as SyliusStateMachineInterface;
use Webmozart\Assert\Assert;

final class WinzouStateMachine implements StateMachineInterface
{
private FactoryInterface $factory;

public function __construct(FactoryInterface $factory)
{
$this->factory = $factory;
}

public function getTransitionToState(object $subject, string $graphName, string $toState): ?string
{
$stateMachine = $this->factory->get($subject, $graphName);
Assert::isInstanceOf($stateMachine, SyliusStateMachineInterface::class);

return $stateMachine->getTransitionToState($toState);
}

public function apply(object $subject, string $graphName, string $transition, array $context = []): void
{
$stateMachine = $this->factory->get($subject, $graphName);

$stateMachine->apply($transition);
}
}
Loading
Loading