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..2b979c1 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,6 +211,18 @@ 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( @@ -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);