fix(tests): Fix PHPUnit deprecation warnings in tests

Turn data providers into static methods, mostly.

Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
pull/54905/head
Côme Chilliet 4 weeks ago
parent 1bbf12c465
commit ac61839d1e
No known key found for this signature in database
GPG Key ID: A3E2F658B28C760A
  1. 48
      apps/files_sharing/tests/Middleware/OCSShareAPIMiddlewareTest.php
  2. 99
      apps/settings/tests/SetupChecks/WellKnownUrlsTest.php
  3. 29
      apps/theming/tests/Settings/PersonalTest.php
  4. 4
      autotest.sh
  5. 197
      tests/lib/Collaboration/Collaborators/GroupPluginTest.php
  6. 160
      tests/lib/Collaboration/Collaborators/UserPluginTest.php
  7. 203
      tests/lib/DB/QueryBuilder/QueryBuilderTest.php

@ -4,6 +4,7 @@
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Files_Sharing\Tests\Middleware;
use OCA\Files_Sharing\Controller\ShareAPIController;
@ -13,18 +14,16 @@ use OCP\AppFramework\OCS\OCSNotFoundException;
use OCP\AppFramework\OCSController;
use OCP\IL10N;
use OCP\Share\IManager;
use PHPUnit\Framework\MockObject\MockObject;
/**
* @package OCA\Files_Sharing\Middleware\SharingCheckMiddleware
*/
class OCSShareAPIMiddlewareTest extends \Test\TestCase {
private IManager&MockObject $shareManager;
private IL10N&MockObject $l;
/** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
private $shareManager;
/** @var IL10N */
private $l;
/** @var OCSShareAPIMiddleware */
private $middleware;
private OCSShareAPIMiddleware $middleware;
protected function setUp(): void {
parent::setUp();
@ -37,49 +36,44 @@ class OCSShareAPIMiddlewareTest extends \Test\TestCase {
$this->middleware = new OCSShareAPIMiddleware($this->shareManager, $this->l);
}
public function dataBeforeController() {
public static function dataBeforeController() {
return [
[
$this->createMock(Controller::class),
Controller::class,
false,
false
],
[
$this->createMock(Controller::class),
Controller::class,
true,
false
],
[
$this->createMock(OCSController::class),
OCSController::class,
false,
false
],
[
$this->createMock(OCSController::class),
OCSController::class,
true,
false
],
[
$this->createMock(ShareAPIController::class),
ShareAPIController::class,
false,
true
],
[
$this->createMock(ShareAPIController::class),
ShareAPIController::class,
true,
false
],
];
}
/**
*
* @param Controller $controller
* @param bool $enabled
* @param bool $exception
*/
#[\PHPUnit\Framework\Attributes\DataProvider('dataBeforeController')]
public function testBeforeController(Controller $controller, $enabled, $exception): void {
public function testBeforeController(string $controllerClass, bool $enabled, bool $exception): void {
$controller = $this->createMock($controllerClass);
$this->shareManager->method('shareApiEnabled')->willReturn($enabled);
try {
@ -93,24 +87,20 @@ class OCSShareAPIMiddlewareTest extends \Test\TestCase {
public function dataAfterController() {
return [
[
$this->createMock(Controller::class),
Controller::class,
],
[
$this->createMock(OCSController::class),
OCSController::class,
],
[
$this->createMock(ShareAPIController::class),
ShareAPIController::class,
],
];
}
/**
*
* @param Controller $controller
* @param bool $called
*/
#[\PHPUnit\Framework\Attributes\DataProvider('dataAfterController')]
public function testAfterController(Controller $controller): void {
public function testAfterController(string $controllerClass): void {
$controller = $this->createMock($controllerClass);
if ($controller instanceof ShareAPIController) {
$controller->expects($this->once())->method('cleanup');
}

@ -99,12 +99,34 @@ class WellKnownUrlsTest extends TestCase {
*/
#[\PHPUnit\Framework\Attributes\DataProvider('dataTestResponses')]
public function testResponses($responses, string $expectedSeverity): void {
$createResponse = function (int $statuscode, array $header = []): IResponse&MockObject {
$response = $this->createMock(IResponse::class);
$response->expects($this->any())
->method('getStatusCode')
->willReturn($statuscode);
$response->expects($this->any())
->method('getHeader')
->willReturnCallback(fn ($name) => $header[$name] ?? '');
return $response;
};
$this->config
->expects($this->once())
->method('getSystemValueBool')
->with('check_for_working_wellknown_setup')
->willReturn(true);
/* Use generate to mock a Generator, and $createResponse to create the response objects */
$responses = array_map(
fn (array $items) => $this->generate(
array_map(
fn (array $item) => $createResponse(...$item),
$items,
)
),
$responses,
);
$this->setupcheck
->expects($this->atLeastOnce())
->method('runRequest')
@ -114,90 +136,79 @@ class WellKnownUrlsTest extends TestCase {
$this->assertEquals($expectedSeverity, $result->getSeverity());
}
public function dataTestResponses(): array {
$createResponse = function (int $statuscode, array $header = []): IResponse&MockObject {
$response = $this->createMock(IResponse::class);
$response->expects($this->any())
->method('getStatusCode')
->willReturn($statuscode);
$response->expects($this->any())
->method('getHeader')
->willReturnCallback(fn ($name) => $header[$name] ?? '');
return $response;
};
public static function dataTestResponses(): array {
$wellKnownHeader = ['X-NEXTCLOUD-WELL-KNOWN' => 'yes'];
return [
'expected codes' => [
[
$this->generate([$createResponse(200, $wellKnownHeader)]),
$this->generate([$createResponse(200, $wellKnownHeader)]),
$this->generate([$createResponse(207)]),
$this->generate([$createResponse(207)]),
[[200, $wellKnownHeader]],
[[200, $wellKnownHeader]],
[[207]],
[[207]],
],
SetupResult::SUCCESS,
],
'late response with expected codes' => [
[
$this->generate([$createResponse(404), $createResponse(200, $wellKnownHeader)]),
$this->generate([$createResponse(404), $createResponse(200, $wellKnownHeader)]),
$this->generate([$createResponse(404), $createResponse(207)]),
$this->generate([$createResponse(404), $createResponse(207)]),
[[404], [200, $wellKnownHeader]],
[[404], [200, $wellKnownHeader]],
[[404], [207]],
[[404], [207]],
],
SetupResult::SUCCESS,
],
'working but disabled webfinger' => [
[
$this->generate([$createResponse(404, $wellKnownHeader)]),
$this->generate([$createResponse(404, $wellKnownHeader)]),
$this->generate([$createResponse(207)]),
$this->generate([$createResponse(207)]),
[[404, $wellKnownHeader]],
[[404, $wellKnownHeader]],
[[207]],
[[207]],
],
SetupResult::SUCCESS,
],
'unauthorized webdav but with correct configured redirect' => [
[
$this->generate([$createResponse(404, $wellKnownHeader)]),
$this->generate([$createResponse(404, $wellKnownHeader)]),
$this->generate([$createResponse(401, ['X-Guzzle-Redirect-History' => 'https://example.com,https://example.com/remote.php/dav/'])]),
$this->generate([$createResponse(401, ['X-Guzzle-Redirect-History' => 'https://example.com/remote.php/dav/'])]),
[[404, $wellKnownHeader]],
[[404, $wellKnownHeader]],
[[401, ['X-Guzzle-Redirect-History' => 'https://example.com,https://example.com/remote.php/dav/']]],
[[401, ['X-Guzzle-Redirect-History' => 'https://example.com/remote.php/dav/']]],
],
SetupResult::SUCCESS,
],
'not configured path' => [
[
$this->generate([$createResponse(404)]),
$this->generate([$createResponse(404)]),
$this->generate([$createResponse(404)]),
$this->generate([$createResponse(404)]),
[[404]],
[[404]],
[[404]],
[[404]],
],
SetupResult::WARNING,
],
'Invalid webfinger' => [
[
$this->generate([$createResponse(404)]),
$this->generate([$createResponse(404, $wellKnownHeader)]),
$this->generate([$createResponse(207)]),
$this->generate([$createResponse(207)]),
[[404]],
[[404, $wellKnownHeader]],
[[207]],
[[207]],
],
SetupResult::WARNING,
],
'Invalid nodeinfo' => [
[
$this->generate([$createResponse(404, $wellKnownHeader)]),
$this->generate([$createResponse(404)]),
$this->generate([$createResponse(207)]),
$this->generate([$createResponse(207)]),
[[404, $wellKnownHeader]],
[[404]],
[[207]],
[[207]],
],
SetupResult::WARNING,
],
'Invalid caldav' => [
[
$this->generate([$createResponse(404, $wellKnownHeader)]),
$this->generate([$createResponse(404, $wellKnownHeader)]),
$this->generate([$createResponse(404)]),
$this->generate([$createResponse(207)]),
[[404, $wellKnownHeader]],
[[404, $wellKnownHeader]],
[[404]],
[[207]],
],
SetupResult::WARNING,
],

@ -29,6 +29,7 @@ use OCP\IL10N;
use OCP\INavigationManager;
use OCP\IURLGenerator;
use OCP\IUserSession;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;
@ -69,28 +70,30 @@ class PersonalTest extends TestCase {
);
}
public function dataTestGetForm(): array {
public static function dataTestGetForm(): array {
return [
['', [
$this->formatThemeForm('default'),
$this->formatThemeForm('light'),
$this->formatThemeForm('dark'),
$this->formatThemeForm('light-highcontrast'),
$this->formatThemeForm('dark-highcontrast'),
$this->formatThemeForm('opendyslexic'),
'default',
'light',
'dark',
'light-highcontrast',
'dark-highcontrast',
'opendyslexic',
]],
['dark', [
$this->formatThemeForm('dark'),
$this->formatThemeForm('opendyslexic'),
'dark',
'opendyslexic',
]],
];
}
/**
* @param string[] $enabledThemes
*/
#[\PHPUnit\Framework\Attributes\DataProvider('dataTestGetForm')]
#[DataProvider('dataTestGetForm')]
public function testGetForm(string $enforcedTheme, array $themesState): void {
$themesState = array_map(
$this->formatThemeForm(...),
$themesState
);
$this->config->expects($this->once())
->method('getSystemValueString')
->with('enforce_theme', '')

@ -396,8 +396,8 @@ function execute_tests {
echo "No coverage"
fi
echo "$PHPUNIT" --fail-on-warning --fail-on-risky --display-deprecations --display-phpunit-deprecations --colors=always --configuration phpunit-autotest.xml $GROUP $COVER --log-junit "autotest-results-$DB.xml" "$2" "$3"
"$PHPUNIT" --fail-on-warning --fail-on-risky --display-deprecations --display-phpunit-deprecations --colors=always --configuration phpunit-autotest.xml $GROUP $COVER --log-junit "autotest-results-$DB.xml" "$2" "$3"
echo "$PHPUNIT" --fail-on-warning --fail-on-risky --display-warnings --display-deprecations --display-phpunit-deprecations --colors=always --configuration phpunit-autotest.xml $GROUP $COVER --log-junit "autotest-results-$DB.xml" "$2" "$3"
"$PHPUNIT" --fail-on-warning --fail-on-risky --display-warnings --display-deprecations --display-phpunit-deprecations --colors=always --configuration phpunit-autotest.xml $GROUP $COVER --log-junit "autotest-results-$DB.xml" "$2" "$3"
RESULT=$?
if [ "$PRIMARY_STORAGE_CONFIG" == "swift" ] ; then

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
@ -16,32 +18,23 @@ use OCP\IGroupManager;
use OCP\IUser;
use OCP\IUserSession;
use OCP\Share\IShare;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;
class GroupPluginTest extends TestCase {
/** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
protected $config;
/** @var IGroupManager|\PHPUnit\Framework\MockObject\MockObject */
protected $groupManager;
/** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
protected $session;
private IConfig&MockObject $config;
private IGroupManager&MockObject $groupManager;
private IUserSession&MockObject $session;
private IUser&MockObject $user;
/** @var ISearchResult */
protected $searchResult;
protected ISearchResult $searchResult;
/** @var GroupPlugin */
protected $plugin;
/** @var int */
protected $limit = 2;
/** @var int */
protected $offset = 0;
/** @var IUser|\PHPUnit\Framework\MockObject\MockObject */
protected $user;
protected int $limit = 2;
protected int $offset = 0;
protected function setUp(): void {
parent::setUp();
@ -57,7 +50,7 @@ class GroupPluginTest extends TestCase {
$this->user = $this->getUserMock('admin', 'Administrator');
}
public function instantiatePlugin() {
public function instantiatePlugin(): void {
// cannot be done within setUp, because dependent mocks needs to be set
// up with configuration etc. first
$this->plugin = new GroupPlugin(
@ -67,7 +60,7 @@ class GroupPluginTest extends TestCase {
);
}
public function getUserMock($uid, $displayName) {
public function getUserMock(string $uid, string $displayName): IUser&MockObject {
$user = $this->createMock(IUser::class);
$user->expects($this->any())
@ -81,13 +74,7 @@ class GroupPluginTest extends TestCase {
return $user;
}
/**
* @param string $gid
* @param null $displayName
* @param bool $hide
* @return IGroup|\PHPUnit\Framework\MockObject\MockObject
*/
protected function getGroupMock($gid, $displayName = null, $hide = false) {
protected function getGroupMock(string $gid, ?string $displayName = null, bool $hide = false): IGroup&MockObject {
$group = $this->createMock(IGroup::class);
$group->expects($this->any())
@ -109,7 +96,7 @@ class GroupPluginTest extends TestCase {
return $group;
}
public function dataGetGroups(): array {
public static function dataGetGroups(): array {
return [
['test', false, true, false, [], [], [], [], true, false],
['test', false, false, false, [], [], [], [], true, false],
@ -118,7 +105,7 @@ class GroupPluginTest extends TestCase {
// group without display name
[
'test', false, true, false,
[$this->getGroupMock('test1')],
[['test1']],
[],
[],
[['label' => 'test1', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']]],
@ -128,7 +115,7 @@ class GroupPluginTest extends TestCase {
// group with display name, search by id
[
'test', false, true, false,
[$this->getGroupMock('test1', 'Test One')],
[['test1', 'Test One']],
[],
[],
[['label' => 'Test One', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']]],
@ -138,7 +125,7 @@ class GroupPluginTest extends TestCase {
// group with display name, search by display name
[
'one', false, true, false,
[$this->getGroupMock('test1', 'Test One')],
[['test1', 'Test One']],
[],
[],
[['label' => 'Test One', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']]],
@ -148,7 +135,7 @@ class GroupPluginTest extends TestCase {
// group with display name, search by display name, exact expected
[
'Test One', false, true, false,
[$this->getGroupMock('test1', 'Test One')],
[['test1', 'Test One']],
[],
[['label' => 'Test One', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']]],
[],
@ -157,7 +144,7 @@ class GroupPluginTest extends TestCase {
],
[
'test', false, false, false,
[$this->getGroupMock('test1')],
[['test1']],
[],
[],
[],
@ -167,8 +154,8 @@ class GroupPluginTest extends TestCase {
[
'test', false, true, false,
[
$this->getGroupMock('test'),
$this->getGroupMock('test1'),
['test'],
['test1'],
],
[],
[['label' => 'test', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test']]],
@ -179,8 +166,8 @@ class GroupPluginTest extends TestCase {
[
'test', false, false, false,
[
$this->getGroupMock('test'),
$this->getGroupMock('test1'),
['test'],
['test1'],
],
[],
[['label' => 'test', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test']]],
@ -191,8 +178,8 @@ class GroupPluginTest extends TestCase {
[
'test', false, true, false,
[
$this->getGroupMock('test0'),
$this->getGroupMock('test1'),
['test0'],
['test1'],
],
[],
[],
@ -201,25 +188,25 @@ class GroupPluginTest extends TestCase {
['label' => 'test1', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']],
],
false,
null,
false,
],
[
'test', false, false, false,
[
$this->getGroupMock('test0'),
$this->getGroupMock('test1'),
['test0'],
['test1'],
],
[],
[],
[],
true,
null,
false,
],
[
'test', false, true, false,
[
$this->getGroupMock('test0'),
$this->getGroupMock('test1'),
['test0'],
['test1'],
],
[],
[
@ -230,13 +217,13 @@ class GroupPluginTest extends TestCase {
['label' => 'test1', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']],
],
false,
$this->getGroupMock('test'),
['test'],
],
[
'test', false, false, false,
[
$this->getGroupMock('test0'),
$this->getGroupMock('test1'),
['test0'],
['test1'],
],
[],
[
@ -244,17 +231,17 @@ class GroupPluginTest extends TestCase {
],
[],
true,
$this->getGroupMock('test'),
['test'],
],
['test', true, true, false, [], [], [], [], true, false],
['test', true, false, false, [], [], [], [], true, false],
[
'test', true, true, false,
[
$this->getGroupMock('test1'),
$this->getGroupMock('test2'),
['test1'],
['test2'],
],
[$this->getGroupMock('test1')],
[['test1']],
[],
[['label' => 'test1', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']]],
false,
@ -263,10 +250,10 @@ class GroupPluginTest extends TestCase {
[
'test', true, false, false,
[
$this->getGroupMock('test1'),
$this->getGroupMock('test2'),
['test1'],
['test2'],
],
[$this->getGroupMock('test1')],
[['test1']],
[],
[],
true,
@ -275,10 +262,10 @@ class GroupPluginTest extends TestCase {
[
'test', true, true, false,
[
$this->getGroupMock('test'),
$this->getGroupMock('test1'),
['test'],
['test1'],
],
[$this->getGroupMock('test')],
[['test']],
[['label' => 'test', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test']]],
[],
false,
@ -287,10 +274,10 @@ class GroupPluginTest extends TestCase {
[
'test', true, false, false,
[
$this->getGroupMock('test'),
$this->getGroupMock('test1'),
['test'],
['test1'],
],
[$this->getGroupMock('test')],
[['test']],
[['label' => 'test', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test']]],
[],
true,
@ -299,10 +286,10 @@ class GroupPluginTest extends TestCase {
[
'test', true, true, false,
[
$this->getGroupMock('test'),
$this->getGroupMock('test1'),
['test'],
['test1'],
],
[$this->getGroupMock('test1')],
[['test1']],
[],
[['label' => 'test1', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']]],
false,
@ -311,10 +298,10 @@ class GroupPluginTest extends TestCase {
[
'test', true, false, false,
[
$this->getGroupMock('test'),
$this->getGroupMock('test1'),
['test'],
['test1'],
],
[$this->getGroupMock('test1')],
[['test1']],
[],
[],
true,
@ -323,10 +310,10 @@ class GroupPluginTest extends TestCase {
[
'test', true, true, false,
[
$this->getGroupMock('test'),
$this->getGroupMock('test1'),
['test'],
['test1'],
],
[$this->getGroupMock('test'), $this->getGroupMock('test0'), $this->getGroupMock('test1')],
[['test'], ['test0'], ['test1']],
[['label' => 'test', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test']]],
[['label' => 'test1', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']]],
false,
@ -335,10 +322,10 @@ class GroupPluginTest extends TestCase {
[
'test', true, false, false,
[
$this->getGroupMock('test'),
$this->getGroupMock('test1'),
['test'],
['test1'],
],
[$this->getGroupMock('test'), $this->getGroupMock('test0'), $this->getGroupMock('test1')],
[['test'], ['test0'], ['test1']],
[['label' => 'test', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test']]],
[],
true,
@ -347,37 +334,37 @@ class GroupPluginTest extends TestCase {
[
'test', true, true, false,
[
$this->getGroupMock('test0'),
$this->getGroupMock('test1'),
['test0'],
['test1'],
],
[$this->getGroupMock('test'), $this->getGroupMock('test0'), $this->getGroupMock('test1')],
[['test'], ['test0'], ['test1']],
[],
[
['label' => 'test0', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test0']],
['label' => 'test1', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']],
],
false,
null,
false,
],
[
'test', true, false, false,
[
$this->getGroupMock('test0'),
$this->getGroupMock('test1'),
['test0'],
['test1'],
],
[$this->getGroupMock('test'), $this->getGroupMock('test0'), $this->getGroupMock('test1')],
[['test'], ['test0'], ['test1']],
[],
[],
true,
null,
false,
],
[
'test', true, true, false,
[
$this->getGroupMock('test0'),
$this->getGroupMock('test1'),
['test0'],
['test1'],
],
[$this->getGroupMock('test'), $this->getGroupMock('test0'), $this->getGroupMock('test1')],
[['test'], ['test0'], ['test1']],
[
['label' => 'test', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test']],
],
@ -386,27 +373,27 @@ class GroupPluginTest extends TestCase {
['label' => 'test1', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']],
],
false,
$this->getGroupMock('test'),
['test'],
],
[
'test', true, false, false,
[
$this->getGroupMock('test0'),
$this->getGroupMock('test1'),
['test0'],
['test1'],
],
[$this->getGroupMock('test'), $this->getGroupMock('test0'), $this->getGroupMock('test1')],
[['test'], ['test0'], ['test1']],
[
['label' => 'test', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test']],
],
[],
true,
$this->getGroupMock('test'),
['test'],
],
[
'test', false, false, false,
[
$this->getGroupMock('test', null, true),
$this->getGroupMock('test1'),
['test', null, true],
['test1'],
],
[],
[],
@ -417,20 +404,7 @@ class GroupPluginTest extends TestCase {
];
}
/**
*
* @param string $searchTerm
* @param bool $shareWithGroupOnly
* @param bool $shareeEnumeration
* @param bool $groupSharingDisabled
* @param array $groupResponse
* @param array $userGroupsResponse
* @param array $exactExpected
* @param array $expected
* @param bool $reachedEnd
* @param bool|IGroup $singleGroup
*/
#[\PHPUnit\Framework\Attributes\DataProvider('dataGetGroups')]
#[DataProvider('dataGetGroups')]
public function testSearch(
string $searchTerm,
bool $shareWithGroupOnly,
@ -441,8 +415,19 @@ class GroupPluginTest extends TestCase {
array $exactExpected,
array $expected,
bool $reachedEnd,
$singleGroup,
array|false $singleGroup,
): void {
$groupResponse = array_map(
fn ($args) => $this->getGroupMock(...$args),
$groupResponse
);
if (is_array($singleGroup)) {
$singleGroup = $this->getGroupMock(...$singleGroup);
}
$userGroupsResponse = array_map(
fn ($args) => $this->getGroupMock(...$args),
$userGroupsResponse
);
$this->config->expects($this->any())
->method('getAppValue')
->willReturnCallback(

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
@ -12,34 +14,24 @@ use OC\Collaboration\Collaborators\UserPlugin;
use OC\KnownUser\KnownUserService;
use OCP\Collaboration\Collaborators\ISearchResult;
use OCP\IConfig;
use OCP\IGroup;
use OCP\IGroupManager;
use OCP\IUser;
use OCP\IUserManager;
use OCP\IUserSession;
use OCP\Share\IShare;
use OCP\UserStatus\IManager as IUserStatusManager;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;
class UserPluginTest extends TestCase {
/** @var IConfig|MockObject */
protected $config;
/** @var IUserManager|MockObject */
protected $userManager;
/** @var IGroupManager|MockObject */
protected $groupManager;
/** @var IUserSession|MockObject */
protected $session;
/** @var KnownUserService|MockObject */
protected $knownUserService;
/** @var IUserStatusManager|MockObject */
protected $userStatusManager;
private IConfig&MockObject $config;
private IUserManager&MockObject $userManager;
private IGroupManager&MockObject $groupManager;
private IUserSession&MockObject $session;
private KnownUserService&MockObject $knownUserService;
private IUserStatusManager&MockObject $userStatusManager;
private IUser&MockObject $user;
/** @var UserPlugin */
protected $plugin;
@ -51,9 +43,6 @@ class UserPluginTest extends TestCase {
protected int $offset = 0;
/** @var IUser|MockObject */
protected $user;
protected function setUp(): void {
parent::setUp();
@ -74,7 +63,7 @@ class UserPluginTest extends TestCase {
$this->user = $this->getUserMock('admin', 'Administrator');
}
public function instantiatePlugin() {
public function instantiatePlugin(): void {
// cannot be done within setUp, because dependent mocks needs to be set
// up with configuration etc. first
$this->plugin = new UserPlugin(
@ -87,7 +76,7 @@ class UserPluginTest extends TestCase {
);
}
public function mockConfig($mockedSettings) {
public function mockConfig($mockedSettings): void {
$this->config->expects($this->any())
->method('getAppValue')
->willReturnCallback(
@ -97,7 +86,7 @@ class UserPluginTest extends TestCase {
);
}
public function getUserMock($uid, $displayName, $enabled = true, $groups = []) {
public function getUserMock(string $uid, string $displayName, bool $enabled = true, array $groups = []): IUser&MockObject {
$user = $this->createMock(IUser::class);
$user->expects($this->any())
@ -115,17 +104,7 @@ class UserPluginTest extends TestCase {
return $user;
}
public function getGroupMock($gid) {
$group = $this->createMock(IGroup::class);
$group->expects($this->any())
->method('getGID')
->willReturn($gid);
return $group;
}
public function dataGetUsers(): array {
public static function dataGetUsers(): array {
return [
['test', false, true, [], [], [], [], true, false],
['test', false, false, [], [], [], [], true, false],
@ -135,33 +114,33 @@ class UserPluginTest extends TestCase {
'test', false, true, [], [],
[
['label' => 'Test', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test'],
], [], true, $this->getUserMock('test', 'Test'),
], [], true, ['test', 'Test'],
],
[
'test', false, false, [], [],
[
['label' => 'Test', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test'],
], [], true, $this->getUserMock('test', 'Test'),
], [], true, ['test', 'Test'],
],
[
'test', true, true, [], [],
[], [], true, $this->getUserMock('test', 'Test'),
[], [], true, ['test', 'Test'],
],
[
'test', true, false, [], [],
[], [], true, $this->getUserMock('test', 'Test'),
[], [], true, ['test', 'Test'],
],
[
'test', true, true, ['test-group'], [['test-group', 'test', 2, 0, []]],
[
['label' => 'Test', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test'],
], [], true, $this->getUserMock('test', 'Test'),
], [], true, ['test', 'Test'],
],
[
'test', true, false, ['test-group'], [['test-group', 'test', 2, 0, []]],
[
['label' => 'Test', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test'],
], [], true, $this->getUserMock('test', 'Test'),
], [], true, ['test', 'Test'],
],
[
'test',
@ -169,7 +148,7 @@ class UserPluginTest extends TestCase {
true,
[],
[
$this->getUserMock('test1', 'Test One'),
['test1', 'Test One'],
],
[],
[
@ -184,7 +163,7 @@ class UserPluginTest extends TestCase {
false,
[],
[
$this->getUserMock('test1', 'Test One'),
['test1', 'Test One'],
],
[],
[],
@ -197,8 +176,8 @@ class UserPluginTest extends TestCase {
true,
[],
[
$this->getUserMock('test1', 'Test One'),
$this->getUserMock('test2', 'Test Two'),
['test1', 'Test One'],
['test2', 'Test Two'],
],
[],
[
@ -214,8 +193,8 @@ class UserPluginTest extends TestCase {
false,
[],
[
$this->getUserMock('test1', 'Test One'),
$this->getUserMock('test2', 'Test Two'),
['test1', 'Test One'],
['test2', 'Test Two'],
],
[],
[],
@ -228,9 +207,9 @@ class UserPluginTest extends TestCase {
true,
[],
[
$this->getUserMock('test0', 'Test'),
$this->getUserMock('test1', 'Test One'),
$this->getUserMock('test2', 'Test Two'),
['test0', 'Test'],
['test1', 'Test One'],
['test2', 'Test Two'],
],
[
['label' => 'Test', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test0'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test0'],
@ -248,9 +227,9 @@ class UserPluginTest extends TestCase {
true,
[],
[
$this->getUserMock('test0', 'Test'),
$this->getUserMock('test1', 'Test One'),
$this->getUserMock('test2', 'Test Two'),
['test0', 'Test'],
['test1', 'Test One'],
['test2', 'Test Two'],
],
[
['label' => 'Test', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test0'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test0'],
@ -270,9 +249,9 @@ class UserPluginTest extends TestCase {
false,
[],
[
$this->getUserMock('test0', 'Test'),
$this->getUserMock('test1', 'Test One'),
$this->getUserMock('test2', 'Test Two'),
['test0', 'Test'],
['test1', 'Test One'],
['test2', 'Test Two'],
],
[
['label' => 'Test', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test0'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test0'],
@ -296,7 +275,7 @@ class UserPluginTest extends TestCase {
],
true,
false,
[['test1', $this->getUserMock('test1', 'Test One')]],
[['test1', ['test1', 'Test One']]],
],
[
'test',
@ -311,7 +290,7 @@ class UserPluginTest extends TestCase {
[],
true,
false,
[['test1', $this->getUserMock('test1', 'Test One')]],
[['test1', ['test1', 'Test One']]],
],
[
'test',
@ -336,8 +315,8 @@ class UserPluginTest extends TestCase {
true,
false,
[
['test1', $this->getUserMock('test1', 'Test One')],
['test2', $this->getUserMock('test2', 'Test Two')],
['test1', ['test1', 'Test One']],
['test2', ['test2', 'Test Two']],
],
],
[
@ -360,8 +339,8 @@ class UserPluginTest extends TestCase {
true,
false,
[
['test1', $this->getUserMock('test1', 'Test One')],
['test2', $this->getUserMock('test2', 'Test Two')],
['test1', ['test1', 'Test One']],
['test2', ['test2', 'Test Two']],
],
],
[
@ -386,8 +365,8 @@ class UserPluginTest extends TestCase {
false,
false,
[
['test', $this->getUserMock('test', 'Test One')],
['test2', $this->getUserMock('test2', 'Test Two')],
['test', ['test', 'Test One']],
['test2', ['test2', 'Test Two']],
],
],
[
@ -410,40 +389,34 @@ class UserPluginTest extends TestCase {
true,
false,
[
['test', $this->getUserMock('test', 'Test One')],
['test2', $this->getUserMock('test2', 'Test Two')],
['test', ['test', 'Test One']],
['test2', ['test2', 'Test Two']],
],
],
];
}
/**
*
* @param string $searchTerm
* @param bool $shareWithGroupOnly
* @param bool $shareeEnumeration
* @param array $groupResponse
* @param array $userResponse
* @param array $exactExpected
* @param array $expected
* @param bool $reachedEnd
* @param bool|IUser $singleUser
* @param array $users
*/
#[\PHPUnit\Framework\Attributes\DataProvider('dataGetUsers')]
#[DataProvider('dataGetUsers')]
public function testSearch(
$searchTerm,
$shareWithGroupOnly,
$shareeEnumeration,
string $searchTerm,
bool $shareWithGroupOnly,
bool $shareeEnumeration,
array $groupResponse,
array $userResponse,
array $exactExpected,
array $expected,
$reachedEnd,
$singleUser,
bool $reachedEnd,
array|false $singleUser,
array $users = [],
$shareeEnumerationPhone = false,
bool $shareeEnumerationPhone = false,
): void {
if ($singleUser !== false) {
$singleUser = $this->getUserMock(...$singleUser);
}
$users = array_map(
fn ($args) => [$args[0], $this->getUserMock(...$args[1])],
$users
);
$this->mockConfig(['core' => [
'shareapi_only_share_with_group_members' => $shareWithGroupOnly ? 'yes' : 'no',
'shareapi_allow_share_dialog_user_enumeration' => $shareeEnumeration? 'yes' : 'no',
@ -458,6 +431,10 @@ class UserPluginTest extends TestCase {
->willReturn($this->user);
if (!$shareWithGroupOnly) {
$userResponse = array_map(
fn ($args) => $this->getUserMock(...$args),
$userResponse
);
if ($shareeEnumerationPhone) {
$this->userManager->expects($this->once())
->method('searchKnownUsersByDisplayName')
@ -534,13 +511,8 @@ class UserPluginTest extends TestCase {
];
}
/**
* @param array $users
* @param array $expectedUIDs
* @param $currentUserId
*/
#[\PHPUnit\Framework\Attributes\DataProvider('takeOutCurrentUserProvider')]
public function testTakeOutCurrentUser(array $users, array $expectedUIDs, $currentUserId): void {
#[DataProvider('takeOutCurrentUserProvider')]
public function testTakeOutCurrentUser(array $users, array $expectedUIDs, ?string $currentUserId): void {
$this->instantiatePlugin();
$this->session->expects($this->once())
@ -717,7 +689,7 @@ class UserPluginTest extends TestCase {
];
}
#[\PHPUnit\Framework\Attributes\DataProvider('dataSearchEnumeration')]
#[DataProvider('dataSearchEnumeration')]
public function testSearchEnumerationLimit($search, $userGroups, $matchingUsers, $result, $mockedSettings): void {
$this->mockConfig($mockedSettings);

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
@ -15,11 +17,11 @@ use OC\DB\QueryBuilder\Parameter;
use OC\DB\QueryBuilder\QueryBuilder;
use OC\SystemConfig;
use OCP\DB\IResult;
use OCP\DB\QueryBuilder\ILiteral;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\DB\QueryBuilder\IQueryFunction;
use OCP\IDBConnection;
use OCP\Server;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
/**
@ -30,17 +32,11 @@ use Psr\Log\LoggerInterface;
* @package Test\DB\QueryBuilder
*/
class QueryBuilderTest extends \Test\TestCase {
/** @var QueryBuilder */
protected $queryBuilder;
/** @var IDBConnection */
protected $connection;
/** @var SystemConfig|\PHPUnit\Framework\MockObject\MockObject */
protected $config;
private SystemConfig&MockObject $config;
private LoggerInterface&MockObject $logger;
/** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
protected $logger;
private QueryBuilder $queryBuilder;
private IDBConnection $connection;
protected function setUp(): void {
parent::setUp();
@ -105,7 +101,7 @@ class QueryBuilderTest extends \Test\TestCase {
* @param int|null $firstResult
* @param array $expectedSet
*/
#[\PHPUnit\Framework\Attributes\DataProvider('dataFirstResult')]
#[DataProvider('dataFirstResult')]
public function testFirstResult($firstResult, $expectedSet): void {
$this->deleteTestingRows();
$this->createTestingRows();
@ -142,7 +138,7 @@ class QueryBuilderTest extends \Test\TestCase {
* @param int $maxResult
* @param array $expectedSet
*/
#[\PHPUnit\Framework\Attributes\DataProvider('dataMaxResults')]
#[DataProvider('dataMaxResults')]
public function testMaxResults($maxResult, $expectedSet): void {
$this->deleteTestingRows();
$this->createTestingRows();
@ -164,10 +160,7 @@ class QueryBuilderTest extends \Test\TestCase {
$this->deleteTestingRows();
}
public function dataSelect(): array {
$config = $this->createMock(SystemConfig::class);
$logger = $this->createMock(LoggerInterface::class);
$queryBuilder = new QueryBuilder(Server::get(IDBConnection::class), $config, $logger);
public static function dataSelect(): array {
return [
// select('column1')
[['configvalue'], ['configvalue' => '99']],
@ -179,31 +172,30 @@ class QueryBuilderTest extends \Test\TestCase {
[[['configvalue', 'configkey']], ['configvalue' => '99', 'configkey' => 'testing1']],
// select(new Literal('column1'))
[[$queryBuilder->expr()->literal('column1')], [], 'column1'],
[['l::column1'], [], 'column1'],
// select('column1', 'column2')
[[$queryBuilder->expr()->literal('column1'), 'configkey'], ['configkey' => 'testing1'], 'column1'],
// select(new Literal('column1'), 'column2')
[['l::column1', 'configkey'], ['configkey' => 'testing1'], 'column1'],
// select(['column1', 'column2'])
[[[$queryBuilder->expr()->literal('column1'), 'configkey']], ['configkey' => 'testing1'], 'column1'],
// select([new Literal('column1'), 'column2'])
[[['l::column1', 'configkey']], ['configkey' => 'testing1'], 'column1'],
];
}
/**
*
* @param array $selectArguments
* @param array $expected
* @param string $expectedLiteral
*/
#[\PHPUnit\Framework\Attributes\DataProvider('dataSelect')]
public function testSelect($selectArguments, $expected, $expectedLiteral = ''): void {
#[DataProvider('dataSelect')]
public function testSelect(array $selectArguments, array $expected, string $expectedLiteral = ''): void {
$this->deleteTestingRows();
$this->createTestingRows();
call_user_func_array(
[$this->queryBuilder, 'select'],
$selectArguments
array_walk_recursive(
$selectArguments,
function (string &$arg) {
if (\str_starts_with($arg, 'l::')) {
$arg = $this->queryBuilder->expr()->literal(substr($arg, 3));
}
},
);
$this->queryBuilder->select(...$selectArguments);
$this->queryBuilder->from('*PREFIX*appconfig')
->where($this->queryBuilder->expr()->eq(
@ -232,18 +224,19 @@ class QueryBuilderTest extends \Test\TestCase {
$this->deleteTestingRows();
}
public function dataSelectAlias(): array {
$config = $this->createMock(SystemConfig::class);
$logger = $this->createMock(LoggerInterface::class);
$queryBuilder = new QueryBuilder(Server::get(IDBConnection::class), $config, $logger);
public static function dataSelectAlias(): array {
return [
['configvalue', 'cv', ['cv' => '99']],
[$queryBuilder->expr()->literal('column1'), 'thing', ['thing' => 'column1']],
['l::column1', 'thing', ['thing' => 'column1']],
];
}
#[\PHPUnit\Framework\Attributes\DataProvider('dataSelectAlias')]
public function testSelectAlias(string|ILiteral $select, string $alias, array $expected): void {
#[DataProvider('dataSelectAlias')]
public function testSelectAlias(string $select, string $alias, array $expected): void {
if (str_starts_with($select, 'l::')) {
$select = $this->queryBuilder->expr()->literal(substr($select, 3));
}
$this->deleteTestingRows();
$this->createTestingRows();
@ -335,10 +328,7 @@ class QueryBuilderTest extends \Test\TestCase {
$this->deleteTestingRows('testFirstResult2');
}
public function dataAddSelect(): array {
$config = $this->createMock(SystemConfig::class);
$logger = $this->createMock(LoggerInterface::class);
$queryBuilder = new QueryBuilder(Server::get(IDBConnection::class), $config, $logger);
public static function dataAddSelect(): array {
return [
// addSelect('column1')
[['configvalue'], ['appid' => 'testFirstResult', 'configvalue' => '99']],
@ -350,27 +340,30 @@ class QueryBuilderTest extends \Test\TestCase {
[[['configvalue', 'configkey']], ['appid' => 'testFirstResult', 'configvalue' => '99', 'configkey' => 'testing1']],
// select(new Literal('column1'))
[[$queryBuilder->expr()->literal('column1')], ['appid' => 'testFirstResult'], 'column1'],
[['l::column1'], ['appid' => 'testFirstResult'], 'column1'],
// select('column1', 'column2')
[[$queryBuilder->expr()->literal('column1'), 'configkey'], ['appid' => 'testFirstResult', 'configkey' => 'testing1'], 'column1'],
// select(new Literal('column1'), 'column2')
[['l::column1', 'configkey'], ['appid' => 'testFirstResult', 'configkey' => 'testing1'], 'column1'],
// select(['column1', 'column2'])
[[[$queryBuilder->expr()->literal('column1'), 'configkey']], ['appid' => 'testFirstResult', 'configkey' => 'testing1'], 'column1'],
// select([new Literal('column1'), 'column2'])
[[['l::column1', 'configkey']], ['appid' => 'testFirstResult', 'configkey' => 'testing1'], 'column1'],
];
}
/**
*
* @param array $selectArguments
* @param array $expected
* @param string $expectedLiteral
*/
#[\PHPUnit\Framework\Attributes\DataProvider('dataAddSelect')]
public function testAddSelect($selectArguments, $expected, $expectedLiteral = ''): void {
#[DataProvider('dataAddSelect')]
public function testAddSelect(array $selectArguments, array $expected, string $expectedLiteral = ''): void {
$this->deleteTestingRows();
$this->createTestingRows();
array_walk_recursive(
$selectArguments,
function (string &$arg) {
if (\str_starts_with($arg, 'l::')) {
$arg = $this->queryBuilder->expr()->literal(substr($arg, 3));
}
},
);
$this->queryBuilder->select('appid');
call_user_func_array(
@ -419,7 +412,7 @@ class QueryBuilderTest extends \Test\TestCase {
* @param array $expectedQueryPart
* @param string $expectedQuery
*/
#[\PHPUnit\Framework\Attributes\DataProvider('dataDelete')]
#[DataProvider('dataDelete')]
public function testDelete($tableName, $tableAlias, $expectedQueryPart, $expectedQuery): void {
$this->queryBuilder->delete($tableName, $tableAlias);
@ -448,7 +441,7 @@ class QueryBuilderTest extends \Test\TestCase {
* @param array $expectedQueryPart
* @param string $expectedQuery
*/
#[\PHPUnit\Framework\Attributes\DataProvider('dataUpdate')]
#[DataProvider('dataUpdate')]
public function testUpdate($tableName, $tableAlias, $expectedQueryPart, $expectedQuery): void {
$this->queryBuilder->update($tableName, $tableAlias);
@ -475,7 +468,7 @@ class QueryBuilderTest extends \Test\TestCase {
* @param array $expectedQueryPart
* @param string $expectedQuery
*/
#[\PHPUnit\Framework\Attributes\DataProvider('dataInsert')]
#[DataProvider('dataInsert')]
public function testInsert($tableName, $expectedQueryPart, $expectedQuery): void {
$this->queryBuilder->insert($tableName);
@ -490,12 +483,9 @@ class QueryBuilderTest extends \Test\TestCase {
);
}
public function dataFrom(): array {
$config = $this->createMock(SystemConfig::class);
$logger = $this->createMock(LoggerInterface::class);
$qb = new QueryBuilder(Server::get(IDBConnection::class), $config, $logger);
public static function dataFrom(): array {
return [
[$qb->createFunction('(' . $qb->select('*')->from('test')->getSQL() . ')'), 'q', null, null, [
['function', 'q', null, null, [
['table' => '(SELECT * FROM `*PREFIX*test`)', 'alias' => '`q`']
], '(SELECT * FROM `*PREFIX*test`) `q`'],
['data', null, null, null, [['table' => '`*PREFIX*data`', 'alias' => null]], '`*PREFIX*data`'],
@ -511,17 +501,15 @@ class QueryBuilderTest extends \Test\TestCase {
];
}
/**
*
* @param string|IQueryFunction $table1Name
* @param string $table1Alias
* @param string|IQueryFunction $table2Name
* @param string $table2Alias
* @param array $expectedQueryPart
* @param string $expectedQuery
*/
#[\PHPUnit\Framework\Attributes\DataProvider('dataFrom')]
public function testFrom($table1Name, $table1Alias, $table2Name, $table2Alias, $expectedQueryPart, $expectedQuery): void {
#[DataProvider('dataFrom')]
public function testFrom(string $table1Name, ?string $table1Alias, ?string $table2Name, ?string $table2Alias, array $expectedQueryPart, string $expectedQuery): void {
$config = $this->createMock(SystemConfig::class);
$logger = $this->createMock(LoggerInterface::class);
$queryBuilder = new QueryBuilder(Server::get(IDBConnection::class), $config, $logger);
if ($table1Name === 'function') {
$table1Name = $queryBuilder->createFunction('(' . $queryBuilder->select('*')->from('test')->getSQL() . ')');
}
$this->queryBuilder->from($table1Name, $table1Alias);
if ($table2Name !== null) {
$this->queryBuilder->from($table2Name, $table2Alias);
@ -568,7 +556,7 @@ class QueryBuilderTest extends \Test\TestCase {
* @param array $expectedQueryPart
* @param string $expectedQuery
*/
#[\PHPUnit\Framework\Attributes\DataProvider('dataJoin')]
#[DataProvider('dataJoin')]
public function testJoin($fromAlias, $tableName, $tableAlias, $condition, $expectedQueryPart, $expectedQuery): void {
$this->queryBuilder->from('data1', 'd1');
$this->queryBuilder->join(
@ -598,7 +586,7 @@ class QueryBuilderTest extends \Test\TestCase {
* @param array $expectedQueryPart
* @param string $expectedQuery
*/
#[\PHPUnit\Framework\Attributes\DataProvider('dataJoin')]
#[DataProvider('dataJoin')]
public function testInnerJoin($fromAlias, $tableName, $tableAlias, $condition, $expectedQueryPart, $expectedQuery): void {
$this->queryBuilder->from('data1', 'd1');
$this->queryBuilder->innerJoin(
@ -648,7 +636,7 @@ class QueryBuilderTest extends \Test\TestCase {
* @param array $expectedQueryPart
* @param string $expectedQuery
*/
#[\PHPUnit\Framework\Attributes\DataProvider('dataLeftJoin')]
#[DataProvider('dataLeftJoin')]
public function testLeftJoin($fromAlias, $tableName, $tableAlias, $condition, $expectedQueryPart, $expectedQuery): void {
$this->queryBuilder->from('data1', 'd1');
$this->queryBuilder->leftJoin(
@ -698,7 +686,7 @@ class QueryBuilderTest extends \Test\TestCase {
* @param array $expectedQueryPart
* @param string $expectedQuery
*/
#[\PHPUnit\Framework\Attributes\DataProvider('dataRightJoin')]
#[DataProvider('dataRightJoin')]
public function testRightJoin($fromAlias, $tableName, $tableAlias, $condition, $expectedQueryPart, $expectedQuery): void {
$this->queryBuilder->from('data1', 'd1');
$this->queryBuilder->rightJoin(
@ -737,7 +725,7 @@ class QueryBuilderTest extends \Test\TestCase {
* @param array $expectedQueryPart
* @param string $expectedQuery
*/
#[\PHPUnit\Framework\Attributes\DataProvider('dataSet')]
#[DataProvider('dataSet')]
public function testSet($partOne1, $partOne2, $partTwo1, $partTwo2, $expectedQueryPart, $expectedQuery): void {
$this->queryBuilder->update('data');
$this->queryBuilder->set($partOne1, $partOne2);
@ -769,7 +757,7 @@ class QueryBuilderTest extends \Test\TestCase {
* @param array $expectedQueryPart
* @param string $expectedQuery
*/
#[\PHPUnit\Framework\Attributes\DataProvider('dataWhere')]
#[DataProvider('dataWhere')]
public function testWhere($whereArguments, $expectedQueryPart, $expectedQuery): void {
$this->queryBuilder->select('column');
call_user_func_array(
@ -794,7 +782,7 @@ class QueryBuilderTest extends \Test\TestCase {
* @param array $expectedQueryPart
* @param string $expectedQuery
*/
#[\PHPUnit\Framework\Attributes\DataProvider('dataWhere')]
#[DataProvider('dataWhere')]
public function testAndWhere($whereArguments, $expectedQueryPart, $expectedQuery): void {
$this->queryBuilder->select('column');
call_user_func_array(
@ -826,7 +814,7 @@ class QueryBuilderTest extends \Test\TestCase {
* @param array $expectedQueryPart
* @param string $expectedQuery
*/
#[\PHPUnit\Framework\Attributes\DataProvider('dataOrWhere')]
#[DataProvider('dataOrWhere')]
public function testOrWhere($whereArguments, $expectedQueryPart, $expectedQuery): void {
$this->queryBuilder->select('column');
call_user_func_array(
@ -858,7 +846,7 @@ class QueryBuilderTest extends \Test\TestCase {
* @param array $expectedQueryPart
* @param string $expectedQuery
*/
#[\PHPUnit\Framework\Attributes\DataProvider('dataGroupBy')]
#[DataProvider('dataGroupBy')]
public function testGroupBy($groupByArguments, $expectedQueryPart, $expectedQuery): void {
$this->queryBuilder->select('column');
call_user_func_array(
@ -890,7 +878,7 @@ class QueryBuilderTest extends \Test\TestCase {
* @param array $expectedQueryPart
* @param string $expectedQuery
*/
#[\PHPUnit\Framework\Attributes\DataProvider('dataAddGroupBy')]
#[DataProvider('dataAddGroupBy')]
public function testAddGroupBy($groupByArguments, $expectedQueryPart, $expectedQuery): void {
$this->queryBuilder->select('column');
$this->queryBuilder->groupBy('column1');
@ -923,7 +911,7 @@ class QueryBuilderTest extends \Test\TestCase {
* @param array $expectedQueryPart
* @param string $expectedQuery
*/
#[\PHPUnit\Framework\Attributes\DataProvider('dataSetValue')]
#[DataProvider('dataSetValue')]
public function testSetValue($column, $value, $expectedQueryPart, $expectedQuery): void {
$this->queryBuilder->insert('data');
$this->queryBuilder->setValue($column, $value);
@ -946,7 +934,7 @@ class QueryBuilderTest extends \Test\TestCase {
* @param array $expectedQueryPart
* @param string $expectedQuery
*/
#[\PHPUnit\Framework\Attributes\DataProvider('dataSetValue')]
#[DataProvider('dataSetValue')]
public function testValues($column, $value, $expectedQueryPart, $expectedQuery): void {
$this->queryBuilder->insert('data');
$this->queryBuilder->values([
@ -987,7 +975,7 @@ class QueryBuilderTest extends \Test\TestCase {
* @param array $expectedQueryPart
* @param string $expectedQuery
*/
#[\PHPUnit\Framework\Attributes\DataProvider('dataHaving')]
#[DataProvider('dataHaving')]
public function testHaving($havingArguments, $expectedQueryPart, $expectedQuery): void {
call_user_func_array(
[$this->queryBuilder, 'having'],
@ -1028,7 +1016,7 @@ class QueryBuilderTest extends \Test\TestCase {
* @param array $expectedQueryPart
* @param string $expectedQuery
*/
#[\PHPUnit\Framework\Attributes\DataProvider('dataAndHaving')]
#[DataProvider('dataAndHaving')]
public function testAndHaving($havingArguments, $expectedQueryPart, $expectedQuery): void {
$this->queryBuilder->having('condition1');
call_user_func_array(
@ -1070,7 +1058,7 @@ class QueryBuilderTest extends \Test\TestCase {
* @param array $expectedQueryPart
* @param string $expectedQuery
*/
#[\PHPUnit\Framework\Attributes\DataProvider('dataOrHaving')]
#[DataProvider('dataOrHaving')]
public function testOrHaving($havingArguments, $expectedQueryPart, $expectedQuery): void {
$this->queryBuilder->having('condition1');
call_user_func_array(
@ -1104,7 +1092,7 @@ class QueryBuilderTest extends \Test\TestCase {
* @param array $expectedQueryPart
* @param string $expectedQuery
*/
#[\PHPUnit\Framework\Attributes\DataProvider('dataOrderBy')]
#[DataProvider('dataOrderBy')]
public function testOrderBy($sort, $order, $expectedQueryPart, $expectedQuery): void {
$this->queryBuilder->orderBy($sort, $order);
@ -1141,7 +1129,7 @@ class QueryBuilderTest extends \Test\TestCase {
* @param array $expectedQueryPart
* @param string $expectedQuery
*/
#[\PHPUnit\Framework\Attributes\DataProvider('dataAddOrderBy')]
#[DataProvider('dataAddOrderBy')]
public function testAddOrderBy($sort2, $order2, $order1, $expectedQueryPart, $expectedQuery): void {
$this->queryBuilder->orderBy('column1', $order1);
$this->queryBuilder->addOrderBy($sort2, $order2);
@ -1194,10 +1182,7 @@ class QueryBuilderTest extends \Test\TestCase {
}
}
public function dataGetTableName(): array {
$config = $this->createMock(SystemConfig::class);
$logger = $this->createMock(LoggerInterface::class);
$qb = new QueryBuilder(Server::get(IDBConnection::class), $config, $logger);
public static function dataGetTableName(): array {
return [
['*PREFIX*table', null, '`*PREFIX*table`'],
['*PREFIX*table', true, '`*PREFIX*table`'],
@ -1207,20 +1192,18 @@ class QueryBuilderTest extends \Test\TestCase {
['table', true, '`*PREFIX*table`'],
['table', false, '`table`'],
[$qb->createFunction('(' . $qb->select('*')->from('table')->getSQL() . ')'), null, '(SELECT * FROM `*PREFIX*table`)'],
[$qb->createFunction('(' . $qb->select('*')->from('table')->getSQL() . ')'), true, '(SELECT * FROM `*PREFIX*table`)'],
[$qb->createFunction('(' . $qb->select('*')->from('table')->getSQL() . ')'), false, '(SELECT * FROM `*PREFIX*table`)'],
['function', null, '(SELECT * FROM `*PREFIX*table`)'],
['function', true, '(SELECT * FROM `*PREFIX*table`)'],
['function', false, '(SELECT * FROM `*PREFIX*table`)'],
];
}
/**
*
* @param string|IQueryFunction $tableName
* @param bool $automatic
* @param string $expected
*/
#[\PHPUnit\Framework\Attributes\DataProvider('dataGetTableName')]
public function testGetTableName($tableName, $automatic, $expected): void {
#[DataProvider('dataGetTableName')]
public function testGetTableName(string $tableName, ?bool $automatic, string $expected): void {
if ($tableName === 'function') {
$tableName = $this->queryBuilder->createFunction('(' . $this->queryBuilder->select('*')->from('table')->getSQL() . ')');
}
if ($automatic !== null) {
$this->queryBuilder->automaticTablePrefix($automatic);
}
@ -1243,7 +1226,7 @@ class QueryBuilderTest extends \Test\TestCase {
* @param string $prefix
* @param string $expected
*/
#[\PHPUnit\Framework\Attributes\DataProvider('dataGetColumnName')]
#[DataProvider('dataGetColumnName')]
public function testGetColumnName($column, $prefix, $expected): void {
$this->assertSame(
$expected,

Loading…
Cancel
Save