Skip to content

Commit 3f5b125

Browse files
committed
[phpspec-2-phpunit] migration of tests (StateMachine)
1 parent 44d53e9 commit 3f5b125

File tree

3 files changed

+126
-85
lines changed

3 files changed

+126
-85
lines changed

src/Component/spec/StateMachine/OperationStateMachineSpec.php

Lines changed: 63 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111

1212
declare(strict_types=1);
1313

14-
namespace spec\Sylius\Resource\StateMachine;
14+
namespace Sylius\Resource\Tests\StateMachine;
1515

16-
use PhpSpec\ObjectBehavior;
16+
use PHPUnit\Framework\TestCase;
1717
use Psr\Container\ContainerInterface;
1818
use Sylius\Resource\Context\Context;
1919
use Sylius\Resource\Metadata\Create;
@@ -22,80 +22,99 @@
2222
use Sylius\Resource\StateMachine\OperationStateMachine;
2323
use Sylius\Resource\StateMachine\OperationStateMachineInterface;
2424

25-
final class OperationStateMachineSpec extends ObjectBehavior
25+
final class OperationStateMachineTest extends TestCase
2626
{
27-
function let(ContainerInterface $locator): void
27+
private ContainerInterface $locator;
28+
29+
private OperationStateMachine $operationStateMachine;
30+
31+
protected function setUp(): void
2832
{
29-
$this->beConstructedWith($locator);
33+
$this->locator = $this->createMock(ContainerInterface::class);
34+
$this->operationStateMachine = new OperationStateMachine($this->locator);
3035
}
3136

32-
function it_is_initializable(): void
37+
public function testItIsInitializable(): void
3338
{
34-
$this->shouldHaveType(OperationStateMachine::class);
39+
$this->assertInstanceOf(OperationStateMachine::class, $this->operationStateMachine);
3540
}
3641

37-
function it_calls_can_method_from_operation_state_machine_as_string(
38-
ContainerInterface $locator,
39-
OperationStateMachineInterface $stateMachine,
40-
\stdClass $data,
41-
): void {
42+
public function testItCallsCanMethodFromOperationStateMachineAsString(): void
43+
{
44+
$stateMachine = $this->createMock(OperationStateMachineInterface::class);
45+
$data = new \stdClass();
4246
$operation = (new Create())->withStateMachineComponent('symfony');
4347
$context = new Context();
4448

45-
$locator->has('symfony')->willReturn(true);
46-
$locator->get('symfony')->willReturn($stateMachine);
49+
$this->locator->expects($this->once())
50+
->method('has')
51+
->with('symfony')
52+
->willReturn(true);
4753

48-
$stateMachine->can($data, $operation, $context)->willReturn(true)->shouldBeCalled();
54+
$this->locator->expects($this->once())
55+
->method('get')
56+
->with('symfony')
57+
->willReturn($stateMachine);
4958

50-
$this->can($data, $operation, $context)->shouldReturn(true);
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));
5165
}
5266

