Files
Dto/tests/DtoTest.php
2025-12-12 10:46:50 +04:00

177 lines
5.6 KiB
PHP

<?php
declare(strict_types=1);
namespace Diffhead\PHP\Dto\Tests;
use Diffhead\PHP\Dto\Dto;
use Diffhead\PHP\Dto\Property;
use PHPUnit\Framework\TestCase;
class DtoTest extends TestCase
{
public function testProperlyInitializes(): void
{
$dto = DtoExample::fromArray([
'name' => 'John Doe',
'age' => 30,
]);
$this->assertSame('John Doe', $dto->name->value());
$this->assertTrue($dto->name->exists());
$this->assertSame(30, $dto->age->value());
$this->assertTrue($dto->age->exists());
$this->assertNull($dto->homeAddress->value());
$this->assertFalse($dto->homeAddress->exists());
}
public function testInitializationFromKebabCase(): void
{
$dto = DtoExample::fromArray([
'name' => 'Jane Doe',
'age' => 25,
'home-address' => '39 Vicar Lane, Sandilands, United Kingdom',
]);
$this->assertSame('Jane Doe', $dto->name->value());
$this->assertTrue($dto->name->exists());
$this->assertSame(25, $dto->age->value());
$this->assertTrue($dto->age->exists());
$this->assertSame('39 Vicar Lane, Sandilands, United Kingdom', $dto->homeAddress->value());
$this->assertTrue($dto->homeAddress->exists());
}
public function testInitializationFromPascalCase(): void
{
$dto = DtoExample::fromArray([
'name' => 'Alice',
'age' => 28,
'home_address' => '123 Main St, Anytown, USA',
]);
$this->assertSame('Alice', $dto->name->value());
$this->assertTrue($dto->name->exists());
$this->assertSame(28, $dto->age->value());
$this->assertTrue($dto->age->exists());
$this->assertSame('123 Main St, Anytown, USA', $dto->homeAddress->value());
$this->assertTrue($dto->homeAddress->exists());
}
public function testInitializationFromCamelCase(): void
{
$dto = DtoExample::fromArray([
'name' => 'Bob',
'age' => 35,
'homeAddress' => '456 Elm St, Othertown, USA',
]);
$this->assertSame('Bob', $dto->name->value());
$this->assertTrue($dto->name->exists());
$this->assertSame(35, $dto->age->value());
$this->assertTrue($dto->age->exists());
$this->assertSame('456 Elm St, Othertown, USA', $dto->homeAddress->value());
$this->assertTrue($dto->homeAddress->exists());
}
public function testGetStrictExistingPropertiesValues(): void
{
$dto = DtoExample::fromArray([
'name' => 'Charlie',
'age' => 40,
]);
$values = $dto->getValues(['name', 'age', 'home_address'], true);
$this->assertArrayHasKey('name', $values);
$this->assertSame('Charlie', $values['name']);
$this->assertArrayHasKey('age', $values);
$this->assertSame(40, $values['age']);
$this->assertArrayNotHasKey('home_address', $values);
}
public function testGetNonStrictExistingPropertiesValues(): void
{
$dto = DtoExample::fromArray([
'name' => 'Diana',
]);
$values = $dto->getValues(['name', 'age', 'home_address'], false);
$this->assertArrayHasKey('name', $values);
$this->assertSame('Diana', $values['name']);
$this->assertArrayHasKey('age', $values);
$this->assertNull($values['age']);
$this->assertArrayHasKey('home_address', $values);
$this->assertNull($values['home_address']);
}
public function testGetStrictNonExistingPropertiesValues(): void
{
$dto = DtoExample::fromArray([]);
$values = $dto->getValues(['name', 'age'], true);
$this->assertEmpty($values);
}
public function testGetNonStrictNonExistingPropertiesValues(): void
{
$dto = DtoExample::fromArray([]);
$values = $dto->getValues(['name', 'age'], false);
$this->assertArrayHasKey('name', $values);
$this->assertNull($values['name']);
$this->assertArrayHasKey('age', $values);
$this->assertNull($values['age']);
}
public function testGetKebabCasePropertiesValues(): void
{
$dto = DtoExample::fromArray([
'name' => 'Eve',
'age' => 29,
'home-address' => '789 Oak St, Sometown, USA',
]);
$values = $dto->getValues(['name', 'age', 'home-address'], false);
$this->assertArrayHasKey('name', $values);
$this->assertSame('Eve', $values['name']);
$this->assertArrayHasKey('age', $values);
$this->assertSame(29, $values['age']);
$this->assertArrayHasKey('home-address', $values);
$this->assertSame('789 Oak St, Sometown, USA', $values['home-address']);
}
public function testGetCamelCasePropertiesValues(): void
{
$dto = DtoExample::fromArray([
'name' => 'Frank',
'age' => 33,
'homeAddress' => '101 Pine St, Newcity, USA',
]);
$values = $dto->getValues(['name', 'age', 'homeAddress'], false);
$this->assertArrayHasKey('name', $values);
$this->assertSame('Frank', $values['name']);
$this->assertArrayHasKey('age', $values);
$this->assertSame(33, $values['age']);
$this->assertArrayHasKey('homeAddress', $values);
$this->assertSame('101 Pine St, Newcity, USA', $values['homeAddress']);
}
}
/**
* @property \Diffhead\PHP\Dto\Property<string> $name
* @property \Diffhead\PHP\Dto\Property<int> $age
* @property \Diffhead\PHP\Dto\Property<string> $homeAddress
*/
class DtoExample extends Dto
{
protected Property $name;
protected Property $age;
protected Property $homeAddress;
}