1
0

ResourceLocatorTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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() {
  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. array($this->logger, $theme, $core_map, $party_map, $appsRoots ),
  28. '', true, true, true, array());
  29. }
  30. public function testConstructor() {
  31. $locator = $this->getResourceLocator('theme',
  32. array('core'=>'map'), array('3rd'=>'party'), array('foo'=>'bar'));
  33. $this->assertAttributeEquals('theme', 'theme', $locator);
  34. $this->assertAttributeEquals('core', 'serverroot', $locator);
  35. $this->assertAttributeEquals(array('core'=>'map','3rd'=>'party'), 'mapping', $locator);
  36. $this->assertAttributeEquals('3rd', 'thirdpartyroot', $locator);
  37. $this->assertAttributeEquals('map', 'webroot', $locator);
  38. $this->assertAttributeEquals(array(), 'resources', $locator);
  39. }
  40. public function testFind() {
  41. $locator = $this->getResourceLocator('theme',
  42. array('core' => 'map'), array('3rd' => 'party'), array('foo' => 'bar'));
  43. $locator->expects($this->once())
  44. ->method('doFind')
  45. ->with('foo');
  46. $locator->expects($this->once())
  47. ->method('doFindTheme')
  48. ->with('foo');
  49. /** @var \OC\Template\ResourceLocator $locator */
  50. $locator->find(array('foo'));
  51. }
  52. public function testFindNotFound() {
  53. $locator = $this->getResourceLocator('theme',
  54. array('core'=>'map'), array('3rd'=>'party'), array('foo'=>'bar'));
  55. $locator->expects($this->once())
  56. ->method('doFind')
  57. ->with('foo')
  58. ->will($this->throwException(new ResourceNotFoundException('foo', 'map')));
  59. $locator->expects($this->once())
  60. ->method('doFindTheme')
  61. ->with('foo')
  62. ->will($this->throwException(new ResourceNotFoundException('foo', 'map')));
  63. $this->logger->expects($this->exactly(2))
  64. ->method('debug')
  65. ->with($this->stringContains('map/foo'));
  66. /** @var \OC\Template\ResourceLocator $locator */
  67. $locator->find(array('foo'));
  68. }
  69. public function testAppendIfExist() {
  70. $locator = $this->getResourceLocator('theme',
  71. array(__DIR__=>'map'), array('3rd'=>'party'), array('foo'=>'bar'));
  72. /** @var \OC\Template\ResourceLocator $locator */
  73. $method = new \ReflectionMethod($locator, 'appendIfExist');
  74. $method->setAccessible(true);
  75. $method->invoke($locator, __DIR__, basename(__FILE__), 'webroot');
  76. $resource1 = array(__DIR__, 'webroot', basename(__FILE__));
  77. $this->assertEquals(array($resource1), $locator->getResources());
  78. $method->invoke($locator, __DIR__, basename(__FILE__));
  79. $resource2 = array(__DIR__, 'map', basename(__FILE__));
  80. $this->assertEquals(array($resource1, $resource2), $locator->getResources());
  81. $method->invoke($locator, __DIR__, 'does-not-exist');
  82. $this->assertEquals(array($resource1, $resource2), $locator->getResources());
  83. }
  84. }