Пример #1
0
 public function testEventFilters()
 {
     $inputData = array('where' => array('field' => 1, 'field2' => 2, 'field3' => array('field3' => 3, 'field4' => 4), 'field3a' => array(3, 4), 'and' => array('field2' => 123, 'field3' => 321, 'Lt' => array('field5' => array('asd', 1, 2, 3), 'field6' => 1, 'field7' => 8), 'Gt' => array('field8' => array('asd', 1, 2, 3), 'field9' => 3, 'field10' => 'ololo')), 'or' => array('field2' => 00, 'field3' => 111, 'Gt' => array('field8' => array('asd', 1, 2, 3), 'field9' => 3, 'field10' => 'ololo'), 'nEq' => array('field11' => array('asd', 1, 2, 3), 'field12' => 'asd'), 'like' => array('field14' => 'OlOlo')), 'some_piece_of_shit' => array('field4' => 123)), 'limit' => 100520, 'skip' => 45, 'order' => array('field100' => 'AsC', 'field200' => 'DeSc', 'field300' => 'ololo'), 'some_crap' => array('asd' => array('dsfkjldflkjdf' => 1133), 'qwe' => 1133, 'zzzz' => 'dlcvlkj'));
     $eventDispatcher = new EventDispatcher();
     $eventDispatcher->addListener(GroupEvent::EVENT_NAME, function (GroupEvent $event) {
         if ($event->getGroupName() === FilterCondition::CONDITION_AND) {
             throw new InvalidConditionException();
         }
     });
     $eventDispatcher->addListener(LimitEvent::EVENT_NAME, function (LimitEvent $event) {
         $event->getLimitBuilder()->setLimit(12345);
     });
     $eventDispatcher->addListener(OffsetEvent::EVENT_NAME, function (OffsetEvent $event) {
         $event->getOffsetBuilder()->setOffset(54321);
     });
     $eventDispatcher->addListener(OrderEvent::EVENT_NAME, function (OrderEvent $event) {
         if ($event->getField() === 'field100') {
             throw new InvalidConditionException();
         }
     });
     $eventDispatcher->addListener(FieldEvent::EVENT_NAME, function (FieldEvent $event) {
         if (!in_array($event->getField(), array('field200', 'field3', 'field15'))) {
             throw new InvalidConditionException();
         }
     });
     $eventDispatcher->addListener(ConditionEvent::EVENT_NAME, function (ConditionEvent $event) {
         $condition = $event->getCondition();
         if ($condition->getOperator() === FilterCondition::CONDITION_LIKE) {
             $value = mb_strtolower($condition->getValue());
             $condition->setValue($value);
         }
     });
     $builder = new Builder($eventDispatcher);
     $builder->build($inputData);
     $this->assertCount(1, $builder->getFilters());
     $filters = $builder->getFilters();
     $this->assertEquals('or', $filters[0]->getGroup());
     /* @var $expected ConditionInterface[] */
     $expected = array(FilterConditionBuilder::create('eq', 'field3', 111, $eventDispatcher), FilterConditionBuilder::create('like', 'field15', 'ololo', $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());
     }
     $this->assertEquals(12345, $builder->getLimit());
     $this->assertEquals(54321, $builder->getOffset());
     $this->assertEquals(array(new Order('field200', 'desc')), $builder->getOrders());
 }
Пример #2
0
 public function testComplex()
 {
     $inputData = ['where' => ['field' => 1, 'field2' => 2, 'field3' => ['field3' => 3, 'field4' => 4], 'field3a' => [3, 4], 'and' => ['field2' => 123, 'field3' => 321, 'field5' => ['Lt' => ['asd', 1, 2, 3]], 'field6' => ['Lt' => 1], 'field7' => ['Lt' => 8], 'field8' => ['Gt' => ['asd', 1, 2, 3]], 'field9' => ['Gt' => 3], 'field10' => ['Gt' => 'ololo']], 'or' => ['field2' => 00, 'field3' => 111, 'field8' => ['Gt' => ['asd', 1, 2, 3]], 'field9' => ['Gt' => 3], 'field10' => ['Gt' => 'ololo'], 'field11' => ['nEq' => ['asd', 1, 2, 3]], 'field12' => ['nEq' => 'asd']], 'like' => ['field15' => 'ololo', 'field16' => 'pewpew'], 'some_piece_of_shit' => ['field4' => 123]], 'limit' => 100520, 'skip' => 45, 'order' => ['field100' => 'AsC', 'field200' => 'DeSc', 'field300' => 'ololo'], 'some_crap' => ['asd' => ['dsfkjldflkjdf' => 1133], 'qwe' => 1133, 'zzzz' => 'dlcvlkj']];
     $builder = new Builder();
     $builder->build($inputData);
     $this->assertEquals(45, $builder->getOffset());
     $this->assertEquals(100520, $builder->getLimit());
     $expectOrders = [new Order('field100', 'asc'), new Order('field200', 'desc')];
     $this->assertEquals($expectOrders, $builder->getOrders());
     /* @var $expectFilters FilterGroupConditionBuilder[] */
     $expectFilters = [FilterGroupConditionBuilder::create('and', ['field' => 1]), FilterGroupConditionBuilder::create('and', ['field2' => 2]), FilterGroupConditionBuilder::create('and', ['field3a' => [3, 4]]), FilterGroupConditionBuilder::create('and', ['field2' => 123, 'field3' => 321, 'field6' => ['lt' => 1], 'field7' => ['lt' => 8], 'field9' => ['gt' => 3]]), FilterGroupConditionBuilder::create('or', ['field2' => 00, 'field3' => 111, 'field9' => ['gt' => 3], 'field11' => ['neq' => ['asd', 1, 2, 3]], 'field12' => ['neq' => 'asd']]), FilterGroupConditionBuilder::create('and', ['field15' => ['like' => 'ololo'], 'field16' => ['like' => 'pewpew']])];
     foreach ($builder->getFilters() as $key => $builder) {
         $this->assertEquals($expectFilters[$key]->getGroup(), $builder->getGroup());
         $this->assertEquals($expectFilters[$key]->getConditions(), $builder->getConditions());
     }
 }