public function testPreRenderWithPagination()
{
$posts = array();
for ($i = 1; $i <= 20; ++$i) {
$document = new Document();
$document->setTitle('Post #' . $i);
$posts[] = $document;
}
$document = new Document();
$document->setLayout('default');
$document->setPath('index.html');
$document->setBody('{{ render_documents(paginate(carew.posts, 4)) }}');
$this->getTwigGlobals()->fromArray(array('posts' => $posts));
$event = new CarewEvent(array($document));
$this->twigListenner->preRender($event);
$documents = $event->getSubject();
$this->assertCount(5, $documents);
$lis = array();
foreach (array_values($documents) as $key => $document) {
$page = $key + 1;
$path = 1 == $page ? 'index.html' : sprintf('index-page-%s.html', $page);
$this->assertSame($path, $document->getPath());
$crawler = new Crawler($document->getBody());
$this->assertCount(2, $crawler->filter('ul'));
$this->assertCount(4, $crawler->filter('ul')->eq(0)->filter('li'));
foreach ($crawler->filter('ul')->eq(0)->filter('li') as $li) {
$lis[] = trim($li->textContent);
}
$this->assertCount(5, $crawler->filter('ul')->eq(1)->filter('li'));
for ($i = 1; $i <= 5; ++$i) {
$class = $page == $i ? 'active' : null;
$this->assertSame($class, $crawler->filter('ul')->eq(1)->filter('li')->eq($i - 1)->attr('class'), sprintf('Class "active" is present only when $i == $page, ($i = %s, $page = %s)', $i, $page));
$this->assertSame('page ' . $i, $crawler->filter('ul')->eq(1)->filter('li')->eq($i - 1)->text(), sprintf('($i = %s, $page = %s)', $i, $page));
$href = 1 == $i ? 'index.html' : sprintf('index-page-%s.html', $i);
$this->assertSame($href, $crawler->filter('ul')->eq(1)->filter('li')->eq($i - 1)->filter('a')->attr('href'), sprintf('($i = %s, $page = %s)', $i, $page));
}
}
sort($lis);
$expected = array('Post #1', 'Post #10', 'Post #11', 'Post #12', 'Post #13', 'Post #14', 'Post #15', 'Post #16', 'Post #17', 'Post #18', 'Post #19', 'Post #2', 'Post #20', 'Post #3', 'Post #4', 'Post #5', 'Post #6', 'Post #7', 'Post #8', 'Post #9');
$this->assertSame($expected, $lis);
}