Testing

Learn how to test your integration.

When 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 mock that returns the values your test expects.

Unit testing

Inject the Plug interface into your controllers and services, then provide a mock in your tests.

This example mocks a content fetch with PHPUnit:

12345678910111213141516171819
<?php
use 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->index()->getContent());    }}

Integration testing

To test a request end-to-end, replace the Plug service in the test container with a mock so you control the responses.

In a WebTestCase, swap the service before issuing the request:

1234567891011121314151617181920212223
<?php
use Croct\Plug\Plug;use Croct\Plug\FetchResponse;use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
final class HomeControllerTest extends WebTestCase{    public function testRendersHero(): void    {        $client = static::createClient();
        $croct = $this->createMock(Plug::class);        $croct->method('fetchContent')            ->willReturn(new FetchResponse(['title' => 'Welcome']));
        static::getContainer()->set(Plug::class, $croct);
        $client->request('GET', '/');
        self::assertSelectorTextContains('h1', 'Welcome');    }}

To exercise the real SDK against a mock server instead, point the base_endpoint_url at it in the test environment:

config/packages/test/croct.yaml
croct:    base_endpoint_url: 'http://localhost:8080'

Both approaches keep your tests deterministic and isolated from the network.