Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make PHPUnit explain deprecations and warnings?

I'm just learning how to use PHPUnit. I'm running it from the command line, and among the results I get OK, but there are issues! Tests: 5, Assertions: 15, Warnings: 2, Deprecations: 5.

I can't work out how to get it to explain what caused the deprecations and warnings counts. How do I find out what's causing it in my test code? I've tried adding the flag --display-deprecations but it doesn't change anything.

I'm using flags --testdox, --stderr and -v but they aren't helping. And I've tried --debug and --verbose but it says neither of them are supported flags.

How do I know what the problem in my code is? While I'm at it, I might as well ask you guys. Here's my test code:

final class FetchForErrorsTest extends TestCase
{
    /**
     * @dataProvider urlsForBasicTest
     */
    public function testNoErrors($url)
    {
        $rr = new RequestResponse(TestConf::$rootUrl.'/'.$url);
        $body = $rr->get();
        $this->assertStringEndsWith('</html>', rtrim($body));
        $this->assertEquals($rr->status, 200);
    }
    public static function urlsForBasicTest()
    {
        return [
            ['index.php'],
            ...
        ];
    }
}

I'm also getting deprecation and warning alerts for the following, without any further explanations:

final class DIContainerTest extends TestCase
{
    public static $di;
    public static $foo;
    public static function setUpBeforeClass(): void
    {
        self::$di = new DIContainer();
        self::$di->register(
            ["Bar", "BarInterface"], 
            "Bar", 'singleton'
        );
        self::$di->register(
            ["Foo", "FooInterface"],
            "Foo"
        );
    }
    public function testCalledFactory()
    {
        self::$foo = self::$di->call("FooInterface::make");
        $this->assertEquals(get_class(self::$foo), "Foo");
    }
    public function testRecursivelyInstantatedConcretes()
    {
        self::$foo = self::$di->call("FooInterface::make");
        $this->assertEquals(get_class(self::$foo->bar), "Bar");
    }
}

It would be really good to find a way to make it more explicit about what the deprecations and warnings are.

Thanks

like image 372
CL22 Avatar asked Aug 30 '25 14:08

CL22


1 Answers

You need to configure the phpunit.xml file to customize how it treats warnings, deprecations etc. This normally goes in the root of your project.

It will look something like this and there are a number of ways you can configure it I just picked some options here check the docs at https://docs.phpunit.de/en/10.5/configuration.html for all the options.

<?xml version="1.0" encoding="UTF-8"?>
<phpunit
         displayDetailsOnTestsThatTriggerDeprecations="true"
         displayDetailsOnTestsThatTriggerErrors="true"
         displayDetailsOnTestsThatTriggerNotices="true"
         displayDetailsOnTestsThatTriggerWarnings="true"
         displayDetailsOnPhpunitDeprecations="true"
         >
</phpunit>
like image 109
mbwasi Avatar answered Sep 02 '25 20:09

mbwasi