LoaderTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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\Config\Tests\Loader;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Config\Loader\Loader;
  13. class LoaderTest extends TestCase
  14. {
  15. public function testGetSetResolver()
  16. {
  17. $resolver = $this->getMockBuilder('Symfony\Component\Config\Loader\LoaderResolverInterface')->getMock();
  18. $loader = new ProjectLoader1();
  19. $loader->setResolver($resolver);
  20. $this->assertSame($resolver, $loader->getResolver(), '->setResolver() sets the resolver loader');
  21. }
  22. public function testResolve()
  23. {
  24. $resolvedLoader = $this->getMockBuilder('Symfony\Component\Config\Loader\LoaderInterface')->getMock();
  25. $resolver = $this->getMockBuilder('Symfony\Component\Config\Loader\LoaderResolverInterface')->getMock();
  26. $resolver->expects($this->once())
  27. ->method('resolve')
  28. ->with('foo.xml')
  29. ->will($this->returnValue($resolvedLoader));
  30. $loader = new ProjectLoader1();
  31. $loader->setResolver($resolver);
  32. $this->assertSame($loader, $loader->resolve('foo.foo'), '->resolve() finds a loader');
  33. $this->assertSame($resolvedLoader, $loader->resolve('foo.xml'), '->resolve() finds a loader');
  34. }
  35. /**
  36. * @expectedException \Symfony\Component\Config\Exception\FileLoaderLoadException
  37. */
  38. public function testResolveWhenResolverCannotFindLoader()
  39. {
  40. $resolver = $this->getMockBuilder('Symfony\Component\Config\Loader\LoaderResolverInterface')->getMock();
  41. $resolver->expects($this->once())
  42. ->method('resolve')
  43. ->with('FOOBAR')
  44. ->will($this->returnValue(false));
  45. $loader = new ProjectLoader1();
  46. $loader->setResolver($resolver);
  47. $loader->resolve('FOOBAR');
  48. }
  49. public function testImport()
  50. {
  51. $resolvedLoader = $this->getMockBuilder('Symfony\Component\Config\Loader\LoaderInterface')->getMock();
  52. $resolvedLoader->expects($this->once())
  53. ->method('load')
  54. ->with('foo')
  55. ->will($this->returnValue('yes'));
  56. $resolver = $this->getMockBuilder('Symfony\Component\Config\Loader\LoaderResolverInterface')->getMock();
  57. $resolver->expects($this->once())
  58. ->method('resolve')
  59. ->with('foo')
  60. ->will($this->returnValue($resolvedLoader));
  61. $loader = new ProjectLoader1();
  62. $loader->setResolver($resolver);
  63. $this->assertEquals('yes', $loader->import('foo'));
  64. }
  65. public function testImportWithType()
  66. {
  67. $resolvedLoader = $this->getMockBuilder('Symfony\Component\Config\Loader\LoaderInterface')->getMock();
  68. $resolvedLoader->expects($this->once())
  69. ->method('load')
  70. ->with('foo', 'bar')
  71. ->will($this->returnValue('yes'));
  72. $resolver = $this->getMockBuilder('Symfony\Component\Config\Loader\LoaderResolverInterface')->getMock();
  73. $resolver->expects($this->once())
  74. ->method('resolve')
  75. ->with('foo', 'bar')
  76. ->will($this->returnValue($resolvedLoader));
  77. $loader = new ProjectLoader1();
  78. $loader->setResolver($resolver);
  79. $this->assertEquals('yes', $loader->import('foo', 'bar'));
  80. }
  81. }
  82. class ProjectLoader1 extends Loader
  83. {
  84. public function load($resource, $type = null)
  85. {
  86. }
  87. public function supports($resource, $type = null)
  88. {
  89. return \is_string($resource) && 'foo' === pathinfo($resource, PATHINFO_EXTENSION);
  90. }
  91. public function getType()
  92. {
  93. }
  94. }