public function testExecutePagination()
{
$this->deleteDir($webDir = __DIR__ . '/fixtures/pagination/web');
$application = $this->runApplication(dirname($webDir));
$this->assertSame(0, $application->getStatusCode());
$lis = array();
for ($page = 1; $page <= 4; ++$page) {
$path = 1 == $page ? 'index.html' : sprintf('index-page-%s.html', $page);
$this->assertTrue(file_exists($webDir . '/' . $path), $path);
$crawler = new Crawler(file_get_contents($webDir . '/' . $path));
// One ul for each document, one ul for each pages
$this->assertCount(2, $crawler->filter('ul'));
// 14 pages + 1 index = 15 = 4 (item by page) * 3 (pages) + 3 (item on the last page)
$this->assertCount(4 == $page ? 3 : 4, $crawler->filter('ul')->eq(0)->filter('li'), "\$page == {$page}");
foreach ($crawler->filter('ul')->eq(0)->filter('li') as $li) {
$lis[] = trim($li->textContent);
}
// There is 4 pages
$this->assertCount(4, $crawler->filter('ul')->eq(1)->filter('li'));
// Check of pagination
for ($i = 1; $i <= 4; ++$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));
}
}
$this->assertFalse(file_exists($webDir . '/index-page-5.html'));
sort($lis);
$expected = array('Index', 'Page1', 'Page10', 'Page11', 'Page12', 'Page13', 'Page14', 'Page2', 'Page3', 'Page4', 'Page5', 'Page6', 'Page7', 'Page8', 'Page9');
$this->assertSame($expected, $lis);
$this->deleteDir($webDir);
}