neclimdul/phpunit_exceptions

Provides additional methods for consistently testing methods in PHPUnit 9 and 10.

1.0.1 2024-05-03 16:58 UTC

This package is auto-updated.

Last update: 2024-10-20 20:31:45 UTC


README

This projects aims to provide some simple tools to make testing exceptions easier in the PHPUnit testing framework.

It was inspired by similar functionality in Laravel.

Installation

composer require neclimdul/phpunit_exceptions

Usage

<?php

use NecLimDul\PhpUnitExceptions\ExceptionAssertionTrait;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request
use Symfony\Component\HttpKernel\Exception\HttpException;

class MyTest extends TestCase {

  use ExceptionAssertionTrait;

  public function testBadRequest() {
    $request = new Request();
    $this->assertThrows(function () use ($request) {
      (new MyMiddleware())->handle($request, function ($request) {});
    }, HttpException::class);
  }

}