Unit testing

Learn how to unit test code that uses the SDK.

When unit testing code that uses the SDK, you do not want to make real API calls. Because Plug is an interface, you can replace it with a test double that returns the values your test expects.

Mocking the SDK

Inject the Plug interface into your code instead of the concrete Croct class, then provide a mock in your tests.

This example mocks a content fetch with PHPUnit:

123456789101112131415161718
<?phpuse Croct\Plug\Plug;use Croct\Plug\FetchResponse;use PHPUnit\Framework\TestCase;
final class HomeControllerTest extends TestCase{    public function testRendersHeadline(): void    {        $croct = $this->createMock(Plug::class);        $croct->method('fetchContent')            ->willReturn(new FetchResponse(['title' => 'Welcome']));
        $controller = new HomeController($croct);
        $this->assertStringContainsString('Welcome', $controller->render());    }}

Mocking the interface keeps your unit tests fast and deterministic, with no network access.