-
-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #437 from gianarb/feature/comment-types
Doctrine comment types
- Loading branch information
Showing
6 changed files
with
265 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
44
tests/DoctrineORMModuleTest/Options/DBALConnectionTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
131
tests/DoctrineORMModuleTest/Service/DBALConnectionFactoryTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |