Dashboard sipadu mbip
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ObjectRouteLoaderTest.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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(string $resourceString): void
  60. {
  61. $loader = new ObjectRouteLoaderForTest();
  62. $loader->load($resourceString);
  63. }
  64. public function getBadResourceStrings()
  65. {
  66. return [
  67. ['Foo:Bar:baz'],
  68. ['Foo::Bar::baz'],
  69. ['Foo:'],
  70. ['Foo::'],
  71. [':Foo'],
  72. ['::Foo'],
  73. ];
  74. }
  75. /**
  76. * @expectedException \LogicException
  77. */
  78. public function testExceptionOnNoObjectReturned()
  79. {
  80. $loader = new ObjectRouteLoaderForTest();
  81. $loader->loaderMap = ['my_service' => 'NOT_AN_OBJECT'];
  82. $loader->load('my_service::method');
  83. }
  84. /**
  85. * @expectedException \BadMethodCallException
  86. */
  87. public function testExceptionOnBadMethod()
  88. {
  89. $loader = new ObjectRouteLoaderForTest();
  90. $loader->loaderMap = ['my_service' => new \stdClass()];
  91. $loader->load('my_service::method');
  92. }
  93. /**
  94. * @expectedException \LogicException
  95. */
  96. public function testExceptionOnMethodNotReturningCollection()
  97. {
  98. $service = $this->getMockBuilder('stdClass')
  99. ->setMethods(['loadRoutes'])
  100. ->getMock();
  101. $service->expects($this->once())
  102. ->method('loadRoutes')
  103. ->willReturn('NOT_A_COLLECTION');
  104. $loader = new ObjectRouteLoaderForTest();
  105. $loader->loaderMap = ['my_service' => $service];
  106. $loader->load('my_service::loadRoutes');
  107. }
  108. }
  109. class ObjectRouteLoaderForTest extends ObjectRouteLoader
  110. {
  111. public $loaderMap = [];
  112. protected function getServiceObject($id)
  113. {
  114. return isset($this->loaderMap[$id]) ? $this->loaderMap[$id] : null;
  115. }
  116. }
  117. class RouteService
  118. {
  119. private $collection;
  120. public function __construct($collection)
  121. {
  122. $this->collection = $collection;
  123. }
  124. public function loadRoutes()
  125. {
  126. return $this->collection;
  127. }
  128. }