|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Sylius package. |
| 5 | + * |
| 6 | + * (c) Sylius Sp. z o.o. |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace Sylius\Resource\Tests\StateMachine; |
| 15 | + |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | +use Psr\Container\ContainerInterface; |
| 18 | +use Sylius\Resource\Context\Context; |
| 19 | +use Sylius\Resource\Metadata\Create; |
| 20 | +use Sylius\Resource\Metadata\Index; |
| 21 | +use Sylius\Resource\Metadata\StateMachineAwareOperationInterface; |
| 22 | +use Sylius\Resource\StateMachine\OperationStateMachine; |
| 23 | +use Sylius\Resource\StateMachine\OperationStateMachineInterface; |
| 24 | + |
| 25 | +final class OperationStateMachineTest extends TestCase |
| 26 | +{ |
| 27 | + private ContainerInterface $locator; |
| 28 | + |
| 29 | + private OperationStateMachine $operationStateMachine; |
| 30 | + |
| 31 | + protected function setUp(): void |
| 32 | + { |
| 33 | + $this->locator = $this->createMock(ContainerInterface::class); |
| 34 | + $this->operationStateMachine = new OperationStateMachine($this->locator); |
| 35 | + } |
| 36 | + |
| 37 | + public function testItIsInitializable(): void |
| 38 | + { |
| 39 | + $this->assertInstanceOf(OperationStateMachine::class, $this->operationStateMachine); |
| 40 | + } |
| 41 | + |
| 42 | + public function testItCallsCanMethodFromOperationStateMachineAsString(): void |
| 43 | + { |
| 44 | + $stateMachine = $this->createMock(OperationStateMachineInterface::class); |
| 45 | + $data = new \stdClass(); |
| 46 | + $operation = (new Create())->withStateMachineComponent('symfony'); |
| 47 | + $context = new Context(); |
| 48 | + |
| 49 | + $this->locator->expects($this->once()) |
| 50 | + ->method('has') |
| 51 | + ->with('symfony') |
| 52 | + ->willReturn(true); |
| 53 | + |
| 54 | + $this->locator->expects($this->once()) |
| 55 | + ->method('get') |
| 56 | + ->with('symfony') |
| 57 | + ->willReturn($stateMachine); |
| 58 | + |
| 59 | + $stateMachine->expects($this->once()) |
| 60 | + ->method('can') |
| 61 | + ->with($data, $operation, $context) |
| 62 | + ->willReturn(true); |
| 63 | + |
| 64 | + $this->assertTrue($this->operationStateMachine->can($data, $operation, $context)); |
| 65 | + } |
| 66 | + |
| 67 | + public function testItReturnsFalseIfNoOperationStateMachineHasBeenConfiguredOnOperation(): void |
| 68 | + { |
| 69 | + $data = new \stdClass(); |
| 70 | + $operation = new Create(); |
| 71 | + $context = new Context(); |
| 72 | + |
| 73 | + $this->assertFalse($this->operationStateMachine->can($data, $operation, $context)); |
| 74 | + } |
| 75 | + |
| 76 | + public function testItCallsApplyMethodFromOperationStateMachineAsString(): void |
| 77 | + { |
| 78 | + $stateMachine = $this->createMock(OperationStateMachineInterface::class); |
| 79 | + $data = new \stdClass(); |
| 80 | + $operation = (new Create())->withStateMachineComponent('symfony'); |
| 81 | + $context = new Context(); |
| 82 | + |
| 83 | + $this->locator->expects($this->once()) |
| 84 | + ->method('has') |
| 85 | + ->with('symfony') |
| 86 | + ->willReturn(true); |
| 87 | + |
| 88 | + $this->locator->expects($this->once()) |
| 89 | + ->method('get') |
| 90 | + ->with('symfony') |
| 91 | + ->willReturn($stateMachine); |
| 92 | + |
| 93 | + $stateMachine->expects($this->once()) |
| 94 | + ->method('apply') |
| 95 | + ->with($data, $operation, $context); |
| 96 | + |
| 97 | + $this->operationStateMachine->apply($data, $operation, $context); |
| 98 | + } |
| 99 | + |
| 100 | + public function testItDoesNothingIfNoOperationStateMachineHasBeenConfiguredOnOperation(): void |
| 101 | + { |
| 102 | + $data = new \stdClass(); |
| 103 | + $operation = new Create(); |
| 104 | + $context = new Context(); |
| 105 | + |
| 106 | + $this->operationStateMachine->apply($data, $operation, $context); |
| 107 | + $this->expectNotToPerformAssertions(); |
| 108 | + } |
| 109 | + |
| 110 | + public function testItThrowsAnExceptionWhenOperationDoesNotImplementAStateMachine(): void |
| 111 | + { |
| 112 | + $data = new \stdClass(); |
| 113 | + $operation = new Index(); |
| 114 | + |
| 115 | + $this->expectException(\LogicException::class); |
| 116 | + $this->expectExceptionMessage(sprintf('Expected an instance of %s. Got: %s', StateMachineAwareOperationInterface::class, Index::class)); |
| 117 | + |
| 118 | + $this->operationStateMachine->can($data, $operation, new Context()); |
| 119 | + } |
| 120 | +} |
0 commit comments