From 51385c9283ade1be8f7894f5cfb5c481e70243a9 Mon Sep 17 00:00:00 2001 From: Martin Keckeis Date: Wed, 28 Aug 2013 18:30:51 +0200 Subject: [PATCH] Doctrine2 source start testing... --- src/ZfcDatagrid/Column/AbstractColumn.php | 18 ++ src/ZfcDatagrid/DataSource/Doctrine2.php | 20 +- .../Doctrine2/AbstractDoctrine2Test.php | 151 +++++++++++++++ .../Doctrine2/Mocks/ConnectionMock.php | 176 ++++++++++++++++++ .../Doctrine2/Mocks/DatabasePlatformMock.php | 151 +++++++++++++++ .../DataSource/Doctrine2/Mocks/DriverMock.php | 94 ++++++++++ .../Doctrine2/Mocks/EntityManagerMock.php | 101 ++++++++++ .../DataSource/Doctrine2Test.php | 29 +++ .../DataSource/ZendSelectTest.php | 9 +- tests/ZfcDatagridTest/PrepareDataTest.php | 41 +++- 10 files changed, 773 insertions(+), 17 deletions(-) create mode 100644 tests/ZfcDatagridTest/DataSource/Doctrine2/AbstractDoctrine2Test.php create mode 100644 tests/ZfcDatagridTest/DataSource/Doctrine2/Mocks/ConnectionMock.php create mode 100644 tests/ZfcDatagridTest/DataSource/Doctrine2/Mocks/DatabasePlatformMock.php create mode 100644 tests/ZfcDatagridTest/DataSource/Doctrine2/Mocks/DriverMock.php create mode 100644 tests/ZfcDatagridTest/DataSource/Doctrine2/Mocks/EntityManagerMock.php create mode 100644 tests/ZfcDatagridTest/DataSource/Doctrine2Test.php diff --git a/src/ZfcDatagrid/Column/AbstractColumn.php b/src/ZfcDatagrid/Column/AbstractColumn.php index 97389d5a..c372cdcf 100644 --- a/src/ZfcDatagrid/Column/AbstractColumn.php +++ b/src/ZfcDatagrid/Column/AbstractColumn.php @@ -419,6 +419,12 @@ public function isTranslationEnabled () return (bool) $this->translationEnabled; } + /** + * Replace the column values with the applied values + * + * @param array $values + * @param boolean $notReplacedGetEmpty + */ public function setReplaceValues (array $values, $notReplacedGetEmpty = true) { $this->replaceValues = $values; @@ -428,6 +434,10 @@ public function setReplaceValues (array $values, $notReplacedGetEmpty = true) $this->setFilterSelectOptions($values); } + /** + * + * @return boolean + */ public function hasReplaceValues () { if (count($this->replaceValues) > 0) @@ -436,11 +446,19 @@ public function hasReplaceValues () return false; } + /** + * + * @return array + */ public function getReplaceValues () { return $this->replaceValues; } + /** + * + * @return boolean + */ public function notReplacedGetEmpty () { return $this->notReplacedGetEmpty; diff --git a/src/ZfcDatagrid/DataSource/Doctrine2.php b/src/ZfcDatagrid/DataSource/Doctrine2.php index 0bd0583a..615904da 100644 --- a/src/ZfcDatagrid/DataSource/Doctrine2.php +++ b/src/ZfcDatagrid/DataSource/Doctrine2.php @@ -4,7 +4,7 @@ use ZfcDatagrid\Filter; use ZfcDatagrid\DataSource\Doctrine2Paginator as PaginatorAdapter; use ZfcDatagrid\Column; -use Doctrine\ORM; +use Doctrine\ORM; use Doctrine\ORM\Query\Expr; class Doctrine2 extends AbstractDataSource @@ -21,23 +21,29 @@ class Doctrine2 extends AbstractDataSource * * @param mixed $data */ - public function __construct ($data) + public function __construct($data) { if ($data instanceof ORM\QueryBuilder) { $this->queryBuilder = $data; } else { - throw new \Exception("Unknown data input..." . get_class($data)); + $return = $data; + if (is_object($data)) { + $return = get_class($return); + } + throw new \InvalidArgumentException("Unknown data input..." . $return); } } - + /** + * * @return ORM\QueryBuilder */ - public function getData(){ + public function getData() + { return $this->queryBuilder; } - public function execute () + public function execute() { $queryBuilder = $this->queryBuilder; @@ -117,7 +123,7 @@ public function execute () * @throws \Exception * @return \Doctrine\ORM\Query\Expr */ - private function getWhereExpression ($operator, $colString, $values) + private function getWhereExpression($operator, $colString, $values) { $expr = new Expr(); diff --git a/tests/ZfcDatagridTest/DataSource/Doctrine2/AbstractDoctrine2Test.php b/tests/ZfcDatagridTest/DataSource/Doctrine2/AbstractDoctrine2Test.php new file mode 100644 index 00000000..a0c00e3a --- /dev/null +++ b/tests/ZfcDatagridTest/DataSource/Doctrine2/AbstractDoctrine2Test.php @@ -0,0 +1,151 @@ +=')) { + $reader = new \Doctrine\Common\Annotations\CachedReader(new \Doctrine\Common\Annotations\AnnotationReader(), new ArrayCache()); + } else + if (version_compare(\Doctrine\Common\Version::VERSION, '2.2.0-DEV', '>=')) { + // Register the ORM Annotations in the AnnotationRegistry + $reader = new \Doctrine\Common\Annotations\SimpleAnnotationReader(); + $reader->addNamespace('Doctrine\ORM\Mapping'); + $reader = new \Doctrine\Common\Annotations\CachedReader($reader, new ArrayCache()); + } else + if (version_compare(\Doctrine\Common\Version::VERSION, '2.1.0-BETA3-DEV', '>=')) { + $reader = new \Doctrine\Common\Annotations\AnnotationReader(); + $reader->setIgnoreNotImportedAnnotations(true); + $reader->setEnableParsePhpImports(false); + if ($alias) { + $reader->setAnnotationNamespaceAlias('Doctrine\ORM\Mapping\\', $alias); + } else { + $reader->setDefaultAnnotationNamespace('Doctrine\ORM\Mapping\\'); + } + $reader = new \Doctrine\Common\Annotations\CachedReader(new \Doctrine\Common\Annotations\IndexedReader($reader), new ArrayCache()); + } else { + $reader = new \Doctrine\Common\Annotations\AnnotationReader(); + if ($alias) { + $reader->setAnnotationNamespaceAlias('Doctrine\ORM\Mapping\\', $alias); + } else { + $reader->setDefaultAnnotationNamespace('Doctrine\ORM\Mapping\\'); + } + } + \Doctrine\Common\Annotations\AnnotationRegistry::registerFile(__DIR__ . "/../../../lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php"); + return new \Doctrine\ORM\Mapping\Driver\AnnotationDriver($reader, (array) $paths); + } + + /** + * Creates an EntityManager for testing purposes. + * + * NOTE: The created EntityManager will have its dependant DBAL parts completely + * mocked out using a DriverMock, ConnectionMock, etc. These mocks can then + * be configured in the tests to simulate the DBAL behavior that is desired + * for a particular test, + * + * @param \Doctrine\DBAL\Connection|array $conn + * @param mixed $conf + * @param \Doctrine\Common\EventManager|null $eventManager + * @param bool $withSharedMetadata + * + * @return \Doctrine\ORM\EntityManager + */ + protected function _getTestEntityManager($conn = null, $conf = null, $eventManager = null, $withSharedMetadata = true) + { + $metadataCache = $withSharedMetadata ? self::getSharedMetadataCacheImpl() : new \Doctrine\Common\Cache\ArrayCache(); + + $config = new \Doctrine\ORM\Configuration(); + + $config->setMetadataCacheImpl($metadataCache); + $config->setMetadataDriverImpl($config->newDefaultAnnotationDriver(array(), true)); + $config->setQueryCacheImpl(self::getSharedQueryCacheImpl()); + $config->setProxyDir(__DIR__ . '/Proxies'); + $config->setProxyNamespace('Doctrine\Tests\Proxies'); + + if ($conn === null) { + $conn = array( + 'driverClass' => 'ZfcDatagridTest\DataSource\Doctrine2\Mocks\DriverMock', + 'wrapperClass' => 'ZfcDatagridTest\DataSource\Doctrine2\Mocks\ConnectionMock', + 'user' => 'john', + 'password' => 'wayne' + ); + } + + if (is_array($conn)) { + $conn = \Doctrine\DBAL\DriverManager::getConnection($conn, $config, $eventManager); + } + + return \ZfcDatagridTest\DataSource\Doctrine2\Mocks\EntityManagerMock::create($conn, $config, $eventManager); + } + + /** + * + * @return \Doctrine\Common\Cache\Cache + */ + private static function getSharedMetadataCacheImpl() + { + if (self::$_metadataCacheImpl === null) { + self::$_metadataCacheImpl = new \Doctrine\Common\Cache\ArrayCache(); + } + + return self::$_metadataCacheImpl; + } + + /** + * + * @return \Doctrine\Common\Cache\Cache + */ + private static function getSharedQueryCacheImpl() + { + if (self::$_queryCacheImpl === null) { + self::$_queryCacheImpl = new \Doctrine\Common\Cache\ArrayCache(); + } + + return self::$_queryCacheImpl; + } + + public function setUp() + { + $this->em = $this->_getTestEntityManager(); + } + +} \ No newline at end of file diff --git a/tests/ZfcDatagridTest/DataSource/Doctrine2/Mocks/ConnectionMock.php b/tests/ZfcDatagridTest/DataSource/Doctrine2/Mocks/ConnectionMock.php new file mode 100644 index 00000000..9e009e75 --- /dev/null +++ b/tests/ZfcDatagridTest/DataSource/Doctrine2/Mocks/ConnectionMock.php @@ -0,0 +1,176 @@ +_platformMock = new DatabasePlatformMock(); + + parent::__construct($params, $driver, $config, $eventManager); + + // Override possible assignment of platform to database platform mock + $this->_platform = $this->_platformMock; + } + + /** + * @ERROR!!! + */ + public function getDatabasePlatform() + { + return $this->_platformMock; + } + + /** + * @ERROR!!! + */ + public function insert($tableName, array $data, array $types = array()) + { + $this->_inserts[$tableName][] = $data; + } + + /** + * @ERROR!!! + */ + public function executeUpdate($query, array $params = array(), array $types = array()) + { + $this->_executeUpdates[] = array( + 'query' => $query, + 'params' => $params, + 'types' => $types + ); + } + + /** + * @ERROR!!! + */ + public function lastInsertId($seqName = null) + { + return $this->_lastInsertId; + } + + /** + * @ERROR!!! + */ + public function fetchColumn($statement, array $params = array(), $colnum = 0) + { + return $this->_fetchOneResult; + } + + /** + * @ERROR!!! + */ + public function quote($input, $type = null) + { + if (is_string($input)) { + return "'" . $input . "'"; + } + return $input; + } + + /* Mock API */ + + /** + * + * @param mixed $fetchOneResult + * + * @return void + */ + public function setFetchOneResult($fetchOneResult) + { + $this->_fetchOneResult = $fetchOneResult; + } + + /** + * + * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform + * + * @return void + */ + public function setDatabasePlatform($platform) + { + $this->_platformMock = $platform; + } + + /** + * + * @param int $id + * + * @return void + */ + public function setLastInsertId($id) + { + $this->_lastInsertId = $id; + } + + /** + * + * @return array + */ + public function getInserts() + { + return $this->_inserts; + } + + /** + * + * @return array + */ + public function getExecuteUpdates() + { + return $this->_executeUpdates; + } + + /** + * + * @return void + */ + public function reset() + { + $this->_inserts = array(); + $this->_lastInsertId = 0; + } +} \ No newline at end of file diff --git a/tests/ZfcDatagridTest/DataSource/Doctrine2/Mocks/DatabasePlatformMock.php b/tests/ZfcDatagridTest/DataSource/Doctrine2/Mocks/DatabasePlatformMock.php new file mode 100644 index 00000000..ff69048b --- /dev/null +++ b/tests/ZfcDatagridTest/DataSource/Doctrine2/Mocks/DatabasePlatformMock.php @@ -0,0 +1,151 @@ +_prefersIdentityColumns; + } + + /** + * {@inheritdoc} + */ + public function prefersSequences() + { + return $this->_prefersSequences; + } + + /** + * {@inheritdoc} + */ + public function getSequenceNextValSQL($sequenceName) + { + return $this->_sequenceNextValSql; + } + + /** + * {@inheritdoc} + */ + public function getBooleanTypeDeclarationSQL(array $field) + { + } + + /** + * {@inheritdoc} + */ + public function getIntegerTypeDeclarationSQL(array $field) + { + } + + /** + * {@inheritdoc} + */ + public function getBigIntTypeDeclarationSQL(array $field) + { + } + + /** + * {@inheritdoc} + */ + public function getSmallIntTypeDeclarationSQL(array $field) + { + } + + /** + * {@inheritdoc} + */ + protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef) + { + } + + /** + * {@inheritdoc} + */ + public function getVarcharTypeDeclarationSQL(array $field) + { + } + + /** + * {@inheritdoc} + */ + public function getClobTypeDeclarationSQL(array $field) + { + } + + /* MOCK API */ + + /** + * @param bool $bool + * + * @return void + */ + public function setPrefersIdentityColumns($bool) + { + $this->_prefersIdentityColumns = $bool; + } + + /** + * @param bool $bool + * + * @return void + */ + public function setPrefersSequences($bool) + { + $this->_prefersSequences = $bool; + } + + /** + * @param string $sql + * + * @return void + */ + public function setSequenceNextValSql($sql) + { + $this->_sequenceNextValSql = $sql; + } + + /** + * {@inheritdoc} + */ + public function getName() + { + return 'mock'; + } + + /** + * {@inheritdoc} + */ + protected function initializeDoctrineTypeMappings() + { + } + + /** + * {@inheritdoc} + */ + public function getBlobTypeDeclarationSQL(array $field) + { + throw DBALException::notSupported(__METHOD__); + } +} diff --git a/tests/ZfcDatagridTest/DataSource/Doctrine2/Mocks/DriverMock.php b/tests/ZfcDatagridTest/DataSource/Doctrine2/Mocks/DriverMock.php new file mode 100644 index 00000000..4ccba2e1 --- /dev/null +++ b/tests/ZfcDatagridTest/DataSource/Doctrine2/Mocks/DriverMock.php @@ -0,0 +1,94 @@ +_platformMock) { + $this->_platformMock = new DatabasePlatformMock(); + } + return $this->_platformMock; + } + + /** + * @ERROR!!! + */ + public function getSchemaManager(\Doctrine\DBAL\Connection $conn) + { + if ($this->_schemaManagerMock == null) { + return new SchemaManagerMock($conn); + } else { + return $this->_schemaManagerMock; + } + } + + /* MOCK API */ + + /** + * + * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform + * + * @return void + */ + public function setDatabasePlatform(\Doctrine\DBAL\Platforms\AbstractPlatform $platform) + { + $this->_platformMock = $platform; + } + + /** + * + * @param \Doctrine\DBAL\Schema\AbstractSchemaManager $sm + * + * @return void + */ + public function setSchemaManager(\Doctrine\DBAL\Schema\AbstractSchemaManager $sm) + { + $this->_schemaManagerMock = $sm; + } + + /** + * @ERROR!!! + */ + public function getName() + { + return 'mock'; + } + + /** + * @ERROR!!! + */ + public function getDatabase(\Doctrine\DBAL\Connection $conn) + { + return; + } +} \ No newline at end of file diff --git a/tests/ZfcDatagridTest/DataSource/Doctrine2/Mocks/EntityManagerMock.php b/tests/ZfcDatagridTest/DataSource/Doctrine2/Mocks/EntityManagerMock.php new file mode 100644 index 00000000..8d18c38a --- /dev/null +++ b/tests/ZfcDatagridTest/DataSource/Doctrine2/Mocks/EntityManagerMock.php @@ -0,0 +1,101 @@ +. + */ + +namespace ZfcDatagridTest\DataSource\Doctrine2\Mocks; + +use Doctrine\ORM\Proxy\ProxyFactory; + +/** + * Special EntityManager mock used for testing purposes. + */ +class EntityManagerMock extends \Doctrine\ORM\EntityManager +{ + /** + * @var \Doctrine\ORM\UnitOfWork|null + */ + private $_uowMock; + + /** + * @var \Doctrine\ORM\Proxy\ProxyFactory|null + */ + private $_proxyFactoryMock; + + /** + * {@inheritdoc} + */ + public function getUnitOfWork() + { + return isset($this->_uowMock) ? $this->_uowMock : parent::getUnitOfWork(); + } + + /* Mock API */ + + /** + * Sets a (mock) UnitOfWork that will be returned when getUnitOfWork() is called. + * + * @param \Doctrine\ORM\UnitOfWork $uow + * + * @return void + */ + public function setUnitOfWork($uow) + { + $this->_uowMock = $uow; + } + + /** + * @param \Doctrine\ORM\Proxy\ProxyFactory $proxyFactory + * + * @return void + */ + public function setProxyFactory($proxyFactory) + { + $this->_proxyFactoryMock = $proxyFactory; + } + + /** + * @return \Doctrine\ORM\Proxy\ProxyFactory + */ + public function getProxyFactory() + { + return isset($this->_proxyFactoryMock) ? $this->_proxyFactoryMock : parent::getProxyFactory(); + } + + /** + * Mock factory method to create an EntityManager. + * + * {@inheritdoc} + */ + public static function create($conn, \Doctrine\ORM\Configuration $config = null, + \Doctrine\Common\EventManager $eventManager = null) + { + if (is_null($config)) { + $config = new \Doctrine\ORM\Configuration(); + $config->setProxyDir(__DIR__ . '/../Proxies'); + $config->setProxyNamespace('Doctrine\Tests\Proxies'); + $config->setMetadataDriverImpl($config->newDefaultAnnotationDriver(array(), true)); + } + if (is_null($eventManager)) { + $eventManager = new \Doctrine\Common\EventManager(); + } + + return new EntityManagerMock($conn, $config, $eventManager); + } +} diff --git a/tests/ZfcDatagridTest/DataSource/Doctrine2Test.php b/tests/ZfcDatagridTest/DataSource/Doctrine2Test.php new file mode 100644 index 00000000..47c7fc0c --- /dev/null +++ b/tests/ZfcDatagridTest/DataSource/Doctrine2Test.php @@ -0,0 +1,29 @@ +em->createQueryBuilder(); + + $source = new Doctrine2($data); + + $this->assertInstanceOf('Doctrine\ORM\QueryBuilder', $source->getData()); + $this->assertSame($data, $source->getData()); + + $this->setExpectedException('InvalidArgumentException'); + $source = new Doctrine2(new \stdClass('something')); + + } +} \ No newline at end of file diff --git a/tests/ZfcDatagridTest/DataSource/ZendSelectTest.php b/tests/ZfcDatagridTest/DataSource/ZendSelectTest.php index 2faea501..ceed3766 100644 --- a/tests/ZfcDatagridTest/DataSource/ZendSelectTest.php +++ b/tests/ZfcDatagridTest/DataSource/ZendSelectTest.php @@ -1,6 +1,11 @@ addFilter($filter); $source->execute(); -// $this->assertEquals(2, $source->getPaginatorAdapter() -// ->count()); + // $this->assertEquals(2, $source->getPaginatorAdapter() + // ->count()); } } diff --git a/tests/ZfcDatagridTest/PrepareDataTest.php b/tests/ZfcDatagridTest/PrepareDataTest.php index 4f868c93..bbf0eca7 100644 --- a/tests/ZfcDatagridTest/PrepareDataTest.php +++ b/tests/ZfcDatagridTest/PrepareDataTest.php @@ -184,6 +184,31 @@ public function testPrepareReplace() $this->assertEquals($data, $prepare->getData()); } + public function testPrepareReplaceEmpty() + { + $data = $this->data; + + $col2 = clone $this->col2; + $col2->setReplaceValues(array( + 'y' => 'yes', + ), false); + $prepare = new PrepareData($data, array( + $this->colId, + $this->col1, + $col2 + )); + + $data[0]['idConcated'] = '1'; + $data[1]['idConcated'] = '2'; + $data[2]['idConcated'] = '3'; + + $data[0]['col2'] = 'n'; + $data[1]['col2'] = ''; + $data[2]['col2'] = 'yes'; + + $this->assertEquals($data, $prepare->getData()); + } + public function testPrepareReplaceTranslate() { $data = $this->data; @@ -277,18 +302,18 @@ public function testPrepareReplaceTranslateArray() $data[1]['col2'] = ''; $data[0]['col3'] = array( - 'Tag 1', //replaced - 'Tag 2' //translated - ); + 'Tag 1', // replaced + 'Tag 2' // translated + ); $data[1]['col3'] = array( - 'tag3', - 'Tag 1' //translated - ); + 'tag3', + 'Tag 1' // translated + ); $data[2]['col3'] = array( - 'Tag 2', //replaced - 'tag5' + 'Tag 2', // replaced + 'tag5' ); $this->assertEquals($data, $prepare->getData());