chore: Adapt TemplateLayout tests

Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
pull/51029/head
Côme Chilliet 2025-03-03 10:07:40 +07:00 committed by Côme Chilliet
parent 558f4c854d
commit 7d64c63acf
1 changed files with 36 additions and 17 deletions

@ -14,50 +14,69 @@ use OC\TemplateLayout;
use OCP\App\IAppManager; use OCP\App\IAppManager;
use OCP\AppFramework\Http\TemplateResponse; use OCP\AppFramework\Http\TemplateResponse;
use OCP\IConfig; use OCP\IConfig;
use OCP\INavigationManager;
use OCP\Template\ITemplateManager;
use PHPUnit\Framework\MockObject\MockObject;
class TemplateLayoutTest extends \Test\TestCase { class TemplateLayoutTest extends \Test\TestCase {
private IConfig&MockObject $config;
private IAppManager&MockObject $appManager;
private InitialStateService&MockObject $initialState;
private INavigationManager&MockObject $navigationManager;
private ITemplateManager&MockObject $templateManager;
private TemplateLayout $templateLayout;
protected function setUp(): void {
parent::setUp();
$this->config = $this->createMock(IConfig::class);
$this->appManager = $this->createMock(IAppManager::class);
$this->initialState = $this->createMock(InitialStateService::class);
$this->navigationManager = $this->createMock(INavigationManager::class);
$this->templateManager = $this->createMock(ITemplateManager::class);
}
/** @dataProvider dataVersionHash */ /** @dataProvider dataVersionHash */
public function testVersionHash($path, $file, $installed, $debug, $expected): void { public function testVersionHash($path, $file, $installed, $debug, $expected): void {
$appManager = $this->createMock(IAppManager::class); $this->appManager->expects(self::any())
$appManager->expects(self::any())
->method('getAppVersion') ->method('getAppVersion')
->willReturnCallback(fn ($appId) => match ($appId) { ->willReturnCallback(fn ($appId) => match ($appId) {
'shippedApp' => 'shipped_1', 'shippedApp' => 'shipped_1',
'otherApp' => 'other_2', 'otherApp' => 'other_2',
default => "$appId", default => "$appId",
}); });
$appManager->expects(self::any()) $this->appManager->expects(self::any())
->method('isShipped') ->method('isShipped')
->willReturnCallback(fn (string $app) => $app === 'shippedApp'); ->willReturnCallback(fn (string $app) => $app === 'shippedApp');
$config = $this->createMock(IConfig::class); $this->config->expects(self::atLeastOnce())
$config->expects(self::atLeastOnce())
->method('getSystemValueBool') ->method('getSystemValueBool')
->willReturnMap([ ->willReturnMap([
['installed', false, $installed], ['installed', false, $installed],
['debug', false, $debug], ['debug', false, $debug],
]); ]);
$config->expects(self::any()) $this->config->expects(self::any())
->method('getAppValue') ->method('getAppValue')
->with('theming', 'cachebuster', '0') ->with('theming', 'cachebuster', '0')
->willReturn('42'); ->willReturn('42');
$initialState = $this->createMock(InitialStateService::class); $this->templateLayout = $this->getMockBuilder(TemplateLayout::class)
$this->overwriteService(IConfig::class, $config);
$this->overwriteService(IAppManager::class, $appManager);
$this->overwriteService(InitialStateService::class, $initialState);
$layout = $this->getMockBuilder(TemplateLayout::class)
->onlyMethods(['getAppNamefromPath']) ->onlyMethods(['getAppNamefromPath'])
->setConstructorArgs([TemplateResponse::RENDER_AS_ERROR]) ->setConstructorArgs([
$this->config,
$this->appManager,
$this->initialState,
$this->navigationManager,
$this->templateManager,
])
->getMock(); ->getMock();
$layout = $this->templateLayout->getPageTemplate(TemplateResponse::RENDER_AS_ERROR, '');
self::invokePrivate(TemplateLayout::class, 'versionHash', ['version_hash']); self::invokePrivate(TemplateLayout::class, 'versionHash', ['version_hash']);
$layout->expects(self::any()) $this->templateLayout->expects(self::any())
->method('getAppNamefromPath') ->method('getAppNamefromPath')
->willReturnCallback(fn ($appName) => match($appName) { ->willReturnCallback(fn ($appName) => match($appName) {
'apps/shipped' => 'shippedApp', 'apps/shipped' => 'shippedApp',
@ -65,7 +84,7 @@ class TemplateLayoutTest extends \Test\TestCase {
default => false, default => false,
}); });
$hash = self::invokePrivate($layout, 'getVersionHashSuffix', [$path, $file]); $hash = self::invokePrivate($this->templateLayout, 'getVersionHashSuffix', [$path, $file]);
self::assertEquals($expected, $hash); self::assertEquals($expected, $hash);
} }