ldc-arcadia-genesis-php-client/tests/Unit/Lib/ApiDto/IncoShortTest.php

116 lines
4 KiB
PHP
Raw Permalink Normal View History

<?php
declare(strict_types=1);
namespace Tests\Unit\Lib\ApiDto;
use DateTimeImmutable;
use DateTimeInterface;
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto;
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\IncoShort;
use Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\IncoId;
/**
* Creates a complete IncoShort object graph.
*/
function createCompleteIncoShort(
string $id = '100',
string $name = 'Poulet rôti',
string $libelle = 'Poulet rôti 1kg',
string $description = 'Poulet rôti cuit prêt à consommer',
?DateTimeInterface $createdAt = null,
?DateTimeInterface $updatedAt = null,
): IncoShort {
return new IncoShort(
id: new IncoId($id),
name: $name,
libelle: $libelle,
description: $description,
created_at: $createdAt ?? new DateTimeImmutable('2024-01-01T10:00:00+00:00'),
updated_at: $updatedAt ?? new DateTimeImmutable('2024-06-01T12:00:00+00:00'),
);
}
describe('IncoShort DTO test', function (): void {
it('can instantiate IncoShort with complete object graph', function (): void {
// GIVEN
$createdAt = new DateTimeImmutable('2024-01-01T10:00:00+00:00');
$updatedAt = new DateTimeImmutable('2024-06-01T12:00:00+00:00');
// WHEN
$short = createCompleteIncoShort(
id: '100',
name: 'Poulet rôti',
libelle: 'Poulet rôti 1kg',
description: 'Poulet rôti cuit prêt à consommer',
createdAt: $createdAt,
updatedAt: $updatedAt,
);
// THEN
expect($short)->toBeInstanceOf(IncoShort::class)
->and($short)->toBeInstanceOf(Dto::class)
->and($short->id)->toBeInstanceOf(IncoId::class)
->and($short->id->value)->toBe('100')
->and($short->name)->toBe('Poulet rôti')
->and($short->libelle)->toBe('Poulet rôti 1kg')
->and($short->description)->toBe('Poulet rôti cuit prêt à consommer')
->and($short->created_at)->toBe($createdAt)
->and($short->updated_at)->toBe($updatedAt);
});
it('can jsonSerialize complete IncoShort object graph', function (): void {
// GIVEN
$createdAt = new DateTimeImmutable('2024-01-01T10:00:00+00:00');
$updatedAt = new DateTimeImmutable('2024-06-01T12:00:00+00:00');
$short = createCompleteIncoShort(createdAt: $createdAt, updatedAt: $updatedAt);
// WHEN
$serialized = $short->jsonSerialize();
// THEN
expect($serialized)->toBe([
'id' => '100',
'name' => 'Poulet rôti',
'libelle' => 'Poulet rôti 1kg',
'description' => 'Poulet rôti cuit prêt à consommer',
'created_at' => $createdAt,
'updated_at' => $updatedAt,
]);
$jsonEncoded = json_encode($short);
expect($jsonEncoded)->toBeString();
$decoded = json_decode((string) $jsonEncoded, true);
expect($decoded['id'])->toBe('100')
->and($decoded['name'])->toBe('Poulet rôti')
->and($decoded['libelle'])->toBe('Poulet rôti 1kg')
->and($decoded['description'])->toBe('Poulet rôti cuit prêt à consommer');
});
it('can instantiate IncoShort with null timestamps', function (): void {
// GIVEN / WHEN
$short = new IncoShort(
id: new IncoId('200'),
name: 'Dinde',
libelle: 'Escalope de dinde',
description: 'Escalope de dinde 500g',
created_at: null,
updated_at: null,
);
// THEN
expect($short->created_at)->toBeNull()
->and($short->updated_at)->toBeNull();
$serialized = $short->jsonSerialize();
expect($serialized)->toBe([
'id' => '200',
'name' => 'Dinde',
'libelle' => 'Escalope de dinde',
'description' => 'Escalope de dinde 500g',
'created_at' => null,
'updated_at' => null,
]);
});
});