public function testSwitchParameters() { $input = array('where' => array('foo' => 'bar')); $eventDispatcher = new EventDispatcher(); $eventDispatcher->addListener(OperatorEvent::EVENT_NAME, function (OperatorEvent $event) { if ($event->getField() === 'foo') { $event->setField('pew-pew'); if ($event->getValue() === 'bar') { $value = [1, 2, 3, 4]; } else { $value = [5, 6, 7, 8]; } $event->setValue($value); } }); $builder = new Builder($eventDispatcher); $builder->build($input); $this->assertCount(1, $builder->getFilters()); $filters = $builder->getFilters(); $this->assertEquals('and', $filters[0]->getGroup()); /* @var $expected ConditionInterface[] */ $expected = array(FilterConditionBuilder::create(FilterCondition::CONDITION_IN, 'pew-pew', [1, 2, 3, 4], $eventDispatcher)); foreach ($filters[0]->getConditions() as $key => $condition) { $this->assertEquals($expected[$key]->getValue(), $condition->getValue()); $this->assertEquals($expected[$key]->getField(), $condition->getField()); $this->assertEquals($expected[$key]->getOperator(), $condition->getOperator()); } }
public function testElasticTransformer() { $inputQuery = ['where' => ['user_id' => ['nin' => [3]], 'product_id' => ['neq' => '27'], 'or' => ['details.id' => ['lte' => 27, 'gte' => 10], 'user_id' => ['in' => [1]]], 'description' => ['like' => 'My awe']], 'order' => ['user_id' => 'asc']]; $builder = new Builder(); $esBuilder = new ElasticSearchTransformer($builder); $result = $esBuilder->build()->getResult(); $this->assertEquals(['size' => 25, 'from' => 0], $result); $builder->build($inputQuery); $expect = ['from' => 0, 'size' => 25, 'body' => ['sort' => [['user_id' => 'asc']], 'query' => ['filtered' => ['filter' => ['bool' => ['must_not' => [['terms' => ['user_id' => [3]]], ['term' => ['product_id' => 27]]], 'should' => [['range' => ['details.id' => ['lte' => 27]]], ['range' => ['details.id' => ['gte' => 10]]]], 'must' => [['regexp' => ['description' => ['value' => 'My awe.*']]]]]]]]]]; $esBuilder = new ElasticSearchTransformer($builder); $result = $esBuilder->build()->getResult(); $this->assertEquals($expect, $result); }
public function testFieldNamedAsLikeFilters() { $inputData = json_decode('{"where":{"like":1}}', 1); $builder = new Builder(); $arrayFilters = $builder->build($inputData)->getFilters(); $this->assertEquals($arrayFilters[0]->getConditions(), [EqCondition::create('like', 1)]); $inputData = json_decode('{"where":{"like":{"like":1}}}', 1); $builder = new Builder(); $arrayFilters = $builder->build($inputData)->getFilters(); $this->assertEquals($arrayFilters[0]->getConditions(), [LikeCondition::create('like', 1)]); $inputData = json_decode('{"where":{"like":{"like":1}}}', 1); $builder = new Builder(); $arrayFilters = $builder->build($inputData)->getFilters(); $this->assertEquals($arrayFilters[0]->getConditions(), [LikeCondition::create('like', 1)]); }