53-
function it_returns_false_if_no_operation_state_machine_has_been_configured_on_operation(
54-
ContainerInterface $locator,
55-
OperationStateMachineInterface $stateMachine,
56-
\stdClass $data,
57-
): void {
67+
public function testItReturnsFalseIfNoOperationStateMachineHasBeenConfiguredOnOperation(): void
68+
{
69+
$data = new \stdClass();
5870
$operation = new Create();
5971
$context = new Context();
6072

61-
$locator->has('\App\StateMachine')->willReturn(false);
62-
63-
$this->can($data, $operation, $context)->shouldReturn(false);
73+
$this->assertFalse($this->operationStateMachine->can($data, $operation, $context));
6474
}
6575

66-
function it_calls_apply_method_from_operation_state_machine_as_string(
67-
ContainerInterface $locator,
68-
OperationStateMachineInterface $stateMachine,
69-
\stdClass $data,
70-
): void {
76+
public function testItCallsApplyMethodFromOperationStateMachineAsString(): void
77+
{
78+
$stateMachine = $this->createMock(OperationStateMachineInterface::class);
79+
$data = new \stdClass();
7180
$operation = (new Create())->withStateMachineComponent('symfony');
7281
$context = new Context();
7382

74-
$locator->has('symfony')->willReturn(true);
75-
$locator->get('symfony')->willReturn($stateMachine);
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);
7692

77-
$stateMachine->apply($data, $operation, $context)->shouldBeCalled();
93+
$stateMachine->expects($this->once())
94+
->method('apply')
95+
->with($data, $operation, $context);
7896

79-
$this->apply($data, $operation, $context);
97+
$this->operationStateMachine->apply($data, $operation, $context);
8098
}
8199

82-
function it_does_nothing_if_no_operation_state_machine_has_been_configured_on_operation(
83-
ContainerInterface $locator,
84-
\stdClass $data,
85-
): void {
100+
public function testItDoesNothingIfNoOperationStateMachineHasBeenConfiguredOnOperation(): void
101+
{
102+
$data = new \stdClass();
86103
$operation = new Create();
87104
$context = new Context();
88105

89-
$this->apply($data, $operation, $context);
106+
$this->operationStateMachine->apply($data, $operation, $context);
107+
$this->expectNotToPerformAssertions();
90108
}
91109

92-
function it_throws_an_exception_when_operation_does_not_implement_a_state_machine(
93-
\stdClass $data,
94-
): void {
110+
public function testItThrowsAnExceptionWhenOperationDoesNotImplementAStateMachine(): void
111+
{
112+
$data = new \stdClass();
95113
$operation = new Index();
96114

97-
$this->shouldThrow(
98-
new \LogicException(sprintf('Expected an instance of %s. Got: %s', StateMachineAwareOperationInterface::class, Index::class)),
99-
)->during('can', [$data, $operation, new Context()]);
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());
100119
}
101120
}

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

Lines changed: 50 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,60 +11,80 @@
1111

1212
declare(strict_types=1);
1313

14-
namespace spec\Sylius\Resource\StateMachine\State;
14+
namespace Sylius\Resource\Tests\StateMachine\State;
1515

16-
use PhpSpec\ObjectBehavior;
16+
use PHPUnit\Framework\TestCase;
1717
use Sylius\Resource\Context\Context;
1818
use Sylius\Resource\Metadata\Create;
1919
use Sylius\Resource\State\ProcessorInterface;
2020
use Sylius\Resource\StateMachine\OperationStateMachineInterface;
2121
use Sylius\Resource\StateMachine\State\ApplyStateMachineTransitionProcessor;
2222

23-
final class ApplyStateMachineTransitionProcessorSpec extends ObjectBehavior
23+
final class ApplyStateMachineTransitionProcessorTest extends TestCase
2424
{
25-
function let(
26-
OperationStateMachineInterface $operationStateMachine,
27-
ProcessorInterface $writeProcessor,
28-
): void {
29-
$this->beConstructedWith($operationStateMachine, $writeProcessor);
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+
);
3039
}
3140

32-
function it_is_initializable(): void
41+
public function testItIsInitializable(): void
3342
{
34-
$this->shouldHaveType(ApplyStateMachineTransitionProcessor::class);
43+
$this->assertInstanceOf(ApplyStateMachineTransitionProcessor::class, $this->processor);
3544
}
3645

37-
function it_applies_state_machine_transition_if_possible(
38-
\stdClass $data,
39-
OperationStateMachineInterface $operationStateMachine,
40-
ProcessorInterface $writeProcessor,
41-
): void {
46+
public function testItAppliesStateMachineTransitionIfPossible(): void
47+
{
48+
$data = new \stdClass();
4249
$operation = new Create();
43-
4450
$context = new Context();
4551

46-
$operationStateMachine->can($data, $operation, $context)->willReturn(true)->shouldBeCalled();
47-
$operationStateMachine->apply($data, $operation, $context)->shouldBeCalled();
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);
4860

49-
$writeProcessor->process($data, $operation, $context)->willReturn(null)->shouldBeCalled();
61+
$this->writeProcessor->expects($this->once())
62+
->method('process')
63+
->with($data, $operation, $context)
64+
->willReturn(null);
5065

51-
$this->process($data, $operation, $context);
66+
$this->processor->process($data, $operation, $context);
5267
}
5368

54-
function it_does_nothing_when_transition_is_not_possible(
55-
\stdClass $data,
56-
OperationStateMachineInterface $operationStateMachine,
57-
ProcessorInterface $writeProcessor,
58-
): void {
69+
public function testItDoesNothingWhenTransitionIsNotPossible(): void
70+
{
71+
$data = new \stdClass();
5972
$operation = new Create();
60-
6173
$context = new Context();
6274

63-
$operationStateMachine->can($data, $operation, $context)->willReturn(false)->shouldBeCalled();
64-
$operationStateMachine->apply($data, $operation, $context)->shouldNotBeCalled();
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');
6582

66-
$writeProcessor->process($data, $operation, $context)->willReturn(null)->shouldBeCalled();
83+
$this->writeProcessor->expects($this->once())
84+
->method('process')
85+
->with($data, $operation, $context)
86+
->willReturn(null);
6787

68-
$this->process($data, $operation, $context)->shouldReturn(null);
88+
$this->assertNull($this->processor->process($data, $operation, $context));
6989
}
7090
}

src/Component/spec/StateMachine/StateMachineSpec.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,19 @@
1111

1212
declare(strict_types=1);
1313

14-
namespace spec\Sylius\Resource\StateMachine;
14+
namespace Sylius\Resource\Tests\StateMachine;
1515

16-
use PhpSpec\ObjectBehavior;
16+
use PHPUnit\Framework\TestCase;
1717
use Sylius\Resource\StateMachine\StateMachine;
1818
use Sylius\Resource\Tests\Dummy\PullRequest;
1919

20-
final class StateMachineSpec extends ObjectBehavior
20+
final class StateMachineTest extends TestCase
2121
{
22-
function let(): void
22+
private StateMachine $stateMachine;
23+
24+
protected function setUp(): void
2325
{
24-
$this->beConstructedWith(new PullRequest(), [
26+
$this->stateMachine = new StateMachine(new PullRequest(), [
2527
'graph' => 'pull_request',
2628
'property_path' => 'currentPlace',
2729
'places' => [
@@ -37,18 +39,18 @@ function let(): void
3739
]);
3840
}
3941

40-
function it_is_initializable(): void
42+
public function testItIsInitializable(): void
4143
{
42-
$this->shouldHaveType(StateMachine::class);
44+
$this->assertInstanceOf(StateMachine::class, $this->stateMachine);
4345
}
4446

45-
function it_gets_transition_from_a_state(): void
47+
public function testItGetsTransitionFromAState(): void
4648
{
47-
$this->getTransitionFromState('start')->shouldReturn('submit');
49+
$this->assertSame('submit', $this->stateMachine->getTransitionFromState('start'));
4850
}
4951

50-
function it_gets_transition_to_a_state(): void
52+
public function testItGetsTransitionToAState(): void
5153
{
52-
$this->getTransitionToState('test')->shouldReturn('submit');
54+
$this->assertSame('submit', $this->stateMachine->getTransitionToState('test'));
5355
}
5456
}

0 commit comments

Comments
 (0)