Skip to content

Commit

Permalink
Merge pull request #437 from gianarb/feature/comment-types
Browse files Browse the repository at this point in the history
Doctrine comment types
  • Loading branch information
Gianluca Arbezzano committed Oct 5, 2015
2 parents 900e8d4 + ac4fa73 commit 12bce9b
Show file tree
Hide file tree
Showing 6 changed files with 265 additions and 0 deletions.
14 changes: 14 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@ return array(
),
```

### Option to set the doctrine type comment (DC2Type:myType) for custom types

```php
'doctrine' => array(
'connection' => array(
'orm_default' => array(
'doctrineCommentedTypes' => array(
'mytype'
),
),
),
),
```

### How to Define Relationships with Abstract Classes and Interfaces (ResolveTargetEntityListener)

```php
Expand Down
21 changes: 21 additions & 0 deletions src/DoctrineORMModule/Options/DBALConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ class DBALConnection extends AbstractOptions
*/
protected $doctrineTypeMappings = array();

/**
* @var array
*/
protected $doctrineCommentedTypes = array();

/**
* @param string $configuration
*/
Expand Down Expand Up @@ -152,6 +157,22 @@ public function getDoctrineTypeMappings()
return $this->doctrineTypeMappings;
}

/**
* @param array $doctrineCommentedTypes
*/
public function setDoctrineCommentedTypes(array $doctrineCommentedTypes)
{
$this->doctrineCommentedTypes = $doctrineCommentedTypes;
}

/**
* @return array
*/
public function getDoctrineCommentedTypes()
{
return $this->doctrineCommentedTypes;
}

/**
* @param null|string $driverClass
*/
Expand Down
5 changes: 5 additions & 0 deletions src/DoctrineORMModule/Service/DBALConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
namespace DoctrineORMModule\Service;

use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Types\Type;
use DoctrineModule\Service\AbstractFactory;
use Zend\ServiceManager\ServiceLocatorInterface;

Expand Down Expand Up @@ -62,6 +63,10 @@ public function createService(ServiceLocatorInterface $sl)
$platform->registerDoctrineTypeMapping($dbType, $doctrineType);
}

foreach ($options->getDoctrineCommentedTypes() as $type) {
$platform->markDoctrineTypeCommented(Type::getType($type));
}

return $connection;
}

Expand Down
50 changes: 50 additions & 0 deletions tests/DoctrineORMModuleTest/Assets/Types/MoneyType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace DoctrineORMModuleTest\Assets\Types;

use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Platforms\AbstractPlatform;

/**
* My custom datatype.
*/
class MoneyType extends Type
{
const MONEY = 'money';

public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
{
return 'MyMoney';
}

public function convertToPHPValue($value, AbstractPlatform $platform)
{
return new Money($value);
}

public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
return $value->toDecimal();
}

public function getName()
{
return self::MONEY;
}
}
44 changes: 44 additions & 0 deletions tests/DoctrineORMModuleTest/Options/DBALConnectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/

namespace DoctrineORMModuleTest\Options;

use PHPUnit_Framework_TestCase;
use DoctrineORMModule\Options\DBALConnection;
use Doctrine\ORM\Repository\DefaultRepositoryFactory;

/**
* @covers \DoctrineORMModule\Options\DBALConnection
*/
class DBALConnectionTest extends PHPUnit_Framework_TestCase
{
public function testSetNullCommentedTypes()
{
$options = new DBALConnection();
$options->setDoctrineCommentedTypes([]);
$this->assertSame(array(), $options->getDoctrineCommentedTypes());
}

public function testSetGetCommentedTypes()
{
$options = new DBALConnection();
$options->setDoctrineCommentedTypes(array('mytype'));
$this->assertSame(array('mytype'), $options->getDoctrineCommentedTypes());
}
}
131 changes: 131 additions & 0 deletions tests/DoctrineORMModuleTest/Service/DBALConnectionFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/

namespace DoctrineORMModuleTest\Service;

use PHPUnit_Framework_TestCase;
use DoctrineORMModuleTest\Assets\Types\MoneyType;
use DoctrineORMModule\Service\DBALConnectionFactory;
use Doctrine\DBAL\Types\Type;
use Doctrine\Common\Cache\ArrayCache;
use Doctrine\Common\EventManager;
use Zend\ServiceManager\ServiceManager;
use DoctrineORMModule\Service\ConfigurationFactory;

/**
* @covers \DoctrineORMModule\Service\DBALConnectionFactory
*/
class DBALConnectionFactoryTest extends PHPUnit_Framework_TestCase
{
/**
* @var ServiceManager
*/
protected $serviceManager;
/**
* @var DBALConnectionFactory
*/
protected $factory;

/**
* {@inheritDoc}
*/
public function setUp()
{
$this->serviceManager = new ServiceManager();
$this->factory = new DBALConnectionFactory('orm_default');
$this->serviceManager->setService('doctrine.cache.array', new ArrayCache());
$this->serviceManager->setService('doctrine.eventmanager.orm_default', new EventManager());
}

public function testDoctrineMappingTypeReturnCorrectParent()
{
$config = array(
'doctrine' => array(
'connection' => array(
'orm_default' => array(
'driverClass' => 'Doctrine\DBAL\Driver\PDOSqlite\Driver',
'params' => array(
'memory' => true,
),
'doctrineTypeMappings' => array(
'money' => 'string'
),
)
),
),
);
$configurationMock = $this->getMockBuilder('Doctrine\ORM\Configuration')
->disableOriginalConstructor()
->getMock();

$this->serviceManager->setService('doctrine.configuration.orm_default', $configurationMock);
$this->serviceManager->setService('Config', $config);
$this->serviceManager->setService('Configuration', $config);

$dbal = $this->factory->createService($this->serviceManager);
$platform = $dbal->getDatabasePlatform();
$this->assertSame('string', $platform->getDoctrineTypeMapping("money"));
}

public function testDoctrineAddCustomCommentedType()
{
$config = array(
'doctrine' => array(
'connection' => array(
'orm_default' => array(
'driverClass' => 'Doctrine\DBAL\Driver\PDOSqlite\Driver',
'params' => array(
'memory' => true,
),
'doctrineTypeMappings' => array(
'money' => 'money',
),
'doctrineCommentedTypes' => array(
'money'
),
)
),
'configuration' => array(
'orm_default' => array(
'types' => array(
'money' => 'DoctrineORMModuleTest\Assets\Types\MoneyType',
),
),
),
),
);
$this->serviceManager->setService('Config', $config);
$this->serviceManager->setService('Configuration', $config);
$this->serviceManager->setService(
'doctrine.driver.orm_default',
$this->getMock('Doctrine\Common\Persistence\Mapping\Driver\MappingDriver')
);
$configurationFactory = new ConfigurationFactory('orm_default');
$this->serviceManager->setService(
'doctrine.configuration.orm_default',
$configurationFactory->createService($this->serviceManager)
);
$dbal = $this->factory->createService($this->serviceManager);
$platform = $dbal->getDatabasePlatform();
$type = Type::getType($platform->getDoctrineTypeMapping("money"));

$this->assertInstanceOf('DoctrineORMModuleTest\Assets\Types\MoneyType', $type);
$this->assertTrue($platform->isCommentedDoctrineType($type));
}
}

0 comments on commit 12bce9b

Please sign in to comment.