|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony CMF package. |
| 5 | + * |
| 6 | + * (c) 2011-2014 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 | + { |
| 51 | + $this->routeProvider->expects($this->at($i)) |
| 52 | + ->method('getRoutesPaged') |
| 53 | + ->with($range[0], $range[1]) |
| 54 | + ->will($this->returnValue(array_slice($routes, $range[0], $range[1]))); |
| 55 | + } |
| 56 | + |
| 57 | + $route_collection = new PagedRouteCollection($this->routeProvider, $routesLoadedInParallel); |
| 58 | + |
| 59 | + $counter = 0; |
| 60 | + foreach ($route_collection as $route_name => $route) { |
| 61 | + // Ensure the route did not changed. |
| 62 | + $this->assertEquals($routes[$route_name], $route); |
| 63 | + // Ensure that the order did not changed. |
| 64 | + $this->assertEquals($route_name, $names[$counter]); |
| 65 | + $counter++; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Provides test data for testIterator(). |
| 71 | + */ |
| 72 | + public function providerIterator() |
| 73 | + { |
| 74 | + $data = array(); |
| 75 | + // Non total routes. |
| 76 | + $data[] = array(0, 20, array(array(0, 20))); |
| 77 | + // Less total routes than loaded in parallel. |
| 78 | + $data[] = array(10, 20, array(array(0, 20))); |
| 79 | + // Exact the same amount of routes then loaded in parallel. |
| 80 | + $data[] = array(20, 20, array(array(0, 20), array(20, 20))); |
| 81 | + // Less than twice the amount. |
| 82 | + $data[] = array(39, 20, array(array(0, 20), array(20, 20))); |
| 83 | + // More total routes than loaded in parallel. |
| 84 | + $data[] = array(40, 20, array(array(0, 20), array(20, 20), array(40, 20))); |
| 85 | + $data[] = array(41, 20, array(array(0, 20), array(20, 20), array(40, 20))); |
| 86 | + // why not. |
| 87 | + $data[] = array(42, 23, array(array(0, 23), array(23, 23))); |
| 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 | +} |
0 commit comments