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 blocks and services, then provide a mock in your tests to control what it returns.

This example mocks a content fetch with PHPUnit:

123456789101112131415161718192021
<?php
declare(strict_types=1);
use Croct\Plug\Plug;use Croct\Plug\FetchResponse;use PHPUnit\Framework\TestCase;
final class HomeHeroBlockTest extends TestCase{    public function testBuildsHero(): void    {        $croct = $this->createMock(Plug::class);        $croct->method('fetchContent')            ->willReturn(new FetchResponse(['title' => 'Welcome']));
        $block = new HomeHeroBlock([], 'home_hero', [], $croct);
        $this->assertSame('Welcome', $block->build()['#context']['hero']['title']);    }}

Kernel testing

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

In a KernelTestBase, swap the service before rendering:

1234567891011121314151617181920212223
<?php
declare(strict_types=1);
use Croct\Plug\Plug;use Croct\Plug\FetchResponse;use Drupal\KernelTests\KernelTestBase;
final class HomeHeroBlockTest extends KernelTestBase{    protected static $modules = ['croct', 'my_module'];
    public function testRendersHero(): void    {        $croct = $this->createMock(Plug::class);        $croct->method('fetchContent')            ->willReturn(new FetchResponse(['title' => 'Welcome']));
        $this->container->set(Plug::class, $croct);
        // Build your block and assert on the resulting render array.    }}

This keeps your tests deterministic and isolated from the network.