Skip to content

Commit

Permalink
Doctrine2 source start testing...
Browse files Browse the repository at this point in the history
  • Loading branch information
ThaDafinser committed Aug 28, 2013
1 parent 341ce90 commit 51385c9
Show file tree
Hide file tree
Showing 10 changed files with 773 additions and 17 deletions.
18 changes: 18 additions & 0 deletions src/ZfcDatagrid/Column/AbstractColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -428,6 +434,10 @@ public function setReplaceValues (array $values, $notReplacedGetEmpty = true)
$this->setFilterSelectOptions($values);
}

/**
*
* @return boolean
*/
public function hasReplaceValues ()
{
if (count($this->replaceValues) > 0)
Expand All @@ -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;
Expand Down
20 changes: 13 additions & 7 deletions src/ZfcDatagrid/DataSource/Doctrine2.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;

Expand Down Expand Up @@ -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();

Expand Down
151 changes: 151 additions & 0 deletions tests/ZfcDatagridTest/DataSource/Doctrine2/AbstractDoctrine2Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?php
namespace ZfcDatagridTest\DataSource\Doctrine2;

use PHPUnit_Framework_TestCase;
use ZfcDatagrid\Filter;
use ZfcDatagridTest\DataSource\DataSourceTestCase;

/**
* @copyright goes to: https://github.com/doctrine/doctrine2/blob/master/tests/Doctrine/Tests/OrmTestCase.php
*
* @group DataSource
* @covers ZfcDatagrid\DataSource\Doctrine2
*/
abstract class AbstractDoctrine2Test extends DataSourceTestCase
{

/**
* The metadata cache that is shared between all ORM tests (except functional tests).
*
* @var \Doctrine\Common\Cache\Cache null
*/
private static $_metadataCacheImpl = null;

/**
* The query cache that is shared between all ORM tests (except functional tests).
*
* @var \Doctrine\Common\Cache\Cache null
*/
private static $_queryCacheImpl = null;

/**
* @var \Doctrine\ORM\EntityManager
*/
protected $em;


/**
*
* @param array $paths
* @param mixed $alias
*
* @return \Doctrine\ORM\Mapping\Driver\AnnotationDriver
*/
protected function createAnnotationDriver($paths = array(), $alias = null)
{
if (version_compare(\Doctrine\Common\Version::VERSION, '3.0.0', '>=')) {
$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();
}

}
Loading

0 comments on commit 51385c9

Please sign in to comment.