Home > Blockchain >  How to solve "PHP Fatal error: Declaration must be comatible with PHPUnit\Framework\TestCase:
How to solve "PHP Fatal error: Declaration must be comatible with PHPUnit\Framework\TestCase:

Time:03-23

I use PHPUnit 9.0., Laravel framework 8.* and PHP 7.4.

I run into an PHP Fatal error by using a Trait that has a same method name like a class at the PhpUnit Framework.

The Error is:

PHP Fatal error:  Declaration of App\Traits\ExampleTrait::getStatus(App\Example\ExampleConnectionInterface $example_connection, $unique_id) 
must be compatible with PHPUnit\Framework\TestCase::getStatus(): int in /home/vagrant/code/tests/Integration/JobTests/ExampleJobTest.php 
on line 204

The ExampleJobTest.php looks like this:

<?php

namespace Tests\Integration\JobTests\Examples;

use App\Traits\ExampleTrait;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Tests\TestCase;

class ExampleTest extends TestCase
{
    use WithoutMiddleware;
    use DatabaseTransactions;
    use ExampleTrait;

    protected function setUp(): void
    {
        parent::setUp();

        $this->example_connection = new ExampleConnection();
    }

}

I have to use the App\Traits\ExampleTrait many times for different Tests and everytime I run in this error. When I rename the function App\Traits\ExampleTrait::getStatus(...) into another name the error doesn't occur.

Of Course, the easiest and fastest way would be to rename the function in App\Traits\ExampleTrait, but my team asked me to find another solution.

So, is there way to fix this error without renaming the function name in App\Traits\ExampleTrait?

CodePudding user response:

PHPUnit's TestCase class already uses a method named getStatus. You cannot override it if the signatures don't match, and you should not override it, unless you really need this to be overriden. PHPUnit uses this method internally


Solution: Choose a different name for that method and you're done.

CodePudding user response:

First of all, you should NEVER use (a trait) from App inside a test class... So you are already doing something wrong...

Second of all, the error is pretty explanatory, you are overriding PHPUnit\Framework\TestCase::getStatus because your trait implements the same method, so you have some solutions:

  1. Stop using the trait inside your test/move it into the tests folder and rename the method.
  2. Use an alias for the method:
use ExampleTrait {
    ExampleTrait::getStatus as exampleGetStatus;
}
  1. I have no idea what is getStatus doing (also rename your trait please, what the hell is ExampleTrait?), but you can update getStatus to have the same signing as the TestCase method. Your trait's method should be getStatus(): int without any parameters, but I doubt this would fix your issue as I have no idea what you are using this method for...

So, the best solution for you would be 1..

CodePudding user response:

You can simply rename the ExampleTrait::getStatus in ExampleTest

use ExampleTrait {
    ExampleTrait::getStatus as exampleTraitGetStatus;
}
  • Related