feat : Ajout de tests unitaires
This commit is contained in:
102
tests/BovinIdentificationProviderTest.php
Normal file
102
tests/BovinIdentificationProviderTest.php
Normal file
@@ -0,0 +1,102 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Tests\State;
|
||||||
|
|
||||||
|
use ApiPlatform\Metadata\Operation;
|
||||||
|
use App\State\BovinIdentificationProvider;
|
||||||
|
use DateTimeImmutable;
|
||||||
|
use Malio\EdnotifBundle\Bovin\Api\BovinApiInterface;
|
||||||
|
use Malio\EdnotifBundle\Bovin\Dto\AnimalFileDto;
|
||||||
|
use Malio\EdnotifBundle\Bovin\Dto\BovinIdentificationDto;
|
||||||
|
use Malio\EdnotifBundle\Bovin\Dto\BovinRef;
|
||||||
|
use Malio\EdnotifBundle\Bovin\Dto\DateValueDto;
|
||||||
|
use Malio\EdnotifBundle\Bovin\Dto\ExploitationRef;
|
||||||
|
use Malio\EdnotifBundle\Bovin\Dto\MovementDto;
|
||||||
|
use Malio\EdnotifBundle\Bovin\Dto\ParentInfoDto;
|
||||||
|
use Malio\EdnotifBundle\Bovin\Dto\PresencePeriodDto;
|
||||||
|
use Malio\EdnotifBundle\Shared\Dto\StandardResponseDto;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
final class BovinIdentificationProviderTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testReturnsNullWhenNumeroNationalMissing(): void
|
||||||
|
{
|
||||||
|
$api = $this->createMock(BovinApiInterface::class);
|
||||||
|
$api->expects(self::never())->method('getAnimalFile');
|
||||||
|
|
||||||
|
$provider = new BovinIdentificationProvider($api);
|
||||||
|
|
||||||
|
$result = $provider->provide($this->createStub(Operation::class), []);
|
||||||
|
|
||||||
|
self::assertNull($result);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testMapsIdentificationAndPresencePeriods(): void
|
||||||
|
{
|
||||||
|
$api = $this->createMock(BovinApiInterface::class);
|
||||||
|
|
||||||
|
$identification = new BovinIdentificationDto(
|
||||||
|
bovin: new BovinRef('FR', 'IGNORED'),
|
||||||
|
sex: 'F',
|
||||||
|
breedType: 'LIM',
|
||||||
|
birthDate: new DateValueDto(new DateTimeImmutable('2024-01-02'), 'Y'),
|
||||||
|
workNumber: 'W123',
|
||||||
|
isFilie: true,
|
||||||
|
motherCarrier: new ParentInfoDto(new BovinRef('FR', 'MOM1'), 'CHA'),
|
||||||
|
fatherIpg: new ParentInfoDto(new BovinRef('FR', 'DAD1'), 'BBB'),
|
||||||
|
birthExploitation: new ExploitationRef('FR', 'EXP1'),
|
||||||
|
);
|
||||||
|
|
||||||
|
$presencePeriods = [
|
||||||
|
new PresencePeriodDto(
|
||||||
|
new MovementDto(new DateTimeImmutable('2024-03-01'), 'ENTRY', null),
|
||||||
|
new MovementDto(new DateTimeImmutable('2024-03-10'), 'EXIT', null),
|
||||||
|
),
|
||||||
|
];
|
||||||
|
|
||||||
|
$animalFile = new AnimalFileDto(
|
||||||
|
new StandardResponseDto(true, null),
|
||||||
|
$identification,
|
||||||
|
$presencePeriods,
|
||||||
|
null
|
||||||
|
);
|
||||||
|
|
||||||
|
$api->expects(self::once())
|
||||||
|
->method('getAnimalFile')
|
||||||
|
->with('FR123', 'FR')
|
||||||
|
->willReturn($animalFile)
|
||||||
|
;
|
||||||
|
|
||||||
|
$provider = new BovinIdentificationProvider($api);
|
||||||
|
|
||||||
|
$result = $provider->provide(
|
||||||
|
$this->createStub(Operation::class),
|
||||||
|
['numeroNational' => 'FR123']
|
||||||
|
);
|
||||||
|
|
||||||
|
self::assertNotNull($result);
|
||||||
|
self::assertSame('FR123', $result->numeroNational);
|
||||||
|
self::assertSame('F', $result->sex);
|
||||||
|
self::assertSame('LIM', $result->breedType);
|
||||||
|
self::assertSame('W123', $result->workNumber);
|
||||||
|
self::assertSame('2024-01-02', $result->birthDate);
|
||||||
|
self::assertSame('Y', $result->birthDateCompletenessFlag);
|
||||||
|
self::assertTrue($result->isFilie);
|
||||||
|
self::assertSame('MOM1', $result->motherNationalNumber);
|
||||||
|
self::assertSame('CHA', $result->motherBreedType);
|
||||||
|
self::assertSame('DAD1', $result->fatherNationalNumber);
|
||||||
|
self::assertSame('BBB', $result->fatherBreedType);
|
||||||
|
self::assertSame('EXP1', $result->birthExploitationNumber);
|
||||||
|
|
||||||
|
self::assertCount(1, $result->presencePeriods);
|
||||||
|
self::assertSame('2024-03-01', $result->presencePeriods[0]->entryDate);
|
||||||
|
self::assertSame('ENTRY', $result->presencePeriods[0]->entryCause);
|
||||||
|
self::assertSame('2024-03-10', $result->presencePeriods[0]->exitDate);
|
||||||
|
self::assertSame('EXIT', $result->presencePeriods[0]->exitCause);
|
||||||
|
}
|
||||||
|
}
|
||||||
48
tests/State/MeProviderTest.php
Normal file
48
tests/State/MeProviderTest.php
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Tests\State;
|
||||||
|
|
||||||
|
use ApiPlatform\Metadata\Operation;
|
||||||
|
use App\Entity\User;
|
||||||
|
use App\State\MeProvider;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use Symfony\Bundle\SecurityBundle\Security;
|
||||||
|
use Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
final class MeProviderTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testProvideReturnUser(): void
|
||||||
|
{
|
||||||
|
$user = new User();
|
||||||
|
|
||||||
|
$security = $this->createStub(Security::class);
|
||||||
|
$security->method('getUser')->willReturn($user);
|
||||||
|
|
||||||
|
$provider = new MeProvider($security);
|
||||||
|
|
||||||
|
$result = $provider->provide($this->createStub(Operation::class));
|
||||||
|
|
||||||
|
self::assertSame($user, $result);
|
||||||
|
self::assertInstanceOf(User::class, $result);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testProvideThrowAccessDeniedException(): void
|
||||||
|
{
|
||||||
|
$user = null;
|
||||||
|
|
||||||
|
$security = $this->createStub(Security::class);
|
||||||
|
$security->method('getUser')->willReturn($user);
|
||||||
|
|
||||||
|
$provider = new MeProvider($security);
|
||||||
|
|
||||||
|
$this->expectException(AccessDeniedException::class);
|
||||||
|
$this->expectExceptionMessage('User not authenticated.');
|
||||||
|
|
||||||
|
$provider->provide($this->createStub(Operation::class));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user