Separate Doctrine\UuidTupe into an external library

Closes #51
This commit is contained in:
Ben Ramsey
2015-06-21 18:05:49 -05:00
parent 2d45a33475
commit de02e532b2
5 changed files with 2 additions and 254 deletions
+2 -3
View File
@@ -31,7 +31,6 @@
"moontoast/math": "~1.1",
"ircmaxell/random-lib": "~1.0",
"symfony/console": "~2.3",
"doctrine/dbal": ">=2.3",
"phpunit/phpunit": "~4.5",
"squizlabs/php_codesniffer": "^2.3",
"jakub-onderka/php-parallel-lint": "^0.9.0",
@@ -39,10 +38,10 @@
},
"bin": ["bin/uuid"],
"suggest": {
"ramsey/uuid-doctrine": "Allow the use of a UUID as Doctrine field type.",
"moontoast/math": "Support for converting UUID to 128-bit integer (in string form).",
"ircmaxell/random-lib": "Provides RandomLib to use with the RandomLibAdapter",
"symfony/console": "Support for use of the bin/uuid command line tool.",
"doctrine/dbal": "Allow the use of a UUID as doctrine field type."
"symfony/console": "Support for use of the bin/uuid command line tool."
},
"autoload": {
"psr-4": {"Ramsey\\Uuid\\": "src/"}
-108
View File
@@ -1,108 +0,0 @@
<?php
/**
* This file is part of the Ramsey\Uuid library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) 2012-2014 Ben Ramsey <http://benramsey.com>
* @license http://opensource.org/licenses/MIT MIT
*/
namespace Ramsey\Uuid\Doctrine;
use InvalidArgumentException;
use Ramsey\Uuid\Uuid;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Platforms\AbstractPlatform;
/**
* Field type mapping for the Doctrine Database Abstraction Layer (DBAL).
*
* UUID fields will be stored as a string in the database and converted back to
* the Uuid value object when querying.
*/
class UuidType extends Type
{
/**
* @var string
*/
const NAME = 'uuid';
/**
* {@inheritdoc}
*
* @param array $fieldDeclaration
* @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform
*/
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
{
return $platform->getGuidTypeDeclarationSQL($fieldDeclaration);
}
/**
* {@inheritdoc}
*
* @param string|null $value
* @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform
*/
public function convertToPHPValue($value, AbstractPlatform $platform)
{
if (empty($value)) {
return null;
}
if ($value instanceof Uuid) {
return $value;
}
try {
$uuid = Uuid::fromString($value);
} catch (InvalidArgumentException $e) {
throw ConversionException::conversionFailed($value, self::NAME);
}
return $uuid;
}
/**
* {@inheritdoc}
*
* @param Uuid|null $value
* @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform
*/
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
if (empty($value)) {
return null;
}
if ($value instanceof Uuid || Uuid::isValid($value)) {
return (string) $value;
}
throw ConversionException::conversionFailed($value, self::NAME);
}
/**
* {@inheritdoc}
*
* @return string
*/
public function getName()
{
return self::NAME;
}
/**
* {@inheritdoc}
*
* @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform
* @return boolean
*/
public function requiresSQLCommentHint(AbstractPlatform $platform)
{
return true;
}
}
-123
View File
@@ -1,123 +0,0 @@
<?php
namespace Ramsey\Uuid;
use Doctrine\DBAL\Types\Type;
use Doctrine\Tests\DBAL\Mocks\MockPlatform;
use Ramsey\Uuid\TestCase;
class UuidTypeTest extends TestCase
{
private $platform;
private $type;
public static function setUpBeforeClass()
{
if (class_exists('Doctrine\\DBAL\\Types\\Type')) {
Type::addType('uuid', 'Ramsey\Uuid\Doctrine\UuidType');
}
}
protected function setUp()
{
$this->skipIfNoDoctrineDbal();
$this->skip64BitTest();
$this->platform = $this->getPlatformMock();
$this->platform->expects($this->any())
->method('getGuidTypeDeclarationSQL')
->will($this->returnValue('DUMMYVARCHAR()'));
$this->type = Type::getType('uuid');
}
/**
* @covers Ramsey\Uuid\Doctrine\UuidType::convertToDatabaseValue
*/
public function testUuidConvertsToDatabaseValue()
{
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$expected = $uuid->toString();
$actual = $this->type->convertToDatabaseValue($uuid, $this->platform);
$this->assertEquals($expected, $actual);
}
/**
* @covers Ramsey\Uuid\Doctrine\UuidType::convertToDatabaseValue
*/
public function testInvalidUuidConversionForDatabaseValue()
{
$this->setExpectedException('Doctrine\DBAL\Types\ConversionException');
$this->type->convertToDatabaseValue('abcdefg', $this->platform);
}
/**
* @covers Ramsey\Uuid\Doctrine\UuidType::convertToDatabaseValue
*/
public function testNullConversionForDatabaseValue()
{
$this->assertNull($this->type->convertToDatabaseValue(null, $this->platform));
}
/**
* @covers Ramsey\Uuid\Doctrine\UuidType::convertToPHPValue
*/
public function testUuidConvertsToPHPValue()
{
$uuid = $this->type->convertToPHPValue('ff6f8cb0-c57d-11e1-9b21-0800200c9a66', $this->platform);
$this->assertInstanceOf('Ramsey\Uuid\Uuid', $uuid);
$this->assertEquals('ff6f8cb0-c57d-11e1-9b21-0800200c9a66', $uuid->toString());
}
/**
* @covers Ramsey\Uuid\Doctrine\UuidType::convertToPHPValue
*/
public function testInvalidUuidConversionForPHPValue()
{
$this->setExpectedException('Doctrine\DBAL\Types\ConversionException');
$this->type->convertToPHPValue('abcdefg', $this->platform);
}
/**
* @covers Ramsey\Uuid\Doctrine\UuidType::convertToPHPValue
*/
public function testNullConversionForPHPValue()
{
$this->assertNull($this->type->convertToPHPValue(null, $this->platform));
}
/**
* @covers Ramsey\Uuid\Doctrine\UuidType::getName
*/
public function testGetName()
{
$this->assertEquals('uuid', $this->type->getName());
}
/**
* @covers Ramsey\Uuid\Doctrine\UuidType::getSqlDeclaration
*/
public function testGetGuidTypeDeclarationSQL()
{
$this->assertEquals('DUMMYVARCHAR()', $this->type->getSqlDeclaration(array('length' => 36), $this->platform));
}
/**
* @covers Ramsey\Uuid\Doctrine\UuidType::requiresSQLCommentHint
*/
public function testRequiresSQLCommentHint()
{
$this->assertTrue($this->type->requiresSQLCommentHint($this->platform));
}
/**
* @return \PHPUnit_Framework_MockObject_MockObject
*/
private function getPlatformMock()
{
return $this->getMockBuilder('Doctrine\DBAL\Platforms\AbstractPlatform')
->setMethods(array('getGuidTypeDeclarationSQL'))
->getMockForAbstractClass();
}
}
-14
View File
@@ -30,15 +30,6 @@ class TestCase extends \PHPUnit_Framework_TestCase
}
}
protected function skipIfNoDoctrineDbal()
{
if (!$this->hasDoctrineDbal()) {
$this->markTestSkipped(
'Skipping test that requires doctrine/dbal.'
);
}
}
protected function hasMoontoastMath()
{
return class_exists('Moontoast\\Math\\BigNumber');
@@ -48,9 +39,4 @@ class TestCase extends \PHPUnit_Framework_TestCase
{
return class_exists('Symfony\\Component\\Console\\Application');
}
protected function hasDoctrineDbal()
{
return class_exists('Doctrine\\DBAL\\Types\\Type');
}
}
-6
View File
@@ -1,5 +1,4 @@
<?php
error_reporting(E_ALL | E_STRICT);
date_default_timezone_set('UTC');
@@ -9,11 +8,6 @@ if (!file_exists(dirname(__DIR__) . '/vendor/autoload.php')) {
. "See http://getcomposer.org for help with installing composer\n");
}
// Set a default timezone for HHVM tests
date_default_timezone_set('UTC');
// Include the Composer autoloader
$loader = include realpath(dirname(__FILE__) . '/../vendor/autoload.php');
$loader->add("Doctrine\Tests\DBAL", __DIR__."/../vendor/doctrine/dbal/tests");
$loader->addPsr4('Ramsey\\Uuid\\', __DIR__);