# PHPUnit email and OTP testing

MailSlurp fits PHPUnit when your PHP test suite drives application requests, browser helpers, or Laravel feature tests and the MailSlurp PHP client handles disposable inboxes and wait methods. This keeps email verification and password-reset checks inside the same assertions you already run in CI.

## Install

```bash
composer require --dev mailslurp/mailslurp-client-php
```

## Create an inbox with the PHP SDK

```php
$options = new \MailSlurp\Models\CreateInboxDto();
$options->setName("Test inbox");
$options->setPrefix("test");
$inbox = $inboxController->createInboxWithOptions($options);
```

## Example PHPUnit OTP flow

```php
<?php

use MailSlurp\Apis\InboxControllerApi;
use MailSlurp\Apis\WaitForControllerApi;
use MailSlurp\Configuration;
use PHPUnit\Framework\TestCase;

final class SignupOtpTest extends TestCase
{
    public function testCanExtractVerificationCode(): void
    {
        $config = Configuration::getDefaultConfiguration()
            ->setApiKey('x-api-key', getenv("API_KEY"));

        $inboxController = new InboxControllerApi(null, $config);
        $waitForController = new WaitForControllerApi(null, $config);

        $inbox = $inboxController->createInboxWithDefaults();

        // Submit $inbox->getEmailAddress() in the app under test here.

        $email = $waitForController->waitForLatestEmail(
            $inbox->getId(),
            120000,
            true
        );

        preg_match(
            '/(?:verification code|OTP|code)[:\\s-]*(\\d{6})/',
            $email->getBody() ?? '',
            $matches
        );

        $this->assertArrayHasKey(1, $matches);
        $this->assertNotEmpty($matches[1]);
    }
}
```

## Example projects

- [Laravel PHPUnit example](https://github.com/mailslurp/examples/tree/master/php-laravel-phpunit)
- [Composer PHPUnit example](https://github.com/mailslurp/examples/tree/master/php-composer-phpunit)

<div data-component="DocsExamplesList" class="docs-card-grid docs-examples-list" data-example-terms="phpunit" data-example-count="2">
<a class="docs-card docs-example-card" href="https://www.github.com/mailslurp/examples/tree/master/php-composer-phpunit" target="_blank" rel="noopener noreferrer"><span class="docs-card-title">Php Composer Phpunit</span><span class="docs-card-description">php example repository: <code>php-composer-phpunit</code></span></a>
<a class="docs-card docs-example-card" href="https://www.github.com/mailslurp/examples/tree/master/php-laravel-phpunit" target="_blank" rel="noopener noreferrer"><span class="docs-card-title">Php Laravel Phpunit</span><span class="docs-card-description">php example repository: <code>php-laravel-phpunit</code></span></a>
</div>

## Related docs

- [PHP SDK](/docs/php/)
- [Wait for methods](/docs/wait-for/)
- [Integration testing guide](/docs/testing/)
