ObjectLoaderTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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\ObjectLoader;
  13. use Symfony\Component\Routing\Route;
  14. use Symfony\Component\Routing\RouteCollection;
  15. class ObjectLoaderTest extends TestCase
  16. {
  17. public function testLoadCallsServiceAndReturnsCollection()
  18. {
  19. $loader = new TestObjectLoader('some-env');
  20. // create a basic collection that will be returned
  21. $collection = new RouteCollection();
  22. $collection->add('foo', new Route('/foo'));
  23. $loader->loaderMap = [
  24. 'my_route_provider_service' => new TestObjectLoaderRouteService($collection, 'some-env'),
  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. * @dataProvider getBadResourceStrings
  36. */
  37. public function testExceptionWithoutSyntax(string $resourceString)
  38. {
  39. $this->expectException(\InvalidArgumentException::class);
  40. $loader = new TestObjectLoader();
  41. $loader->load($resourceString);
  42. }
  43. public static function getBadResourceStrings()
  44. {
  45. return [
  46. ['Foo:Bar:baz'],
  47. ['Foo::Bar::baz'],
  48. ['Foo:'],
  49. ['Foo::'],
  50. [':Foo'],
  51. ['::Foo'],
  52. ];
  53. }
  54. public function testExceptionOnNoObjectReturned()
  55. {
  56. $this->expectException(\TypeError::class);
  57. $loader = new TestObjectLoader();
  58. $loader->loaderMap = ['my_service' => 'NOT_AN_OBJECT'];
  59. $loader->load('my_service::method');
  60. }
  61. public function testExceptionOnBadMethod()
  62. {
  63. $this->expectException(\BadMethodCallException::class);
  64. $loader = new TestObjectLoader();
  65. $loader->loaderMap = ['my_service' => new \stdClass()];
  66. $loader->load('my_service::method');
  67. }
  68. public function testExceptionOnMethodNotReturningCollection()
  69. {
  70. $this->expectException(\LogicException::class);
  71. $service = $this->getMockBuilder(\stdClass::class)
  72. ->addMethods(['loadRoutes'])
  73. ->getMock();
  74. $service->expects($this->once())
  75. ->method('loadRoutes')
  76. ->willReturn('NOT_A_COLLECTION');
  77. $loader = new TestObjectLoader();
  78. $loader->loaderMap = ['my_service' => $service];
  79. $loader->load('my_service::loadRoutes');
  80. }
  81. }
  82. class TestObjectLoader extends ObjectLoader
  83. {
  84. public $loaderMap = [];
  85. public function supports($resource, string $type = null): bool
  86. {
  87. return 'service';
  88. }
  89. protected function getObject(string $id): object
  90. {
  91. return $this->loaderMap[$id] ?? null;
  92. }
  93. }
  94. class TestObjectLoaderRouteService
  95. {
  96. private $collection;
  97. private $env;
  98. public function __construct($collection, string $env = null)
  99. {
  100. $this->collection = $collection;
  101. $this->env = $env;
  102. }
  103. public function loadRoutes(TestObjectLoader $loader, string $env = null)
  104. {
  105. if ($this->env !== $env) {
  106. throw new \InvalidArgumentException(sprintf('Expected env "%s", "%s" given.', $this->env, $env));
  107. }
  108. return $this->collection;
  109. }
  110. }