Skip to content

Commit 456684f

Browse files
authored
Merge pull request #59362 from nextcloud/test/deprecations
test: resolve PHPUnit deprecation warning about `addMethods`
2 parents 04c1250 + e0c1b74 commit 456684f

6 files changed

Lines changed: 93 additions & 55 deletions

File tree

apps/dav/tests/unit/Connector/Sabre/FileTest.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@
4343
use Test\Traits\MountProviderTrait;
4444
use Test\Traits\UserTrait;
4545

46+
/**
47+
* Internal helper to mock legacy hook receiver.
48+
*/
49+
interface EventHandlerMock {
50+
public function writeCallback(): void;
51+
public function postWriteCallback(): void;
52+
}
53+
4654
/**
4755
* Class File
4856
*
@@ -822,9 +830,7 @@ public function testPutLocking(): void {
822830

823831
$wasLockedPre = false;
824832
$wasLockedPost = false;
825-
$eventHandler = $this->getMockBuilder(\stdclass::class)
826-
->addMethods(['writeCallback', 'postWriteCallback'])
827-
->getMock();
833+
$eventHandler = $this->createMock(EventHandlerMock::class);
828834

829835
// both pre and post hooks might need access to the file,
830836
// so only shared lock is acceptable

apps/files_sharing/lib/Controller/ShareAPIController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1787,7 +1787,7 @@ private function getRoomShareHelper() {
17871787
throw new QueryException();
17881788
}
17891789

1790-
return $this->serverContainer->get('\OCA\Talk\Share\Helper\ShareAPIController');
1790+
return $this->serverContainer->get(\OCA\Talk\Share\Helper\ShareAPIController::class);
17911791
}
17921792

17931793
/**

apps/files_sharing/tests/Controller/ShareAPIControllerTest.php

Lines changed: 38 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace OCA\Files_Sharing\Tests\Controller;
1010

1111
use OC\Files\Storage\Wrapper\Wrapper;
12+
use OC\Session\Internal;
1213
use OCA\Federation\TrustedServers;
1314
use OCA\Files_Sharing\Controller\ShareAPIController;
1415
use OCA\Files_Sharing\External\Storage;
@@ -26,6 +27,7 @@
2627
use OCP\Files\IRootFolder;
2728
use OCP\Files\Mount\IMountPoint;
2829
use OCP\Files\Mount\IShareOwnerlessMount;
30+
use OCP\Files\Node;
2931
use OCP\Files\NotFoundException;
3032
use OCP\Files\Storage\IStorage;
3133
use OCP\IAppConfig;
@@ -59,6 +61,16 @@
5961
use Test\TestCase;
6062
use Test\Traits\EmailValidatorTrait;
6163

64+
/**
65+
* Internal mock to allow mocking the Talk controller used for room shares,
66+
* needed when Talk is not installed during tests (PHPUnit does not allow mocking of non-existing classes).
67+
*/
68+
interface InternalTalkShareAPIController {
69+
public function formatShare(IShare $share): array;
70+
public function canAccessShare(IShare $share, string $user): bool;
71+
public function createShare(IShare $share, string $shareWith, int $permissions, string $expireDate): void;
72+
}
73+
6274
/**
6375
* Class ShareAPIControllerTest
6476
*
@@ -1831,25 +1843,15 @@ public function testCanAccessRoomShare(
18311843
->with('spreed')
18321844
->willReturn(true);
18331845

1834-
// This is not possible anymore with PHPUnit 10+
1835-
// as `setMethods` was removed and now real reflection is used, thus the class needs to exist.
1836-
// $helper = $this->getMockBuilder('\OCA\Talk\Share\Helper\ShareAPIController')
1837-
$helper = $this->getMockBuilder(\stdClass::class)
1838-
->addMethods(['canAccessShare'])
1839-
->getMock();
1840-
$helper->method('canAccessShare')
1846+
$this->mockTalkController()
1847+
->method('canAccessShare')
18411848
->with($share, $this->currentUser)
18421849
->willReturn($canAccessShareByHelper);
1843-
1844-
$this->serverContainer->method('get')
1845-
->with('\OCA\Talk\Share\Helper\ShareAPIController')
1846-
->willReturn($helper);
18471850
}
18481851

18491852
$this->assertEquals($expected, $this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
18501853
}
18511854

1852-
18531855
public function testCreateShareNoPath(): void {
18541856
$this->expectException(OCSNotFoundException::class);
18551857
$this->expectExceptionMessage('Please specify a file or folder path');
@@ -2663,13 +2665,8 @@ public function testCreateShareRoom(): void {
26632665
->with('spreed')
26642666
->willReturn(true);
26652667

2666-
// This is not possible anymore with PHPUnit 10+
2667-
// as `setMethods` was removed and now real reflection is used, thus the class needs to exist.
2668-
// $helper = $this->getMockBuilder('\OCA\Talk\Share\Helper\ShareAPIController')
2669-
$helper = $this->getMockBuilder(\stdClass::class)
2670-
->addMethods(['createShare'])
2671-
->getMock();
2672-
$helper->method('createShare')
2668+
$this->mockTalkController()
2669+
->method('createShare')
26732670
->with(
26742671
$share,
26752672
'recipientRoom',
@@ -2684,10 +2681,6 @@ function ($share): void {
26842681
}
26852682
);
26862683

2687-
$this->serverContainer->method('get')
2688-
->with('\OCA\Talk\Share\Helper\ShareAPIController')
2689-
->willReturn($helper);
2690-
26912684
$this->shareManager->method('createShare')
26922685
->with($this->callback(function (IShare $share) use ($path) {
26932686
return $share->getNode() === $path
@@ -2772,13 +2765,8 @@ public function testCreateShareRoomHelperThrowException(): void {
27722765
->with('spreed')
27732766
->willReturn(true);
27742767

2775-
// This is not possible anymore with PHPUnit 10+
2776-
// as `setMethods` was removed and now real reflection is used, thus the class needs to exist.
2777-
// $helper = $this->getMockBuilder('\OCA\Talk\Share\Helper\ShareAPIController')
2778-
$helper = $this->getMockBuilder(\stdClass::class)
2779-
->addMethods(['createShare'])
2780-
->getMock();
2781-
$helper->method('createShare')
2768+
$this->mockTalkController()
2769+
->method('createShare')
27822770
->with(
27832771
$share,
27842772
'recipientRoom',
@@ -2790,10 +2778,6 @@ function ($share): void {
27902778
}
27912779
);
27922780

2793-
$this->serverContainer->method('get')
2794-
->with('\OCA\Talk\Share\Helper\ShareAPIController')
2795-
->willReturn($helper);
2796-
27972781
$this->shareManager->expects($this->never())->method('createShare');
27982782

27992783
$ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_ROOM, 'recipientRoom');
@@ -4941,6 +4925,7 @@ function ($user) {
49414925
$expects['attributes'] = \json_encode($shareParams['attributes']);
49424926
}
49434927
if (isset($shareParams['node'])) {
4928+
/** @var Node&MockObject */
49444929
$node = $this->createMock($shareParams['node']['class']);
49454930

