Skip to content

Commit

Permalink
- updated WorkflowstartListener test to check new behaviour of the Or…
Browse files Browse the repository at this point in the history
…derWorkflowStart
  • Loading branch information
24198 committed Jul 27, 2020
1 parent 87716bd commit 88337f5
Showing 1 changed file with 68 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Event\PostFlushEventArgs;

use Oro\Bundle\WorkflowBundle\Model\WorkflowStartArguments;
use PHPUnit\Framework\TestCase;

use Oro\Bundle\WorkflowBundle\Model\Workflow;
Expand Down Expand Up @@ -47,11 +48,12 @@ protected function setUp()
public function testEntityIsNotEligibleToProcess()
{
$entity = $this->createMock(OrderItem::class);
$this->runFirstSequenceOfWorkflowListenerTest($entity, static::never());
$this->setWorkflowManagerHasApplicableWorkflowsMockExpectation($entity, static::never(), false);
$this->runPostPersistWorkflowListenerTest($entity, static::never(), null, $this->workflowManagerMock);
/** @var PostFlushEventArgs|\PHPUnit\Framework\MockObject\MockObject $eventPostFlushArgs */
$eventPostFlushArgs = $this->createMock(PostFlushEventArgs::class);
$this->doctrineHelperMock->expects(static::never())
->method('getEntityManagerForClass');
$this->workflowManagerMock->expects(static::never())
->method('massStartWorkflow');

$this->orderWorkflowStartListener->postFlush($eventPostFlushArgs);
}
Expand All @@ -62,30 +64,44 @@ public function testEntityIsNotEligibleToProcess()
public function testEntityIsEligibleToProcess()
{
$entity = $this->createMock(Order::class);
$this->runFirstSequenceOfWorkflowListenerTest($entity, static::once(), 1);
/** @var PostFlushEventArgs|\PHPUnit\Framework\MockObject\MockObject $eventPostFlushArgs */
$eventPostFlushArgs = $this->createPostFlushEventArgsMock($entity);
$this->setWorkflowManagerHasApplicableWorkflowsMockExpectation($entity);

/** @var Workflow|\PHPUnit\Framework\MockObject\MockObject $workflowMock */
$workflowMock = $this->createMock(Workflow::class);
$workflowMock->expects(static::once())
->method('getName')
->willReturn(WorkflowNameProviderInterface::ORDER_WORKFLOW_1);
$this->setWorkflowManagerHasApplicableWorkflowsMockExpectation($entity, static::once());
$this->runPostPersistWorkflowListenerTest(
$entity,
static::once(),
[ WorkflowNameProviderInterface::ORDER_WORKFLOW_1 => $workflowMock ],
$this->workflowManagerMock
);

$this->workflowManagerMock->expects(static::once())
->method('getApplicableWorkflows')
->with($entity)
->willReturn([WorkflowNameProviderInterface::ORDER_WORKFLOW_1 => $workflowMock]);
/** @var PostFlushEventArgs|\PHPUnit\Framework\MockObject\MockObject $eventPostFlushArgs */
$eventPostFlushArgs = $this->createPostFlushEventArgsMock($entity);
$expectedSchedule = [
0 =>
new WorkflowStartArguments(
WorkflowNameProviderInterface::ORDER_WORKFLOW_1,
$entity,
[],
'pending'
)
];

$this->assertAttributeEquals(
$expectedSchedule,
'entitiesScheduledForWorkflowStart',
$this->orderWorkflowStartListener
);

$this->workflowManagerMock->expects(static::once())
->method('startWorkflow')
->with(
WorkflowNameProviderInterface::ORDER_WORKFLOW_1,
$entity,
OrderWorkflowStartListener::TRANSIT_TO_STEP
);
->method('massStartWorkflow')
->with($expectedSchedule);

$this->orderWorkflowStartListener->postFlush($eventPostFlushArgs);

$this->assertAttributeEmpty('entitiesScheduledForWorkflowStart', $this->orderWorkflowStartListener);
}

/**
Expand All @@ -94,29 +110,30 @@ public function testEntityIsEligibleToProcess()
public function testTwoApplicableWorkflowsOnOrderEntity()
{
$entity = $this->createMock(Order::class);
$this->runFirstSequenceOfWorkflowListenerTest($entity, static::once(), 1);
$workflowMock = $this->createMock(Workflow::class);

$this->setWorkflowManagerHasApplicableWorkflowsMockExpectation($entity, static::once());
$this->runPostPersistWorkflowListenerTest(
$entity,
static::once(),
[
WorkflowNameProviderInterface::ORDER_WORKFLOW_1 => $workflowMock,
WorkflowNameProviderInterface::ORDER_WORKFLOW_2 => $workflowMock
],
$this->workflowManagerMock
);
$this->assertAttributeEmpty('entitiesScheduledForWorkflowStart', $this->orderWorkflowStartListener);
/** @var PostFlushEventArgs|\PHPUnit\Framework\MockObject\MockObject $eventPostFlushArgs */
$eventPostFlushArgs = $this->createPostFlushEventArgsMock($entity);
$this->setWorkflowManagerHasApplicableWorkflowsMockExpectation($entity);

