Skip to content

Explicit API for expecting PHP errors, warnings, and notices #3775

@sebastianbergmann

Description

@sebastianbergmann

The following methods should be added to TestCase:

  • expectDeprecation() for E_DEPRECATED and E_USER_DEPRECATED
  • expectNotice() for E_NOTICE, E_USER_NOTICE, and E_STRICT
  • expectWarning() for E_WARNING and E_USER_WARNING
  • expectError() for everything else

Then

<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;

final class Test extends TestCase
{
    public function testOne()
    {
        $this->expectNotice();

        $a = $b;
    }
}

can be used instead of

<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Error\Notice;

final class Test extends TestCase
{
    public function testOne()
    {
        $this->expectException(Notice::class);

        $a = $b;
    }
}

to expect the code to trigger an E_NOTICE, for instance.

Metadata

Metadata

Labels

type/enhancementA new idea that should be implemented

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions