Skip to content

Commit

Permalink
Updated calculation controllers unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentmuller committed Jul 10, 2024
1 parent 665500a commit 1c99c4b
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 13 deletions.
132 changes: 121 additions & 11 deletions tests/Controller/CalculationArchiveControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@

use App\Controller\CalculationArchiveController;
use App\Entity\CalculationState;
use App\Repository\CalculationStateRepository;
use Doctrine\ORM\Exception\ORMException;
use PHPUnit\Framework\Attributes\CoversClass;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

#[CoversClass(CalculationArchiveController::class)]
Expand All @@ -31,29 +34,136 @@ public static function getRoutes(): \Iterator
}

/**
* @throws \Doctrine\ORM\Exception\ORMException
* @throws ORMException
*/
protected function addEntities(): void
public function testArchiveEditableEmpty(): void
{
if (!$this->editState instanceof CalculationState) {
$this->editState = new CalculationState();
$this->editState->setCode('Editable')->setEditable(true);
$this->addEntity($this->editState);
$this->addNotEditState();
$repository = $this->getService(CalculationStateRepository::class);

/** @psalm-var CalculationState[] $entities */
$entities = $repository->getEditableQueryBuilder()
->getQuery()
->getResult();
if ([] !== $entities) {
foreach ($entities as $entity) {
$repository->remove($entity, false);
}
$repository->flush();
}

if (!$this->notEditSate instanceof CalculationState) {
$this->notEditSate = new CalculationState();
$this->notEditSate->setCode('NotEditable')->setEditable(false);
$this->addEntity($this->notEditSate);
$this->checkRoute(
url: '/admin/archive',
username: self::ROLE_ADMIN,
expected: Response::HTTP_FOUND,
method: Request::METHOD_POST,
);
}

/**
* @throws ORMException
*/
public function testArchiveNotEditableEmpty(): void
{
$this->addEditState();
$repository = $this->getService(CalculationStateRepository::class);

/** @psalm-var CalculationState[] $entities */
$entities = $repository->getNotEditableQueryBuilder()
->getQuery()
->getResult();
if ([] !== $entities) {
foreach ($entities as $entity) {
$repository->remove($entity, false);
}
$repository->flush();
}

$this->checkRoute(
url: '/admin/archive',
username: self::ROLE_ADMIN,
expected: Response::HTTP_FOUND,
method: Request::METHOD_POST,
);
}

/**
* @throws ORMException|\Exception
*/
public function testArchiveSuccess(): void
{
$this->addEntities();
$data = [
'form[date]' => '2024-06-01',
'form[simulate]' => '1',
'form[confirm]' => '1',
];

$repository = $this->getService(CalculationStateRepository::class);

/** @psalm-var ?CalculationState $editState */
$editState = $repository->getEditableQueryBuilder()
->setMaxResults(1)
->getQuery()
->getSingleResult();
$data['form[sources][0]'] = $editState?->getId();

/** @psalm-var ?CalculationState $noEditable */
$noEditable = $repository->getNotEditableQueryBuilder()
->setMaxResults(1)
->getQuery()
->getSingleResult();
$data['form[target]'] = $noEditable?->getId();

$this->checkForm(
uri: '/admin/archive',
id: 'archive.edit.submit',
data: $data,
followRedirect: false
);
}

/**
* @throws \Doctrine\ORM\Exception\ORMException
* @throws ORMException
*/
protected function addEntities(): void
{
$this->addEditState();
$this->addNotEditState();
}

/**
* @throws ORMException
*/
protected function deleteEntities(): void
{
$this->editState = $this->deleteEntity($this->editState);
$this->notEditSate = $this->deleteEntity($this->notEditSate);
}

/**
* @throws ORMException
*/
private function addEditState(): void
{
if ($this->editState instanceof CalculationState) {
return;
}
$this->editState = new CalculationState();
$this->editState->setCode('Editable')->setEditable(true);
$this->addEntity($this->editState);
}

/**
* @throws ORMException
*/
private function addNotEditState(): void
{
if ($this->notEditSate instanceof CalculationState) {
return;
}
$this->notEditSate = new CalculationState();
$this->notEditSate->setCode('NotEditable')->setEditable(false);
$this->addEntity($this->notEditSate);
}
}
3 changes: 1 addition & 2 deletions tests/Controller/CalculationUpdateControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ public static function getRoutes(): \Iterator
}

/**
* @throws ORMException
* @throws \Exception
* @throws ORMException|\Exception
*/
public function testUpdate(): void
{
Expand Down

0 comments on commit 1c99c4b

Please sign in to comment.