ObjectRouteLoaderTest.php 4.0 KB

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