public function testCheckPass() { $supplied = array('and-id-eq-value-12'); $criterion = new Criterion(); $criterion->setLogic('and')->setKey('id')->setOperand('eq')->setType('value')->setValue('12'); $filter = new Filter(); $filter->addCriterion($criterion); $expected = $filter; $requirements = array('fields' => array('id')); $actual = FilterType::check($supplied, $requirements); // Compare actual and existing params $this->assertInstanceOf(get_class($filter), $actual); $this->assertEquals($expected, $actual); }
public function testCheckFiltersWithOneFilterPass() { $supplied = array('and-id-eq-value-12'); $criterion = new Criterion(); $criterion->setLogic('and')->setKey('id')->setOperand('eq')->setType('value')->setValue('12'); $filterA = new Filter(); $filterA->addCriterion($criterion); $expected = array($filterA); $requirements = array('fields' => array('id')); $actual = PseudoTypes::checkFilters($supplied, $requirements); // Compare actual and existing params $this->assertInternalType('array', $actual); $this->assertInstanceOf(get_class($filterA), $actual[0]); $this->assertEquals($expected, $actual); }
public function testToStringFail() { $criterion = new Criterion(); $expected = 'and-make-eq-value-Audi'; $criterion->setLogic('and')->setKey('make')->setOperand('eq')->setType('value')->setValue('audi'); $this->assertNotSame($expected, $criterion->toString()); }
public function inNiniCriterionsProvider() { $inNiniValueAndCriterion = new Criterion(); $inNiniValueAndCriterion->setLogic('and')->setKey('foo')->setOperand('nini')->setType('value')->setValue('abc,def,xyz,123'); $inNiniValueAndClause = new Clause(); $inNiniValueAndClause->setStatement('AND `foo` NOT IN (:filter_0_0 COLLATE utf8_general_ci, :filter_0_1 COLLATE utf8_general_ci, :filter_0_2 COLLATE utf8_general_ci, :filter_0_3 COLLATE utf8_general_ci)')->setParameters(array('filter_0_0' => 'abc', 'filter_0_1' => 'def', 'filter_0_2' => 'xyz', 'filter_0_3' => '123')); $inNiniValueOrCriterion = new Criterion(); $inNiniValueOrCriterion->setLogic('or')->setKey('foo')->setOperand('nini')->setType('value')->setValue('abc,def,xyz,123'); $inNiniValueOrClause = new Clause(); $inNiniValueOrClause->setStatement('OR `foo` NOT IN (:filter_0_0 COLLATE utf8_general_ci, :filter_0_1 COLLATE utf8_general_ci, :filter_0_2 COLLATE utf8_general_ci, :filter_0_3 COLLATE utf8_general_ci)')->setParameters(array('filter_0_0' => 'abc', 'filter_0_1' => 'def', 'filter_0_2' => 'xyz', 'filter_0_3' => '123')); $inNiniTypeAndCriterion = new Criterion(); $inNiniTypeAndCriterion->setLogic('and')->setKey('foo')->setOperand('nini')->setType('field')->setValue('bar,loo,foo'); $inNiniTypeAndClause = new Clause(); $inNiniTypeAndClause->setStatement('AND `foo` NOT IN (`bar` COLLATE utf8_general_ci, `loo` COLLATE utf8_general_ci, `foo` COLLATE utf8_general_ci)'); $inNiniTypeOrCriterion = new Criterion(); $inNiniTypeOrCriterion->setLogic('or')->setKey('foo')->setOperand('nini')->setType('field')->setValue('bar,loo,foo'); $inNiniTypeOrClause = new Clause(); $inNiniTypeOrClause->setStatement('OR `foo` NOT IN (`bar` COLLATE utf8_general_ci, `loo` COLLATE utf8_general_ci, `foo` COLLATE utf8_general_ci)'); return array(array($inNiniValueAndCriterion, $inNiniValueAndClause), array($inNiniValueOrCriterion, $inNiniValueOrClause), array($inNiniTypeAndCriterion, $inNiniTypeAndClause), array($inNiniTypeOrCriterion, $inNiniTypeOrClause)); }
public static function criteriaToCriterion($filter, $requirements = array(), $index = 0) { // Detect filter settings if (!is_string($filter)) { $error = 'value for index ' . $index . ' must be a string in format of' . ' {logic}-{field}-{operand}-{type}-{value}' . ' Example: and-name-eq-value-smith'; throw new InvalidDataValueException($error); } // Get filter setting from string // All filters should follow standard pattern: // {logic}-{field}-{operand}-{type}-{value} // Example: and-id-eq-value-12 $parts = explode('-', $filter, 5); // Reserve space for error message in regards to fields $filedsPatternMessage = ''; // Check for fields prerequisites if defined if (isset($requirements['fields'])) { $filedsPatternMessage = ', part 2 (field) must be one of ' . '(' . implode(', ', $requirements['fields']) . ')'; if (!in_array($parts[1], $requirements['fields'])) { // Requirements miss match $error = 'value for index ' . $index . $filedsPatternMessage; throw new InvalidDataValueException($error); } } if (!(count($parts) === 5) || !in_array($parts[0], Criterion::$criterionLogic) || !in_array($parts[2], Criterion::$criterionOperands)) { // Filter pattern does not match standard $error = 'value for index ' . $index . ', and part 1 (logic) must be one of ' . '(' . implode(', ', Criterion::$criterionLogic) . ')' . $filedsPatternMessage . ', and part 3 (operand) must be one of ' . '(' . implode(', ', Criterion::$criterionOperands) . ')'; throw new InvalidDataValueException($error); } // Check value for boolean operand is one of true ore false if (in_array($parts[2], Criterion::getBoolOperands())) { // Now that we know that this is boolean operand // let's check value is one of boolean type if (!in_array(strtolower($parts[4]), Criterion::$criterionBooleanValues)) { $error = 'value for index ' . $index . ', and part 5 (value) must be one of ' . '(' . implode(', ', Criterion::$criterionBooleanValues) . ')' . ' when using boolean operand'; throw new InvalidDataValueException($error); } } $criterion = new Criterion(); try { $criterion->setLogic($parts[0])->setKey($parts[1])->setOperand($parts[2])->setType($parts[3])->setValue($parts[4]); } catch (InvalidArgumentException $e) { $error = 'value for filter index ' . $index . ', ' . 'part 4 (type) must be one of (field or value)'; throw new InvalidDataValueException($error); } return $criterion; }