-
-
Notifications
You must be signed in to change notification settings - Fork 163
[phpspec-2-phpunit] migration of tests (StateMachine) #955
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
101 changes: 0 additions & 101 deletions
101
src/Component/spec/StateMachine/OperationStateMachineSpec.php
This file was deleted.
Oops, something went wrong.
70 changes: 0 additions & 70 deletions
70
src/Component/spec/StateMachine/State/ApplyStateMachineTransitionProcessorSpec.php
This file was deleted.
Oops, something went wrong.
120 changes: 120 additions & 0 deletions
120
src/Component/tests/StateMachine/OperationStateMachineTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| <?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 Sylius\Resource\Tests\StateMachine; | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
| use Psr\Container\ContainerInterface; | ||
| use Sylius\Resource\Context\Context; | ||
| use Sylius\Resource\Metadata\Create; | ||
| use Sylius\Resource\Metadata\Index; | ||
| use Sylius\Resource\Metadata\StateMachineAwareOperationInterface; | ||
| use Sylius\Resource\StateMachine\OperationStateMachine; | ||
| use Sylius\Resource\StateMachine\OperationStateMachineInterface; | ||
|
|
||
| final class OperationStateMachineTest extends TestCase | ||
| { | ||
| private ContainerInterface $locator; | ||
|
|
||
| private OperationStateMachine $operationStateMachine; | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| $this->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()); | ||
| } | ||
| } | ||
90 changes: 90 additions & 0 deletions
90
src/Component/tests/StateMachine/State/ApplyStateMachineTransitionProcessorTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| <?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 Sylius\Resource\Tests\StateMachine\State; | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
| use Sylius\Resource\Context\Context; | ||
| use Sylius\Resource\Metadata\Create; | ||
| use Sylius\Resource\State\ProcessorInterface; | ||
| use Sylius\Resource\StateMachine\OperationStateMachineInterface; | ||
| use Sylius\Resource\StateMachine\State\ApplyStateMachineTransitionProcessor; | ||
|
|
||
| final class ApplyStateMachineTransitionProcessorTest extends TestCase | ||
| { | ||
| private OperationStateMachineInterface $operationStateMachine; | ||
|
|
||
| private ProcessorInterface $writeProcessor; | ||
|
|
||
| private ApplyStateMachineTransitionProcessor $processor; | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| $this->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)); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be removed as it would crash in the setup method if that was not the case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mamazu sounds good, let's do it in a follow-up PR