From c745b3fb8075d6776265aecb4fcefebc9a2467d7 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Thu, 17 Oct 2024 15:10:52 +0800 Subject: [PATCH] MDL-81308 core: Use a helper for consecutive invocation counts --- .../dataprivacy/tests/expired_contexts_test.php | 8 ++++---- admin/tool/langimport/tests/locale_test.php | 4 ++-- admin/tool/usertours/tests/tour_test.php | 6 +++--- .../tests/activity_custom_completion_test.php | 2 +- course/tests/category_hooks_test.php | 2 +- files/tests/converter_test.php | 6 +++--- lib/filestorage/tests/file_system_test.php | 2 +- lib/phpunit/classes/base_testcase.php | 13 +++++++++++++ lib/tests/completionlib_test.php | 10 +++++----- .../exportable_items/exportable_filearea_test.php | 2 +- .../exportable_items/exportable_textarea_test.php | 2 +- repository/dropbox/tests/api_test.php | 8 ++++---- .../tests/googledocs_search_content_test.php | 2 +- .../local/browser/googledocs_drive_content_test.php | 2 +- 14 files changed, 41 insertions(+), 28 deletions(-) diff --git a/admin/tool/dataprivacy/tests/expired_contexts_test.php b/admin/tool/dataprivacy/tests/expired_contexts_test.php index ffd5d51a57a..a1922eece6b 100644 --- a/admin/tool/dataprivacy/tests/expired_contexts_test.php +++ b/admin/tool/dataprivacy/tests/expired_contexts_test.php @@ -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'), diff --git a/admin/tool/langimport/tests/locale_test.php b/admin/tool/langimport/tests/locale_test.php index ad0eed13d77..694794233fa 100644 --- a/admin/tool/langimport/tests/locale_test.php +++ b/admin/tool/langimport/tests/locale_test.php @@ -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', }); diff --git a/admin/tool/usertours/tests/tour_test.php b/admin/tool/usertours/tests/tour_test.php index ebc27d30702..e5ae803250b 100644 --- a/admin/tool/usertours/tests/tour_test.php +++ b/admin/tool/usertours/tests/tour_test.php @@ -531,7 +531,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); @@ -586,7 +586,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); @@ -794,7 +794,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]; }); } diff --git a/completion/tests/activity_custom_completion_test.php b/completion/tests/activity_custom_completion_test.php index 79fc49442ac..12be3e1d3c4 100644 --- a/completion/tests/activity_custom_completion_test.php +++ b/completion/tests/activity_custom_completion_test.php @@ -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]; }); diff --git a/course/tests/category_hooks_test.php b/course/tests/category_hooks_test.php index a12eb4d92a4..62114235749 100644 --- a/course/tests/category_hooks_test.php +++ b/course/tests/category_hooks_test.php @@ -190,7 +190,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']; diff --git a/files/tests/converter_test.php b/files/tests/converter_test.php index 1e866c6762a..f58fde21b82 100644 --- a/files/tests/converter_test.php +++ b/files/tests/converter_test.php @@ -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, }); diff --git a/lib/filestorage/tests/file_system_test.php b/lib/filestorage/tests/file_system_test.php index ecef8f13020..32a09cb4520 100644 --- a/lib/filestorage/tests/file_system_test.php +++ b/lib/filestorage/tests/file_system_test.php @@ -1222,7 +1222,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, }); diff --git a/lib/phpunit/classes/base_testcase.php b/lib/phpunit/classes/base_testcase.php index 3ef4aa5cc04..4145f4ad62e 100644 --- a/lib/phpunit/classes/base_testcase.php +++ b/lib/phpunit/classes/base_testcase.php @@ -1,4 +1,5 @@ getInvocationCount(); + } // phpcs:enable } diff --git a/lib/tests/completionlib_test.php b/lib/tests/completionlib_test.php index 6972f59e803..22aaf6da228 100644 --- a/lib/tests/completionlib_test.php +++ b/lib/tests/completionlib_test.php @@ -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'), diff --git a/lib/tests/content/export/exportable_items/exportable_filearea_test.php b/lib/tests/content/export/exportable_items/exportable_filearea_test.php index f49ca5bedb4..b1d70c05c17 100644 --- a/lib/tests/content/export/exportable_items/exportable_filearea_test.php +++ b/lib/tests/content/export/exportable_items/exportable_filearea_test.php @@ -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); diff --git a/lib/tests/content/export/exportable_items/exportable_textarea_test.php b/lib/tests/content/export/exportable_items/exportable_textarea_test.php index 7a695d6e50c..1a90bfd2a3a 100644 --- a/lib/tests/content/export/exportable_items/exportable_textarea_test.php +++ b/lib/tests/content/export/exportable_items/exportable_textarea_test.php @@ -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()) diff --git a/repository/dropbox/tests/api_test.php b/repository/dropbox/tests/api_test.php index 7ee672e207b..383ef508d05 100644 --- a/repository/dropbox/tests/api_test.php +++ b/repository/dropbox/tests/api_test.php @@ -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); diff --git a/repository/googledocs/tests/googledocs_search_content_test.php b/repository/googledocs/tests/googledocs_search_content_test.php index c39b23abc5c..271f0a4b98e 100644 --- a/repository/googledocs/tests/googledocs_search_content_test.php +++ b/repository/googledocs/tests/googledocs_search_content_test.php @@ -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); diff --git a/repository/googledocs/tests/local/browser/googledocs_drive_content_test.php b/repository/googledocs/tests/local/browser/googledocs_drive_content_test.php index 6a84a2a4994..11d5d8b226e 100644 --- a/repository/googledocs/tests/local/browser/googledocs_drive_content_test.php +++ b/repository/googledocs/tests/local/browser/googledocs_drive_content_test.php @@ -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);