ViewFileViewFinderTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace Illuminate\Tests\View;
  3. use Illuminate\Filesystem\Filesystem;
  4. use Illuminate\View\FileViewFinder;
  5. use InvalidArgumentException;
  6. use Mockery as m;
  7. use PHPUnit\Framework\TestCase;
  8. class ViewFileViewFinderTest extends TestCase
  9. {
  10. protected function tearDown(): void
  11. {
  12. m::close();
  13. }
  14. public function testBasicViewFinding()
  15. {
  16. $finder = $this->getFinder();
  17. $finder->getFilesystem()->shouldReceive('exists')->once()->with(__DIR__.'/foo.blade.php')->andReturn(true);
  18. $this->assertEquals(__DIR__.'/foo.blade.php', $finder->find('foo'));
  19. }
  20. public function testCascadingFileLoading()
  21. {
  22. $finder = $this->getFinder();
  23. $finder->getFilesystem()->shouldReceive('exists')->once()->with(__DIR__.'/foo.blade.php')->andReturn(false);
  24. $finder->getFilesystem()->shouldReceive('exists')->once()->with(__DIR__.'/foo.php')->andReturn(true);
  25. $this->assertEquals(__DIR__.'/foo.php', $finder->find('foo'));
  26. }
  27. public function testDirectoryCascadingFileLoading()
  28. {
  29. $finder = $this->getFinder();
  30. $finder->addLocation(__DIR__.'/nested');
  31. $finder->getFilesystem()->shouldReceive('exists')->once()->with(__DIR__.'/foo.blade.php')->andReturn(false);
  32. $finder->getFilesystem()->shouldReceive('exists')->once()->with(__DIR__.'/foo.php')->andReturn(false);
  33. $finder->getFilesystem()->shouldReceive('exists')->once()->with(__DIR__.'/foo.css')->andReturn(false);
  34. $finder->getFilesystem()->shouldReceive('exists')->once()->with(__DIR__.'/foo.html')->andReturn(false);
  35. $finder->getFilesystem()->shouldReceive('exists')->once()->with(__DIR__.'/nested/foo.blade.php')->andReturn(true);
  36. $this->assertEquals(__DIR__.'/nested/foo.blade.php', $finder->find('foo'));
  37. }
  38. public function testNamespacedBasicFileLoading()
  39. {
  40. $finder = $this->getFinder();
  41. $finder->addNamespace('foo', __DIR__.'/foo');
  42. $finder->getFilesystem()->shouldReceive('exists')->once()->with(__DIR__.'/foo/bar/baz.blade.php')->andReturn(true);
  43. $this->assertEquals(__DIR__.'/foo/bar/baz.blade.php', $finder->find('foo::bar.baz'));
  44. }
  45. public function testCascadingNamespacedFileLoading()
  46. {
  47. $finder = $this->getFinder();
  48. $finder->addNamespace('foo', __DIR__.'/foo');
  49. $finder->getFilesystem()->shouldReceive('exists')->once()->with(__DIR__.'/foo/bar/baz.blade.php')->andReturn(false);
  50. $finder->getFilesystem()->shouldReceive('exists')->once()->with(__DIR__.'/foo/bar/baz.php')->andReturn(true);
  51. $this->assertEquals(__DIR__.'/foo/bar/baz.php', $finder->find('foo::bar.baz'));
  52. }
  53. public function testDirectoryCascadingNamespacedFileLoading()
  54. {
  55. $finder = $this->getFinder();
  56. $finder->addNamespace('foo', [__DIR__.'/foo', __DIR__.'/bar']);
  57. $finder->getFilesystem()->shouldReceive('exists')->once()->with(__DIR__.'/foo/bar/baz.blade.php')->andReturn(false);
  58. $finder->getFilesystem()->shouldReceive('exists')->once()->with(__DIR__.'/foo/bar/baz.php')->andReturn(false);
  59. $finder->getFilesystem()->shouldReceive('exists')->once()->with(__DIR__.'/foo/bar/baz.css')->andReturn(false);
  60. $finder->getFilesystem()->shouldReceive('exists')->once()->with(__DIR__.'/foo/bar/baz.html')->andReturn(false);
  61. $finder->getFilesystem()->shouldReceive('exists')->once()->with(__DIR__.'/bar/bar/baz.blade.php')->andReturn(true);
  62. $this->assertEquals(__DIR__.'/bar/bar/baz.blade.php', $finder->find('foo::bar.baz'));
  63. }
  64. public function testExceptionThrownWhenViewNotFound()
  65. {
  66. $this->expectException(InvalidArgumentException::class);
  67. $finder = $this->getFinder();
  68. $finder->getFilesystem()->shouldReceive('exists')->once()->with(__DIR__.'/foo.blade.php')->andReturn(false);
  69. $finder->getFilesystem()->shouldReceive('exists')->once()->with(__DIR__.'/foo.php')->andReturn(false);
  70. $finder->getFilesystem()->shouldReceive('exists')->once()->with(__DIR__.'/foo.css')->andReturn(false);
  71. $finder->getFilesystem()->shouldReceive('exists')->once()->with(__DIR__.'/foo.html')->andReturn(false);
  72. $finder->find('foo');
  73. }
  74. public function testExceptionThrownOnInvalidViewName()
  75. {
  76. $this->expectException(InvalidArgumentException::class);
  77. $this->expectExceptionMessage('No hint path defined for [name].');
  78. $finder = $this->getFinder();
  79. $finder->find('name::');
  80. }
  81. public function testExceptionThrownWhenNoHintPathIsRegistered()
  82. {
  83. $this->expectException(InvalidArgumentException::class);
  84. $this->expectExceptionMessage('No hint path defined for [name].');
  85. $finder = $this->getFinder();
  86. $finder->find('name::foo');
  87. }
  88. public function testAddingExtensionPrependsNotAppends()
  89. {
  90. $finder = $this->getFinder();
  91. $finder->addExtension('baz');
  92. $extensions = $finder->getExtensions();
  93. $this->assertSame('baz', reset($extensions));
  94. }
  95. public function testAddingExtensionsReplacesOldOnes()
  96. {
  97. $finder = $this->getFinder();
  98. $finder->addExtension('baz');
  99. $finder->addExtension('baz');
  100. $this->assertCount(5, $finder->getExtensions());
  101. }
  102. public function testPassingViewWithHintReturnsTrue()
  103. {
  104. $finder = $this->getFinder();
  105. $this->assertTrue($finder->hasHintInformation('hint::foo.bar'));
  106. }
  107. public function testPassingViewWithoutHintReturnsFalse()
  108. {
  109. $finder = $this->getFinder();
  110. $this->assertFalse($finder->hasHintInformation('foo.bar'));
  111. }
  112. public function testPassingViewWithFalseHintReturnsFalse()
  113. {
  114. $finder = $this->getFinder();
  115. $this->assertFalse($finder->hasHintInformation('::foo.bar'));
  116. }
  117. public function pathsProvider()
  118. {
  119. return [
  120. ['incorrect_path', 'incorrect_path'],
  121. ];
  122. }
  123. /**
  124. * @dataProvider pathsProvider
  125. */
  126. public function testNormalizedPaths($originalPath, $exceptedPath)
  127. {
  128. $finder = $this->getFinder();
  129. $finder->prependLocation($originalPath);
  130. $normalizedPath = $finder->getPaths()[0];
  131. $this->assertSame($exceptedPath, $normalizedPath);
  132. }
  133. protected function getFinder()
  134. {
  135. return new FileViewFinder(m::mock(Filesystem::class), [__DIR__]);
  136. }
  137. }