Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
test: Remove more withConsecutive
Signed-off-by: Joas Schilling <[email protected]>
  • Loading branch information
nickvergessen committed May 15, 2025
commit 53b116b8a54ec7606441e35b7e6c8697a704c5a4
95 changes: 56 additions & 39 deletions tests/lib/AppFramework/Db/QBMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ protected function setUp(): void {
$this->mapper = new QBTestMapper($this->db);
}


public function testInsertEntityParameterTypeMapping(): void {
$datetime = new \DateTimeImmutable();
$entity = new QBTestEntity();
Expand All @@ -117,31 +117,40 @@ public function testInsertEntityParameterTypeMapping(): void {
$booleanParam = $this->qb->createNamedParameter('boolean_prop', IQueryBuilder::PARAM_BOOL);
$datetimeParam = $this->qb->createNamedParameter('datetime_prop', IQueryBuilder::PARAM_DATETIME_IMMUTABLE);

$createNamedParameterCalls = [
[123, IQueryBuilder::PARAM_INT, null],
[true, IQueryBuilder::PARAM_BOOL, null],
['string', IQueryBuilder::PARAM_STR, null],
[456, IQueryBuilder::PARAM_INT, null],
[false, IQueryBuilder::PARAM_BOOL, null],
[$datetime, IQueryBuilder::PARAM_DATETIME_IMMUTABLE, null],
];
$this->qb->expects($this->exactly(6))
->method('createNamedParameter')
->withConsecutive(
[$this->equalTo(123), $this->equalTo(IQueryBuilder::PARAM_INT)],
[$this->equalTo(true), $this->equalTo(IQueryBuilder::PARAM_BOOL)],
[$this->equalTo('string'), $this->equalTo(IQueryBuilder::PARAM_STR)],
[$this->equalTo(456), $this->equalTo(IQueryBuilder::PARAM_INT)],
[$this->equalTo(false), $this->equalTo(IQueryBuilder::PARAM_BOOL)],
[$this->equalTo($datetime), $this->equalTo(IQueryBuilder::PARAM_DATETIME_IMMUTABLE)],
);
->willReturnCallback(function() use (&$createNamedParameterCalls) {
$expected = array_shift($createNamedParameterCalls);
$this->assertEquals($expected, func_get_args());
});

$setValueCalls = [
['int_prop', $intParam],
['bool_prop', $boolParam],
['string_prop', $stringParam],
['integer_prop', $integerParam],
['boolean_prop', $booleanParam],
['datetime_prop', $datetimeParam],
];
$this->qb->expects($this->exactly(6))
->method('setValue')
->withConsecutive(
[$this->equalTo('int_prop'), $this->equalTo($intParam)],
[$this->equalTo('bool_prop'), $this->equalTo($boolParam)],
[$this->equalTo('string_prop'), $this->equalTo($stringParam)],
[$this->equalTo('integer_prop'), $this->equalTo($integerParam)],
[$this->equalTo('boolean_prop'), $this->equalTo($booleanParam)],
[$this->equalTo('datetime_prop'), $this->equalTo($datetimeParam)],
);
->willReturnCallback(function() use (&$setValueCalls) {
$expected = array_shift($setValueCalls);
$this->assertEquals($expected, func_get_args());
});

$this->mapper->insert($entity);
}


public function testUpdateEntityParameterTypeMapping(): void {
$datetime = new \DateTimeImmutable();
$entity = new QBTestEntity();
Expand All @@ -163,30 +172,38 @@ public function testUpdateEntityParameterTypeMapping(): void {
$jsonParam = $this->qb->createNamedParameter('json_prop', IQueryBuilder::PARAM_JSON);
$datetimeParam = $this->qb->createNamedParameter('datetime_prop', IQueryBuilder::PARAM_DATETIME_IMMUTABLE);

$createNamedParameterCalls = [
[123, IQueryBuilder::PARAM_INT, null],
[true, IQueryBuilder::PARAM_BOOL, null],
['string', IQueryBuilder::PARAM_STR, null],
[456, IQueryBuilder::PARAM_INT, null],
[false, IQueryBuilder::PARAM_BOOL, null],
[['hello' => 'world'], IQueryBuilder::PARAM_JSON, null],
[$datetime, IQueryBuilder::PARAM_DATETIME_IMMUTABLE, null],
[789, IQueryBuilder::PARAM_INT, null],
];
$this->qb->expects($this->exactly(8))
->method('createNamedParameter')
->withConsecutive(
[$this->equalTo(123), $this->equalTo(IQueryBuilder::PARAM_INT)],
[$this->equalTo(true), $this->equalTo(IQueryBuilder::PARAM_BOOL)],
[$this->equalTo('string'), $this->equalTo(IQueryBuilder::PARAM_STR)],
[$this->equalTo(456), $this->equalTo(IQueryBuilder::PARAM_INT)],
[$this->equalTo(false), $this->equalTo(IQueryBuilder::PARAM_BOOL)],
[$this->equalTo(['hello' => 'world']), $this->equalTo(IQueryBuilder::PARAM_JSON)],
[$this->equalTo($datetime), $this->equalTo(IQueryBuilder::PARAM_DATETIME_IMMUTABLE)],
[$this->equalTo(789), $this->equalTo(IQueryBuilder::PARAM_INT)],
);

->willReturnCallback(function() use (&$createNamedParameterCalls) {
$expected = array_shift($createNamedParameterCalls);
$this->assertEquals($expected, func_get_args());
});

$setCalls = [
['int_prop', $intParam],
['bool_prop', $boolParam],
['string_prop', $stringParam],
['integer_prop', $integerParam],
['boolean_prop', $booleanParam],
['json_prop', $datetimeParam],
['datetime_prop', $datetimeParam],
];
$this->qb->expects($this->exactly(7))
->method('set')
->withConsecutive(
[$this->equalTo('int_prop'), $this->equalTo($intParam)],
[$this->equalTo('bool_prop'), $this->equalTo($boolParam)],
[$this->equalTo('string_prop'), $this->equalTo($stringParam)],
[$this->equalTo('integer_prop'), $this->equalTo($integerParam)],
[$this->equalTo('boolean_prop'), $this->equalTo($booleanParam)],
[$this->equalTo('json_prop'), $this->equalTo($jsonParam)],
[$this->equalTo('datetime_prop'), $this->equalTo($datetimeParam)],
);
->willReturnCallback(function() use (&$setCalls) {
$expected = array_shift($setCalls);
$this->assertEquals($expected, func_get_args());
});

$this->expr->expects($this->once())
->method('eq')
Expand All @@ -196,7 +213,7 @@ public function testUpdateEntityParameterTypeMapping(): void {
$this->mapper->update($entity);
}


public function testGetParameterTypeForProperty(): void {
$entity = new QBTestEntity();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,19 @@ public function testBeforeControllerWithMultipleAttributes(): void {
->expects($this->once())
->method('getRemoteAddress')
->willReturn('::1');

$calls = [
['::1', 'first'],
['::1', 'second'],
];
$this->throttler
->expects($this->exactly(2))
->method('sleepDelayOrThrowOnMax')
->withConsecutive(
['::1', 'first'],
['::1', 'second'],
);
->willReturnCallback(function() use (&$calls) {
$expected = array_shift($calls);
$this->assertEquals($expected, func_get_args());
return 0;
});

$controller = new TestController('test', $this->request);
$this->reflector->reflect($controller, 'multipleAttributes');
Expand Down Expand Up @@ -221,20 +227,31 @@ public function testAfterControllerWithMultipleAttributesGeneralMatch(): void {
->expects($this->once())
->method('getRemoteAddress')
->willReturn('::1');

$sleepCalls = [
['::1', 'first'],
['::1', 'second'],
];
$this->throttler
->expects($this->exactly(2))
->method('sleepDelayOrThrowOnMax')
->withConsecutive(
['::1', 'first'],
['::1', 'second'],
);
->willReturnCallback(function() use (&$sleepCalls) {
$expected = array_shift($sleepCalls);
$this->assertEquals($expected, func_get_args());
return 0;
});

$attemptCalls = [
['first', '::1', []],
['second', '::1', []],
];
$this->throttler
->expects($this->exactly(2))
->method('registerAttempt')
->withConsecutive(
['first', '::1'],
['second', '::1'],
);
->willReturnCallback(function() use (&$attemptCalls) {
$expected = array_shift($attemptCalls);
$this->assertEquals($expected, func_get_args());
});

$controller = new TestController('test', $this->request);
$this->reflector->reflect($controller, 'multipleAttributes');
Expand Down
53 changes: 27 additions & 26 deletions tests/lib/AppFramework/Routing/RoutingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -348,23 +348,24 @@ private function assertOCSResource($yaml, $resourceName, $url, $controllerName,

$urlWithParam = $url . '/{' . $paramName . '}';

$calls = [
['name' => 'ocs.app1.' . $resourceName . '.index', 'pattern' => $url, 'route' => $indexRoute],
['name' => 'ocs.app1.' . $resourceName . '.show', 'pattern' => $urlWithParam, 'route' => $showRoute],
['name' => 'ocs.app1.' . $resourceName . '.create', 'pattern' => $url, 'route' => $createRoute],
['name' => 'ocs.app1.' . $resourceName . '.update', 'pattern' => $urlWithParam, 'route' => $updateRoute],
['name' => 'ocs.app1.' . $resourceName . '.destroy', 'pattern' => $urlWithParam, 'route' => $destroyRoute],
];

// we expect create to be called five times:
$router
->expects($this->exactly(5))
->method('create')
->withConsecutive(
[$this->equalTo('ocs.app1.' . $resourceName . '.index'), $this->equalTo($url)],
[$this->equalTo('ocs.app1.' . $resourceName . '.show'), $this->equalTo($urlWithParam)],
[$this->equalTo('ocs.app1.' . $resourceName . '.create'), $this->equalTo($url)],
[$this->equalTo('ocs.app1.' . $resourceName . '.update'), $this->equalTo($urlWithParam)],
[$this->equalTo('ocs.app1.' . $resourceName . '.destroy'), $this->equalTo($urlWithParam)],
)->willReturnOnConsecutiveCalls(
$indexRoute,
$showRoute,
$createRoute,
$updateRoute,
$destroyRoute,
);
->willReturnCallback(function(string $name, string $pattern) use (&$calls) {
$expected = array_shift($calls);
$this->assertEquals($expected['name'], $name);
$this->assertEquals($expected['pattern'], $pattern);
return $expected['route'];
});

// load route configuration
$config = new RouteConfig($container, $router, $yaml);
Expand Down Expand Up @@ -402,23 +403,23 @@ private function assertResource($yaml, $resourceName, $url, $controllerName, $pa

$urlWithParam = $url . '/{' . $paramName . '}';

$calls = [
['name' => 'app1.' . $resourceName . '.index', 'pattern' => $url, 'route' => $indexRoute],
['name' => 'app1.' . $resourceName . '.show', 'pattern' => $urlWithParam, 'route' => $showRoute],
['name' => 'app1.' . $resourceName . '.create', 'pattern' => $url, 'route' => $createRoute],
['name' => 'app1.' . $resourceName . '.update', 'pattern' => $urlWithParam, 'route' => $updateRoute],
['name' => 'app1.' . $resourceName . '.destroy', 'pattern' => $urlWithParam, 'route' => $destroyRoute],
];
// we expect create to be called five times:
$router
->expects($this->exactly(5))
->method('create')
->withConsecutive(
[$this->equalTo('app1.' . $resourceName . '.index'), $this->equalTo($url)],
[$this->equalTo('app1.' . $resourceName . '.show'), $this->equalTo($urlWithParam)],
[$this->equalTo('app1.' . $resourceName . '.create'), $this->equalTo($url)],
[$this->equalTo('app1.' . $resourceName . '.update'), $this->equalTo($urlWithParam)],
[$this->equalTo('app1.' . $resourceName . '.destroy'), $this->equalTo($urlWithParam)],
)->willReturnOnConsecutiveCalls(
$indexRoute,
$showRoute,
$createRoute,
$updateRoute,
$destroyRoute,
);
->willReturnCallback(function(string $name, string $pattern) use (&$calls) {
$expected = array_shift($calls);
$this->assertEquals($expected['name'], $name);
$this->assertEquals($expected['pattern'], $pattern);
return $expected['route'];
});

// load route configuration
$config = new RouteConfig($container, $router, $yaml);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,18 @@ public function testHandle(): void {
$token2,
$token3,
]);

$calls = [
['user123', 1],
['user123', 2],
['user123', 3],
];
$this->manager->expects($this->exactly(3))
->method('invalidateTokenById')
->withConsecutive(
['user123', 1],
['user123', 2],
['user123', 3]
);
->willReturnCallback(function() use (&$calls) {
$expected = array_shift($calls);
$this->assertEquals($expected, func_get_args());
});
$this->logger->expects($this->never())
->method('error');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;

abstract class ALoginCommandTest extends TestCase {
abstract class ALoginTestCommand extends TestCase {
/** @var IRequest|MockObject */
protected $request;

Expand All @@ -36,7 +36,7 @@ abstract class ALoginCommandTest extends TestCase {
/** @var IUser|MockObject */
protected $user;

/** @var ALoginCommand */
/** @var ALoginTestCommand */
protected $cmd;

protected function setUp(): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use OCP\IConfig;
use PHPUnit\Framework\MockObject\MockObject;

class ClearLostPasswordTokensCommandTest extends ALoginCommandTest {
class ClearLostPasswordTokensCommandTest extends ALoginTestCommand {
/** @var IConfig|MockObject */
private $config;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use OC\User\Session;
use PHPUnit\Framework\MockObject\MockObject;

class CompleteLoginCommandTest extends ALoginCommandTest {
class CompleteLoginCommandTest extends ALoginTestCommand {
/** @var Session|MockObject */
private $session;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use OCP\IConfig;
use PHPUnit\Framework\MockObject\MockObject;

class CreateSessionTokenCommandTest extends ALoginCommandTest {
class CreateSessionTokenCommandTest extends ALoginTestCommand {
/** @var IConfig|MockObject */
private $config;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use OCP\IConfig;
use PHPUnit\Framework\MockObject\MockObject;

class FinishRememberedLoginCommandTest extends ALoginCommandTest {
class FinishRememberedLoginCommandTest extends ALoginTestCommand {
/** @var Session|MockObject */
private $userSession;
/** @var IConfig|MockObject */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;

class LoggedInCheckCommandTest extends ALoginCommandTest {
class LoggedInCheckCommandTest extends ALoginTestCommand {
/** @var LoggerInterface|MockObject */
private $logger;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use OCP\IUserManager;
use PHPUnit\Framework\MockObject\MockObject;

class PreLoginHookCommandTest extends ALoginCommandTest {
class PreLoginHookCommandTest extends ALoginTestCommand {
/** @var IUserManager|MockObject */
private $userManager;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use OCP\ISession;
use PHPUnit\Framework\MockObject\MockObject;

class SetUserTimezoneCommandTest extends ALoginCommandTest {
class SetUserTimezoneCommandTest extends ALoginTestCommand {
/** @var IConfig|MockObject */
private $config;

Expand Down
2 changes: 1 addition & 1 deletion tests/lib/Authentication/Login/TwoFactorCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use OCP\IURLGenerator;
use PHPUnit\Framework\MockObject\MockObject;

class TwoFactorCommandTest extends ALoginCommandTest {
class TwoFactorCommandTest extends ALoginTestCommand {
/** @var Manager|MockObject */
private $twoFactorManager;

Expand Down
2 changes: 1 addition & 1 deletion tests/lib/Authentication/Login/UidLoginCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use OC\User\Manager;
use PHPUnit\Framework\MockObject\MockObject;

class UidLoginCommandTest extends ALoginCommandTest {
class UidLoginCommandTest extends ALoginTestCommand {
/** @var Manager|MockObject */
private $userManager;

Expand Down
Loading