Skip to content

Commit

Permalink
test(PageController): mocking OC_Helper functions
Browse files Browse the repository at this point in the history
Signed-off-by: Kai Henseler <[email protected]>
  • Loading branch information
bromiesTM committed Sep 17, 2024
1 parent 8715665 commit 14c8ff3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 17 deletions.
21 changes: 17 additions & 4 deletions lib/Controller/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,20 +96,20 @@ public function index(): TemplateResponse {

$user = $this->userManager->get($this->uid);

$storageInfo = $this->helper::getStorageInfo('/');
$storageInfo = $this->getStorageInfo('/');
if ($storageInfo['quota'] === FileInfo::SPACE_UNLIMITED) {
$totalSpace = 'Unlimited';
} else {
$totalSpace = $this->helper::humanFileSize($storageInfo['total']);
$totalSpace = $this->humanFileSize($storageInfo['total']);
}

$this->initialState->provideInitialState(
'personalInfoParameters',
[
'languageMap' => $this->getLanguageMap($user),
'totalSpace' => $totalSpace,
'freeSpace' => $this->helper::humanFileSize($storageInfo['free']),
'usage' => $this->helper::humanFileSize($storageInfo['used']),
'freeSpace' => $this->humanFileSize($storageInfo['free']),
'usage' => $this->humanFileSize($storageInfo['used']),
'usageRelative' => round($storageInfo['relative']),
]
);
Expand Down Expand Up @@ -205,4 +205,17 @@ private function getCustomClientURL(): array {
'apps.ios.id' => $this->config->getSystemValue('ionos_customclient_ios_appid'),
];
}

public function getStorageInfo(
$path,
$rootInfo = null,
$includeMountPoints = true,
$useCache = true
): array {
return $this->helper::getStorageInfo($path, $rootInfo, $includeMountPoints, $useCache);
}

public function humanFileSize(int $size): string {
return $this->helper::humanFileSize($size);
}
}
43 changes: 30 additions & 13 deletions tests/Controller/PageControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,22 @@ protected function setUp(): void {
$this->initialState = $this->createMock(IInitialState::class);
$this->userSession = $this->createMock(IUserSession::class);
$this->uid = "mock-user-id-123";
$this->helper = $this->getMockBuilder(\OC_Helper::class)
$this->helper = $this->createMock(\OC_Helper::class);
$this->controller = $this->getMockBuilder(PageController::class)
->setConstructorArgs([
$this->config,
$this->userManager,
$this->l10nFactory,
$this->tokenProvider,
$this->session,
$this->initialState,
$this->userSession,
$this->uid,
$this->helper
])
->onlyMethods(['getStorageInfo', 'humanFileSize'])
->getMock();

$this->controller = new PageController(
$this->config,
$this->userManager,
$this->l10nFactory,
$this->tokenProvider,
$this->session,
$this->initialState,
$this->userSession,
$this->uid,
$this->helper
);

$mockCurrentSessionId = "mock-session-id-123";

$mockCurrentSessionTokenId = 1;
Expand All @@ -88,6 +88,23 @@ protected function setUp(): void {
->method('getId')
->willReturn($mockCurrentSessionId);

$this->controller->expects($this->once())
->method('getStorageInfo')
->willReturn([
'used' => 123,
'quota' => 100,
'total' => 100,
'relative' => 123,
'free' => 0,
'owner' => 'MyName',
'ownerDisplayName' => 'MyDisplayName',
]);

// mock humanFileSize
$this->controller->expects($this->any())
->method('humanFileSize')
->willReturn('mocked-human-file-size');

// Contains also the current session's token
$mockTokenList = [
$mockSessionAppToken,
Expand Down

0 comments on commit 14c8ff3

Please sign in to comment.