From f304c8970c49ff202eeb87ecad9b4c74f0d3e639 Mon Sep 17 00:00:00 2001 From: Martin Keckeis Date: Sun, 18 Aug 2013 21:42:09 +0200 Subject: [PATCH] First tests for ZendSelect * Split of ZendSelect * UnitTests --- src/ZfcDatagrid/DataSource/PhpArray.php | 18 --- .../DataSource/PhpArray/Filter.php | 25 ++- src/ZfcDatagrid/DataSource/ZendSelect.php | 88 +--------- .../DataSource/ZendSelect/Filter.php | 146 +++++++++++++++++ .../DataSource/ZendSelect/FilterTest.php | 151 ++++++++++++++++++ 5 files changed, 318 insertions(+), 110 deletions(-) create mode 100644 src/ZfcDatagrid/DataSource/ZendSelect/Filter.php create mode 100644 tests/ZfcDatagridTest/DataSource/ZendSelect/FilterTest.php diff --git a/src/ZfcDatagrid/DataSource/PhpArray.php b/src/ZfcDatagrid/DataSource/PhpArray.php index 44fc5729..dcc3e40c 100644 --- a/src/ZfcDatagrid/DataSource/PhpArray.php +++ b/src/ZfcDatagrid/DataSource/PhpArray.php @@ -50,24 +50,6 @@ public function execute() $data = $this->sortArrayMultiple($data, $this->getSortConditions()); } - // if (count($this->sortConditions) > 0) { - // $sortConditions = $this->sortConditions; - - // // @todo UGLY SOLUTION!!! - // if (count($sortConditions) === 1) { - // $sort1 = $this->getSortArrayParameter($sortConditions[0]); - - // $data = $this->sortArrayMultiple($data, $sort1[0], $sort1[1]); - // } elseif (count($sortConditions) === 2) { - // $sort1 = $this->getSortArrayParameter($sortConditions[0]); - // $sort2 = $this->getSortArrayParameter($sortConditions[1]); - - // $data = $this->sortArrayMultiple($data, $sort1[0], $sort1[1], $sort2[0], $sort2[1]); - // } else { - // throw new \Exception('Too much column sorts defined!'); - // } - // } - /** * Step 2) Apply filters */ diff --git a/src/ZfcDatagrid/DataSource/PhpArray/Filter.php b/src/ZfcDatagrid/DataSource/PhpArray/Filter.php index 1ba0b985..01e40da4 100644 --- a/src/ZfcDatagrid/DataSource/PhpArray/Filter.php +++ b/src/ZfcDatagrid/DataSource/PhpArray/Filter.php @@ -13,17 +13,16 @@ class Filter private $filter; /** - * - * @param string $uniqueId - * @param array $valuesToFilter + * + * @param \ZfcDatagrid\Filter $filter */ - public function __construct(\ZfcDatagrid\Filter $filter) + public function __construct(DatagridFilter $filter) { $this->filter = $filter; } /** - * + * * @return \ZfcDatagrid\Filter */ public function getFilter() @@ -31,7 +30,12 @@ public function getFilter() return $this->filter; } - private function getRowValue($row) + /** + * + * @param array $row + * @return Ambigous + */ + private function getRowValue(array $row) { $rowValue = $row[$this->getFilter() ->getColumn() @@ -44,7 +48,14 @@ private function getRowValue($row) return $rowValue; } - public function applyFilter($row) + /** + * Does the value get filtered? + * + * @param array $row + * @throws \Exception + * @return boolean + */ + public function applyFilter(array $row) { $return = false; diff --git a/src/ZfcDatagrid/DataSource/ZendSelect.php b/src/ZfcDatagrid/DataSource/ZendSelect.php index 2b5d534c..337235bf 100644 --- a/src/ZfcDatagrid/DataSource/ZendSelect.php +++ b/src/ZfcDatagrid/DataSource/ZendSelect.php @@ -109,99 +109,17 @@ public function execute() } } - $adapter = $this->sqlObject->getAdapter(); - $qi = function ($name) use($adapter) - { - return $adapter->getPlatform()->quoteIdentifier($name); - }; - $qv = function ($value) use($adapter) - { - return $adapter->getPlatform()->quoteValue($value); - }; - $fp = function ($name) use($adapter) - { - return $adapter->getDriver()->formatParameterName($name); - }; - /** * Step 3) Apply filters */ + $filterColumn = new ZendSelect\Filter($this->getAdapter(), $select); foreach ($this->getFilters() as $filter) { + /* @var $filter \ZfcDatagrid\Filter */ if ($filter->isColumnFilter() === true) { + $filterColumn->applyFilter($filter); - $column = $filter->getColumn(); - $colString = $column->getUniqueId(); - $values = $filter->getValues(); - foreach ($values as $value) { - switch ($filter->getOperator()) { - - case Filter::LIKE: - $select->where->like($colString, '%' . $value . '%'); - break; - - case Filter::LIKE_LEFT: - $select->where->like($colString, '%' . $value); - break; - - case Filter::LIKE_RIGHT: - $select->where->like($colString, $value . '%'); - break; - - case Filter::NOT_LIKE: - $select->where->literal($qi($colString) . ' NOT LIKE ?', array( - '%' . $value . '%' - )); - break; - - case Filter::NOT_LIKE_LEFT: - $select->where->literal($qi($colString) . 'NOT LIKE ?', array( - '%' . $value - )); - break; - - case Filter::NOT_LIKE_RIGHT: - $select->where->literal($qi($colString) . 'NOT LIKE ?', array( - $value . '%' - )); - break; - - case Filter::EQUAL: - $select->where->equalTo($colString, $value); - break; - - case Filter::NOT_EQUAL: - $select->where->notEqualTo($colString, $value); - break; - - case Filter::GREATER_EQUAL: - $select->where->greaterThanOrEqualTo($colString, $value); - break; - - case Filter::GREATER: - $select->where->greaterThan($colString, $value); - break; - - case Filter::LESS_EQUAL: - $select->where->lessThanOrEqualTo($colString, $value); - break; - - case Filter::LESS: - $select->where->lessThan($colString, $value); - break; - - case Filter::BETWEEN: - $min = min($values); - $max = max($values); - $select->where->between($colString, $min, $max); - break; - - default: - throw new \Exception('This operator is currently not supported: ' . $filter->getOperator()); - break; - } - } } } diff --git a/src/ZfcDatagrid/DataSource/ZendSelect/Filter.php b/src/ZfcDatagrid/DataSource/ZendSelect/Filter.php new file mode 100644 index 00000000..20d5a19c --- /dev/null +++ b/src/ZfcDatagrid/DataSource/ZendSelect/Filter.php @@ -0,0 +1,146 @@ +sql = $sql; + $this->select = $select; + } + + /** + * + * @return \Zend\Db\Sql\Sql + */ + public function getSql() + { + return $this->sql; + } + + /** + * + * @return \Zend\Db\Sql\Select + */ + public function getSelect() + { + return $this->select; + } + + /** + * + * @param DatagridFilter $filter + */ + public function applyFilter(DatagridFilter $filter) + { + $select = $this->getSelect(); + + $adapter = $this->getSql()->getAdapter(); + $qi = function ($name) use($adapter) + { + return $adapter->getPlatform()->quoteIdentifier($name); + }; + $qv = function ($value) use($adapter) + { + return $adapter->getPlatform()->quoteValue($value); + }; + + $wheres = array(); + + $colString = $filter->getColumn()->getUniqueId(); + $values = $filter->getValues(); + foreach ($values as $value) { + $where = new Where(); + + switch ($filter->getOperator()) { + + case DatagridFilter::LIKE: + $wheres[] = $where->like($colString, '%' . $value . '%'); + break; + + case DatagridFilter::LIKE_LEFT: + $wheres[] = $where->like($colString, '%' . $value); + break; + + case DatagridFilter::LIKE_RIGHT: + $wheres[] = $where->like($colString, $value . '%'); + break; + + case DatagridFilter::NOT_LIKE: + $wheres[] = $where->literal($qi($colString) . ' NOT LIKE ?', array( + '%' . $value . '%' + )); + break; + + case DatagridFilter::NOT_LIKE_LEFT: + $wheres[] = $where->literal($qi($colString) . 'NOT LIKE ?', array( + '%' . $value + )); + break; + + case DatagridFilter::NOT_LIKE_RIGHT: + $wheres[] = $where->literal($qi($colString) . 'NOT LIKE ?', array( + $value . '%' + )); + break; + + case DatagridFilter::EQUAL: + $wheres[] = $where->equalTo($colString, $value); + break; + + case DatagridFilter::NOT_EQUAL: + $wheres[] = $where->notEqualTo($colString, $value); + break; + + case DatagridFilter::GREATER_EQUAL: + $wheres[] = $where->greaterThanOrEqualTo($colString, $value); + break; + + case DatagridFilter::GREATER: + $wheres[] = $where->greaterThan($colString, $value); + break; + + case DatagridFilter::LESS_EQUAL: + $wheres[] = $where->lessThanOrEqualTo($colString, $value); + break; + + case DatagridFilter::LESS: + $wheres[] = $where->lessThan($colString, $value); + break; + + case DatagridFilter::BETWEEN: + $min = min($values); + $max = max($values); + $wheres[] = $where->between($colString, $min, $max); + break 2; + + default: + throw new \Exception('This operator is currently not supported: ' . $filter->getOperator()); + break; + } + } + + $set = new PredicateSet($wheres, PredicateSet::OP_OR); + + $select->where->andPredicate($set); + } +} diff --git a/tests/ZfcDatagridTest/DataSource/ZendSelect/FilterTest.php b/tests/ZfcDatagridTest/DataSource/ZendSelect/FilterTest.php new file mode 100644 index 00000000..8bf56bef --- /dev/null +++ b/tests/ZfcDatagridTest/DataSource/ZendSelect/FilterTest.php @@ -0,0 +1,151 @@ +column = $this->getMockForAbstractClass('ZfcDatagrid\Column\AbstractColumn'); + $this->column->setUniqueId('myCol'); + + $this->column2 = $this->getMockForAbstractClass('ZfcDatagrid\Column\AbstractColumn'); + $this->column2->setUniqueId('myCol2'); + + $this->mockDriver = $this->getMock('Zend\Db\Adapter\Driver\DriverInterface'); + $this->mockConnection = $this->getMock('Zend\Db\Adapter\Driver\ConnectionInterface'); + $this->mockDriver->expects($this->any()) + ->method('checkEnvironment') + ->will($this->returnValue(true)); + $this->mockDriver->expects($this->any()) + ->method('getConnection') + ->will($this->returnValue($this->mockConnection)); + $this->mockPlatform = $this->getMock('Zend\Db\Adapter\Platform\PlatformInterface'); + $this->mockStatement = $this->getMock('Zend\Db\Adapter\Driver\StatementInterface'); + $this->mockDriver->expects($this->any()) + ->method('createStatement') + ->will($this->returnValue($this->mockStatement)); + + $this->adapter = new Adapter($this->mockDriver, $this->mockPlatform); + + $sql = new Sql($this->adapter, 'foo'); + + $select = new Select('myTable'); + $select->columns(array( + 'myCol', + 'myCol2' + )); + + $this->filterSelect = new FilterSelect($sql, $select); + } + + public function testBasic() + { + $this->assertInstanceOf('Zend\Db\Sql\Select', $this->filterSelect->getSelect()); + $this->assertInstanceOf('Zend\Db\Sql\Sql', $this->filterSelect->getSql()); + + //Test two filters + $filter = new \ZfcDatagrid\Filter(); + $filter->setFromColumn($this->column, '~myValue,123'); + + $filter2 = new \ZfcDatagrid\Filter(); + $filter2->setFromColumn($this->column2, '~myValue,123'); + + $filterSelect = clone $this->filterSelect; + $filterSelect->applyFilter($filter); + $filterSelect->applyFilter($filter2); + + $select = $filterSelect->getSelect(); + /* @var $where \Zend\Db\Sql\Where */ + $where = $select->getRawState('where'); + + $predicates = $where->getPredicates(); + $this->assertEquals(2, count($predicates)); + } + + public function testLike() + { + $filter = new \ZfcDatagrid\Filter(); + $filter->setFromColumn($this->column, '~myValue,123'); + + $filterSelect = clone $this->filterSelect; + $filterSelect->applyFilter($filter); + + $select = $filterSelect->getSelect(); + /* @var $where \Zend\Db\Sql\Where */ + $where = $select->getRawState('where'); + + $predicates = $where->getPredicates(); + $this->assertEquals(1, count($predicates)); + + //First nesting + $this->assertEquals(2, count($predicates[0])); + + /* @var $predicateSet \Zend\Db\Sql\Predicate\PredicateSet */ + $predicateSet = $predicates[0][1]; + + $where = $predicateSet->getPredicates()[0][1]; + $like = $where->getPredicates()[0][1]; + $this->assertInstanceOf('Zend\Db\Sql\Predicate\Like', $like); + $this->assertEquals('%myValue%', $like->getLike()); + + $where = $predicateSet->getPredicates()[1][1]; + $like = $where->getPredicates()[0][1]; + $this->assertInstanceOf('Zend\Db\Sql\Predicate\Like', $like); + $this->assertEquals('%123%', $like->getLike()); + } + + public function testLikeLeft() + { + $filter = new \ZfcDatagrid\Filter(); + $filter->setFromColumn($this->column, '~%myValue,123'); + + $filterSelect = clone $this->filterSelect; + $filterSelect->applyFilter($filter); + + $select = $filterSelect->getSelect(); + /* @var $where \Zend\Db\Sql\Where */ + $where = $select->getRawState('where'); + + $predicates = $where->getPredicates(); + + //First nesting + $this->assertEquals(2, count($predicates[0])); + + /* @var $predicateSet \Zend\Db\Sql\Predicate\PredicateSet */ + $predicateSet = $predicates[0][1]; + + $where = $predicateSet->getPredicates()[0][1]; + $like = $where->getPredicates()[0][1]; + $this->assertInstanceOf('Zend\Db\Sql\Predicate\Like', $like); + } +}