Skip to content
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

[phpspec-2-phpunit] migration of tests (StateMachine) #955

Open
wants to merge 2 commits into
base: 1.13
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 0 additions & 101 deletions src/Component/spec/StateMachine/OperationStateMachineSpec.php

This file was deleted.

This file was deleted.

121 changes: 121 additions & 0 deletions src/Component/tests/StateMachine/OperationStateMachineTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?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 LogicException;
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());
}
}
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));
}
}
Loading
Loading