From 41ffc470a0cf9caa167d832c0a69a316ed441c94 Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Fri, 30 Aug 2024 11:30:06 +0200 Subject: [PATCH] Adjust test to check creation of onHold if user is allowed to apply a transition --- .../WorkflowOnHoldControllerTest.php | 87 +++++++++++++------ 1 file changed, 62 insertions(+), 25 deletions(-) diff --git a/src/Bundle/ChillMainBundle/Tests/Controller/WorkflowOnHoldControllerTest.php b/src/Bundle/ChillMainBundle/Tests/Controller/WorkflowOnHoldControllerTest.php index db9e0cc64..785d5d33c 100644 --- a/src/Bundle/ChillMainBundle/Tests/Controller/WorkflowOnHoldControllerTest.php +++ b/src/Bundle/ChillMainBundle/Tests/Controller/WorkflowOnHoldControllerTest.php @@ -17,52 +17,89 @@ use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Symfony\Component\Security\Core\Security; use Symfony\Component\Workflow\Registry; +use Symfony\Component\Workflow\Transition; use Symfony\Component\Workflow\Workflow; class WorkflowOnHoldControllerTest extends KernelTestCase { - public function testPutOnHoldSuccess() + private $entityManager; + private $security; + private $registry; + private $entityWorkflowStepHoldRepository; + private $entityWorkflow; + private $workflow; + private $currentStep; + private $currentUser; + + protected function setUp(): void { self::bootKernel(); // Mocks - $entityManager = $this->createMock(EntityManagerInterface::class); - $security = $this->createMock(Security::class); - $registry = $this->createMock(Registry::class); - $entityWorkflowStepHoldRepository = $this->createMock(EntityWorkflowStepHoldRepository::class); + $this->entityManager = $this->createMock(EntityManagerInterface::class); + $this->security = $this->createMock(Security::class); + $this->registry = $this->createMock(Registry::class); + $this->entityWorkflowStepHoldRepository = $this->createMock(EntityWorkflowStepHoldRepository::class); - $entityWorkflow = $this->createMock(EntityWorkflow::class); - $workflow = $this->createMock(Workflow::class); - $request = new Request(); - $currentStep = $this->createMock(EntityWorkflowStep::class); - $currentUser = $this->createMock(\Chill\MainBundle\Entity\User::class); + $this->entityWorkflow = $this->createMock(EntityWorkflow::class); + $this->workflow = $this->createMock(Workflow::class); + $this->currentStep = $this->createMock(EntityWorkflowStep::class); + $this->currentUser = $this->createMock(\Chill\MainBundle\Entity\User::class); // Define expected behaviors for the mocks - $entityWorkflow->method('getCurrentStep')->willReturn($currentStep); - $security->method('getUser')->willReturn($currentUser); - $entityWorkflow->method('getWorkflowName')->willReturn('workflow_name'); - $registry->method('get')->with($entityWorkflow, 'workflow_name')->willReturn($workflow); - $workflow->method('getEnabledTransitions')->with($entityWorkflow)->willReturn([]); - $entityWorkflow->method('getUsersInvolved')->willReturn([]); + $this->entityWorkflow->method('getCurrentStep')->willReturn($this->currentStep); + $this->security->method('getUser')->willReturn($this->currentUser); + $this->entityWorkflow->method('getWorkflowName')->willReturn('workflow_name'); + $this->registry->method('get')->with($this->entityWorkflow, 'workflow_name')->willReturn($this->workflow); + $this->workflow->method('getEnabledTransitions')->with($this->entityWorkflow)->willReturn([]); + $this->entityWorkflow->method('getUsersInvolved')->willReturn([]); + } - $entityManager->expects($this->once()) + public function testPutOnHoldPersistence(): void + { + // Adjust mock for getEnabledTransitions to return a non-empty array + $transition = $this->createMock(Transition::class); + $transition->method('getName')->willReturn('dummy_transition'); + + $this->workflow->method('getEnabledTransitions') + ->with($this->entityWorkflow) + ->willReturn([$transition]); + + $this->workflow->method('can') + ->with($this->entityWorkflow, 'dummy_transition') + ->willReturn(true); + + $this->entityManager->expects($this->once()) ->method('persist') ->with($this->isInstanceOf(EntityWorkflowStepHold::class)); - $entityManager->expects($this->once()) + $this->entityManager->expects($this->once()) ->method('flush'); $controller = new WorkflowOnHoldController( - $entityManager, - $security, - $registry, - $entityWorkflowStepHoldRepository + $this->entityManager, + $this->security, + $this->registry, + $this->entityWorkflowStepHoldRepository ); -/* // Smoke test - $response = $controller->putOnHold($entityWorkflow, $request); + $request = new Request(); + $controller->putOnHold($this->entityWorkflow, $request); + } + + public function testPutOnHoldSmokeTest(): void + { + $controller = new WorkflowOnHoldController( + $this->entityManager, + $this->security, + $this->registry, + $this->entityWorkflowStepHoldRepository + ); + + $request = new Request(); + $response = $controller->putOnHold($this->entityWorkflow, $request); $this->assertInstanceOf(Response::class, $response); - $this->assertEquals(302, $response->getStatusCode());*/ + $this->assertEquals(302, $response->getStatusCode()); } }