ResourceLocatorTest.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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\Template\ResourceNotFoundException;
  10. use OCP\ILogger;
  11. class ResourceLocatorTest extends \Test\TestCase {
  12. /** @var \PHPUnit\Framework\MockObject\MockObject */
  13. protected $logger;
  14. protected function setUp(): void {
  15. parent::setUp();
  16. $this->logger = $this->createMock(ILogger::class);
  17. }
  18. /**
  19. * @param string $theme
  20. * @param array $core_map
  21. * @param array $party_map
  22. * @param array $appsRoots
  23. * @return \PHPUnit\Framework\MockObject\MockObject
  24. */
  25. public function getResourceLocator($theme, $core_map, $party_map, $appsRoots) {
  26. return $this->getMockForAbstractClass('OC\Template\ResourceLocator',
  27. [$this->logger, $theme, $core_map, $party_map, $appsRoots ],
  28. '', true, true, true, []);
  29. }
  30. public function testFind() {
  31. $locator = $this->getResourceLocator('theme',
  32. ['core' => 'map'], ['3rd' => 'party'], ['foo' => 'bar']);
  33. $locator->expects($this->once())
  34. ->method('doFind')
  35. ->with('foo');
  36. $locator->expects($this->once())
  37. ->method('doFindTheme')
  38. ->with('foo');
  39. /** @var \OC\Template\ResourceLocator $locator */
  40. $locator->find(['foo']);
  41. }
  42. public function testFindNotFound() {
  43. $locator = $this->getResourceLocator('theme',
  44. ['core'=>'map'], ['3rd'=>'party'], ['foo'=>'bar']);
  45. $locator->expects($this->once())
  46. ->method('doFind')
  47. ->with('foo')
  48. ->will($this->throwException(new ResourceNotFoundException('foo', 'map')));
  49. $locator->expects($this->once())
  50. ->method('doFindTheme')
  51. ->with('foo')
  52. ->will($this->throwException(new ResourceNotFoundException('foo', 'map')));
  53. $this->logger->expects($this->exactly(2))
  54. ->method('debug')
  55. ->with($this->stringContains('map/foo'));
  56. /** @var \OC\Template\ResourceLocator $locator */
  57. $locator->find(['foo']);
  58. }
  59. public function testAppendIfExist() {
  60. $locator = $this->getResourceLocator('theme',
  61. [__DIR__=>'map'], ['3rd'=>'party'], ['foo'=>'bar']);
  62. /** @var \OC\Template\ResourceLocator $locator */
  63. $method = new \ReflectionMethod($locator, 'appendIfExist');
  64. $method->setAccessible(true);
  65. $method->invoke($locator, __DIR__, basename(__FILE__), 'webroot');
  66. $resource1 = [__DIR__, 'webroot', basename(__FILE__)];
  67. $this->assertEquals([$resource1], $locator->getResources());
  68. $method->invoke($locator, __DIR__, basename(__FILE__));
  69. $resource2 = [__DIR__, 'map', basename(__FILE__)];
  70. $this->assertEquals([$resource1, $resource2], $locator->getResources());
  71. $method->invoke($locator, __DIR__, 'does-not-exist');
  72. $this->assertEquals([$resource1, $resource2], $locator->getResources());
  73. }
  74. }