ResourceLocatorTest.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * Copyright (c) 2013 Bart Visscher <bartv@thisnet.nl>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace Test\Template;
  9. use OC\SystemConfig;
  10. use OC\Template\ResourceNotFoundException;
  11. use Psr\Log\LoggerInterface;
  12. class ResourceLocatorTest extends \Test\TestCase {
  13. /** @var \PHPUnit\Framework\MockObject\MockObject */
  14. protected $logger;
  15. protected function setUp(): void {
  16. parent::setUp();
  17. $this->logger = $this->createMock(LoggerInterface::class);
  18. }
  19. /**
  20. * @param string $theme
  21. * @return \PHPUnit\Framework\MockObject\MockObject
  22. */
  23. public function getResourceLocator($theme) {
  24. $systemConfig = $this->createMock(SystemConfig::class);
  25. $systemConfig
  26. ->expects($this->any())
  27. ->method('getValue')
  28. ->with('theme', '')
  29. ->willReturn($theme);
  30. $this->overwriteService(SystemConfig::class, $systemConfig);
  31. return $this->getMockForAbstractClass('OC\Template\ResourceLocator',
  32. [$this->logger],
  33. '', true, true, true, []);
  34. }
  35. public function testFind() {
  36. $locator = $this->getResourceLocator('theme');
  37. $locator->expects($this->once())
  38. ->method('doFind')
  39. ->with('foo');
  40. $locator->expects($this->once())
  41. ->method('doFindTheme')
  42. ->with('foo');
  43. /** @var \OC\Template\ResourceLocator $locator */
  44. $locator->find(['foo']);
  45. }
  46. public function testFindNotFound() {
  47. $systemConfig = $this->createMock(SystemConfig::class);
  48. $systemConfig->method('getValue')
  49. ->with('theme', '')
  50. ->willReturn('theme');
  51. $this->overwriteService(SystemConfig::class, $systemConfig);
  52. $locator = $this->getResourceLocator('theme',
  53. ['core' => 'map'], ['3rd' => 'party'], ['foo' => 'bar']);
  54. $locator->expects($this->once())
  55. ->method('doFind')
  56. ->with('foo')
  57. ->will($this->throwException(new ResourceNotFoundException('foo', 'map')));
  58. $locator->expects($this->once())
  59. ->method('doFindTheme')
  60. ->with('foo')
  61. ->will($this->throwException(new ResourceNotFoundException('foo', 'map')));
  62. $this->logger->expects($this->exactly(2))
  63. ->method('debug')
  64. ->with($this->stringContains('map/foo'));
  65. /** @var \OC\Template\ResourceLocator $locator */
  66. $locator->find(['foo']);
  67. }
  68. public function testAppendIfExist() {
  69. $locator = $this->getResourceLocator('theme');
  70. /** @var \OC\Template\ResourceLocator $locator */
  71. $method = new \ReflectionMethod($locator, 'appendIfExist');
  72. $method->setAccessible(true);
  73. $method->invoke($locator, __DIR__, basename(__FILE__), 'webroot');
  74. $resource1 = [__DIR__, 'webroot', basename(__FILE__)];
  75. $this->assertEquals([$resource1], $locator->getResources());
  76. $method->invoke($locator, __DIR__, 'does-not-exist');
  77. $this->assertEquals([$resource1], $locator->getResources());
  78. }
  79. }