49464931
$node->method('getMimeType')->willReturn($shareParams['node']['mimeType']);
@@ -5214,22 +5199,13 @@ public function testFormatRoomShare(array $expects, bool $helperAvailable, array
52145199
->with('spreed')
52155200
->willReturn(true);
52165201

5217-
// This is not possible anymore with PHPUnit 10+
5218-
// as `setMethods` was removed and now real reflection is used, thus the class needs to exist.
5219-
// $helper = $this->getMockBuilder('\OCA\Talk\Share\Helper\ShareAPIController')
5220-
$helper = $this->getMockBuilder(\stdClass::class)
5221-
->addMethods(['formatShare', 'canAccessShare'])
5222-
->getMock();
5223-
$helper->method('formatShare')
5202+
$helper = $this->mockTalkController();
5203+
$helper ->method('formatShare')
52245204
->with($share)
52255205
->willReturn($formatShareByHelper);
52265206
$helper->method('canAccessShare')
52275207
->with($share)
52285208
->willReturn(true);
5229-
5230-
$this->serverContainer->method('get')
5231-
->with('\OCA\Talk\Share\Helper\ShareAPIController')
5232-
->willReturn($helper);
52335209
}
52345210

52355211
$result = $this->invokePrivate($this->ocs, 'formatShare', [$share]);
@@ -5647,4 +5623,21 @@ public function testWrapperStorageUnwrapped(): void {
56475623

56485624
$this->invokePrivate($ocs, 'checkInheritedAttributes', [$share]);
56495625
}
5626+
5627+
/**
5628+
* Helper to allow testing Talk integration even if Talk
5629+
* is not available during tests.
5630+
*/
5631+
private function mockTalkController(): MockObject {
5632+
if (class_exists(\OCA\Talk\Share\Helper\ShareAPIController::class)) {
5633+
$helper = $this->createMock(\OCA\Talk\Share\Helper\ShareAPIController::class);
5634+
} else {
5635+
$helper = $this->createMock(InternalTalkShareAPIController::class);
5636+
}
5637+
5638+
$this->serverContainer->method('get')
5639+
->with(\OCA\Talk\Share\Helper\ShareAPIController::class)
5640+
->willReturn($helper);
5641+
return $helper;
5642+
}
56505643
}

