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

120 changes: 120 additions & 0 deletions src/Component/tests/StateMachine/OperationStateMachineTest.php
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);
}
Comment on lines +37 to +40
Copy link
Contributor

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.

Copy link
Member

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


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