Skip to content

Commit 3d972c8

Browse files
authored
[phpspec-2-phpunit] migration of tests (StateMachine) (#955)
| Q | A | --------------- | ----- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Related tickets | | License | MIT
2 parents 307b7c0 + 1f23ce0 commit 3d972c8

File tree

5 files changed

+223
-182
lines changed

5 files changed

+223
-182
lines changed

src/Component/spec/StateMachine/OperationStateMachineSpec.php

Lines changed: 0 additions & 101 deletions
This file was deleted.

src/Component/spec/StateMachine/State/ApplyStateMachineTransitionProcessorSpec.php

Lines changed: 0 additions & 70 deletions
This file was deleted.
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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\State;
15+
16+
use PHPUnit\Framework\TestCase;
17+
use Sylius\Resource\Context\Context;
18+
use Sylius\Resource\Metadata\Create;
19+
use Sylius\Resource\State\ProcessorInterface;
20+
use Sylius\Resource\StateMachine\OperationStateMachineInterface;
21+
use Sylius\Resource\StateMachine\State\ApplyStateMachineTransitionProcessor;
22+
23+
final class ApplyStateMachineTransitionProcessorTest extends TestCase
24+
{
25+
private OperationStateMachineInterface $operationStateMachine;
26+
27+
private ProcessorInterface $writeProcessor;
28+
29+
private ApplyStateMachineTransitionProcessor $processor;
30+
31+
protected function setUp(): void
32+
{
33+
$this->operationStateMachine = $this->createMock(OperationStateMachineInterface::class);
34+
$this->writeProcessor = $this->createMock(ProcessorInterface::class);
35+
$this->processor = new ApplyStateMachineTransitionProcessor(
36+
$this->operationStateMachine,
37+
$this->writeProcessor,
38+
);
39+
}
40+
41+
public function testItIsInitializable(): void
42+
{
43+
$this->assertInstanceOf(ApplyStateMachineTransitionProcessor::class, $this->processor);
44+
}
45+
46+
public function testItAppliesStateMachineTransitionIfPossible(): void
47+
{
48+
$data = new \stdClass();
49+
$operation = new Create();
50+
$context = new Context();
51+
52+
$this->operationStateMachine->expects($this->once())
53+
->method('can')
54+
->with($data, $operation, $context)
55+
->willReturn(true);
56+
57+
$this->operationStateMachine->expects($this->once())
58+
->method('apply')
59+
->with($data, $operation, $context);
60+
61+
$this->writeProcessor->expects($this->once())
62+
->method('process')
63+
->with($data, $operation, $context)
64+
->willReturn(null);
65+
66+
$this->processor->process($data, $operation, $context);
67+
}
68+
69+
public function testItDoesNothingWhenTransitionIsNotPossible(): void
70+
{
71+
$data = new \stdClass();
72+
$operation = new Create();
73+
$context = new Context();
74+
75+
$this->operationStateMachine->expects($this->once())
76+
->method('can')
77+
->with($data, $operation, $context)
78+
->willReturn(false);
79+
80+
$this->operationStateMachine->expects($this->never())
81+
->method('apply');
82+
83+
$this->writeProcessor->expects($this->once())
84+
->method('process')
85+
->with($data, $operation, $context)
86+
->willReturn(null);
87+
88+
$this->assertNull($this->processor->process($data, $operation, $context));
89+
}
90+
}

0 commit comments

Comments
 (0)