build/psalm-baseline.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1471,12 +1471,6 @@
14711471
<code><![CDATA[Circles]]></code>
14721472
<code><![CDATA[Circles]]></code>
14731473
</UndefinedClass>
1474-
<UndefinedDocblockClass>
1475-
<code><![CDATA[$this->getRoomShareHelper()]]></code>
1476-
<code><![CDATA[$this->getRoomShareHelper()]]></code>
1477-
<code><![CDATA[$this->getRoomShareHelper()]]></code>
1478-
<code><![CDATA[\OCA\Talk\Share\Helper\ShareAPIController]]></code>
1479-
</UndefinedDocblockClass>
14801474
</file>
14811475
<file src="apps/files_sharing/lib/Controller/ShareController.php">
14821476
<DeprecatedClass>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/**
4+
* SPDX-FileCopyrightText: Nextcloud GmbH and Nextcloud contributors
5+
* SPDX-License-Identifier: AGPL-3.0-or-later
6+
*/
7+
namespace OCA\Talk\Share\Helper;
8+
9+
/**
10+
* Helper of OCA\Files_Sharing\Controller\ShareAPIController for room shares.
11+
*
12+
* The methods of this class are called from the ShareAPIController to perform
13+
* actions or checks specific to room shares.
14+
*/
15+
class ShareAPIController
16+
{
17+
public function __construct(protected string $userId, protected \OCA\Talk\Manager $manager, protected \OCA\Talk\Service\ParticipantService $participantService, protected \OCP\AppFramework\Utility\ITimeFactory $timeFactory, protected \OCP\IL10N $l, protected \OCP\IURLGenerator $urlGenerator)
18+
{
19+
}
20+
/**
21+
* Formats the specific fields of a room share for OCS output.
22+
*
23+
* The returned fields override those set by the main ShareAPIController.
24+
*/
25+
public function formatShare(\OCP\Share\IShare $share): array
26+
{
27+
}
28+
/**
29+
* Prepares the given share to be passed to OC\Share20\Manager::createShare.
30+
*
31+
* @throws OCSNotFoundException
32+
*/
33+
public function createShare(\OCP\Share\IShare $share, string $shareWith, int $permissions, string $expireDate): void
34+
{
35+
}
36+
/**
37+
* Returns whether the given user can access the given room share or not.
38+
*
39+
* A user can access a room share only if they are a participant of the room.
40+
*/
41+
public function canAccessShare(\OCP\Share\IShare $share, string $user): bool
42+
{
43+
}
44+
}

psalm.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
<file name="3rdparty/sabre/uri/lib/functions.php" />
9898
<file name="build/stubs/app_api.php" />
9999
<file name="build/stubs/php-polyfill.php" />
100+
<file name="build/stubs/oca_talk_share_helper_shareapicontroller.php" />
100101
</stubs>
101102
<issueHandlers>
102103
<LessSpecificReturnStatement errorLevel="error"/>

0 commit comments

Comments
 (0)