$workflowMock = $this->createMock(Workflow::class);
$this->workflowManagerMock->expects(static::once())
->method('getApplicableWorkflows')
->with($entity)
->willReturn(
[
WorkflowNameProviderInterface::ORDER_WORKFLOW_1 => $workflowMock,
WorkflowNameProviderInterface::ORDER_WORKFLOW_2 => $workflowMock
]
);

$workflowMock->expects(static::never())
->method('getName');

$this->workflowManagerMock->expects(static::never())
->method('startWorkflow');
->method('massStartWorkflow');

$this->orderWorkflowStartListener->postFlush($eventPostFlushArgs);
$this->assertAttributeEmpty('entitiesScheduledForWorkflowStart', $this->orderWorkflowStartListener);
}

/**
Expand All @@ -125,22 +142,23 @@ public function testTwoApplicableWorkflowsOnOrderEntity()
public function testZeroOrCustomApplicableWorkflowsOnOrderEntity()
{
$entity = $this->createMock(Order::class);
$this->runFirstSequenceOfWorkflowListenerTest($entity, static::once(), 1);
/** @var PostFlushEventArgs|\PHPUnit\Framework\MockObject\MockObject $eventPostFlushArgs */
$eventPostFlushArgs = $this->createPostFlushEventArgsMock($entity);
$this->setWorkflowManagerHasApplicableWorkflowsMockExpectation($entity);
$workflowMock = $this->createMock(Workflow::class);

$this->workflowManagerMock->expects(static::once())
->method('getApplicableWorkflows')
->with($entity)
->willReturn([]);
$this->setWorkflowManagerHasApplicableWorkflowsMockExpectation($entity, static::once());
$this->runPostPersistWorkflowListenerTest(
$entity,
static::once(),
[],
$this->workflowManagerMock
);

$workflowMock = $this->createMock(Workflow::class);
/** @var PostFlushEventArgs|\PHPUnit\Framework\MockObject\MockObject $eventPostFlushArgs */
$eventPostFlushArgs = $this->createPostFlushEventArgsMock($entity);
$workflowMock->expects(static::never())
->method('getName');

$this->workflowManagerMock->expects(static::never())
->method('startWorkflow');
->method('massStartWorkflow');

$this->orderWorkflowStartListener->postFlush($eventPostFlushArgs);
}
Expand All @@ -166,15 +184,16 @@ protected function createEventArgsMock($entity)
* @param $expected
* @param null $returnValue
*/
protected function runFirstSequenceOfWorkflowListenerTest($entity, $expected, $returnValue = null)
protected function runPostPersistWorkflowListenerTest(
$entity, $expected, $returnValue = null, $workflowManagerMock)
{
$eventArgs = $this->createEventArgsMock($entity);
$entity
$workflowManagerMock
->expects($expected)
->method('getId')
->method('getApplicableWorkflows')
->willReturn($returnValue);

$this->orderWorkflowStartListener->postPersist($eventArgs);
$this->orderWorkflowStartListener->postPersist($eventArgs);
}

/**
Expand All @@ -185,20 +204,6 @@ protected function runFirstSequenceOfWorkflowListenerTest($entity, $expected, $r
protected function createPostFlushEventArgsMock($entity)
{
$eventPostFlushArgs = $this->createMock(PostFlushEventArgs::class);
$entityManager = $this->createMock(EntityManagerInterface::class);
$this->doctrineHelperMock->expects(static::once())
->method('getEntityManagerForClass')
->willReturn($entityManager);

$entityRepository = $this->createMock(EntityRepository::class);
$entityManager->expects(static::once())
->method('getRepository')
->willReturn($entityRepository);

$entityRepository->expects(static::once())
->method('find')
->with(1)
->willReturn($entity);

return $eventPostFlushArgs;
}
Expand All @@ -207,11 +212,11 @@ protected function createPostFlushEventArgsMock($entity)
* {@inheritdoc}
* @param $entity|\PHPUnit\Framework\MockObject\MockObject
*/
protected function setWorkflowManagerHasApplicableWorkflowsMockExpectation($entity)
protected function setWorkflowManagerHasApplicableWorkflowsMockExpectation($entity, $expects, $returnValue = true)
{
$this->workflowManagerMock->expects(static::once())
$this->workflowManagerMock->expects($expects)
->method('hasApplicableWorkflows')
->with($entity)
->willReturn(true);
->willReturn($returnValue);
}
}

0 comments on commit 88337f5

Please sign in to comment.