nextcloud/tests/lib/Template/ResourceLocatorTest.php

97 lines
3.2 KiB
PHP
Raw Normal View History

<?php
/**
* Copyright (c) 2013 Bart Visscher <bartv@thisnet.nl>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
2015-04-20 13:31:07 +03:00
namespace Test\Template;
use OC\Template\ResourceNotFoundException;
2016-09-07 21:26:21 +03:00
use OCP\ILogger;
2015-04-20 13:31:07 +03:00
class ResourceLocatorTest extends \Test\TestCase {
2015-04-20 13:31:07 +03:00
/** @var \PHPUnit_Framework_MockObject_MockObject */
protected $logger;
protected function setUp(): void {
parent::setUp();
2016-09-07 21:26:21 +03:00
$this->logger = $this->createMock(ILogger::class);
}
/**
* @param string $theme
2015-04-20 13:31:07 +03:00
* @param array $core_map
* @param array $party_map
* @param array $appsRoots
* @return \PHPUnit_Framework_MockObject_MockObject
*/
2015-04-20 13:31:07 +03:00
public function getResourceLocator($theme, $core_map, $party_map, $appsRoots) {
return $this->getMockForAbstractClass('OC\Template\ResourceLocator',
[$this->logger, $theme, $core_map, $party_map, $appsRoots ],
'', true, true, true, []);
}
public function testConstructor() {
$locator = $this->getResourceLocator('theme',
['core'=>'map'], ['3rd'=>'party'], ['foo'=>'bar']);
$this->assertAttributeEquals('theme', 'theme', $locator);
$this->assertAttributeEquals('core', 'serverroot', $locator);
$this->assertAttributeEquals(['core'=>'map','3rd'=>'party'], 'mapping', $locator);
$this->assertAttributeEquals('3rd', 'thirdpartyroot', $locator);
$this->assertAttributeEquals('map', 'webroot', $locator);
$this->assertAttributeEquals([], 'resources', $locator);
}
public function testFind() {
$locator = $this->getResourceLocator('theme',
['core' => 'map'], ['3rd' => 'party'], ['foo' => 'bar']);
$locator->expects($this->once())
->method('doFind')
->with('foo');
$locator->expects($this->once())
->method('doFindTheme')
->with('foo');
2015-04-20 13:31:07 +03:00
/** @var \OC\Template\ResourceLocator $locator */
$locator->find(['foo']);
}
public function testFindNotFound() {
$locator = $this->getResourceLocator('theme',
['core'=>'map'], ['3rd'=>'party'], ['foo'=>'bar']);
$locator->expects($this->once())
->method('doFind')
->with('foo')
2015-04-20 13:31:07 +03:00
->will($this->throwException(new ResourceNotFoundException('foo', 'map')));
$locator->expects($this->once())
->method('doFindTheme')
->with('foo')
2015-04-20 13:31:07 +03:00
->will($this->throwException(new ResourceNotFoundException('foo', 'map')));
$this->logger->expects($this->exactly(2))
->method('debug')
2015-04-20 13:31:35 +03:00
->with($this->stringContains('map/foo'));
2015-04-20 13:31:07 +03:00
/** @var \OC\Template\ResourceLocator $locator */
$locator->find(['foo']);
}
public function testAppendIfExist() {
$locator = $this->getResourceLocator('theme',
[__DIR__=>'map'], ['3rd'=>'party'], ['foo'=>'bar']);
2015-04-20 13:31:07 +03:00
/** @var \OC\Template\ResourceLocator $locator */
$method = new \ReflectionMethod($locator, 'appendIfExist');
$method->setAccessible(true);
$method->invoke($locator, __DIR__, basename(__FILE__), 'webroot');
$resource1 = [__DIR__, 'webroot', basename(__FILE__)];
$this->assertEquals([$resource1], $locator->getResources());
$method->invoke($locator, __DIR__, basename(__FILE__));
$resource2 = [__DIR__, 'map', basename(__FILE__)];
$this->assertEquals([$resource1, $resource2], $locator->getResources());
$method->invoke($locator, __DIR__, 'does-not-exist');
$this->assertEquals([$resource1, $resource2], $locator->getResources());
}
}