diff --git a/src/Component/spec/StateMachine/OperationStateMachineSpec.php b/src/Component/spec/StateMachine/OperationStateMachineSpec.php deleted file mode 100644 index 2998dceba..000000000 --- a/src/Component/spec/StateMachine/OperationStateMachineSpec.php +++ /dev/null @@ -1,101 +0,0 @@ -beConstructedWith($locator); - } - - function it_is_initializable(): void - { - $this->shouldHaveType(OperationStateMachine::class); - } - - function it_calls_can_method_from_operation_state_machine_as_string( - ContainerInterface $locator, - OperationStateMachineInterface $stateMachine, - \stdClass $data, - ): void { - $operation = (new Create())->withStateMachineComponent('symfony'); - $context = new Context(); - - $locator->has('symfony')->willReturn(true); - $locator->get('symfony')->willReturn($stateMachine); - - $stateMachine->can($data, $operation, $context)->willReturn(true)->shouldBeCalled(); - - $this->can($data, $operation, $context)->shouldReturn(true); - } - - function it_returns_false_if_no_operation_state_machine_has_been_configured_on_operation( - ContainerInterface $locator, - OperationStateMachineInterface $stateMachine, - \stdClass $data, - ): void { - $operation = new Create(); - $context = new Context(); - - $locator->has('\App\StateMachine')->willReturn(false); - - $this->can($data, $operation, $context)->shouldReturn(false); - } - - function it_calls_apply_method_from_operation_state_machine_as_string( - ContainerInterface $locator, - OperationStateMachineInterface $stateMachine, - \stdClass $data, - ): void { - $operation = (new Create())->withStateMachineComponent('symfony'); - $context = new Context(); - - $locator->has('symfony')->willReturn(true); - $locator->get('symfony')->willReturn($stateMachine); - - $stateMachine->apply($data, $operation, $context)->shouldBeCalled(); - - $this->apply($data, $operation, $context); - } - - function it_does_nothing_if_no_operation_state_machine_has_been_configured_on_operation( - ContainerInterface $locator, - \stdClass $data, - ): void { - $operation = new Create(); - $context = new Context(); - - $this->apply($data, $operation, $context); - } - - function it_throws_an_exception_when_operation_does_not_implement_a_state_machine( - \stdClass $data, - ): void { - $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()]); - } -} diff --git a/src/Component/spec/StateMachine/State/ApplyStateMachineTransitionProcessorSpec.php b/src/Component/spec/StateMachine/State/ApplyStateMachineTransitionProcessorSpec.php deleted file mode 100644 index 4e1b2f0ca..000000000 --- a/src/Component/spec/StateMachine/State/ApplyStateMachineTransitionProcessorSpec.php +++ /dev/null @@ -1,70 +0,0 @@ -beConstructedWith($operationStateMachine, $writeProcessor); - } - - function it_is_initializable(): void - { - $this->shouldHaveType(ApplyStateMachineTransitionProcessor::class); - } - - function it_applies_state_machine_transition_if_possible( - \stdClass $data, - OperationStateMachineInterface $operationStateMachine, - ProcessorInterface $writeProcessor, - ): void { - $operation = new Create(); - - $context = new Context(); - - $operationStateMachine->can($data, $operation, $context)->willReturn(true)->shouldBeCalled(); - $operationStateMachine->apply($data, $operation, $context)->shouldBeCalled(); - - $writeProcessor->process($data, $operation, $context)->willReturn(null)->shouldBeCalled(); - - $this->process($data, $operation, $context); - } - - function it_does_nothing_when_transition_is_not_possible( - \stdClass $data, - OperationStateMachineInterface $operationStateMachine, - ProcessorInterface $writeProcessor, - ): void { - $operation = new Create(); - - $context = new Context(); - - $operationStateMachine->can($data, $operation, $context)->willReturn(false)->shouldBeCalled(); - $operationStateMachine->apply($data, $operation, $context)->shouldNotBeCalled(); - - $writeProcessor->process($data, $operation, $context)->willReturn(null)->shouldBeCalled(); - - $this->process($data, $operation, $context)->shouldReturn(null); - } -} diff --git a/src/Component/tests/StateMachine/OperationStateMachineTest.php b/src/Component/tests/StateMachine/OperationStateMachineTest.php new file mode 100644 index 000000000..b6be2aaad --- /dev/null +++ b/src/Component/tests/StateMachine/OperationStateMachineTest.php @@ -0,0 +1,121 @@ +locator = $this->createMock(ContainerInterface::class); + $this->operationStateMachine = new OperationStateMachine($this->locator); + } + + public function testItIsInitializable(): void + { + $this->assertInstanceOf(OperationStateMachine::class, $this->operationStateMachine); + } + + public function testItCallsCanMethodFromOperationStateMachineAsString(): void + { + $stateMachine = $this->createMock(OperationStateMachineInterface::class); + $data = new \stdClass(); + $operation = (new Create())->withStateMachineComponent('symfony'); + $context = new Context(); + + $this->locator->expects($this->once()) + ->method('has') + ->with('symfony') + ->willReturn(true); + + $this->locator->expects($this->once()) + ->method('get') + ->with('symfony') + ->willReturn($stateMachine); + + $stateMachine->expects($this->once()) + ->method('can') + ->with($data, $operation, $context) + ->willReturn(true); + + $this->assertTrue($this->operationStateMachine->can($data, $operation, $context)); + } + + public function testItReturnsFalseIfNoOperationStateMachineHasBeenConfiguredOnOperation(): void + { + $data = new \stdClass(); + $operation = new Create(); + $context = new Context(); + + $this->assertFalse($this->operationStateMachine->can($data, $operation, $context)); + } + + public function testItCallsApplyMethodFromOperationStateMachineAsString(): void + { + $stateMachine = $this->createMock(OperationStateMachineInterface::class); + $data = new \stdClass(); + $operation = (new Create())->withStateMachineComponent('symfony'); + $context = new Context(); + + $this->locator->expects($this->once()) + ->method('has') + ->with('symfony') + ->willReturn(true); + + $this->locator->expects($this->once()) + ->method('get') + ->with('symfony') + ->willReturn($stateMachine); + + $stateMachine->expects($this->once()) + ->method('apply') + ->with($data, $operation, $context); + + $this->operationStateMachine->apply($data, $operation, $context); + } + + public function testItDoesNothingIfNoOperationStateMachineHasBeenConfiguredOnOperation(): void + { + $data = new \stdClass(); + $operation = new Create(); + $context = new Context(); + + $this->operationStateMachine->apply($data, $operation, $context); + $this->expectNotToPerformAssertions(); + } + + public function testItThrowsAnExceptionWhenOperationDoesNotImplementAStateMachine(): void + { + $data = new \stdClass(); + $operation = new Index(); + + $this->expectException(LogicException::class); + $this->expectExceptionMessage(sprintf('Expected an instance of %s. Got: %s', StateMachineAwareOperationInterface::class, Index::class)); + + $this->operationStateMachine->can($data, $operation, new Context()); + } +} diff --git a/src/Component/tests/StateMachine/State/ApplyStateMachineTransitionProcessorSpec.php b/src/Component/tests/StateMachine/State/ApplyStateMachineTransitionProcessorSpec.php new file mode 100644 index 000000000..25a2ba564 --- /dev/null +++ b/src/Component/tests/StateMachine/State/ApplyStateMachineTransitionProcessorSpec.php @@ -0,0 +1,90 @@ +operationStateMachine = $this->createMock(OperationStateMachineInterface::class); + $this->writeProcessor = $this->createMock(ProcessorInterface::class); + $this->processor = new ApplyStateMachineTransitionProcessor( + $this->operationStateMachine, + $this->writeProcessor, + ); + } + + public function testItIsInitializable(): void + { + $this->assertInstanceOf(ApplyStateMachineTransitionProcessor::class, $this->processor); + } + + public function testItAppliesStateMachineTransitionIfPossible(): void + { + $data = new \stdClass(); + $operation = new Create(); + $context = new Context(); + + $this->operationStateMachine->expects($this->once()) + ->method('can') + ->with($data, $operation, $context) + ->willReturn(true); + + $this->operationStateMachine->expects($this->once()) + ->method('apply') + ->with($data, $operation, $context); + + $this->writeProcessor->expects($this->once()) + ->method('process') + ->with($data, $operation, $context) + ->willReturn(null); + + $this->processor->process($data, $operation, $context); + } + + public function testItDoesNothingWhenTransitionIsNotPossible(): void + { + $data = new \stdClass(); + $operation = new Create(); + $context = new Context(); + + $this->operationStateMachine->expects($this->once()) + ->method('can') + ->with($data, $operation, $context) + ->willReturn(false); + + $this->operationStateMachine->expects($this->never()) + ->method('apply'); + + $this->writeProcessor->expects($this->once()) + ->method('process') + ->with($data, $operation, $context) + ->willReturn(null); + + $this->assertNull($this->processor->process($data, $operation, $context)); + } +} diff --git a/src/Component/spec/StateMachine/StateMachineSpec.php b/src/Component/tests/StateMachine/StateMachineTest.php similarity index 52% rename from src/Component/spec/StateMachine/StateMachineSpec.php rename to src/Component/tests/StateMachine/StateMachineTest.php index d17dd4804..af7d415b0 100644 --- a/src/Component/spec/StateMachine/StateMachineSpec.php +++ b/src/Component/tests/StateMachine/StateMachineTest.php @@ -11,17 +11,19 @@ declare(strict_types=1); -namespace spec\Sylius\Resource\StateMachine; +namespace Sylius\Resource\Tests\StateMachine; -use PhpSpec\ObjectBehavior; +use PHPUnit\Framework\TestCase; use Sylius\Resource\StateMachine\StateMachine; use Sylius\Resource\Tests\Dummy\PullRequest; -final class StateMachineSpec extends ObjectBehavior +final class StateMachineTest extends TestCase { - function let(): void + private StateMachine $stateMachine; + + protected function setUp(): void { - $this->beConstructedWith(new PullRequest(), [ + $this->stateMachine = new StateMachine(new PullRequest(), [ 'graph' => 'pull_request', 'property_path' => 'currentPlace', 'places' => [ @@ -37,18 +39,18 @@ function let(): void ]); } - function it_is_initializable(): void + public function testItIsInitializable(): void { - $this->shouldHaveType(StateMachine::class); + $this->assertInstanceOf(StateMachine::class, $this->stateMachine); } - function it_gets_transition_from_a_state(): void + public function testItGetsTransitionFromAState(): void { - $this->getTransitionFromState('start')->shouldReturn('submit'); + $this->assertSame('submit', $this->stateMachine->getTransitionFromState('start')); } - function it_gets_transition_to_a_state(): void + public function testItGetsTransitionToAState(): void { - $this->getTransitionToState('test')->shouldReturn('submit'); + $this->assertSame('submit', $this->stateMachine->getTransitionToState('test')); } }