4 * This file is part of the Symfony CMF package.
6 * (c) 2011-2015 Symfony CMF
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Cmf\Component\Routing;
14 use Symfony\Cmf\Component\Routing\Test\CmfUnitTestCase;
15 use Symfony\Component\Routing\Route;
18 * Tests the page route collection.
22 class PagedRouteCollectionTest extends CmfUnitTestCase
25 * Contains a mocked route provider.
27 * @var \Symfony\Cmf\Component\Routing\PagedRouteProviderInterface|\PHPUnit_Framework_MockObject_MockObject
29 protected $routeProvider;
31 protected function setUp()
33 $this->routeProvider = $this->getMock('Symfony\Cmf\Component\Routing\PagedRouteProviderInterface');
37 * Tests iterating a small amount of routes.
39 * @dataProvider providerIterator
41 public function testIterator($amountRoutes, $routesLoadedInParallel, $expectedCalls = array())
44 for ($i = 0; $i < $amountRoutes; ++$i) {
45 $routes['test_'.$i] = new Route("/example-$i");
47 $names = array_keys($routes);
49 foreach ($expectedCalls as $i => $range) {
50 $this->routeProvider->expects($this->at($i))
51 ->method('getRoutesPaged')
52 ->with($range[0], $range[1])
53 ->will($this->returnValue(array_slice($routes, $range[0], $range[1])));
56 $route_collection = new PagedRouteCollection($this->routeProvider, $routesLoadedInParallel);
59 foreach ($route_collection as $route_name => $route) {
60 // Ensure the route did not changed.
61 $this->assertEquals($routes[$route_name], $route);
62 // Ensure that the order did not changed.
63 $this->assertEquals($route_name, $names[$counter]);
69 * Provides test data for testIterator().
71 public function providerIterator()
75 $data[] = array(0, 20, array(array(0, 20)));
76 // Less total routes than loaded in parallel.
77 $data[] = array(10, 20, array(array(0, 20)));
78 // Exact the same amount of routes then loaded in parallel.
79 $data[] = array(20, 20, array(array(0, 20), array(20, 20)));
80 // Less than twice the amount.
81 $data[] = array(39, 20, array(array(0, 20), array(20, 20)));
82 // More total routes than loaded in parallel.
83 $data[] = array(40, 20, array(array(0, 20), array(20, 20), array(40, 20)));
84 $data[] = array(41, 20, array(array(0, 20), array(20, 20), array(40, 20)));
86 $data[] = array(42, 23, array(array(0, 23), array(23, 23)));
92 * Tests the count() method.
94 public function testCount()
96 $this->routeProvider->expects($this->once())
97 ->method('getRoutesCount')
98 ->will($this->returnValue(12));
99 $routeCollection = new PagedRouteCollection($this->routeProvider);
100 $this->assertEquals(12, $routeCollection->count());
104 * Tests the rewind method once the iterator is at the end.
106 public function testIteratingAndRewind()
109 for ($i = 0; $i < 30; ++$i) {
110 $routes['test_'.$i] = new Route("/example-$i");
112 $this->routeProvider->expects($this->any())
113 ->method('getRoutesPaged')
114 ->will($this->returnValueMap(array(
115 array(0, 10, array_slice($routes, 0, 10)),
116 array(10, 10, array_slice($routes, 9, 10)),
117 array(20, 10, array()),
120 $routeCollection = new PagedRouteCollection($this->routeProvider, 10);
122 // Force the iterating process.
123 $routeCollection->rewind();
124 for ($i = 0; $i < 29; ++$i) {
125 $routeCollection->next();
127 $routeCollection->rewind();
129 $this->assertEquals('test_0', $routeCollection->key());
130 $this->assertEquals($routes['test_0'], $routeCollection->current());