8000 Add: UnitTest for UuidType and UlidType · symfony/symfony@0e9424c · GitHub
[go: up one dir, main page]

Skip to content

Commit 0e9424c

Browse files
Add: UnitTest for UuidType and UlidType
1 parent a042ded commit 0e9424c

File tree

2 files changed

+278
-0
lines changed

2 files changed

+278
-0
lines changed
Lines changed: 139 additions & 0 deletions
< 8000 td data-grid-cell-id="diff-b8b8cbe280d92c17ed19b8afc9ca0b3e3ed5b379779ea0b404fa1d85f660dcc3-empty-29-0" data-selected="false" role="gridcell" style="background-color:var(--diffBlob-additionNum-bgColor, var(--diffBlob-addition-bgColor-num));text-align:center" tabindex="-1" valign="top" class="focusable-grid-cell diff-line-number position-relative left-side">
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\Tests\Types;
13+
14+
use Doctrine\DBAL\Platforms\AbstractPlatform;
15+
use Doctrine\DBAL\Types\ConversionException;
16+
use Doctrine\DBAL\Types\Type;
17+
use PHPUnit\Framework\TestCase;
18+
use Symfony\Bridge\Doctrine\Types\UlidType;
19+
use Symfony\Component\Uid\AbstractUid;
20+
use Symfony\Component\Uid\Ulid;
21+
22+
final class UlidTypeTest extends TestCase
23+
{
24+
private const DUMMY_ULID = '01EEDQEK6ZAZE93J8KG5B4MBJC';
25+
26+
/** @var AbstractPlatform */
27+
private $platform;
28+
29+
/** @var UlidType */
30+
private $type;
31+
32+
public static function setUpBeforeClass(): void
33+
{
34+
Type::addType('ulid', UlidType::class);
35+
}
36+
37+
protected function setUp(): void
38+
{
39+
$this->platform = $this->createMock(AbstractPlatform::class);
40+
$this->platform
41+
->method('getGuidTypeDeclarationSQL')
42+
->willReturn('DUMMYVARCHAR()');
43+
44+
$this->type = Type::getType('ulid');
45+
}
46+
47+
public function testUlidConvertsToDatabaseValue(): void
48+
{
49+
$ulid = Ulid::fromString(self::DUMMY_ULID);
50+
< 8000 code>51+
$expected = $ulid->__toString();
52+
$actual = $this->type->convertToDatabaseValue($ulid, $this->platform);
53+
54+
$this->assertEquals($expected, $actual);
55+
}
56+
57+
public function testUlidInterfaceConvertsToDatabaseValue(): void
58+
{
59+
$ulid = $this->createMock(AbstractUid::class);
60+
61+
$ulid
62+
->expects($this->once())
63+
->method('__toString')
64+
->willReturn('foo');
65+
66+
$actual = $this->type->convertToDatabaseValue($ulid, $this->platform);
67+
68+
$this->assertEquals('foo', $actual);
69+
}
70+
71+
public function testUlidStringConvertsToDatabaseValue(): void
72+
{
73+
$actual = $this->type->convertToDatabaseValue(self::DUMMY_ULID, $this->platform);
74+
75+
$this->assertEquals(self::DUMMY_ULID, $actual);
76+
}
77+
78+
public function testInvalidUlidConversionForDatabaseValue(): void
79+
{
80+
$this->expectException(ConversionException::class);
81+
82+
$this->type->convertToDatabaseValue('abcdefg', $this->platform);
83+
}
84+
85+
public function testNullConversionForDatabaseValue(): void
86+
{
87+
$this->assertNull($this->type->convertToDatabaseValue(null, $this->platform));
88+
}
89+
90+
public function testUlidInterfaceConvertsToPHPValue(): void
91+
{
92+
$ulid = $this->createMock(AbstractUid::class);
93+
$actual = $this->type->convertToPHPValue($ulid, $this->platform);
94+
95+
$this->assertSame($ulid, $actual);
96+
}
97+
98+
public function testUlidConvertsToPHPValue(): void
99+
{
100+
$ulid = $this->type->convertToPHPValue(self::DUMMY_ULID, $this->platform);
101+
102+
$this->assertInstanceOf(Ulid::class, $ulid);
103+
$this->assertEquals(self::DUMMY_ULID, $ulid->__toString());
104+
}
105+
106+
public function testInvalidUlidConversionForPHPValue(): void
107+
{
108+
$this->expectException(ConversionException::class);
109+
110+
$this->type->convertToPHPValue('abcdefg', $this->platform);
111+
}
112+
113+
public function testNullConversionForPHPValue(): void
114+
{
115+
$this->assertNull($this->type->convertToPHPValue(null, $this->platform));
116+
}
117+
118+
public function testReturnValueIfUlidForPHPValue(): void
119+
{
120+
$ulid = new Ulid();
121+
122+
$this->assertSame($ulid, $this->type->convertToPHPValue($ulid, $this->platform));
123+
}
124+
125+
public function testGetName(): void
126+
{
127+
$this->assertEquals('ulid', $this->type->getName());
128+
}
129+
130+
public function testGetGuidTypeDeclarationSQL(): void
131+
{
132+
$this->assertEquals('DUMMYVARCHAR()', $this->type->getSqlDeclaration(['length' => 36], $this->platform));
133+
}
134+
135+
public function testRequiresSQLCommentHint(): void
136+
{
137+
$this->assertTrue($this->type->requiresSQLCommentHint($this->platform));
138+
}
139+
}
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\Tests\Types;
13+
14+
use Doctrine\DBAL\Platforms\AbstractPlatform;
15+
use Doctrine\DBAL\Types\ConversionException;
16+
use Doctrine\DBAL\Types\Type;
17+
use PHPUnit\Framework\TestCase;
18+
use Symfony\Bridge\Doctrine\Types\UuidType;
19+
use Symfony\Component\Uid\AbstractUid;
20+
use Symfony\Component\Uid\Uuid;
21+
22+
final class UuidTypeTest extends TestCase
23+
{
24+
private const DUMMY_UUID = '9f755235-5a2d-4aba-9605-e9962b312e50';
25+
26+
/** @var AbstractPlatform */
27+
private $platform;
28+
29+
/** @var UuidType */
30+
private $type;
31+
32+
public static function setUpBeforeClass(): void
33+
{
34+
Type::addType('uuid', UuidType::class);
35+
}
36+
37+
protected function setUp(): void
38+
{
39+
$this->platform = $this->createMock(AbstractPlatform::class);
40+
$this->platform
41+
->method('getGuidTypeDeclarationSQL')
42+
->willReturn('DUMMYVARCHAR()');
43+
44+
$this->type = Type::getType('uuid');
45+
}
46+
47+
public function testUuidConvertsToDatabaseValue(): void
48+
{
49+
$uuid = Uuid::fromString(self::DUMMY_UUID);
50+
51+
$expected = $uuid->__toString();
52+
$actual = $this->type->convertToDatabaseValue($uuid, $this->platform);
53+
54+
$this->assertEquals($expected, $actual);
55+
}
56+
57+
public function testUuidInterfaceConvertsToDatabaseValue(): void
58+
{
59+
$uuid = $this->createMock(AbstractUid::class);
60+
61+
$uuid
62+
->expects($this->once())
63+
->method('__toString')
64+
->willReturn('foo');
65+
66+
$actual = $this->type->convertToDatabaseValue($uuid, $this->platform);
67+
68+
$this->assertEquals('foo', $actual);
69+
}
70+
71+
public function testUuidStringConvertsToDatabaseValue(): void
72+
{
73+
$actual = $this->type->convertToDatabaseValue(self::DUMMY_UUID, $this->platform);
74+
75+
$this->assertEquals(self::DUMMY_UUID, $actual);
76+
}
77+
78+
public function testInvalidUuidConversionForDatabaseValue(): void
79+
{
80+
$this->expectException(ConversionException::class);
81+
82+
$this->type->convertToDatabaseValue('abcdefg', $this->platform);
83+
}
84+
85+
public function testNullConversionForDatabaseValue(): void
86+
{
87+
$this->assertNull($this->type->convertToDatabaseValue(null, $this->platform));
88+
}
89+
90+
public function testUuidInterfaceConvertsToPHPValue(): void
91+
{
92+
$uuid = $this->createMock(AbstractUid::class);
93+
$actual = $this->type->convertToPHPValue($uuid, $this->platform);
94+
95+
$this->assertSame($uuid, $actual);
96+
}
97+
98+
public function testUuidConvertsToPHPValue(): void
99+
{
100+
$uuid = $this->type->convertToPHPValue(self::DUMMY_UUID, $this->platform);
101+
102+
$this->assertInstanceOf(Uuid::class, $uuid);
103+
$this->assertEquals(self::DUMMY_UUID, $uuid->__toString());
104+
}
105+
106+
public function testInvalidUuidConversionForPHPValue(): void
107+
{
108+
$this->expectException(ConversionException::class);
109+
110+
$this->type->convertToPHPValue('abcdefg', $this->platform);
111+
}
112+
113+
public function testNullConversionForPHPValue(): void
114+
{
115+
$this->assertNull($this->type->convertToPHPValue(null, $this->platform));
116+
}
117+
118+
public function testReturnValueIfUuidForPHPValue(): void
119+
{
120+
$uuid = Uuid::v4();
121+
122+
$this->assertSame($uuid, $this->type->convertToPHPValue($uuid, $this->platform));
123+
}
124+
125+
public function testGetName(): void
126+
{
127+
$this->assertEquals('uuid', $this->type->getName());
128+
}
129+
130+
public function testGetGuidTypeDeclarationSQL(): void
131+
{
132+
$this->assertEquals('DUMMYVARCHAR()', $this->type->getSqlDeclaration(['length' => 36], $this->platform));
133+
}
134+
135+
public function testRequiresSQLCommentHint(): void
136+
{
137+
$this->assertTrue($this->type->requiresSQLCommentHint($this->platform));
138+
}
139+
}

0 commit comments

Comments
 (0)
0