public function testToString() { $sort = new Sort(); $expected = 'id-asc'; $sort->setField('id')->setDirection('asc'); $this->assertSame($expected, $sort->toString()); }
public function testCheckPass() { $supplied = array('name-asc'); $sort = new Sort(); $sort->setField('name')->setDirection('asc'); $expected = array($sort); $requirements = array('fields' => array('name')); $actual = SortType::check($supplied, $requirements); // Compare actual and existing params $this->assertInternalType('array', $actual); $this->assertInstanceOf(get_class($sort), $actual[0]); $this->assertEquals($expected, $actual); }
/** * Checks if the value is of a given type and * that the value passes requirements specified. * * @param mixed $value Value to be checked * @param array $requirements Additional constraints * @return mixed Cleared value * @throws InvalidDataTypeException */ public static function check($value, array $requirements = array()) { if (!isset($requirements['fields'])) { $error = 'allowable list of fields constraint has not been specified for a sort'; throw new InvalidDataTypeException($error); } if (!is_array($value)) { $error = 'value must be a list of sorts'; throw new InvalidDataTypeException($error); } // reserve space for cleared sorts $sorts = array(); // Iterate through the list of sorts and check each sort individually foreach ($value as $i => $sort) { // Detect sort settings if (!is_string($sort)) { $error = 'value for index ' . $i . ' must be string in format of' . ' {field}-{direction}' . ' Example: id-asc, name-desc'; throw new InvalidDataValueException($error); } // Get sort setting from string // All sorts should follow standard pattern: // {field}-{direction} // Example: id-desc $parts = explode('-', $sort, 2); if (!(count($parts) === 2) || !in_array($parts[0], $requirements['fields'])) { // Sort pattern does not match standard $error = 'value for index ' . $i . ', and part 1 (field) must be one of ' . '(' . implode(', ', $requirements['fields']) . ')' . ', and part 2 (direction) must be one of ' . '(' . implode(', ', Sort::$sortDirections) . ')'; throw new InvalidDataValueException($error); } $sort = new Sort(); try { $sort->setField($parts[0])->setDirection($parts[1]); $sorts[] = $sort; } catch (InvalidArgumentException $e) { $error = 'value for sort index ' . $i . ', ' . 'part 2 (direction) must be one of (' . implode(', ', Sort::$sortDirections) . ')'; throw new InvalidDataValueException($error); } } return $sorts; }
public function getSortSqlPassProvider() { $data = array(); $sorts = array(); $sort1 = new Sort(); $sort1->setField('name'); $sort2 = new Sort(); $sort2->setField('price')->setDirection('DESC'); $expected = 'ORDER BY `name` ASC,`price` DESC'; $data[] = array(array($sort1, $sort2), array(), $expected); return $data; }
public function expandSimpleQueryProvider() { // test WHERE and SORT $data = array(); $options = array(); $query = new Query(); $sql = 'SELECT * FROM `products` '; $expectedSql = 'SELECT * FROM `products` WHERE (`name` LIKE CONCAT("%", :0_filter_0, "%") COLLATE utf8_general_ci OR `name` LIKE CONCAT("%", :0_filter_1, "%") COLLATE utf8_general_ci AND `price` > :0_filter_2) GROUP BY `name`,`price` ORDER BY `name` ASC,`price` DESC'; $expectedParams = array('0_filter_0' => 'galaxy', '0_filter_1' => '4s', '0_filter_2' => '100'); $query->setStatement($sql); $expected = new Query(); $expected->setStatement($expectedSql)->setParameters($expectedParams); $sort1 = new Sort(); $sort1->setField('name'); $sort2 = new Sort(); $sort2->setField('price')->setDirection('DESC'); $filterA = new Filter(); $criterions = array(FilterType::criteriaToCriterion('and-name-inci-value-galaxy'), FilterType::criteriaToCriterion('or-name-inci-value-4s'), FilterType::criteriaToCriterion('and-price-gt-value-100')); $filterA->setCriterions($criterions); $options = array('sort' => array($sort1, $sort2), 'group' => array('name', 'price'), 'filter' => array($filterA)); $data[] = array($query, $options, $expected); // test HAVING $query = new Query(); $query->setStatement($sql); $sql = 'SELECT * FROM `products` '; $expectedSql = 'SELECT * FROM `products` HAVING (`name` = CAST(`price` AS CHAR) COLLATE utf8_bin OR `clicks` > `price`)'; $expected = new Query(); $expected->setStatement($expectedSql); $fieldMap = array('name' => 'having'); $filterA = new Filter(); $criterions = array(FilterType::criteriaToCriterion('and-name-eq-field-price'), FilterType::criteriaToCriterion('or-clicks-gt-field-price')); $filterA->setCriterions($criterions); $options = array('filter' => array($filterA)); $data[] = array($query, $options, $expected, $fieldMap); // test * in field map $query = new Query(); $query->setStatement($sql); $sql = 'SELECT * FROM `products` '; $expectedSql = 'SELECT * FROM `products` HAVING (`name` = CAST(`price` AS CHAR) COLLATE utf8_bin OR `clicks` > `price`)'; $expected = new Query(); $expected->setStatement($expectedSql); $fieldMap = array('*' => 'having'); $filterA = new Filter(); $criterions = array(FilterType::criteriaToCriterion('and-name-eq-field-price'), FilterType::criteriaToCriterion('or-clicks-gt-field-price')); $filterA->setCriterions($criterions); $options = array('filter' => array($filterA)); $data[] = array($query, $options, $expected, $fieldMap); // test LIMIT and offset $query = new Query(); $query->setStatement($sql); $sql = 'SELECT * FROM `products` '; $expectedSql = 'SELECT * FROM `products` WHERE (`name` = CAST(`price` AS CHAR) COLLATE utf8_bin OR `clicks` > `price`) LIMIT 10,20'; $expected = new Query(); $expected->setStatement($expectedSql); $filterA = new Filter(); $criterions = array(FilterType::criteriaToCriterion('and-name-eq-field-price'), FilterType::criteriaToCriterion('or-clicks-gt-field-price')); $filterA->setCriterions($criterions); $options = array('filter' => array($filterA), 'limit' => 20, 'offset' => 10); $data[] = array($query, $options, $expected); // test Single Filter $sql = 'SELECT * FROM `orders` '; $querySingle = new Query(); $querySingle->setStatement($sql); $expectedSql = 'SELECT * FROM `orders` WHERE (`id` > :0_filter_0 AND `id` >= :0_filter_1) LIMIT 2,100'; $expectedParams = array('0_filter_0' => '1', '0_filter_1' => '2'); $expected = new Query(); $expected->setStatement($expectedSql)->setParameters($expectedParams); $filterSingle = new Filter(); $criterions = array(FilterType::criteriaToCriterion('and-id-gt-value-1'), FilterType::criteriaToCriterion('and-id-ge-value-2')); $filterSingle->setCriterions($criterions); $options = array('filter' => $filterSingle, 'limit' => 100, 'offset' => 2); $data[] = array($querySingle, $options, $expected); return $data; }