# 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`](/reference/sdk/php/api/core/plug) is an interface, you can replace it with a mock that returns the values your test expects.

## Unit testing \[#unit-testing]

Inject the [`Plug`](/reference/sdk/php/api/core/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:

```php
<?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 \[#kernel-testing]

To test a block or controller end-to-end, replace the [`Plug`](/reference/sdk/php/api/core/plug) service in the test container with a mock so you control the responses.

In a [`KernelTestBase`](https://www.drupal.org/docs/automated-testing/types-of-tests), swap the service before rendering:

```php
<?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.

## Explore

- [Plug reference](/reference/sdk/php/api/core/plug): Explore the methods you can mock in your tests.
- [Configuration](/reference/sdk/drupal/api/configuration): Explore the settings you can override in your tests.
