# 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]

Type-hint the [`Plug`](/reference/sdk/php/api/core/plug) interface in your controllers and services, then provide a mock in your tests to control what it returns.

This example mocks a content fetch with PHPUnit:

```php
<?php

use Croct\Plug\Plug;
use Croct\Plug\FetchResponse;
use PHPUnit\Framework\TestCase;

final class HeroServiceTest extends TestCase
{
    public function testBuildsHeadline(): void
    {
        $croct = $this->createMock(Plug::class);
        $croct->method('fetchContent')
            ->willReturn(new FetchResponse(['title' => 'Welcome']));

        $service = new HeroService($croct);

        $this->assertSame('Welcome', $service->headline());
    }
}
```

## Feature testing \[#feature-testing]

To test a request end-to-end, swap the [`Plug`](/reference/sdk/php/api/core/plug) binding in the container with a mock so you control the responses.

Use [`mock`](https://laravel.com/docs/mocking#mocking-objects) before issuing the request:

```php
<?php

namespace Tests\Feature;

use Croct\Plug\Plug;
use Croct\Plug\FetchResponse;
use Tests\TestCase;

final class HomePageTest extends TestCase
{
    public function testRendersHero(): void
    {
        $this->mock(Plug::class, function ($mock) {
            $mock->shouldReceive('fetchContent')
                ->andReturn(new FetchResponse(['title' => 'Welcome']));
        });

        $this->get('/')->assertSee('Welcome');
    }
}
```

To exercise the real SDK against a mock server instead, point the [`base_endpoint_url`](/reference/sdk/laravel/api/configuration#base_endpoint_url-prop) at it in the test environment:

**phpunit.xml**

```xml
<php>
    <env name="CROCT_BASE_ENDPOINT_URL" value="http://localhost:8080"/>
</php>
```

Both approaches keep 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/laravel/api/configuration): Explore the options you can override in your tests.
