|
| 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