4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Routing\Tests\Loader;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Routing\Loader\ObjectRouteLoader;
16 use Symfony\Component\Routing\Route;
17 use Symfony\Component\Routing\RouteCollection;
19 class ObjectRouteLoaderTest extends TestCase
21 public function testLoadCallsServiceAndReturnsCollection()
23 $loader = new ObjectRouteLoaderForTest();
25 // create a basic collection that will be returned
26 $collection = new RouteCollection();
27 $collection->add('foo', new Route('/foo'));
29 $loader->loaderMap = array(
30 'my_route_provider_service' => new RouteService($collection),
33 $actualRoutes = $loader->load(
34 'my_route_provider_service:loadRoutes',
38 $this->assertSame($collection, $actualRoutes);
39 // the service file should be listed as a resource
40 $this->assertNotEmpty($actualRoutes->getResources());
44 * @expectedException \InvalidArgumentException
45 * @dataProvider getBadResourceStrings
47 public function testExceptionWithoutSyntax($resourceString)
49 $loader = new ObjectRouteLoaderForTest();
50 $loader->load($resourceString);
53 public function getBadResourceStrings()
63 * @expectedException \LogicException
65 public function testExceptionOnNoObjectReturned()
67 $loader = new ObjectRouteLoaderForTest();
68 $loader->loaderMap = array('my_service' => 'NOT_AN_OBJECT');
69 $loader->load('my_service:method');
73 * @expectedException \BadMethodCallException
75 public function testExceptionOnBadMethod()
77 $loader = new ObjectRouteLoaderForTest();
78 $loader->loaderMap = array('my_service' => new \stdClass());
79 $loader->load('my_service:method');
83 * @expectedException \LogicException
85 public function testExceptionOnMethodNotReturningCollection()
87 $service = $this->getMockBuilder('stdClass')
88 ->setMethods(array('loadRoutes'))
90 $service->expects($this->once())
91 ->method('loadRoutes')
92 ->will($this->returnValue('NOT_A_COLLECTION'));
94 $loader = new ObjectRouteLoaderForTest();
95 $loader->loaderMap = array('my_service' => $service);
96 $loader->load('my_service:loadRoutes');
100 class ObjectRouteLoaderForTest extends ObjectRouteLoader
102 public $loaderMap = array();
104 protected function getServiceObject($id)
106 return isset($this->loaderMap[$id]) ? $this->loaderMap[$id] : null;
114 public function __construct($collection)
116 $this->collection = $collection;
119 public function loadRoutes()
121 return $this->collection;