MDL-81308 core: Use a helper for consecutive invocation counts

This commit is contained in:
Andrew Nicols 2024-10-17 15:10:52 +08:00 committed by Jun Pataleta
parent da7c5d98ec
commit 564de9d4a7
No known key found for this signature in database
GPG Key ID: F83510526D99E2C7
14 changed files with 41 additions and 28 deletions

View File

@ -1378,7 +1378,7 @@ class expired_contexts_test extends \advanced_testcase {
$blockcontext,
$usercontext,
): void {
match ($deleteinvocations->getInvocationCount()) {
match (self::getInvocationCount($deleteinvocations)) {
1 => $this->assertEquals($blockcontext, $context),
2 => $this->assertEquals($usercontext, $context),
default => $this->fail('Unexpected invocation count'),
@ -1604,7 +1604,7 @@ class expired_contexts_test extends \advanced_testcase {
$blockcontext,
$usercontext,
): void {
match ($deleteinvocations->getInvocationCount()) {
match (self::getInvocationCount($deleteinvocations)) {
1 => $this->assertEquals($blockcontext, $context),
2 => $this->assertEquals($usercontext, $context),
default => $this->fail('Unexpected invocation count'),
@ -1665,7 +1665,7 @@ class expired_contexts_test extends \advanced_testcase {
$blockcontext,
$usercontext,
): void {
match ($deleteinvocations->getInvocationCount()) {
match (self::getInvocationCount($deleteinvocations)) {
1 => $this->assertEquals($blockcontext, $context),
2 => $this->assertEquals($usercontext, $context),
default => $this->fail('Unexpected invocation count'),
@ -1892,7 +1892,7 @@ class expired_contexts_test extends \advanced_testcase {
$forumcontext,
$coursecontext,
): void {
match ($deleteinvocations->getInvocationCount()) {
match (self::getInvocationCount($deleteinvocations)) {
1 => $this->assertEquals($forumcontext, $context),
2 => $this->assertEquals($coursecontext, $context),
default => $this->fail('Unexpected invocation count'),

View File

@ -59,7 +59,7 @@ final class locale_test extends \advanced_testcase {
$setinvocations = $this->exactly(2);
$mock
->expects($setinvocations)
->method('set_locale')->willReturnCallback(fn () => match ($setinvocations->getInvocationCount()) {
->method('set_locale')->willReturnCallback(fn () => match (self::getInvocationCount($setinvocations)) {
1 => 'es',
2 => 'en',
});
@ -82,7 +82,7 @@ final class locale_test extends \advanced_testcase {
$setinvocations = $this->exactly(2);
$mock
->expects($setinvocations)
->method('set_locale')->willReturnCallback(fn () => match ($setinvocations->getInvocationCount()) {
->method('set_locale')->willReturnCallback(fn () => match (self::getInvocationCount($setinvocations)) {
1 => false,
2 => 'en',
});

View File

@ -528,7 +528,7 @@ class tour_test extends \advanced_testcase {
$DB->expects($deleteinvocations)
->method('delete_records')
->willReturnCallback(function ($table, $conditions) use ($deleteinvocations, $id) {
switch ($deleteinvocations->getInvocationCount()) {
switch (self::getInvocationCount($deleteinvocations)) {
case 1:
$this->assertEquals('tool_usertours_tours', $table);
$this->assertEquals(['id' => $id], $conditions);
@ -583,7 +583,7 @@ class tour_test extends \advanced_testcase {
$setfieldinvocations,
$expectations,
): void {
$expectation = $expectations[$setfieldinvocations->getInvocationCount() - 1];
$expectation = $expectations[self::getInvocationCount($setfieldinvocations) - 1];
$this->assertEquals('tool_usertours_steps', $table);
$this->assertEquals('sortorder', $field);
$this->assertEquals($expectation[0], $value);
@ -791,7 +791,7 @@ class tour_test extends \advanced_testcase {
$tour->expects($getinvocations)
->method('get_config')
->willReturnCallback(function () use ($getinvocations, $getconfig) {
return $getconfig[$getinvocations->getInvocationCount() - 1];
return $getconfig[self::getInvocationCount($getinvocations) - 1];
});
}

View File

@ -117,7 +117,7 @@ class activity_custom_completion_test extends advanced_testcase {
$stub->expects($stateinvocations)
->method('get_state')
->willReturnCallback(function ($rule) use ($stateinvocations, $rules, $rulestates) {
$index = $stateinvocations->getInvocationCount() - 1;
$index = self::getInvocationCount($stateinvocations) - 1;
$this->assertEquals($rules[$index], $rule);
return $rulestates[$index];
});

View File

@ -189,7 +189,7 @@ class category_hooks_test extends \advanced_testcase {
$mockcategory2->expects($callbackinvocations)
->method('get_plugins_callback_function')
->willReturnCallback(function ($method) use ($callbackinvocations): array {
switch ($callbackinvocations->getInvocationCount()) {
switch (self::getInvocationCount($callbackinvocations)) {
case 1:
$this->assertEquals('can_course_category_delete', $method);
return ['tool_unittest_can_course_category_delete'];

View File

@ -551,7 +551,7 @@ class converter_test extends advanced_testcase {
$converter
->expects($getinvocations)
->method('get_next_converter')
->willReturnCallback(fn (): string => match ($getinvocations->getInvocationCount()) {
->willReturnCallback(fn (): string => match (self::getInvocationCount($getinvocations)) {
1 => get_class($converterinstance),
default => get_class($converterinstance2),
});
@ -573,7 +573,7 @@ class converter_test extends advanced_testcase {
$conversion
->expects($statusinvocations)
->method('get_status')
->willReturnCallback(fn (): int => match ($statusinvocations->getInvocationCount()) {
->willReturnCallback(fn (): int => match (self::getInvocationCount($statusinvocations)) {
// Initial status check.
1 => conversion::STATUS_PENDING,
// Second check to make sure it's still pending after polling.
@ -590,7 +590,7 @@ class converter_test extends advanced_testcase {
$conversion
->expects($instanceinvocations)
->method('get_converter_instance')
->willReturnCallback(fn (): object => match ($instanceinvocations->getInvocationCount()) {
->willReturnCallback(fn (): object => match (self::getInvocationCount($instanceinvocations)) {
1 => $converterinstance,
default => $converterinstance2,
});

View File

@ -1220,7 +1220,7 @@ class file_system_test extends \advanced_testcase {
$fs
->expects($getinvocations)
->method('get_local_path_from_hash')
->willReturnCallback(fn () => match ($getinvocations->getInvocationCount()) {
->willReturnCallback(fn () => match (self::getInvocationCount($getinvocations)) {
1 => '/path/to/remote/file',
2 => $filepath,
});

View File

@ -1,4 +1,5 @@
<?php
use PHPUnit\Framework\MockObject\Rule\InvocationOrder;
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
@ -594,5 +595,17 @@ abstract class base_testcase extends PHPUnit\Framework\TestCase {
return str_replace(' ', ' ', $result);
}
/**
* Helper to get the count of invocation.
*
* This is required because the method to use changed names in PHPUnit 10.0 in a breaking change.
*
* @param \PHPUnit\Framework\MockObject\Rule\InvocationOrder $counter
* @return int
*/
protected static function getInvocationCount(InvocationOrder $counter): int {
return $counter->getInvocationCount();
}
// phpcs:enable
}

View File

@ -252,7 +252,7 @@ class completionlib_test extends advanced_testcase {
->method('get_data')
->with($cm, false, 100)
->willReturnCallback(function () use ($getinvocations, $current1, $current2) {
return match ($getinvocations->getInvocationCount()) {
return match (self::getInvocationCount($getinvocations)) {
1 => $current1,
2 => $current2,
default => $this->fail('Unexpected invocation count'),
@ -279,7 +279,7 @@ class completionlib_test extends advanced_testcase {
$comparewith1,
$comparewith2
): void {
switch ($setinvocations->getInvocationCount()) {
switch (self::getInvocationCount($setinvocations)) {
case 1:
$this->assertEquals($cm, $comparecm);
$comparewith1->evaluate($comparewith);
@ -714,7 +714,7 @@ class completionlib_test extends advanced_testcase {
->willReturnCallback(function ($comparecm, $state, $userid) use ($updateinvocations, $cm): void {
$this->assertEquals($cm, $comparecm);
$this->assertEquals(COMPLETION_UNKNOWN, $state);
switch ($updateinvocations->getInvocationCount()) {
switch (self::getInvocationCount($updateinvocations)) {
case 1:
$this->assertEquals(100, $userid);
break;
@ -1218,7 +1218,7 @@ class completionlib_test extends advanced_testcase {
$DB->expects($inorequalsinvocations)
->method('get_in_or_equal')
->willReturnCallback(function ($paramids) use ($inorequalsinvocations, $ids) {
switch ($inorequalsinvocations->getInvocationCount()) {
switch (self::getInvocationCount($inorequalsinvocations)) {
case 1:
$this->assertEquals(array_slice($ids, 0, 1000), $paramids);
return [' IN whatever', []];
@ -1233,7 +1233,7 @@ class completionlib_test extends advanced_testcase {
$DB->expects($getinvocations)
->method('get_recordset_sql')
->willReturnCallback(function () use ($getinvocations, $progress) {
return match ($getinvocations->getInvocationCount()) {
return match (self::getInvocationCount($getinvocations)) {
1 => new core_completionlib_fake_recordset(array_slice($progress, 0, 1000)),
2 => new core_completionlib_fake_recordset(array_slice($progress, 1000)),
default => $this->fail('Unexpected invocation count'),

View File

@ -244,7 +244,7 @@ class exportable_filearea_test extends advanced_testcase {
$archive->expects($invocations)
->method('add_file_from_stored_file')
->willReturnCallback(function (...$args) use ($invocations, $storedfileargs) {
$this->assertEquals($storedfileargs[$invocations->getInvocationCount() - 1], $args);
$this->assertEquals($storedfileargs[self::getInvocationCount($invocations) - 1], $args);
});
return $exportable->add_to_archive($archive);

View File

@ -330,7 +330,7 @@ EOF;
$archive->expects($invocations)
->method('add_file_from_stored_file')
->willReturnCallback(function (...$args) use ($invocations, $storedfileargs) {
$this->assertEquals($storedfileargs[$invocations->getInvocationCount() - 1], $args);
$this->assertEquals($storedfileargs[self::getInvocationCount($invocations) - 1], $args);
});
$archive->expects($this->never())

View File

@ -407,7 +407,7 @@ class api_test extends \advanced_testcase {
$mock->expects($requestinvocations)
->method('request')
->willReturnCallback(function () use ($requestinvocations): string {
return match ($requestinvocations->getInvocationCount()) {
return match (self::getInvocationCount($requestinvocations)) {
1 => json_encode(['has_more' => true, 'cursor' => 'Example', 'matches' => ['foo', 'bar']]),
2 => json_encode(['has_more' => true, 'cursor' => 'Example', 'matches' => ['baz']]),
3 => json_encode(['has_more' => false, 'cursor' => '', 'matches' => ['bum']]),
@ -420,7 +420,7 @@ class api_test extends \advanced_testcase {
$mock->expects($apiinvocations)
->method('get_api_endpoint')
->willReturnCallback(function ($endpoint) use ($apiinvocations): string {
switch ($apiinvocations->getInvocationCount()) {
switch (self::getInvocationCount($apiinvocations)) {
case 1:
$this->assertEquals('testEndpoint', $endpoint);
return 'https://example.com/api/2/testEndpoint';
@ -484,7 +484,7 @@ class api_test extends \advanced_testcase {
$mock->expects($headerinvocations)
->method('setHeader')
->willReturnCallback(function ($header) use ($data, $headerinvocations): void {
switch ($headerinvocations->getInvocationCount()) {
switch (self::getInvocationCount($headerinvocations)) {
case 1:
$this->assertEquals('Content-Type: ', $header);
break;
@ -571,7 +571,7 @@ class api_test extends \advanced_testcase {
$mock->expects($fetchinvocations)
->method('fetch_dropbox_data')
->willReturnCallback(function ($path, $values) use ($fetchinvocations, $id, $file): object {
switch ($fetchinvocations->getInvocationCount()) {
switch (self::getInvocationCount($fetchinvocations)) {
case 1:
$this->assertEquals('sharing/list_shared_links', $path);
$this->assertEquals(['path' => $id], $values);

View File

@ -73,7 +73,7 @@ class googledocs_search_content_test extends \googledocs_content_testcase {
$searccontents,
$searchparams,
) {
switch ($callinvocations->getInvocationCount()) {
switch (self::getInvocationCount($callinvocations)) {
case 1:
$this->assertEquals('shared_drives_list', $method);

View File

@ -72,7 +72,7 @@ class googledocs_drive_content_test extends \googledocs_content_testcase {
$listparams,
$drivecontents,
) {
switch ($callinvocations->getInvocationCount()) {
switch (self::getInvocationCount($callinvocations)) {
case 1:
$this->assertEquals('shared_drives_list', $method);
$this->assertEquals([], $params);