Skip to content

Commit

Permalink
MDL-81308 core: Use a helper for consecutive invocation counts
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols authored and junpataleta committed Oct 23, 2024
1 parent 16efbca commit 5ab2997
Show file tree
Hide file tree
Showing 14 changed files with 41 additions and 28 deletions.
8 changes: 4 additions & 4 deletions admin/tool/dataprivacy/tests/expired_contexts_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -1378,7 +1378,7 @@ public function test_process_user_context(): void {
$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'),
Expand Down Expand Up @@ -1604,7 +1604,7 @@ public function test_process_user_historic_block_unapproved(): void {
$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'),
Expand Down Expand Up @@ -1665,7 +1665,7 @@ public function test_process_user_historic_unexpired_child(): void {
$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'),
Expand Down Expand Up @@ -1892,7 +1892,7 @@ public function test_process_course_context_approved_children(): void {
$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'),
Expand Down
4 changes: 2 additions & 2 deletions admin/tool/langimport/tests/locale_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function test_check_locale_availability(): void {
$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',
});
Expand All @@ -82,7 +82,7 @@ public function test_check_locale_availability(): void {
$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',
});
Expand Down
6 changes: 3 additions & 3 deletions admin/tool/usertours/tests/tour_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ public function test_remove_persisted(): void {
$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);
Expand Down Expand Up @@ -586,7 +586,7 @@ public function test_reset_step_sortorder(): void {
$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);
Expand Down Expand Up @@ -794,7 +794,7 @@ public function test_get_tour_key($id, $getconfig, $setconfig, $willpersist, $us
$tour->expects($getinvocations)
->method('get_config')
->willReturnCallback(function () use ($getinvocations, $getconfig) {
return $getconfig[$getinvocations->getInvocationCount() - 1];
return $getconfig[self::getInvocationCount($getinvocations) - 1];
});
}

Expand Down
2 changes: 1 addition & 1 deletion completion/tests/activity_custom_completion_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function test_get_overall_completion_state(array $rules, array $rulestate
$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];
});
Expand Down
2 changes: 1 addition & 1 deletion course/tests/category_hooks_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public function test_get_course_category_contents_hook(): void {
$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'];
Expand Down
6 changes: 3 additions & 3 deletions files/tests/converter_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ public function test_poll_conversion_pick_subsequent(): void {
$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),
});
Expand All @@ -573,7 +573,7 @@ public function test_poll_conversion_pick_subsequent(): void {
$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.
Expand All @@ -590,7 +590,7 @@ public function test_poll_conversion_pick_subsequent(): void {
$conversion
->expects($instanceinvocations)
->method('get_converter_instance')
->willReturnCallback(fn (): object => match ($instanceinvocations->getInvocationCount()) {
->willReturnCallback(fn (): object => match (self::getInvocationCount($instanceinvocations)) {
1 => $converterinstance,
default => $converterinstance2,
});
Expand Down
2 changes: 1 addition & 1 deletion lib/filestorage/tests/file_system_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -1222,7 +1222,7 @@ public function test_mimetype_from_storedfile_using_file_content_remote(): void
$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,
});
Expand Down
13 changes: 13 additions & 0 deletions lib/phpunit/classes/base_testcase.php
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -594,5 +595,17 @@ protected static function getNodeText(DOMNode $node) {

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
}
10 changes: 5 additions & 5 deletions lib/tests/completionlib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ public function test_update_state(): void {
->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'),
Expand All @@ -279,7 +279,7 @@ public function test_update_state(): void {
$comparewith1,
$comparewith2
): void {
switch ($setinvocations->getInvocationCount()) {
switch (self::getInvocationCount($setinvocations)) {
case 1:
$this->assertEquals($cm, $comparecm);
$comparewith1->evaluate($comparewith);
Expand Down Expand Up @@ -714,7 +714,7 @@ public function test_reset_all_state(): void {
->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;
Expand Down Expand Up @@ -1218,7 +1218,7 @@ public function test_get_progress_all_lots(): void {
$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', []];
Expand All @@ -1233,7 +1233,7 @@ public function test_get_progress_all_lots(): void {
$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'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ protected function assert_exportable_matches_file(
$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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ protected function assert_exportable_matches_file(
$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())
Expand Down
8 changes: 4 additions & 4 deletions repository/dropbox/tests/api_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ public function test_fetch_dropbox_data_recurse_on_additional_records(): void {
$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']]),
Expand All @@ -420,7 +420,7 @@ public function test_fetch_dropbox_data_recurse_on_additional_records(): void {
$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';
Expand Down Expand Up @@ -484,7 +484,7 @@ public function test_fetch_dropbox_content(): void {
$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;
Expand Down Expand Up @@ -571,7 +571,7 @@ public function test_get_file_share_info_new(): void {
$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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function test_get_content_nodes(string $query, bool $sortcontent, array $
$searccontents,
$searchparams,
) {
switch ($callinvocations->getInvocationCount()) {
switch (self::getInvocationCount($callinvocations)) {
case 1:
$this->assertEquals('shared_drives_list', $method);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function test_get_content_nodes(string $query, string $path, bool $sortco
$listparams,
$drivecontents,
) {
switch ($callinvocations->getInvocationCount()) {
switch (self::getInvocationCount($callinvocations)) {
case 1:
$this->assertEquals('shared_drives_list', $method);
$this->assertEquals([], $params);
Expand Down

0 comments on commit 5ab2997

Please sign in to comment.