1: <?php
2:
3: /*
4: * This file is part of the Symfony package.
5: *
6: * (c) Fabien Potencier <[email protected]>
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\Component\Routing\Tests\Loader;
13:
14: use PHPUnit\Framework\TestCase;
15: use Symfony\Component\Routing\Loader\ObjectRouteLoader;
16: use Symfony\Component\Routing\Route;
17: use Symfony\Component\Routing\RouteCollection;
18:
19: class ObjectRouteLoaderTest extends TestCase
20: {
21: public function testLoadCallsServiceAndReturnsCollection()
22: {
23: $loader = new ObjectRouteLoaderForTest();
24:
25: // create a basic collection that will be returned
26: $collection = new RouteCollection();
27: $collection->add('foo', new Route('/foo'));
28:
29: $loader->loaderMap = array(
30: 'my_route_provider_service' => new RouteService($collection),
31: );
32:
33: $actualRoutes = $loader->load(
34: 'my_route_provider_service:loadRoutes',
35: 'service'
36: );
37:
38: $this->assertSame($collection, $actualRoutes);
39: // the service file should be listed as a resource
40: $this->assertNotEmpty($actualRoutes->getResources());
41: }
42:
43: /**
44: * @expectedException \InvalidArgumentException
45: * @dataProvider getBadResourceStrings
46: */
47: public function testExceptionWithoutSyntax($resourceString)
48: {
49: $loader = new ObjectRouteLoaderForTest();
50: $loader->load($resourceString);
51: }
52:
53: public function getBadResourceStrings()
54: {
55: return array(
56: array('Foo'),
57: array('Bar::baz'),
58: array('Foo:Bar:baz'),
59: );
60: }
61:
62: /**
63: * @expectedException \LogicException
64: */
65: public function testExceptionOnNoObjectReturned()
66: {
67: $loader = new ObjectRouteLoaderForTest();
68: $loader->loaderMap = array('my_service' => 'NOT_AN_OBJECT');
69: $loader->load('my_service:method');
70: }
71:
72: /**
73: * @expectedException \BadMethodCallException
74: */
75: public function testExceptionOnBadMethod()
76: {
77: $loader = new ObjectRouteLoaderForTest();
78: $loader->loaderMap = array('my_service' => new \stdClass());
79: $loader->load('my_service:method');
80: }
81:
82: /**
83: * @expectedException \LogicException
84: */
85: public function testExceptionOnMethodNotReturningCollection()
86: {
87: $service = $this->getMockBuilder('stdClass')
88: ->setMethods(array('loadRoutes'))
89: ->getMock();
90: $service->expects($this->once())
91: ->method('loadRoutes')
92: ->will($this->returnValue('NOT_A_COLLECTION'));
93:
94: $loader = new ObjectRouteLoaderForTest();
95: $loader->loaderMap = array('my_service' => $service);
96: $loader->load('my_service:loadRoutes');
97: }
98: }
99:
100: class ObjectRouteLoaderForTest extends ObjectRouteLoader
101: {
102: public $loaderMap = array();
103:
104: protected function getServiceObject($id)
105: {
106: return isset($this->loaderMap[$id]) ? $this->loaderMap[$id] : null;
107: }
108: }
109:
110: class RouteService
111: {
112: private $collection;
113:
114: public function __construct($collection)
115: {
116: $this->collection = $collection;
117: }
118:
119: public function loadRoutes()
120: {
121: return $this->collection;
122: }
123: }
124: