mirror of
https://github.com/Sylius/Sylius.git
synced 2026-07-14 09:02:12 +00:00
Added tests for process classes
This commit is contained in:
parent
c9b8b3be9a
commit
9c5105d8da
7 changed files with 1493 additions and 0 deletions
|
|
@ -0,0 +1,294 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Sylius package.
|
||||||
|
*
|
||||||
|
* (c) Paweł Jędrzejewski
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Sylius\Bundle\FlowBundle\Tests\Process\Builder;
|
||||||
|
|
||||||
|
use Sylius\Bundle\FlowBundle\Process\Builder\ProcessBuilder;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ProcessBuilder test.
|
||||||
|
*
|
||||||
|
* @author Leszek Prabucki <leszek.prabucki@gmail.com>
|
||||||
|
*/
|
||||||
|
class ProcessBuilderTest extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
private $builder;
|
||||||
|
|
||||||
|
public function setUp()
|
||||||
|
{
|
||||||
|
$this->builder = new TestProcessBuilder($this->getMock('Symfony\Component\DependencyInjection\ContainerInterface'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function shouldCreateProcess()
|
||||||
|
{
|
||||||
|
$process = $this->builder->build($this->getMock('Sylius\Bundle\FlowBundle\Process\Scenario\ProcessScenarioInterface'));
|
||||||
|
|
||||||
|
$this->assertInstanceOf('Sylius\Bundle\FlowBundle\Process\Process', $process);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function shouldBuildScenario()
|
||||||
|
{
|
||||||
|
$scenario = $this->getMock('Sylius\Bundle\FlowBundle\Process\Scenario\ProcessScenarioInterface');
|
||||||
|
$scenario->expects($this->once())
|
||||||
|
->method('build')
|
||||||
|
->with($this->equalTo($this->builder));
|
||||||
|
|
||||||
|
$this->builder->build($scenario);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
* @expectedException \RuntimeException
|
||||||
|
*/
|
||||||
|
public function shouldNotAddWihtoutProcess()
|
||||||
|
{
|
||||||
|
$process = $this->getMock('Sylius\Bundle\FlowBundle\Process\ProcessInterface');
|
||||||
|
|
||||||
|
$this->builder->registerStep('new', $this->getStep('somename'));
|
||||||
|
$this->builder->add('somename', 'new');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
* @covers Sylius\Bundle\FlowBundle\Process\Builder\ProcessBuilder::add
|
||||||
|
*/
|
||||||
|
public function shouldInjectContainerToContainerAwareStep()
|
||||||
|
{
|
||||||
|
$step = $this->getMock('Sylius\Bundle\FlowBundle\Process\Step\ContainerAwareStep');
|
||||||
|
$step->expects($this->once())
|
||||||
|
->method('setContainer')
|
||||||
|
->with($this->isInstanceOf('Symfony\Component\DependencyInjection\ContainerInterface'));
|
||||||
|
|
||||||
|
$this->builder->build($this->getMock('Sylius\Bundle\FlowBundle\Process\Scenario\ProcessScenarioInterface'));
|
||||||
|
$this->builder->add('somename', $step);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
* @covers Sylius\Bundle\FlowBundle\Process\Builder\ProcessBuilder::add
|
||||||
|
* @covers Sylius\Bundle\FlowBundle\Process\Builder\ProcessBuilder::registerStep
|
||||||
|
*/
|
||||||
|
public function shouldAcceptStepAliasWhileAdding()
|
||||||
|
{
|
||||||
|
$step = $this->getStep();
|
||||||
|
$step->expects($this->any())
|
||||||
|
->method('setName')
|
||||||
|
->with($this->equalTo('somename'));
|
||||||
|
|
||||||
|
$this->builder->build($this->getMock('Sylius\Bundle\FlowBundle\Process\Scenario\ProcessScenarioInterface'));
|
||||||
|
$this->builder->registerStep('new', $step);
|
||||||
|
$this->builder->add('somename', 'new');
|
||||||
|
|
||||||
|
$this->assertSame($step, $this->builder->getProcess()->getStepByName('somename'));
|
||||||
|
$this->assertCount(1, $this->builder->getProcess()->getSteps());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
* @covers Sylius\Bundle\FlowBundle\Process\Builder\ProcessBuilder::add
|
||||||
|
* @expectedException InvalidArgumentException
|
||||||
|
*/
|
||||||
|
public function shouldNotAddObjectWhichAreNotSteps()
|
||||||
|
{
|
||||||
|
$this->builder->build($this->getMock('Sylius\Bundle\FlowBundle\Process\Scenario\ProcessScenarioInterface'));
|
||||||
|
$this->builder->add('some', new \stdClass);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
* @expectedException \RuntimeException
|
||||||
|
*/
|
||||||
|
public function shouldNotRemoveStepWithoutProcess()
|
||||||
|
{
|
||||||
|
$this->builder->remove('test');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function shouldRemoveStepFromProcess()
|
||||||
|
{
|
||||||
|
$this->builder->build($this->getMock('Sylius\Bundle\FlowBundle\Process\Scenario\ProcessScenarioInterface'));
|
||||||
|
$this->builder->add('some', $this->getStep('some'));
|
||||||
|
$this->builder->remove('some');
|
||||||
|
|
||||||
|
$this->assertCount(0, $this->builder->getProcess()->getSteps());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
* @expectedException \RuntimeException
|
||||||
|
*/
|
||||||
|
public function shouldNotCheckIfStepIsSetWithoutProcess()
|
||||||
|
{
|
||||||
|
$this->builder->has('test');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function shouldCheckIfStepIsSet()
|
||||||
|
{
|
||||||
|
$this->builder->build($this->getMock('Sylius\Bundle\FlowBundle\Process\Scenario\ProcessScenarioInterface'));
|
||||||
|
|
||||||
|
$this->assertFalse($this->builder->has('some'));
|
||||||
|
$this->builder->add('some', $this->getStep('some'));
|
||||||
|
$this->assertTrue($this->builder->has('some'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
* @expectedException \RuntimeException
|
||||||
|
*/
|
||||||
|
public function shouldNotInjectDisplayRouteWithoutProcess()
|
||||||
|
{
|
||||||
|
$this->builder->setDisplayRoute('display_route');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function shouldInjectDisplayRouteToProcess()
|
||||||
|
{
|
||||||
|
$this->builder->build($this->getMock('Sylius\Bundle\FlowBundle\Process\Scenario\ProcessScenarioInterface'));
|
||||||
|
$this->builder->setDisplayRoute('display_route');
|
||||||
|
|
||||||
|
$this->assertEquals('display_route', $this->builder->getProcess()->getDisplayRoute());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
* @expectedException \RuntimeException
|
||||||
|
*/
|
||||||
|
public function shouldNotInjectForwardRouteWithoutProcess()
|
||||||
|
{
|
||||||
|
$this->builder->setForwardRoute('forward_route');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function shouldInjectForwardRouteToProcess()
|
||||||
|
{
|
||||||
|
$this->builder->build($this->getMock('Sylius\Bundle\FlowBundle\Process\Scenario\ProcessScenarioInterface'));
|
||||||
|
$this->builder->setForwardRoute('forward_route');
|
||||||
|
|
||||||
|
$this->assertEquals('forward_route', $this->builder->getProcess()->getForwardRoute());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
* @expectedException \RuntimeException
|
||||||
|
*/
|
||||||
|
public function shouldNotInjectRedirectWithoutProcess()
|
||||||
|
{
|
||||||
|
$this->builder->setRedirect('redirect');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function shouldInjectRedirectToProcess()
|
||||||
|
{
|
||||||
|
$this->builder->build($this->getMock('Sylius\Bundle\FlowBundle\Process\Scenario\ProcessScenarioInterface'));
|
||||||
|
$this->builder->setRedirect('redirect');
|
||||||
|
|
||||||
|
$this->assertEquals('redirect', $this->builder->getProcess()->getRedirect());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
* @expectedException \RuntimeException
|
||||||
|
*/
|
||||||
|
public function shouldNotInjectValidationClosureWithoutProcess()
|
||||||
|
{
|
||||||
|
$this->builder->validate(function () {
|
||||||
|
return 'my-closure';
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function shouldInjectValidationClosureToProcess()
|
||||||
|
{
|
||||||
|
$this->builder->build($this->getMock('Sylius\Bundle\FlowBundle\Process\Scenario\ProcessScenarioInterface'));
|
||||||
|
$this->builder->validate(function () {
|
||||||
|
return 'my-closure';
|
||||||
|
});
|
||||||
|
|
||||||
|
$validator = $this->builder->getProcess()->getValidator();
|
||||||
|
$this->assertEquals('my-closure', $validator());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
* @expectedException InvalidArgumentException
|
||||||
|
*/
|
||||||
|
public function shouldNotRegisterTwoThisSameSteps()
|
||||||
|
{
|
||||||
|
$this->builder->registerStep('new', $this->getStep('somename'));
|
||||||
|
$this->builder->registerStep('new', $this->getStep('somename'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
* @expectedException InvalidArgumentException
|
||||||
|
*/
|
||||||
|
public function shouldNotLoadStepWhenWasNotRegisteredBefore()
|
||||||
|
{
|
||||||
|
$this->builder->loadStep('new');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function shouldLoadStep()
|
||||||
|
{
|
||||||
|
$step = $this->getStep('somename');
|
||||||
|
$this->builder->registerStep('new', $step);
|
||||||
|
|
||||||
|
$this->assertSame($this->builder->loadStep('new'), $step);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getStep($name = '')
|
||||||
|
{
|
||||||
|
$step = $this->getMock('Sylius\Bundle\FlowBundle\Process\Step\StepInterface');
|
||||||
|
$step->expects($this->any())
|
||||||
|
->method('getName')
|
||||||
|
->will($this->returnValue($name));
|
||||||
|
$step->expects($this->any())
|
||||||
|
->method('displayAction')
|
||||||
|
->will($this->returnValue('displayActionResponse'));
|
||||||
|
$step->expects($this->any())
|
||||||
|
->method('forwardAction')
|
||||||
|
->will($this->returnValue('forwardActionResponse'));
|
||||||
|
|
||||||
|
return $step;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class TestProcessBuilder extends ProcessBuilder
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Method getProcess exists only in TestProcessBuilder to allow testing
|
||||||
|
*/
|
||||||
|
public function getProcess()
|
||||||
|
{
|
||||||
|
return $this->process;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,430 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Sylius package.
|
||||||
|
*
|
||||||
|
* (c) Paweł Jędrzejewski
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Sylius\Bundle\FlowBundle\Tests\Process\Context;
|
||||||
|
|
||||||
|
use Sylius\Bundle\FlowBundle\Storage\StorageInterface;
|
||||||
|
use Sylius\Bundle\FlowBundle\Process\Context\ProcessContextInterface;
|
||||||
|
use Sylius\Bundle\FlowBundle\Process\Context\ProcessContext;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ProcessContext test.
|
||||||
|
*
|
||||||
|
* @author Leszek Prabucki <leszek.prabucki@gmail.com>
|
||||||
|
*/
|
||||||
|
class ProcessContextTest extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
* @dataProvider getMethodsWithoutInitialize
|
||||||
|
* @expectedException RuntimeException
|
||||||
|
*/
|
||||||
|
public function shouldNotExecuteMethodsWithoutContextInitialize($methodName)
|
||||||
|
{
|
||||||
|
$context = new ProcessContext($this->getMock('Sylius\Bundle\FlowBundle\Storage\StorageInterface'));
|
||||||
|
$context->$methodName();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getMethodsWithoutInitialize()
|
||||||
|
{
|
||||||
|
return array(
|
||||||
|
array('isValid'),
|
||||||
|
array('getProcess'),
|
||||||
|
array('getCurrentStep'),
|
||||||
|
array('getPreviousStep'),
|
||||||
|
array('getNextStep'),
|
||||||
|
array('isFirstStep'),
|
||||||
|
array('isLastStep'),
|
||||||
|
array('complete'),
|
||||||
|
array('isCompleted'),
|
||||||
|
array('close'),
|
||||||
|
array('getProgress'),
|
||||||
|
array('getProgress'),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function shouldInitializeStorage()
|
||||||
|
{
|
||||||
|
$storage = $this->getMock('Sylius\Bundle\FlowBundle\Storage\StorageInterface');
|
||||||
|
$storage->expects($this->once())
|
||||||
|
->method('initialize')
|
||||||
|
->with($this->equalTo(md5('scenarioOne')));
|
||||||
|
|
||||||
|
$context = new ProcessContext($storage);
|
||||||
|
$context->initialize($this->getProcess(), $this->getStep('myStep'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function shouldSetPreviousStepWhenInitialize()
|
||||||
|
{
|
||||||
|
$steps = array(
|
||||||
|
$this->getStep('step1'),
|
||||||
|
$this->getStep('step2')
|
||||||
|
);
|
||||||
|
$process = $this->getProcess($steps);
|
||||||
|
$context = new ProcessContext($this->getMock('Sylius\Bundle\FlowBundle\Storage\StorageInterface'));
|
||||||
|
$context->initialize($process, $steps[1]);
|
||||||
|
|
||||||
|
$this->assertEquals('step1', $context->getPreviousStep()->getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function shouldSetNextStepWhenInitialize()
|
||||||
|
{
|
||||||
|
$steps = array(
|
||||||
|
$this->getStep('step1'),
|
||||||
|
$this->getStep('step2')
|
||||||
|
);
|
||||||
|
$process = $this->getProcess($steps);
|
||||||
|
$context = new ProcessContext($this->getMock('Sylius\Bundle\FlowBundle\Storage\StorageInterface'));
|
||||||
|
$context->initialize($process, $steps[0]);
|
||||||
|
|
||||||
|
$this->assertEquals('step2', $context->getNextStep()->getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function shouldSetCurrentStepWhenInitialize()
|
||||||
|
{
|
||||||
|
$steps = array(
|
||||||
|
$this->getStep('step1'),
|
||||||
|
$this->getStep('step2')
|
||||||
|
);
|
||||||
|
$process = $this->getProcess($steps);
|
||||||
|
$context = new ProcessContext($this->getMock('Sylius\Bundle\FlowBundle\Storage\StorageInterface'));
|
||||||
|
$context->initialize($process, $steps[0]);
|
||||||
|
|
||||||
|
$this->assertSame($steps[0], $context->getCurrentStep());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function shouldKnowWhenFirstStep()
|
||||||
|
{
|
||||||
|
$steps = array(
|
||||||
|
$this->getStep('step1'),
|
||||||
|
$this->getStep('step2')
|
||||||
|
);
|
||||||
|
$process = $this->getProcess($steps);
|
||||||
|
|
||||||
|
$firstStepContext = new ProcessContext($this->getMock('Sylius\Bundle\FlowBundle\Storage\StorageInterface'));
|
||||||
|
$firstStepContext->initialize($process, $steps[0]);
|
||||||
|
$lastStepContext = new ProcessContext($this->getMock('Sylius\Bundle\FlowBundle\Storage\StorageInterface'));
|
||||||
|
$lastStepContext->initialize($process, $steps[1]);
|
||||||
|
|
||||||
|
$this->assertTrue($firstStepContext->isFirstStep());
|
||||||
|
$this->assertFalse($lastStepContext->isFirstStep());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function shouldClearStorageWhenClose()
|
||||||
|
{
|
||||||
|
$storage = $this->getMock('Sylius\Bundle\FlowBundle\Storage\StorageInterface');
|
||||||
|
$storage->expects($this->once())
|
||||||
|
->method('clear');
|
||||||
|
|
||||||
|
$context = new ProcessContext($storage);
|
||||||
|
$context->initialize($this->getProcess(), $this->getStep('myStep'));
|
||||||
|
$context->close();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function shouldKnowWhenLastStep()
|
||||||
|
{
|
||||||
|
$steps = array(
|
||||||
|
$this->getStep('step1'),
|
||||||
|
$this->getStep('step2')
|
||||||
|
);
|
||||||
|
$process = $this->getProcess($steps);
|
||||||
|
|
||||||
|
$firstStepContext = new ProcessContext($this->getMock('Sylius\Bundle\FlowBundle\Storage\StorageInterface'));
|
||||||
|
$firstStepContext->initialize($process, $steps[0]);
|
||||||
|
$lastStepContext = new ProcessContext($this->getMock('Sylius\Bundle\FlowBundle\Storage\StorageInterface'));
|
||||||
|
$lastStepContext->initialize($process, $steps[1]);
|
||||||
|
|
||||||
|
$this->assertFalse($firstStepContext->isLastStep());
|
||||||
|
$this->assertTrue($lastStepContext->isLastStep());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function shouldSetRequest()
|
||||||
|
{
|
||||||
|
$request = $this->getMock('Symfony\Component\HttpFoundation\Request');
|
||||||
|
|
||||||
|
$context = new ProcessContext($this->getMock('Sylius\Bundle\FlowBundle\Storage\StorageInterface'));
|
||||||
|
$context->setRequest($request);
|
||||||
|
|
||||||
|
$this->assertSame($request, $context->getRequest());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function shouldGetProcess()
|
||||||
|
{
|
||||||
|
$steps = array(
|
||||||
|
$this->getStep('step1'),
|
||||||
|
$this->getStep('step2')
|
||||||
|
);
|
||||||
|
$process = $this->getProcess($steps);
|
||||||
|
|
||||||
|
$context = new ProcessContext($this->getMock('Sylius\Bundle\FlowBundle\Storage\StorageInterface'));
|
||||||
|
$context->initialize($process, $steps[0]);
|
||||||
|
|
||||||
|
$this->assertSame($process, $context->getProcess());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function shouldNotBeValidWhenProcessValidatorIsNotValid()
|
||||||
|
{
|
||||||
|
$steps = array(
|
||||||
|
$this->getStep('step1'),
|
||||||
|
$this->getStep('step2')
|
||||||
|
);
|
||||||
|
$process = $this->getProcess($steps);
|
||||||
|
$process->expects($this->once())
|
||||||
|
->method('getValidator')
|
||||||
|
->will($this->returnValue(function () {
|
||||||
|
return false;
|
||||||
|
}));
|
||||||
|
|
||||||
|
$context = new ProcessContext($this->getMock('Sylius\Bundle\FlowBundle\Storage\StorageInterface'));
|
||||||
|
$context->initialize($process, $steps[0]);
|
||||||
|
|
||||||
|
$this->assertFalse($context->isValid());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function shouldNotBeValidWhenOneOfPreviousStepIsCompeleted()
|
||||||
|
{
|
||||||
|
$steps = array(
|
||||||
|
$this->getStep('step1'),
|
||||||
|
$this->getStep('step2')
|
||||||
|
);
|
||||||
|
$process = $this->getProcess($steps);
|
||||||
|
|
||||||
|
$storage = new TestArrayStorage();
|
||||||
|
$storage->set('_status.step1', ProcessContextInterface::STEP_STATE_COMPLETED);
|
||||||
|
|
||||||
|
$context = new ProcessContext($storage);
|
||||||
|
$context->initialize($process, $steps[1]);
|
||||||
|
|
||||||
|
$this->assertFalse($context->isValid());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function shouldBeValid()
|
||||||
|
{
|
||||||
|
$steps = array(
|
||||||
|
$this->getStep('step1'),
|
||||||
|
$this->getStep('step2')
|
||||||
|
);
|
||||||
|
$process = $this->getProcess($steps);
|
||||||
|
|
||||||
|
$context = new ProcessContext($this->getMock('Sylius\Bundle\FlowBundle\Storage\StorageInterface'));
|
||||||
|
$context->initialize($process, $steps[0]);
|
||||||
|
|
||||||
|
$this->assertTrue($context->isValid());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function shouldBeValidWithoutSteps()
|
||||||
|
{
|
||||||
|
$process = $this->getProcess(array());
|
||||||
|
|
||||||
|
$context = new ProcessContext($this->getMock('Sylius\Bundle\FlowBundle\Storage\StorageInterface'));
|
||||||
|
$context->initialize($process, $this->getStep('someStep'));
|
||||||
|
|
||||||
|
$this->assertTrue($context->isValid());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function shouldCompleteProcess()
|
||||||
|
{
|
||||||
|
$storage = new TestArrayStorage();
|
||||||
|
|
||||||
|
$steps = array(
|
||||||
|
$this->getStep('step1'),
|
||||||
|
$this->getStep('step2')
|
||||||
|
);
|
||||||
|
$process = $this->getProcess($steps);
|
||||||
|
$context = new ProcessContext($storage);
|
||||||
|
$context->initialize($process, $steps[0]);
|
||||||
|
$this->assertFalse($context->isCompleted());
|
||||||
|
$context->complete();
|
||||||
|
|
||||||
|
$this->assertEquals(ProcessContextInterface::STEP_STATE_COMPLETED, $storage->get('_state.step1'));
|
||||||
|
$this->assertTrue($context->isCompleted());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
* @dataProvider getProgressData
|
||||||
|
*/
|
||||||
|
public function shouldCalculateProgress($steps, $index, $expectedProgress)
|
||||||
|
{
|
||||||
|
$process = $this->getProcess($steps);
|
||||||
|
|
||||||
|
$context = new ProcessContext($this->getMock('Sylius\Bundle\FlowBundle\Storage\StorageInterface'));
|
||||||
|
$context->initialize($process, $steps[$index]);
|
||||||
|
|
||||||
|
$this->assertEquals($context->getProgress(), $expectedProgress);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getProgressData()
|
||||||
|
{
|
||||||
|
return array(
|
||||||
|
array(
|
||||||
|
array(
|
||||||
|
$this->getStep('step1'),
|
||||||
|
$this->getStep('step2')
|
||||||
|
),
|
||||||
|
0,
|
||||||
|
50
|
||||||
|
),
|
||||||
|
array(
|
||||||
|
array(
|
||||||
|
$this->getStep('step1'),
|
||||||
|
$this->getStep('step2')
|
||||||
|
),
|
||||||
|
1,
|
||||||
|
100
|
||||||
|
),
|
||||||
|
array(
|
||||||
|
array(
|
||||||
|
$this->getStep('step1'),
|
||||||
|
$this->getStep('step2'),
|
||||||
|
$this->getStep('step3')
|
||||||
|
),
|
||||||
|
0,
|
||||||
|
33
|
||||||
|
),
|
||||||
|
array(
|
||||||
|
array(
|
||||||
|
$this->getStep('step1'),
|
||||||
|
$this->getStep('step2'),
|
||||||
|
$this->getStep('step3')
|
||||||
|
),
|
||||||
|
1,
|
||||||
|
66
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function shouldInjectStorageBySetter()
|
||||||
|
{
|
||||||
|
$storage1 = $this->getMock('Sylius\Bundle\FlowBundle\Storage\StorageInterface');
|
||||||
|
$storage2 = $this->getMock('Sylius\Bundle\FlowBundle\Storage\StorageInterface');
|
||||||
|
|
||||||
|
$context = new ProcessContext($storage1);
|
||||||
|
$context->setStorage($storage2);
|
||||||
|
|
||||||
|
$this->assertSame($storage2, $context->getStorage());
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getProcess($steps = array())
|
||||||
|
{
|
||||||
|
$process = $this->getMock('Sylius\Bundle\FlowBundle\Process\ProcessInterface');
|
||||||
|
$process->expects($this->any())
|
||||||
|
->method('setScenarioAlias')
|
||||||
|
->with($this->equalTo('scenarioOne'));
|
||||||
|
$process->expects($this->any())
|
||||||
|
->method('getScenarioAlias')
|
||||||
|
->will($this->returnValue('scenarioOne'));
|
||||||
|
$process->expects($this->any())
|
||||||
|
->method('getOrderedSteps')
|
||||||
|
->will($this->returnValue($steps));
|
||||||
|
$process->expects($this->any())
|
||||||
|
->method('countSteps')
|
||||||
|
->will($this->returnValue(count($steps)));
|
||||||
|
|
||||||
|
return $process;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getStep($name)
|
||||||
|
{
|
||||||
|
$step = $this->getMock('Sylius\Bundle\FlowBundle\Process\Step\StepInterface');
|
||||||
|
$step->expects($this->any())
|
||||||
|
->method('getName')
|
||||||
|
->will($this->returnValue($name));
|
||||||
|
$step->expects($this->any())
|
||||||
|
->method('displayAction')
|
||||||
|
->will($this->returnValue('displayActionResponse'));
|
||||||
|
$step->expects($this->any())
|
||||||
|
->method('forwardAction')
|
||||||
|
->will($this->returnValue('forwardActionResponse'));
|
||||||
|
|
||||||
|
return $step;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class TestArrayStorage implements StorageInterface
|
||||||
|
{
|
||||||
|
private $data = array();
|
||||||
|
|
||||||
|
public function initialize($domain)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function has($key)
|
||||||
|
{
|
||||||
|
return isset($this->data[$key]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get($key, $default = null)
|
||||||
|
{
|
||||||
|
return $this->has($key) ? $this->data[$key] : $default;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function set($key, $value)
|
||||||
|
{
|
||||||
|
$this->data[$key] = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function remove($key)
|
||||||
|
{
|
||||||
|
unset($this->data[$key]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function clear()
|
||||||
|
{
|
||||||
|
$this->data = array();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,382 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Sylius package.
|
||||||
|
*
|
||||||
|
* (c) Paweł Jędrzejewski
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Sylius\Bundle\FlowBundle\Tests\Process\Coordinator;
|
||||||
|
|
||||||
|
use Sylius\Bundle\FlowBundle\Process\Coordinator\Coordinator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Coordinator test.
|
||||||
|
*
|
||||||
|
* @author Leszek Prabucki <leszek.prabucki@gmail.com>
|
||||||
|
*/
|
||||||
|
class CoordinatorTest extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
private $coordinator;
|
||||||
|
|
||||||
|
public function setUp()
|
||||||
|
{
|
||||||
|
$router = $this->getRouter(
|
||||||
|
'sylius_flow_display',
|
||||||
|
array(
|
||||||
|
'scenarioAlias' => 'scenarioOne',
|
||||||
|
'stepName' => 'firstStepName'
|
||||||
|
),
|
||||||
|
'http://someurl.dev/step/scenarioOne/firstStepName'
|
||||||
|
);
|
||||||
|
|
||||||
|
$processBuilder = $this->getProcessBuilder($this->getProcess());
|
||||||
|
|
||||||
|
$processContext = $this->getProcessContext();
|
||||||
|
$processContext->expects($this->any())
|
||||||
|
->method('isValid')
|
||||||
|
->will($this->returnValue(true));
|
||||||
|
|
||||||
|
$this->coordinator = $this->createCoordinator($router, $processBuilder, $processContext);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function shouldRedirectToDefaultDisplayActionWhenStarting()
|
||||||
|
{
|
||||||
|
$this->coordinator->registerScenario('scenarioOne', $this->getMock('Sylius\Bundle\FlowBundle\Process\Scenario\ProcessScenarioInterface'));
|
||||||
|
|
||||||
|
$response = $this->coordinator->start('scenarioOne');
|
||||||
|
|
||||||
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
|
||||||
|
$this->assertEquals('http://someurl.dev/step/scenarioOne/firstStepName', $response->getTargetUrl());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function shouldRedirectToMyRouteDisplayActionWhenStarting()
|
||||||
|
{
|
||||||
|
$router = $this->getRouter(
|
||||||
|
'my_route',
|
||||||
|
array(
|
||||||
|
'stepName' => 'firstStepName'
|
||||||
|
),
|
||||||
|
'http://someurl.dev/my-super-route/firstStepName'
|
||||||
|
);
|
||||||
|
|
||||||
|
$process = $this->getProcess();
|
||||||
|
$process->expects($this->any())
|
||||||
|
->method('getDisplayRoute')
|
||||||
|
->will($this->returnValue('my_route'));
|
||||||
|
|
||||||
|
$processBuilder = $this->getProcessBuilder($process);
|
||||||
|
|
||||||
|
$processContext = $this->getProcessContext();
|
||||||
|
$processContext->expects($this->any())
|
||||||
|
->method('isValid')
|
||||||
|
->will($this->returnValue(true));
|
||||||
|
|
||||||
|
$this->coordinator = $this->createCoordinator($router, $processBuilder, $processContext);
|
||||||
|
$this->coordinator->registerScenario('scenarioOne', $this->getMock('Sylius\Bundle\FlowBundle\Process\Scenario\ProcessScenarioInterface'));
|
||||||
|
$response = $this->coordinator->start('scenarioOne');
|
||||||
|
|
||||||
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
|
||||||
|
$this->assertEquals('http://someurl.dev/my-super-route/firstStepName', $response->getTargetUrl());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
* @expectedException InvalidArgumentException
|
||||||
|
* @expectedExceptionMessage Process scenario with alias "scenarioOne" is not registered
|
||||||
|
*/
|
||||||
|
public function shouldNotStartWhenScenarioIsNotRegistered()
|
||||||
|
{
|
||||||
|
$this->coordinator->start('scenarioOne');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
* @expectedException Symfony\Component\HttpKernel\Exception\NotFoundHttpException
|
||||||
|
*/
|
||||||
|
public function shouldNotStartWhenProcessIsNotValid()
|
||||||
|
{
|
||||||
|
$router = $this->getRouter();
|
||||||
|
|
||||||
|
$processBuilder = $this->getProcessBuilder($this->getProcess());
|
||||||
|
|
||||||
|
$processContext = $this->getProcessContext();
|
||||||
|
$processContext->expects($this->any())
|
||||||
|
->method('isValid')
|
||||||
|
->will($this->returnValue(false));
|
||||||
|
|
||||||
|
$this->coordinator = $this->createCoordinator($router, $processBuilder, $processContext);
|
||||||
|
$this->coordinator->registerScenario('scenarioOne', $this->getMock('Sylius\Bundle\FlowBundle\Process\Scenario\ProcessScenarioInterface'));
|
||||||
|
$this->coordinator->start('scenarioOne');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function shouldShowDisplayAction()
|
||||||
|
{
|
||||||
|
$this->coordinator->registerScenario('scenarioOne', $this->getMock('Sylius\Bundle\FlowBundle\Process\Scenario\ProcessScenarioInterface'));
|
||||||
|
|
||||||
|
$this->assertEquals('displayActionResponse', $this->coordinator->display('scenarioOne', 'someStepName'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
* @expectedException Symfony\Component\HttpKernel\Exception\NotFoundHttpException
|
||||||
|
*/
|
||||||
|
public function shouldNotShowDisplayActionWhenProcessIsNotValid()
|
||||||
|
{
|
||||||
|
$router = $this->getRouter();
|
||||||
|
|
||||||
|
$processBuilder = $this->getProcessBuilder($this->getProcess());
|
||||||
|
|
||||||
|
$processContext = $this->getProcessContext();
|
||||||
|
$processContext->expects($this->any())
|
||||||
|
->method('isValid')
|
||||||
|
->will($this->returnValue(false));
|
||||||
|
|
||||||
|
$this->coordinator = $this->createCoordinator($router, $processBuilder, $processContext);
|
||||||
|
$this->coordinator->registerScenario('scenarioOne', $this->getMock('Sylius\Bundle\FlowBundle\Process\Scenario\ProcessScenarioInterface'));
|
||||||
|
$this->coordinator->display('scenarioOne', 'someStepName');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
* @expectedException InvalidArgumentException
|
||||||
|
* @expectedExceptionMessage Process scenario with alias "scenarioOne" is not registered
|
||||||
|
*/
|
||||||
|
public function shouldNotShowDisplayActionWhenScenarioIsNotRegistered()
|
||||||
|
{
|
||||||
|
$this->coordinator->display('scenarioOne', 'someStepName');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
* @expectedException InvalidArgumentException
|
||||||
|
* @expectedExceptionMessage Process scenario with alias "scenarioOne" is not registered
|
||||||
|
*/
|
||||||
|
public function shouldNotShowForwardActionWhenScenarioIsNotRegistered()
|
||||||
|
{
|
||||||
|
$this->coordinator->forward('scenarioOne', 'someStepName');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
* @expectedException Symfony\Component\HttpKernel\Exception\NotFoundHttpException
|
||||||
|
*/
|
||||||
|
public function shouldNotShowForwardWhenProcessIsNotValid()
|
||||||
|
{
|
||||||
|
$router = $this->getRouter();
|
||||||
|
|
||||||
|
$processBuilder = $this->getProcessBuilder($this->getProcess());
|
||||||
|
|
||||||
|
$processContext = $this->getProcessContext();
|
||||||
|
$processContext->expects($this->any())
|
||||||
|
->method('isValid')
|
||||||
|
->will($this->returnValue(false));
|
||||||
|
|
||||||
|
$this->coordinator = $this->createCoordinator($router, $processBuilder, $processContext);
|
||||||
|
$this->coordinator->registerScenario('scenarioOne', $this->getMock('Sylius\Bundle\FlowBundle\Process\Scenario\ProcessScenarioInterface'));
|
||||||
|
$this->coordinator->forward('scenarioOne', 'someStepName');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
* @covers Sylius\Bundle\FlowBundle\Process\Coordinator\Coordinator::forward
|
||||||
|
*/
|
||||||
|
public function shouldShowForwardActionWhenStepIsNotCompleted()
|
||||||
|
{
|
||||||
|
$router = $this->getRouter();
|
||||||
|
|
||||||
|
$processBuilder = $this->getProcessBuilder($this->getProcess());
|
||||||
|
|
||||||
|
$processContext = $this->getProcessContext();
|
||||||
|
$processContext->expects($this->any())
|
||||||
|
->method('isValid')
|
||||||
|
->will($this->returnValue(true));
|
||||||
|
$processContext->expects($this->any())
|
||||||
|
->method('isCompleted')
|
||||||
|
->will($this->returnValue(false));
|
||||||
|
|
||||||
|
$this->coordinator = $this->createCoordinator($router, $processBuilder, $processContext);
|
||||||
|
$this->coordinator->registerScenario('scenarioOne', $this->getMock('Sylius\Bundle\FlowBundle\Process\Scenario\ProcessScenarioInterface'));
|
||||||
|
|
||||||
|
$this->assertEquals('forwardActionResponse', $this->coordinator->forward('scenarioOne', 'someStepName'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
* @expectedException InvalidArgumentException
|
||||||
|
* @expectedExceptionMessage Process scenario with alias "scenarioOne" is already registered
|
||||||
|
*/
|
||||||
|
public function shouldNotRegisterScenarioAgain()
|
||||||
|
{
|
||||||
|
$this->coordinator->registerScenario('scenarioOne', $this->getMock('Sylius\Bundle\FlowBundle\Process\Scenario\ProcessScenarioInterface'));
|
||||||
|
$this->coordinator->registerScenario('scenarioOne', $this->getMock('Sylius\Bundle\FlowBundle\Process\Scenario\ProcessScenarioInterface'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
* @covers Sylius\Bundle\FlowBundle\Process\Coordinator\Coordinator::forward
|
||||||
|
*/
|
||||||
|
public function shouldRedirectToNextStepDisplayActionWhenStepIsCompleted()
|
||||||
|
{
|
||||||
|
$router = $this->getRouter(
|
||||||
|
'sylius_flow_display',
|
||||||
|
array(
|
||||||
|
'scenarioAlias' => 'scenarioOne',
|
||||||
|
'stepName' => 'nextStepName'
|
||||||
|
),
|
||||||
|
'http://someurl.dev/step/scenarioOne/nextStepName'
|
||||||
|
);
|
||||||
|
|
||||||
|
$processBuilder = $this->getProcessBuilder($this->getProcess());
|
||||||
|
|
||||||
|
$processContext = $this->getProcessContext();
|
||||||
|
$processContext->expects($this->any())
|
||||||
|
->method('isValid')
|
||||||
|
->will($this->returnValue(true));
|
||||||
|
$processContext->expects($this->any())
|
||||||
|
->method('getNextStep')
|
||||||
|
->will($this->returnValue(
|
||||||
|
$this->getStep('nextStepName')
|
||||||
|
));
|
||||||
|
$processContext->expects($this->any())
|
||||||
|
->method('isCompleted')
|
||||||
|
->will($this->returnValue(true));
|
||||||
|
|
||||||
|
$this->coordinator = $this->createCoordinator($router, $processBuilder, $processContext);
|
||||||
|
$this->coordinator->registerScenario('scenarioOne', $this->getMock('Sylius\Bundle\FlowBundle\Process\Scenario\ProcessScenarioInterface'));
|
||||||
|
|
||||||
|
$response = $this->coordinator->forward('scenarioOne', 'someStepName');
|
||||||
|
|
||||||
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
|
||||||
|
$this->assertEquals('http://someurl.dev/step/scenarioOne/nextStepName', $response->getTargetUrl());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
* @covers Sylius\Bundle\FlowBundle\Process\Coordinator\Coordinator::forward
|
||||||
|
*/
|
||||||
|
public function shouldDoProcessRedirectWhenLastStepIsCompleted()
|
||||||
|
{
|
||||||
|
|
||||||
|
$router = $this->getRouter(
|
||||||
|
'http://localhost/processRedirect',
|
||||||
|
array(),
|
||||||
|
'http://localhost/processRedirect'
|
||||||
|
);
|
||||||
|
|
||||||
|
$processBuilder = $this->getProcessBuilder($this->getProcess());
|
||||||
|
|
||||||
|
$processContext = $this->getProcessContext();
|
||||||
|
$processContext->expects($this->any())
|
||||||
|
->method('isValid')
|
||||||
|
->will($this->returnValue(true));
|
||||||
|
$processContext->expects($this->any())
|
||||||
|
->method('isCompleted')
|
||||||
|
->will($this->returnValue(true));
|
||||||
|
$processContext->expects($this->once())
|
||||||
|
->method('close');
|
||||||
|
$processContext->expects($this->once())
|
||||||
|
->method('isLastStep')
|
||||||
|
->will($this->returnValue(true));
|
||||||
|
|
||||||
|
$this->coordinator = $this->createCoordinator($router, $processBuilder, $processContext);
|
||||||
|
$this->coordinator->registerScenario('scenarioOne', $this->getMock('Sylius\Bundle\FlowBundle\Process\Scenario\ProcessScenarioInterface'));
|
||||||
|
|
||||||
|
$response = $this->coordinator->forward('scenarioOne', 'someStepName');
|
||||||
|
|
||||||
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
|
||||||
|
$this->assertEquals('http://localhost/processRedirect', $response->getTargetUrl());
|
||||||
|
}
|
||||||
|
|
||||||
|
private function createCoordinator($router, $processBuilder, $processContext)
|
||||||
|
{
|
||||||
|
return new Coordinator(
|
||||||
|
$router,
|
||||||
|
$processBuilder,
|
||||||
|
$processContext
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getRouter($route = '', $secondParam = array(), $url = 'http://someurl.dev')
|
||||||
|
{
|
||||||
|
$router = $this->getMock('Symfony\Component\Routing\RouterInterface');
|
||||||
|
$router->expects($this->any())
|
||||||
|
->method('generate')
|
||||||
|
->with($this->equalTo($route), $this->equalTo($secondParam))
|
||||||
|
->will($this->returnValue($url));
|
||||||
|
|
||||||
|
return $router;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getProcessBuilder($process)
|
||||||
|
{
|
||||||
|
$builder = $this->getMock('Sylius\Bundle\FlowBundle\Process\Builder\ProcessBuilderInterface');
|
||||||
|
$builder->expects($this->any())
|
||||||
|
->method('build')
|
||||||
|
->will($this->returnValue(
|
||||||
|
$process
|
||||||
|
));
|
||||||
|
|
||||||
|
return $builder;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getProcessContext()
|
||||||
|
{
|
||||||
|
return $this->getMock('Sylius\Bundle\FlowBundle\Process\Context\ProcessContextInterface');
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getProcess()
|
||||||
|
{
|
||||||
|
$process = $this->getMock('Sylius\Bundle\FlowBundle\Process\ProcessInterface');
|
||||||
|
$process->expects($this->any())
|
||||||
|
->method('getFirstStep')
|
||||||
|
->will($this->returnValue(
|
||||||
|
$this->getStep('firstStepName')
|
||||||
|
));
|
||||||
|
$process->expects($this->any())
|
||||||
|
->method('getStepByName')
|
||||||
|
->with($this->equalTo('someStepName'))
|
||||||
|
->will($this->returnValue(
|
||||||
|
$this->getStep('someStepName')
|
||||||
|
));
|
||||||
|
$process->expects($this->any())
|
||||||
|
->method('setScenarioAlias')
|
||||||
|
->with($this->equalTo('scenarioOne'));
|
||||||
|
$process->expects($this->any())
|
||||||
|
->method('getScenarioAlias')
|
||||||
|
->will($this->returnValue('scenarioOne'));
|
||||||
|
$process->expects($this->any())
|
||||||
|
->method('getRedirect')
|
||||||
|
->will($this->returnValue('http://localhost/processRedirect'));
|
||||||
|
|
||||||
|
return $process;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getStep($name)
|
||||||
|
{
|
||||||
|
$step = $this->getMock('Sylius\Bundle\FlowBundle\Process\Step\StepInterface');
|
||||||
|
$step->expects($this->any())
|
||||||
|
->method('getName')
|
||||||
|
->will($this->returnValue($name));
|
||||||
|
$step->expects($this->any())
|
||||||
|
->method('displayAction')
|
||||||
|
->will($this->returnValue('displayActionResponse'));
|
||||||
|
$step->expects($this->any())
|
||||||
|
->method('forwardAction')
|
||||||
|
->will($this->returnValue('forwardActionResponse'));
|
||||||
|
|
||||||
|
return $step;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -19,6 +19,7 @@ use Sylius\Bundle\FlowBundle\Process\Step\Step;
|
||||||
* Process test.
|
* Process test.
|
||||||
*
|
*
|
||||||
* @author Paweł Jędrzejewski <pjedrzejewski@diweb.pl>
|
* @author Paweł Jędrzejewski <pjedrzejewski@diweb.pl>
|
||||||
|
* @author Leszek Prabucki <leszek.prabucki@gmail.com>
|
||||||
*/
|
*/
|
||||||
class ProcessTest extends \PHPUnit_Framework_TestCase
|
class ProcessTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
|
|
@ -43,6 +44,276 @@ class ProcessTest extends \PHPUnit_Framework_TestCase
|
||||||
$this->assertSame($name, $process->getStepByIndex($i)->getName());
|
$this->assertSame($name, $process->getStepByIndex($i)->getName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function shouldKeepStepsInOrderAfterSetSteps()
|
||||||
|
{
|
||||||
|
$process = new Process();
|
||||||
|
|
||||||
|
$process->setSteps(array(
|
||||||
|
'foo' => new TestStep(),
|
||||||
|
'bar' => new TestStep(),
|
||||||
|
'foobar' => new TestStep()
|
||||||
|
));
|
||||||
|
|
||||||
|
$correctOrder = array('foo', 'bar', 'foobar');
|
||||||
|
|
||||||
|
foreach ($process->getOrderedSteps() as $i => $step) {
|
||||||
|
$this->assertSame($correctOrder[$i], $step->getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($correctOrder as $i => $name) {
|
||||||
|
$this->assertSame($name, $process->getStepByIndex($i)->getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function shouldAddStep()
|
||||||
|
{
|
||||||
|
$process = new Process();
|
||||||
|
$step1 = new TestStep();
|
||||||
|
$process->addStep('foo', $step1);
|
||||||
|
|
||||||
|
$steps = $process->getSteps();
|
||||||
|
$this->assertSame($step1, $steps['foo']);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
* @covers Sylius\Bundle\FlowBundle\Process\Process::removeStep
|
||||||
|
*/
|
||||||
|
public function shouldRemoveStep()
|
||||||
|
{
|
||||||
|
$process = new Process();
|
||||||
|
$process->addStep('foo', new TestStep());
|
||||||
|
$process->removeStep('foo');
|
||||||
|
|
||||||
|
$this->assertCount(0, $process->getSteps());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
* @covers Sylius\Bundle\FlowBundle\Process\Process::removeStep
|
||||||
|
* @expectedException \InvalidArgumentException
|
||||||
|
*/
|
||||||
|
public function shouldNotRemoveStepWhenWasNotSet()
|
||||||
|
{
|
||||||
|
$process = new Process();
|
||||||
|
$process->removeStep('foo');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function shouldSetSteps()
|
||||||
|
{
|
||||||
|
$process = new Process();
|
||||||
|
$step1 = new TestStep();
|
||||||
|
$process->setSteps(array('foo' => $step1));
|
||||||
|
|
||||||
|
$steps = $process->getSteps();
|
||||||
|
$this->assertSame($step1, $steps['foo']);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
* @expectedException \InvalidArgumentException
|
||||||
|
*/
|
||||||
|
public function shouldNotAddStepWithThisSameNameAgain()
|
||||||
|
{
|
||||||
|
$process = new Process();
|
||||||
|
|
||||||
|
$process->addStep('foo', new TestStep());
|
||||||
|
$process->addStep('foo', new TestStep());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function shouldGetStepUsingIndexAfterSetSteps()
|
||||||
|
{
|
||||||
|
$process = new Process();
|
||||||
|
|
||||||
|
$step1 = new TestStep();
|
||||||
|
$step2 = new TestStep();
|
||||||
|
|
||||||
|
$process->setSteps(array(
|
||||||
|
'foo' => $step1,
|
||||||
|
'bar' => $step2,
|
||||||
|
));
|
||||||
|
|
||||||
|
$this->assertSame($step1, $process->getStepByIndex(0));
|
||||||
|
$this->assertSame($step2, $process->getStepByIndex(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function shouldGetStepUsingIndexAfterStepAddition()
|
||||||
|
{
|
||||||
|
$process = new Process();
|
||||||
|
|
||||||
|
$step1 = new TestStep();
|
||||||
|
$step2 = new TestStep();
|
||||||
|
|
||||||
|
$process->addStep('foo', $step1);
|
||||||
|
$process->addStep('bar', $step2);
|
||||||
|
|
||||||
|
$this->assertSame($step1, $process->getStepByIndex(0));
|
||||||
|
$this->assertSame($step2, $process->getStepByIndex(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
* @expectedException \InvalidArgumentException
|
||||||
|
*/
|
||||||
|
public function shouldNotGetStepUsingIndexWhenWasNotSet()
|
||||||
|
{
|
||||||
|
$process = new Process();
|
||||||
|
$process->getStepByIndex(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function shouldGetStepUsingNameAfterSetSteps()
|
||||||
|
{
|
||||||
|
$process = new Process();
|
||||||
|
|
||||||
|
$step1 = new TestStep();
|
||||||
|
$step2 = new TestStep();
|
||||||
|
|
||||||
|
$process->setSteps(array(
|
||||||
|
'foo' => $step1,
|
||||||
|
'bar' => $step2,
|
||||||
|
));
|
||||||
|
|
||||||
|
$this->assertSame($step1, $process->getStepByName('foo'));
|
||||||
|
$this->assertSame($step2, $process->getStepByName('bar'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function shouldGetStepUsingNameAfterStepAddition()
|
||||||
|
{
|
||||||
|
$process = new Process();
|
||||||
|
|
||||||
|
$step1 = new TestStep();
|
||||||
|
$step2 = new TestStep();
|
||||||
|
|
||||||
|
$process->addStep('foo', $step1);
|
||||||
|
$process->addStep('bar', $step2);
|
||||||
|
|
||||||
|
$this->assertSame($step1, $process->getStepByName('foo'));
|
||||||
|
$this->assertSame($step2, $process->getStepByName('bar'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
* @expectedException \InvalidArgumentException
|
||||||
|
*/
|
||||||
|
public function shouldNotGetStepUsingNameWhenWasNotSet()
|
||||||
|
{
|
||||||
|
$process = new Process();
|
||||||
|
$process->getStepByName('foo');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
* @covers Sylius\Bundle\FlowBundle\Process\Process::getLastStep
|
||||||
|
*/
|
||||||
|
public function shouldGetLastStep()
|
||||||
|
{
|
||||||
|
$process = new Process();
|
||||||
|
|
||||||
|
$step1 = new TestStep();
|
||||||
|
$step2 = new TestStep();
|
||||||
|
|
||||||
|
$process->addStep('foo', $step1);
|
||||||
|
$process->addStep('bar', $step2);
|
||||||
|
|
||||||
|
$this->assertSame($step2, $process->getLastStep());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
* @covers Sylius\Bundle\FlowBundle\Process\Process::getFirstStep
|
||||||
|
*/
|
||||||
|
public function shouldGetFirstStep()
|
||||||
|
{
|
||||||
|
$process = new Process();
|
||||||
|
|
||||||
|
$step1 = new TestStep();
|
||||||
|
$step2 = new TestStep();
|
||||||
|
|
||||||
|
$process->addStep('foo', $step1);
|
||||||
|
$process->addStep('bar', $step2);
|
||||||
|
|
||||||
|
$this->assertSame($step1, $process->getFirstStep());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function shouldSetNeededDataUsingSetter()
|
||||||
|
{
|
||||||
|
$process = new Process();
|
||||||
|
$process->setScenarioAlias('alias');
|
||||||
|
$process->setDisplayRoute('displayRoute');
|
||||||
|
$process->setForwardRoute('forwardRoute');
|
||||||
|
$process->setRedirect('http://somepage');
|
||||||
|
$process->setValidator(function () {
|
||||||
|
return 'Awesome validator';
|
||||||
|
});
|
||||||
|
|
||||||
|
$validator = $process->getValidator();
|
||||||
|
$this->assertSame('alias', $process->getScenarioAlias());
|
||||||
|
$this->assertSame('displayRoute', $process->getDisplayRoute());
|
||||||
|
$this->assertSame('forwardRoute', $process->getForwardRoute());
|
||||||
|
$this->assertSame('http://somepage', $process->getRedirect());
|
||||||
|
$this->assertSame('Awesome validator', $validator());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
* @covers Sylius\Bundle\FlowBundle\Process\Process::countSteps
|
||||||
|
* @dataProvider countStepsDataProvider
|
||||||
|
*/
|
||||||
|
public function shouldCountSteps($steps, $expectedCount)
|
||||||
|
{
|
||||||
|
$process = new Process();
|
||||||
|
|
||||||
|
$process->setSteps($steps);
|
||||||
|
|
||||||
|
$this->assertEquals($process->countSteps(), $expectedCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function countStepsDataProvider()
|
||||||
|
{
|
||||||
|
return array(
|
||||||
|
array(
|
||||||
|
array(new TestStep(), new TestStep()),
|
||||||
|
2
|
||||||
|
),
|
||||||
|
array(
|
||||||
|
array('abc' => new TestStep(), 'abc' => new TestStep()),
|
||||||
|
1
|
||||||
|
),
|
||||||
|
array(
|
||||||
|
array('abc' => new TestStep()),
|
||||||
|
1
|
||||||
|
),
|
||||||
|
array(
|
||||||
|
array('abc' => new TestStep(), 'zzz' => new TestStep(), 'yyy' => new TestStep()),
|
||||||
|
3
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class TestStep extends Step
|
class TestStep extends Step
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,52 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Sylius package.
|
||||||
|
*
|
||||||
|
* (c) Paweł Jędrzejewski
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Sylius\Bundle\FlowBundle\Tests\Process\Step;
|
||||||
|
|
||||||
|
use Sylius\Bundle\FlowBundle\Process\Context\ProcessContextInterface;
|
||||||
|
use Sylius\Bundle\FlowBundle\Process\Step\ContainerAwareStep;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ContainerAwareStepTest test.
|
||||||
|
*
|
||||||
|
* @author Leszek Prabucki <leszek.prabucki@gmail.com>
|
||||||
|
*/
|
||||||
|
class ContainerAwareStepTest extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
* @covers Sylius\Bundle\FlowBundle\Process\Step\ContainerAwareStep::setContainer
|
||||||
|
*/
|
||||||
|
public function shouldInjectContainerBySetter()
|
||||||
|
{
|
||||||
|
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
|
||||||
|
$step = new TestContainerAwareStep();
|
||||||
|
$step->setContainer($container);
|
||||||
|
|
||||||
|
$this->assertSame($step->getContainer(), $container);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class TestContainerAwareStep extends ContainerAwareStep
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Just for check if container setter works
|
||||||
|
*/
|
||||||
|
public function getContainer()
|
||||||
|
{
|
||||||
|
return $this->container;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function displayAction(ProcessContextInterface $context)
|
||||||
|
{
|
||||||
|
// pufff.
|
||||||
|
}
|
||||||
|
}
|
||||||
63
src/Sylius/Bundle/FlowBundle/Tests/Process/Step/StepTest.php
Normal file
63
src/Sylius/Bundle/FlowBundle/Tests/Process/Step/StepTest.php
Normal file
|
|
@ -0,0 +1,63 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Sylius package.
|
||||||
|
*
|
||||||
|
* (c) Paweł Jędrzejewski
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Sylius\Bundle\FlowBundle\Tests\Process\Step;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Step test.
|
||||||
|
*
|
||||||
|
* @author Leszek Prabucki <leszek.prabucki@gmail.com>
|
||||||
|
*/
|
||||||
|
class StepTest extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
* @covers Sylius\Bundle\FlowBundle\Process\Step\Step::isActive
|
||||||
|
*/
|
||||||
|
public function shouldByActiveByDefault()
|
||||||
|
{
|
||||||
|
$step = $this->getStep();
|
||||||
|
|
||||||
|
$this->assertTrue($step->isActive());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
* @covers Sylius\Bundle\FlowBundle\Process\Step\Step::forwardAction
|
||||||
|
*/
|
||||||
|
public function shouldCompleteProcessByDefault()
|
||||||
|
{
|
||||||
|
$processContext = $this->getMock('Sylius\Bundle\FlowBundle\Process\Context\ProcessContextInterface');
|
||||||
|
$processContext->expects($this->once())
|
||||||
|
->method('complete');
|
||||||
|
|
||||||
|
$step = $this->getStep();
|
||||||
|
$step->forwardAction($processContext);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
* @covers Sylius\Bundle\FlowBundle\Process\Step\Step::setName
|
||||||
|
* @covers Sylius\Bundle\FlowBundle\Process\Step\Step::getName
|
||||||
|
*/
|
||||||
|
public function shouldSetName()
|
||||||
|
{
|
||||||
|
$step = $this->getStep();
|
||||||
|
$step->setName('stepName');
|
||||||
|
|
||||||
|
$this->assertSame('stepName', $step->getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getStep()
|
||||||
|
{
|
||||||
|
return $this->getMockForAbstractClass('Sylius\Bundle\FlowBundle\Process\Step\Step');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -12,6 +12,7 @@
|
||||||
<exclude>
|
<exclude>
|
||||||
<directory>./Resources</directory>
|
<directory>./Resources</directory>
|
||||||
<directory>./Tests</directory>
|
<directory>./Tests</directory>
|
||||||
|
<directory>./vendor</directory>
|
||||||
</exclude>
|
</exclude>
|
||||||
</whitelist>
|
</whitelist>
|
||||||
</filter>
|
</filter>
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue