public function testDoctrineAdapter() { $extension = new KnpPaginatorExtension(); $this->container->registerExtension($extension); $this->container->addCompilerPass(new PaginatorConfigurationPass()); $extension->load(array(array()), $this->container); $container = $this->getDumpedContainer(); BaseTestCase::assertSaneContainer($container); $adapter = $container->get('knp_paginator.adapter'); $meta = $this->dm->getClassMetadata(self::FIXTURE_ARTICLE); $qb = $this->dm->createQueryBuilder($meta->name); $qb->sort('title', 'ASC'); $query = $qb->getQuery(); $adapter->setQuery($query); $this->assertEquals(6, $adapter->count()); $items = $adapter->getItems(2, 2); // second page, showing 2 items per page $this->assertEquals(2, count($items)); $item = current($items); $this->assertEquals('Sport', $item->getTitle()); $item = next($items); $this->assertEquals('Spring', $item->getTitle()); }
/** * @dataProvider orderFieldWhitelistProvider */ public function testOrderFieldsWhitelist($orderField, $fieldGetter, $whitelist) { $extension = $this->createExtension(); $container = $this->getDumpedContainer(); BaseTestCase::assertSaneContainer($container); $adapter = $container->get('knp_paginator.adapter'); $meta = $this->em->getClassMetadata(self::FIXTURE_ARTICLE); $query = $this->em->createQuery("SELECT a FROM {$meta->name} a"); if ($whitelist) { $query->setHint(OrderByWalker::HINT_PAGINATOR_SORT_FIELDS_WHITELIST, $whitelist); } $adapter->setQuery($query); $request = $container->get('request'); $request->query->set('sort', $orderField); $request->query->set('direction', 'desc'); $articles = $adapter->getItems(0, 100); if ($fieldGetter !== false) { $actualOrder = array(); foreach ($articles as $article) { $actualOrder[] = $article->{$fieldGetter}(); } $expectedOrder = $actualOrder; rsort($expectedOrder); $this->assertTrue($expectedOrder === $actualOrder); } else { //ok, exception has not been thrown in order to unknown dql query part } }