ObjectRouteLoaderTest.php 3.2 KB

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