a1e5ff3e16a8b50c0bb8bb1ba47216fa57873734
[yaffs-website] / vendor / symfony-cmf / routing / Tests / Routing / PagedRouteCollectionTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony CMF package.
5  *
6  * (c) 2011-2015 Symfony CMF
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Cmf\Component\Routing;
13
14 use Symfony\Cmf\Component\Routing\Test\CmfUnitTestCase;
15 use Symfony\Component\Routing\Route;
16
17 /**
18  * Tests the page route collection.
19  *
20  * @group cmf/routing
21  */
22 class PagedRouteCollectionTest extends CmfUnitTestCase
23 {
24     /**
25      * Contains a mocked route provider.
26      *
27      * @var \Symfony\Cmf\Component\Routing\PagedRouteProviderInterface|\PHPUnit_Framework_MockObject_MockObject
28      */
29     protected $routeProvider;
30
31     protected function setUp()
32     {
33         $this->routeProvider = $this->getMock('Symfony\Cmf\Component\Routing\PagedRouteProviderInterface');
34     }
35
36     /**
37      * Tests iterating a small amount of routes.
38      *
39      * @dataProvider providerIterator
40      */
41     public function testIterator($amountRoutes, $routesLoadedInParallel, $expectedCalls = array())
42     {
43         $routes = array();
44         for ($i = 0; $i < $amountRoutes; ++$i) {
45             $routes['test_'.$i] = new Route("/example-$i");
46         }
47         $names = array_keys($routes);
48
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])));
54         }
55
56         $route_collection = new PagedRouteCollection($this->routeProvider, $routesLoadedInParallel);
57
58         $counter = 0;
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]);
64             ++$counter;
65         }
66     }
67
68     /**
69      * Provides test data for testIterator().
70      */
71     public function providerIterator()
72     {
73         $data = array();
74         // Non total routes.
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)));
85         // why not.
86         $data[] = array(42, 23, array(array(0, 23), array(23, 23)));
87
88         return $data;
89     }
90
91     /**
92      * Tests the count() method.
93      */
94     public function testCount()
95     {
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());
101     }
102
103     /**
104      * Tests the rewind method once the iterator is at the end.
105      */
106     public function testIteratingAndRewind()
107     {
108         $routes = array();
109         for ($i = 0; $i < 30; ++$i) {
110             $routes['test_'.$i] = new Route("/example-$i");
111         }
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()),
118             )));
119
120         $routeCollection = new PagedRouteCollection($this->routeProvider, 10);
121
122         // Force the iterating process.
123         $routeCollection->rewind();
124         for ($i = 0; $i < 29; ++$i) {
125             $routeCollection->next();
126         }
127         $routeCollection->rewind();
128
129         $this->assertEquals('test_0', $routeCollection->key());
130         $this->assertEquals($routes['test_0'], $routeCollection->current());
131     }
132 }