add tests
All checks were successful
/ ls (push) Successful in 20s

This commit is contained in:
lubiana 2024-06-02 11:20:20 +02:00
parent 13bfbbe528
commit eb40f1df49
Signed by: lubiana
SSH key fingerprint: SHA256:gkqM8DUX4Blf6P52fycW8ISTd+4eAHH+Uzu9iyc8hAM
10 changed files with 388 additions and 44 deletions

View file

@ -1,8 +0,0 @@
<?php declare(strict_types=1);
test(
'example',
function (): void {
expect(true)->toBeTrue();
},
);

View file

@ -1,5 +1,6 @@
<?php declare(strict_types=1);
use Mockery\MockInterface;
use Tests\TestCase;
/*
@ -24,7 +25,12 @@ uses(TestCase::class)->in('Feature', 'Unit');
| global functions to help you to reduce the number of lines of code in your test files.
|
*/
function something(): void
/**
* @template T of object
* @param class-string<T> $className
* @return MockInterface&T
*/
function createMock(string $className): MockInterface
{
// ..
return Mockery::mock($className);
}

View file

@ -2,9 +2,15 @@
namespace Tests;
use Lubiana\DoctrineUlid\Types\UlidType;
use Override;
use PHPUnit\Framework\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase
{
//
#[Override]
protected function setUp(): void
{
UlidType::register();
}
}

View file

@ -1,8 +0,0 @@
<?php declare(strict_types=1);
test(
'example',
function (): void {
expect(true)->toBeTrue();
},
);

View file

@ -0,0 +1,33 @@
<?php declare(strict_types=1);
use Doctrine\ORM\EntityManager;
use Lubiana\DoctrineUlid\IdGenerator\UlidGenerator;
use Symfony\Component\Uid\Factory\UlidFactory;
use Symfony\Component\Uid\Ulid;
test(
'UlidGenerator constructs without factory and generate Id',
function (): void {
$ulidGenerator = new UlidGenerator;
$em = createMock(EntityManager::class);
$id = $ulidGenerator->generateId($em, null);
expect($id)
->toBeInstanceOf(Ulid::class);
},
);
test(
'UlidGenerator constructs with factory and generate Id',
function (): void {
$factory = Mockery::mock(UlidFactory::class);
$ulid = new Ulid;
$factory->shouldReceive('create')
->andReturn($ulid)
->once();
$ulidGenerator = new UlidGenerator($factory);
$em = Mockery::mock(EntityManager::class);
$id = $ulidGenerator->generateId($em, null);
expect($id)
->toBe($ulid);
},
);

148
tests/Unit/UlidTypeTest.php Normal file
View file

@ -0,0 +1,148 @@
<?php declare(strict_types=1);
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\Platforms\SQLitePlatform;
use Doctrine\DBAL\Types\Exception\InvalidType;
use Doctrine\DBAL\Types\Exception\ValueNotConvertible;
use Doctrine\DBAL\Types\Type;
use Lubiana\DoctrineUlid\Types\UlidType;
use Symfony\Component\Uid\Ulid;
it(
'returns correct sql declaration on postgresql',
function (): void {
$ulidType = Type::getType(UlidType::NAME);
$platform = new PostgreSQLPlatform;
// If platform supports guid type, declaration is guid type.
$declaration = $ulidType->getSQLDeclaration([], $platform);
expect($declaration)
->toBe('UUID');
},
);
it(
'returns correct sql declaration on sqlite',
function (): void {
$ulidType = Type::getType(UlidType::NAME);
$platform = new SqlitePlatform;
// If platform supports guid type, declaration is guid type.
$declaration = $ulidType->getSQLDeclaration([], $platform);
expect($declaration)
->toBe('BLOB');
},
);
it(
'can convert string to a Ulid object',
function (): void {
$ulidType = Type::getType(UlidType::NAME);
$platform = new SQLitePlatform;
$stringUlid = '01FGRM9X8ACP5D6HZG9K31D6CZ';
$phpValue = $ulidType->convertToPHPValue($stringUlid, $platform);
expect(
$phpValue,
)->toBeInstanceOf(Ulid::class)->and((string) $phpValue)->toBe($stringUlid);
},
);
it(
'throws if value is not string during conversion to PHP Value',
function (): void {
$ulidType = Type::getType(UlidType::NAME);
$platform = new SQLitePlatform;
$nonStringValue = 123;
expect(
static fn(): mixed
=> $ulidType->convertToPHPValue($nonStringValue, $platform),
)->toThrow(
InvalidType::class,
);
},
);
it(
'throws if value is not a valid Ulid string during conversion to PHP Value',
function (): void {
$ulidType = Type::getType(UlidType::NAME);
$platform = new SQLitePlatform;
$invalidUlidString = 'invalid-ulid-string';
expect(
static fn(): mixed
=> $ulidType->convertToPHPValue($invalidUlidString, $platform),
)->toThrow(
ValueNotConvertible::class,
);
},
);
it(
'can convert Ulid object to database value',
function (): void {
$ulidType = Type::getType(UlidType::NAME);
$platform = new SQLitePlatform;
$ulidObject = new Ulid;
$databaseValue = $ulidType->convertToDatabaseValue($ulidObject, $platform);
expect($databaseValue)
->toBe($ulidObject->toBinary());
},
);
it(
'can convert Ulid string to a database value',
function (): void {
$ulidType = Type::getType(UlidType::NAME);
$platform = new SQLitePlatform;
$ulidObject = new Ulid;
$ulidBinary = $ulidObject->toBinary();
$databaseValue = $ulidType->convertToDatabaseValue(
(string) $ulidObject,
$platform,
);
expect($databaseValue)
->toBe($ulidBinary);
},
);
it(
'will return null when value is null',
function (): void {
$ulidType = Type::getType(UlidType::NAME);
$platform = new SQLitePlatform;
$nullValue = null;
$databaseValue = $ulidType->convertToDatabaseValue($nullValue, $platform);
expect($databaseValue)
->toBe(null);
},
);
it(
'throws if value is not string during conversion to Database Value',
function (): void {
$ulidType = Type::getType(UlidType::NAME);
$platform = new SQLitePlatform;
$nonStringValue = 123;
expect(
static fn(): mixed
=> $ulidType->convertToDatabaseValue($nonStringValue, $platform),
)->toThrow(
InvalidType::class,
);
},
);
it(
'throws if value is not a valid Ulid string during conversion to Database Value',
function (): void {
$ulidType = Type::getType(UlidType::NAME);
$platform = new SQLitePlatform;
$invalidUlidString = 'invalid-ulid-string';
expect(
static fn(): mixed
=> $ulidType->convertToDatabaseValue($invalidUlidString, $platform),
)->toThrow(
ValueNotConvertible::class,
);
},
);