Skip to content

Commit

Permalink
replace assertions in test-breaking closures with history-middleware …
Browse files Browse the repository at this point in the history
…based equivalent.

for reference on the new approach, please see https://docs.guzzlephp.org/en/stable/testing.html#history-middleware
  • Loading branch information
stopfstedt committed Oct 23, 2024
1 parent 8b485b6 commit 6e7cc7b
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 65 deletions.
53 changes: 30 additions & 23 deletions tests/ilios_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
83 changes: 41 additions & 42 deletions tests/task/sync_task_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -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(
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 6e7cc7b

Please sign in to comment.