Refactor WorkflowOnHoldController and improve tests

Refactored WorkflowOnHoldController to remove dependencies and improve redirect handling. Updated test cases to use PHPUnit instead of KernelTestCase and mock proper dependencies. Added relationship handling in EntityWorkflowStep to manage EntityWorkflowStepHold instances accurately.
This commit is contained in:
2024-09-06 13:51:07 +02:00
parent ad94310981
commit 49ad25b4c8
4 changed files with 99 additions and 77 deletions

View File

@@ -12,101 +12,105 @@ declare(strict_types=1);
namespace Chill\MainBundle\Tests\Controller;
use Chill\MainBundle\Controller\WorkflowOnHoldController;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Entity\Workflow\EntityWorkflow;
use Chill\MainBundle\Entity\Workflow\EntityWorkflowStep;
use Chill\MainBundle\Entity\Workflow\EntityWorkflowStepHold;
use Chill\MainBundle\Repository\Workflow\EntityWorkflowRepository;
use Chill\MainBundle\Repository\Workflow\EntityWorkflowStepHoldRepository;
use Chill\MainBundle\Workflow\EntityWorkflowMarkingStore;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Workflow\DefinitionBuilder;
use Symfony\Component\Workflow\Registry;
use Symfony\Component\Workflow\SupportStrategy\WorkflowSupportStrategyInterface;
use Symfony\Component\Workflow\Transition;
use Symfony\Component\Workflow\Workflow;
use Symfony\Component\Workflow\WorkflowInterface;
/**
* @internal
*
* @coversNothing
*/
class WorkflowOnHoldControllerTest extends KernelTestCase
class WorkflowOnHoldControllerTest extends TestCase
{
private $entityManager;
private $security;
private $registry;
private $entityWorkflowStepHoldRepository;
private $entityWorkflow;
private $workflow;
private $currentStep;
private $currentUser;
private $controller;
protected function setUp(): void
private function buildRegistry(): Registry
{
self::bootKernel();
$definitionBuilder = new DefinitionBuilder();
$definition = $definitionBuilder
->addPlaces(['initial', 'layout', 'sign'])
->addTransition(new Transition('to_layout', 'initial', 'layout'))
->addTransition(new Transition('to_sign', 'initial', 'sign'))
->build();
// Mocks
$this->entityManager = $this->createMock(EntityManagerInterface::class);
$this->security = $this->createMock(Security::class);
$this->registry = $this->createMock(Registry::class);
$this->entityWorkflowStepHoldRepository = $this->createMock(EntityWorkflowStepHoldRepository::class);
$this->entityWorkflowRepository = $this->createMock(EntityWorkflowRepository::class);
$workflow = new Workflow($definition, new EntityWorkflowMarkingStore(), name: 'dummy_workflow');
$registry = new Registry();
$registry->addWorkflow($workflow, new class () implements WorkflowSupportStrategyInterface {
public function supports(WorkflowInterface $workflow, object $subject): bool
{
return true;
}
});
$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
$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([]);
$this->controller = new WorkflowOnHoldController(
$this->entityManager,
$this->security,
$this->registry,
$this->entityWorkflowStepHoldRepository,
$this->entityWorkflowRepository
);
return $registry;
}
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');
$entityWorkflow = new EntityWorkflow();
$entityWorkflow->setWorkflowName('dummy_workflow');
$security = $this->createMock(Security::class);
$security->method('getUser')->willReturn($user = new User());
$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())
$entityManager = $this->createMock(EntityManagerInterface::class);
$entityManager->expects($this->once())
->method('persist')
->with($this->isInstanceOf(EntityWorkflowStepHold::class));
$this->entityManager->expects($this->once())
$entityManager->expects($this->once())
->method('flush');
$urlGenerator = $this->createMock(UrlGeneratorInterface::class);
$urlGenerator->method('generate')
->with('chill_main_workflow_show', ['id' => null])
->willReturn('/some/url');
$controller = new WorkflowOnHoldController($entityManager, $security, $this->buildRegistry(), $urlGenerator);
$request = new Request();
$this->controller->putOnHold($this->entityWorkflow, $request);
$response = $controller->putOnHold($entityWorkflow, $request);
self::assertEquals(302, $response->getStatusCode());
}
public function testPutOnHoldSmokeTest(): void
public function testRemoveOnHold(): void
{
$request = new Request();
$response = $this->controller->putOnHold($this->entityWorkflow, $request);
$user = new User();
$entityWorkflow = new EntityWorkflow();
$entityWorkflow->setWorkflowName('dummy_workflow');
$onHold = new EntityWorkflowStepHold($step = $entityWorkflow->getCurrentStep(), $user);
$this->assertInstanceOf(Response::class, $response);
$this->assertEquals(302, $response->getStatusCode());
$security = $this->createMock(Security::class);
$security->method('getUser')->willReturn($user);
$entityManager = $this->createMock(EntityManagerInterface::class);
$entityManager->expects($this->once())
->method('remove')
->with($onHold);
$entityManager->expects($this->once())
->method('flush');
$urlGenerator = $this->createMock(UrlGeneratorInterface::class);
$urlGenerator->method('generate')
->with('chill_main_workflow_show', ['id' => null])
->willReturn('/some/url');
$controller = new WorkflowOnHoldController($entityManager, $security, $this->buildRegistry(), $urlGenerator);
$response = $controller->removeOnHold($step);
self::assertEquals(302, $response->getStatusCode());
}
}