ObjectRouteLoaderTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Routing\Tests\Loader;
  11. use Symfony\Component\Routing\Loader\ObjectRouteLoader;
  12. use Symfony\Component\Routing\Route;
  13. use Symfony\Component\Routing\RouteCollection;
  14. class ObjectRouteLoaderTest extends \PHPUnit_Framework_TestCase
  15. {
  16. public function testLoadCallsServiceAndReturnsCollection()
  17. {
  18. $loader = new ObjectRouteLoaderForTest();
  19. // create a basic collection that will be returned
  20. $collection = new RouteCollection();
  21. $collection->add('foo', new Route('/foo'));
  22. $loader->loaderMap = array(
  23. 'my_route_provider_service' => new RouteService($collection),
  24. );
  25. $actualRoutes = $loader->load(
  26. 'my_route_provider_service:loadRoutes',
  27. 'service'
  28. );
  29. $this->assertSame($collection, $actualRoutes);
  30. // the service file should be listed as a resource
  31. $this->assertNotEmpty($actualRoutes->getResources());
  32. }
  33. /**
  34. * @expectedException \InvalidArgumentException
  35. * @dataProvider getBadResourceStrings
  36. */
  37. public function testExceptionWithoutSyntax($resourceString)
  38. {
  39. $loader = new ObjectRouteLoaderForTest();
  40. $loader->load($resourceString);
  41. }
  42. public function getBadResourceStrings()
  43. {
  44. return array(
  45. array('Foo'),
  46. array('Bar::baz'),
  47. array('Foo:Bar:baz'),
  48. );
  49. }
  50. /**
  51. * @expectedException \LogicException
  52. */
  53. public function testExceptionOnNoObjectReturned()
  54. {
  55. $loader = new ObjectRouteLoaderForTest();
  56. $loader->loaderMap = array('my_service' => 'NOT_AN_OBJECT');
  57. $loader->load('my_service:method');
  58. }
  59. /**
  60. * @expectedException \BadMethodCallException
  61. */
  62. public function testExceptionOnBadMethod()
  63. {
  64. $loader = new ObjectRouteLoaderForTest();
  65. $loader->loaderMap = array('my_service' => new \stdClass());
  66. $loader->load('my_service:method');
  67. }
  68. /**
  69. * @expectedException \LogicException
  70. */
  71. public function testExceptionOnMethodNotReturningCollection()
  72. {
  73. $service = $this->getMockBuilder('stdClass')
  74. ->setMethods(array('loadRoutes'))
  75. ->getMock();
  76. $service->expects($this->once())
  77. ->method('loadRoutes')
  78. ->will($this->returnValue('NOT_A_COLLECTION'));
  79. $loader = new ObjectRouteLoaderForTest();
  80. $loader->loaderMap = array('my_service' => $service);
  81. $loader->load('my_service:loadRoutes');
  82. }
  83. }
  84. class ObjectRouteLoaderForTest extends ObjectRouteLoader
  85. {
  86. public $loaderMap = array();
  87. protected function getServiceObject($id)
  88. {
  89. return isset($this->loaderMap[$id]) ? $this->loaderMap[$id] : null;
  90. }
  91. }
  92. class RouteService
  93. {
  94. private $collection;
  95. public function __construct($collection)
  96. {
  97. $this->collection = $collection;
  98. }
  99. public function loadRoutes()
  100. {
  101. return $this->collection;
  102. }
  103. }