Skip to content

Commit

Permalink
[phpspec-2-phpunit] migration of tests (Winzou)
Browse files Browse the repository at this point in the history
  • Loading branch information
loic425 committed Oct 17, 2024
1 parent 44d53e9 commit 54ca44e
Showing 1 changed file with 52 additions and 60 deletions.
112 changes: 52 additions & 60 deletions src/Component/spec/Winzou/StateMachine/OperationStateMachineSpec.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* 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\Resource\Winzou\StateMachine;
namespace Sylius\Resource\Tests\Winzou\StateMachine;

use PhpSpec\ObjectBehavior;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use SM\Factory\Factory;
use SM\StateMachine\StateMachineInterface;
use Sylius\Resource\Context\Context;
Expand All @@ -22,83 +15,82 @@
use Sylius\Resource\Metadata\StateMachineAwareOperationInterface;
use Sylius\Resource\Winzou\StateMachine\OperationStateMachine;

final class OperationStateMachineSpec extends ObjectBehavior
final class OperationStateMachineTest extends TestCase
{
function let(Factory $factory): void
{
$this->beConstructedWith($factory);
}
use ProphecyTrait;

private OperationStateMachine $operationStateMachine;
private Factory|ObjectProphecy $factory;

function it_is_initializable(): void
protected function setUp(): void
{
$this->shouldHaveType(OperationStateMachine::class);
$this->factory = $this->prophesize(Factory::class);
$this->operationStateMachine = new OperationStateMachine($this->factory->reveal());
}

function it_returns_if_transition_is_possible(
\stdClass $data,
Factory $factory,
StateMachineInterface $stateMachine,
): void {
public function testItReturnsIfTransitionIsPossible(): void
{
$data = new \stdClass();
$operation = new Create(stateMachineTransition: 'publish');
$stateMachine = $this->prophesize(StateMachineInterface::class);

$factory->get($data, 'default')->willReturn($stateMachine);

$this->factory->get($data, 'default')->willReturn($stateMachine->reveal());
$stateMachine->can('publish')->willReturn(true);

$this->can($data, $operation, new Context())->shouldReturn(true);
$result = $this->operationStateMachine->can($data, $operation, new Context());

$this->assertTrue($result);
}

function it_applies_transition(
\stdClass $data,
Factory $factory,
StateMachineInterface $stateMachine,
): void {
public function testItAppliesTransition(): void
{
$data = new \stdClass();
$operation = new Create(stateMachineTransition: 'publish');
$stateMachine = $this->prophesize(StateMachineInterface::class);

$factory->get($data, 'default')->willReturn($stateMachine);

$this->factory->get($data, 'default')->willReturn($stateMachine->reveal());
$stateMachine->apply('publish')->willReturn(true);

$this->apply($data, $operation, new Context());
$this->operationStateMachine->apply($data, $operation, new Context());

// No assertion needed as we're checking for exceptions, ensure apply is called
$this->assertTrue(true);
}

function it_throws_an_exception_when_operation_has_no_defined_transition(
\stdClass $data,
Factory $factory,
StateMachineInterface $stateMachine,
): void {
$operation = new Create(name: 'app_dummy_create');
public function testItThrowsAnExceptionWhenOperationHasNoDefinedTransition(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('No State machine transition was found on operation "app_dummy_create".');

$factory->get($data, 'default')->willReturn($stateMachine);
$data = new \stdClass();
$operation = new Create(name: 'app_dummy_create');
$stateMachine = $this->prophesize(StateMachineInterface::class);

$this->shouldThrow(new \InvalidArgumentException('No State machine transition was found on operation "app_dummy_create".'))
->during('can', [$data, $operation, new Context()])
;
$this->factory->get($data, 'default')->willReturn($stateMachine->reveal());

$this->shouldThrow(new \InvalidArgumentException('No State machine transition was found on operation "app_dummy_create".'))
->during('apply', [$data, $operation, new Context()])
;
$this->operationStateMachine->can($data, $operation, new Context());
}

function it_throws_an_exception_when_winzou_state_machine_is_not_available(
\stdClass $data,
): void {
$this->beConstructedWith(null);
public function testItThrowsAnExceptionWhenWinzouStateMachineIsNotAvailable(): void
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('You can not use the "state-machine" if Winzou State Machine is not available. Try running "composer require winzou/state-machine-bundle".');

$operationStateMachine = new OperationStateMachine(null);
$data = new \stdClass();
$operation = new Create(stateMachineTransition: 'publish');

$this->shouldThrow(
new \LogicException('You can not use the "state-machine" if Winzou State Machine is not available. Try running "composer require winzou/state-machine-bundle".'),
)->during('can', [$data, $operation, new Context()]);
$operationStateMachine->can($data, $operation, new Context());
}

function it_throws_an_exception_when_operation_does_not_implement_a_state_machine(
\stdClass $data,
): void {
public function testItThrowsAnExceptionWhenOperationDoesNotImplementAStateMachine(): void
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage(sprintf('Expected an instance of %s. Got: %s', StateMachineAwareOperationInterface::class, Index::class));

$data = new \stdClass();
$operation = new Index();

$this->shouldThrow(
new \LogicException(sprintf('Expected an instance of %s. Got: %s', StateMachineAwareOperationInterface::class, Index::class)),
)->during('can', [$data, $operation, new Context()]);
$this->operationStateMachine->can($data, $operation, new Context());
}
}

0 comments on commit 54ca44e

Please sign in to comment.