From 4963e95feb8a8e174f065ff2e66c5de850066251 Mon Sep 17 00:00:00 2001 From: Stefan Topfstedt Date: Wed, 23 Oct 2024 09:40:43 -0700 Subject: [PATCH] replace assertions in test-breaking closures with history-middleware based equivalent. for reference on the new approach, please see https://docs.guzzlephp.org/en/stable/testing.html#history-middleware --- tests/ilios_test.php | 53 ++++++++++-------- tests/task/sync_task_test.php | 101 +++++++++++++++++----------------- 2 files changed, 80 insertions(+), 74 deletions(-) diff --git a/tests/ilios_test.php b/tests/ilios_test.php index baee274..9ea028f 100644 --- a/tests/ilios_test.php +++ b/tests/ilios_test.php @@ -31,6 +31,7 @@ use GuzzleHttp\Exception\GuzzleException; use GuzzleHttp\Handler\MockHandler; use GuzzleHttp\HandlerStack; +use GuzzleHttp\Middleware; use GuzzleHttp\Psr7\Response; use moodle_exception; use Psr\Http\Message\RequestInterface; @@ -61,19 +62,21 @@ public function test_get_schools(): void { set_config('host_url', 'http://ilios.demo', 'tool_ilioscategoryassignment'); $handlerstack = HandlerStack::create(new MockHandler([ - function(RequestInterface $request) { - $this->assertEquals('/api/v3/schools', $request->getUri()->getPath()); - return new Response(200, [], json_encode([ + new Response(200, [], json_encode([ 'schools' => [ ['id' => 1, 'title' => 'Medicine'], ['id' => 2, 'title' => 'Pharmacy'], ], - ])); - }, + ])), ])); + $container = []; + $history = Middleware::history($container); + $handlerstack->push($history); di::set(http_client::class, new http_client(['handler' => $handlerstack])); $ilios = di::get(ilios::class); $schools = $ilios->get_schools(); + + $this->assertEquals('/api/v3/schools', $container[0]['request']->getUri()->getPath()); $this->assertCount(2, $schools); $this->assertEquals(1, $schools[0]->id); $this->assertEquals('Medicine', $schools[0]->title); @@ -96,23 +99,25 @@ public function test_get_enabled_users_in_school(): void { $schoolid = 123; $handlerstack = HandlerStack::create(new MockHandler([ - function(RequestInterface $request) use ($schoolid) { - $this->assertEquals('/api/v3/users', $request->getUri()->getPath()); - $this->assertEquals( - "filters[enabled]=true&filters[school]={$schoolid}", - urldecode($request->getUri()->getQuery()) - ); - return new Response(200, [], json_encode([ + new Response(200, [], json_encode([ 'users' => [ ['id' => 1, 'campusId' => 'xx00001'], ['id' => 2, 'campusId' => 'xx00002'], ], - ])); - }, + ])), ])); + $container = []; + $history = Middleware::history($container); + $handlerstack->push($history); di::set(http_client::class, new http_client(['handler' => $handlerstack])); $ilios = di::get(ilios::class); $users = $ilios->get_enabled_users_in_school($schoolid); + + $this->assertEquals('/api/v3/users', $container[0]['request']->getUri()->getPath()); + $this->assertEquals( + "filters[enabled]=true&filters[school]={$schoolid}", + urldecode( $container[0]['request']->getUri()->getQuery()) + ); $this->assertCount(2, $users); $this->assertEquals(1, $users[0]->id); $this->assertEquals('xx00001', $users[0]->campusId); @@ -134,19 +139,21 @@ public function test_get(): void { set_config('host_url', 'http://ilios.demo', 'tool_ilioscategoryassignment'); $handlerstack = HandlerStack::create(new MockHandler([ - function (RequestInterface $request) { - $this->assertEquals('/api/v3/schools', $request->getUri()->getPath()); - return new Response(200, [], json_encode([ - 'schools' => [ - ['id' => 1, 'title' => 'Medicine'], - ['id' => 2, 'title' => 'Pharmacy'], - ], - ])); - }, + new Response(200, [], json_encode([ + 'schools' => [ + ['id' => 1, 'title' => 'Medicine'], + ['id' => 2, 'title' => 'Pharmacy'], + ], + ])), ])); + $container = []; + $history = Middleware::history($container); + $handlerstack->push($history); di::set(http_client::class, new http_client(['handler' => $handlerstack])); $ilios = di::get(ilios::class); $data = $ilios->get('schools'); + + $this->assertEquals('/api/v3/schools', $container[0]['request']->getUri()->getPath()); $this->assertCount(2, $data->schools); $this->assertEquals(1, $data->schools[0]->id); $this->assertEquals('Medicine', $data->schools[0]->title); diff --git a/tests/task/sync_task_test.php b/tests/task/sync_task_test.php index 36477ac..850f091 100644 --- a/tests/task/sync_task_test.php +++ b/tests/task/sync_task_test.php @@ -34,6 +34,7 @@ use Exception; use GuzzleHttp\Handler\MockHandler; use GuzzleHttp\HandlerStack; +use GuzzleHttp\Middleware; use GuzzleHttp\Psr7\Response; use moodle_exception; use Psr\Http\Message\RequestInterface; @@ -184,24 +185,12 @@ public function test_execute(): void { // Since the task will be run twice in this test, we'll need to set up two mock responses from Ilios. // We'll let the mocked API respond with the same payload twice in order to check that no state change occurs. $handlerstack = HandlerStack::create(new MockHandler([ - function(RequestInterface $request) use ($schoolid, $mockresponsepayload) { - $this->assertEquals('/api/v3/users', $request->getUri()->getPath()); - $this->assertEquals( - "filters[enabled]=true&filters[school]={$schoolid}", - urldecode($request->getUri()->getQuery()) - ); - return new Response(200, [], json_encode($mockresponsepayload)); - - }, - function(RequestInterface $request) use ($schoolid, $mockresponsepayload) { - $this->assertEquals('/api/v3/users', $request->getUri()->getPath()); - $this->assertEquals( - "filters[enabled]=true&filters[school]={$schoolid}", - urldecode($request->getUri()->getQuery()) - ); - return new Response(200, [], json_encode($mockresponsepayload)); - }, + new Response(200, [], json_encode($mockresponsepayload)), + new Response(200, [], json_encode($mockresponsepayload)), ])); + $container = []; + $history = Middleware::history($container); + $handlerstack->push($history); di::set(http_client::class, new http_client(['handler' => $handlerstack])); // Instantiate the task. @@ -222,15 +211,6 @@ function(RequestInterface $request) use ($schoolid, $mockresponsepayload) { $output = ob_get_contents(); ob_end_clean(); - // Check the captured task output. - $this->assertStringContainsString('Retrieved 5 Ilios user(s) to sync.', $output); - $this->assertStringContainsString( - 'WARNING: Skipping non-matching user accounts with the following Ilios campus IDs: xx99998', - $output - ); - $this->assertStringContainsString('Assigned 2 user(s) into category.', $output); - $this->assertStringContainsString('Un-assigned 1 user(s) from category.', $output); - // Post-sync category role assignment checks. $roleassignments = get_users_from_role_on_context((object)['id' => $roleid], $category->get_context()); $this->assertCount(4, $roleassignments); @@ -251,6 +231,27 @@ function(RequestInterface $request) use ($schoolid, $mockresponsepayload) { $output = ob_get_contents(); ob_end_clean(); + // Check the captured request history. + $this->assertEquals('/api/v3/users', $container[0]['request']->getUri()->getPath()); + $this->assertEquals( + "filters[enabled]=true&filters[school]={$schoolid}", + urldecode($container[0]['request']->getUri()->getQuery()) + ); + $this->assertEquals('/api/v3/users', $container[1]['request']->getUri()->getPath()); + $this->assertEquals( + "filters[enabled]=true&filters[school]={$schoolid}", + urldecode($container[1]['request']->getUri()->getQuery()) + ); + + // Check the captured task output. + $this->assertStringContainsString('Retrieved 5 Ilios user(s) to sync.', $output); + $this->assertStringContainsString( + 'WARNING: Skipping non-matching user accounts with the following Ilios campus IDs: xx99998', + $output + ); + $this->assertStringContainsString('Assigned 2 user(s) into category.', $output); + $this->assertStringContainsString('Un-assigned 1 user(s) from category.', $output); + // Check the captured task output. $this->assertStringContainsString('Retrieved 5 Ilios user(s) to sync.', $output); $this->assertStringContainsString( @@ -302,34 +303,15 @@ public function test_execute_ignore_out_of_band_category_assignments(): void { $handlerstack = HandlerStack::create(new MockHandler([ // User is in the payload, as instructor. - function(RequestInterface $request) use ($schoolid) { - $this->assertEquals('/api/v3/users', $request->getUri()->getPath()); - $this->assertEquals( - "filters[enabled]=true&filters[school]={$schoolid}", - urldecode($request->getUri()->getQuery()) - ); - return new Response(200, [], json_encode(['users' => [['campusId' => 'xx00001', 'instructorGroups' => [1]]]])); - }, + new Response(200, [], json_encode(['users' => [['campusId' => 'xx00001', 'instructorGroups' => [1]]]])), // User is in the payload, as director. - function(RequestInterface $request) use ($schoolid) { - $this->assertEquals('/api/v3/users', $request->getUri()->getPath()); - $this->assertEquals( - "filters[enabled]=true&filters[school]={$schoolid}", - urldecode($request->getUri()->getQuery()) - ); - return new Response(200, [], json_encode(['users' => [['campusId' => 'xx00001', 'directedCourses' => [1]]]])); - - }, + new Response(200, [], json_encode(['users' => [['campusId' => 'xx00001', 'directedCourses' => [1]]]])), // User is not in payload. - function(RequestInterface $request) use ($schoolid) { - $this->assertEquals('/api/v3/users', $request->getUri()->getPath()); - $this->assertEquals( - "filters[enabled]=true&filters[school]={$schoolid}", - urldecode($request->getUri()->getQuery()) - ); - return new Response(200, [], json_encode(['users' => [['campusId' => 'xx99999']]])); - }, + new Response(200, [], json_encode(['users' => [['campusId' => 'xx99999']]])), ])); + $container = []; + $history = Middleware::history($container); + $handlerstack->push($history); di::set(http_client::class, new http_client(['handler' => $handlerstack])); // Instantiate the task. @@ -372,6 +354,23 @@ function(RequestInterface $request) use ($schoolid) { $output = ob_get_contents(); ob_end_clean(); + // Check the captured request history. + $this->assertEquals('/api/v3/users', $container[0]['request']->getUri()->getPath()); + $this->assertEquals( + "filters[enabled]=true&filters[school]={$schoolid}", + urldecode($container[0]['request']->getUri()->getQuery()) + ); + $this->assertEquals('/api/v3/users', $container[1]['request']->getUri()->getPath()); + $this->assertEquals( + "filters[enabled]=true&filters[school]={$schoolid}", + urldecode($container[1]['request']->getUri()->getQuery()) + ); + $this->assertEquals('/api/v3/users', $container[2]['request']->getUri()->getPath()); + $this->assertEquals( + "filters[enabled]=true&filters[school]={$schoolid}", + urldecode($container[2]['request']->getUri()->getQuery()) + ); + // Check the captured task output. $this->assertStringContainsString('No user assignment/un-assignment necessary.', $output);