Http::response('', 200), ]); $result = app(MailmanService::class)->setCredentials('http://mailman.test/api/', 'user', 'secret')->check(); $this->assertTrue($result); Http::assertSentCount(1); Http::assertSent(fn ($request) => 'GET' === $request->method() && 'http://mailman.test/api/system/versions' === $request->url() && $request->header('Authorization') === ['Basic '.base64_encode('user:secret')]); } public function testItFailsWhenChckingCredentials(): void { Http::fake([ 'http://mailman.test/api/system/versions' => Http::response('', 401), ]); $result = app(MailmanService::class)->setCredentials('http://mailman.test/api/', 'user', 'secret')->check(); $this->assertFalse($result); Http::assertSentCount(1); Http::assertSent(fn ($request) => 'GET' === $request->method() && 'http://mailman.test/api/system/versions' === $request->url() && $request->header('Authorization') === ['Basic '.base64_encode('user:secret')]); } public function testItGetsMembersFromList(): void { Http::fake([ 'http://mailman.test/api/lists/listid/roster/member?page=1&count=10' => Http::response(json_encode([ 'entries' => [ ['email' => 'test@example.com'], ['email' => 'test2@example.com'], ], 'total_size' => 2, ]), 200), ]); $result = app(MailmanService::class)->setCredentials('http://mailman.test/api/', 'user', 'secret')->members('listid'); $this->assertEquals(['test@example.com', 'test2@example.com'], $result->toArray()); Http::assertSentCount(1); Http::assertSent(fn ($request) => 'GET' === $request->method() && 'http://mailman.test/api/lists/listid/roster/member?page=1&count=10' === $request->url() && $request->header('Authorization') === ['Basic '.base64_encode('user:secret')]); } public function testItThrowsExceptionWhenLoginFailed(): void { $this->expectException(MailmanServiceException::class); Http::fake([ 'http://mailman.test/api/lists/listid/roster/member?page=1&count=10' => Http::response('', 401), ]); app(MailmanService::class)->setCredentials('http://mailman.test/api/', 'user', 'secret')->members('listid')->first(); } public function listDataProvider(): Generator { foreach (range(3, 40) as $i) { yield [ collect(range(1, $i)) ->map(fn ($num) => ['email' => 'test'.$num.'@example.com']) ->toArray(), ]; } } /** * @dataProvider listDataProvider */ public function testItReturnsMoreThanOneResult(array $totals): void { $totals = collect($totals); foreach ($totals->chunk(10) as $n => $chunk) { Http::fake([ 'http://mailman.test/api/lists/listid/roster/member?page='.($n + 1).'&count=10' => Http::response(json_encode([ 'entries' => $chunk, 'total_size' => $totals->count(), ]), 200), ]); } $result = app(MailmanService::class)->setCredentials('http://mailman.test/api/', 'user', 'secret')->members('listid'); $this->assertCount($totals->count(), $result->toArray()); Http::assertSentCount($totals->chunk(10)->count()); } }