Skip to content

Commit c45663b

Browse files
committed
basic tests
1 parent 965c746 commit c45663b

File tree

9 files changed

+145
-3
lines changed

9 files changed

+145
-3
lines changed

.gitattributes

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1-
*.md export-ignore
21
.* export-ignore
2+
*.md export-ignore
3+
/tests export-ignore
4+
/phpunit.xml.dist export-ignore
5+
/.github export-ignore
6+
/.php-cs-fixer.php export-ignore
37
/docs export-ignore

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/vendor
2-
/composer.lock
2+
/composer.lock
3+
.phpunit.result.cache

composer.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,16 @@
2929
"Pfilsx\\PostgreSQLDoctrine\\": "src/"
3030
}
3131
},
32+
"autoload-dev": {
33+
"psr-4": {
34+
"Pfilsx\\PostgreSQLDoctrine\\Tests\\": "tests/"
35+
}
36+
},
3237
"require-dev": {
3338
"friendsofphp/php-cs-fixer": "^3.13",
3439
"doctrine/orm": "^2.13",
3540
"symfony/serializer": ">=5.4",
36-
"symfony/property-info": ">=5.4"
41+
"symfony/property-info": ">=5.4",
42+
"phpunit/phpunit": "^10.0"
3743
}
3844
}

phpunit.xml.dist

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd">
3+
<testsuites>
4+
<testsuite name="PostgreSQLDoctrine">
5+
<directory suffix="Test.php">./tests</directory>
6+
</testsuite>
7+
</testsuites>
8+
9+
<coverage>
10+
<include>
11+
<directory>./src</directory>
12+
</include>
13+
</coverage>
14+
</phpunit>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Pfilsx\PostgreSQLDoctrine\Tests\Fixtures\Enum;
4+
5+
use Pfilsx\PostgreSQLDoctrine\DBAL\Contract\EnumInterface;
6+
7+
final class TestEnumInterfaceEnum implements EnumInterface
8+
{
9+
public const CASE1 = 'Case1';
10+
11+
public const CASE2 = 'Case2';
12+
13+
public const CASE3 = 'Case3';
14+
15+
public static function cases(): array
16+
{
17+
return [self::CASE1, self::CASE2, self::CASE3];
18+
}
19+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Pfilsx\PostgreSQLDoctrine\Tests\Fixtures\Enum;
4+
5+
enum TestIntBackedEnum: int
6+
{
7+
case Case1 = 1;
8+
case Case2 = 2;
9+
case Case3 = 3;
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Pfilsx\PostgreSQLDoctrine\Tests\Fixtures\Enum;
4+
5+
enum TestStringBackedEnum: string
6+
{
7+
case Case1 = 'Case1';
8+
case Case2 = 'Case2';
9+
case Case3 = 'Case3';
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Pfilsx\PostgreSQLDoctrine\Tests\Fixtures\Enum;
4+
5+
enum TestUnitEnum
6+
{
7+
case Case1;
8+
case Case2;
9+
case Case3;
10+
}

tests/Unit/Tools/EnumToolTest.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace Pfilsx\PostgreSQLDoctrine\Tests\Unit\Tools;
4+
5+
use Doctrine\DBAL\Exception\InvalidArgumentException;
6+
use Pfilsx\PostgreSQLDoctrine\Tests\Fixtures\Enum\TestEnumInterfaceEnum;
7+
use Pfilsx\PostgreSQLDoctrine\Tests\Fixtures\Enum\TestIntBackedEnum;
8+
use Pfilsx\PostgreSQLDoctrine\Tests\Fixtures\Enum\TestStringBackedEnum;
9+
use Pfilsx\PostgreSQLDoctrine\Tests\Fixtures\Enum\TestUnitEnum;
10+
use Pfilsx\PostgreSQLDoctrine\Tools\EnumTool;
11+
use PHPUnit\Framework\TestCase;
12+
13+
final class EnumToolTest extends TestCase
14+
{
15+
/**
16+
* @dataProvider providerTestGetEnumTypeNameFromClassName
17+
* @see EnumTool::getEnumTypeNameFromClassName()
18+
*/
19+
public function testGetEnumTypeNameFromClassName(string $className, ?string $expectedName): void
20+
{
21+
if ($expectedName === null) {
22+
self::expectException(InvalidArgumentException::class);
23+
}
24+
25+
self::assertSame($expectedName, EnumTool::getEnumTypeNameFromClassName($className));
26+
}
27+
28+
public static function providerTestGetEnumTypeNameFromClassName(): array
29+
{
30+
return [
31+
'string enum' => [TestStringBackedEnum::class, 'test_string_backed_enum_type'],
32+
'unit enum' => [TestUnitEnum::class, 'test_unit_enum_type'],
33+
'enum interface' => [TestEnumInterfaceEnum::class, 'test_enum_interface_enum_type'],
34+
'int enum' => [TestIntBackedEnum::class, null],
35+
'other class' => [\stdClass::class, null],
36+
'random string' => ['test', null],
37+
];
38+
}
39+
40+
/**
41+
* @dataProvider providerTestGetEnumLabelsByClassName
42+
* @see EnumTool::getEnumLabelsByClassName()
43+
*/
44+
public function testGetEnumLabelsByClassName(string $className, bool $expectException): void
45+
{
46+
if ($expectException) {
47+
self::expectException(InvalidArgumentException::class);
48+
}
49+
50+
self::assertSame([
51+
'Case1',
52+
'Case2',
53+
'Case3',
54+
], EnumTool::getEnumLabelsByClassName($className));
55+
}
56+
57+
public static function providerTestGetEnumLabelsByClassName(): array
58+
{
59+
return [
60+
'string enum' => [TestStringBackedEnum::class, false],
61+
'unit enum' => [TestUnitEnum::class, false],
62+
'enum interface' => [TestEnumInterfaceEnum::class, false],
63+
'int enum' => [TestIntBackedEnum::class, true],
64+
'other class' => [\stdClass::class, true],
65+
'random string' => ['test', true],
66+
];
67+
}
68+
}

0 commit comments

Comments
 (0)