Home » Php » php – Upgrading PHPUnit from 4.8 to 5.5

php – Upgrading PHPUnit from 4.8 to 5.5

Posted by: admin July 12, 2020 Leave a comment

Questions:

We upgraded our systems to PHP7.0.
This required us to upgrade PHPUnit to 5.5, as 4.8 doesn’t properly support PHP7.
This produces the following error, which nets a failure in phpunit in our CI

PHPUnit_Framework_TestCase::getMock() is deprecated,
use PHPUnit_Framework_TestCase::createMock() or
PHPUnit_Framework_TestCase::getMockBuilder() instead

What it looks like now is that we have to touch 1200+ unit tests to refactor how we build our mocks.

Is there either a way to suppress that warning, or, quickly convert our uses of getMock to createMock, which seems to work differently enough that a global find/replace won’t cut it?

How to&Answers:

You could create additional test class called TestAdapter which will extend PHPUnit_Framework_TestCase

class TestAdapter extends PHPUnit_Framework_TestCase
{
    /**
    * Override your deprecated method
    */
    public function getMock()
    {
        return $this->createMock();
    }
}

Then you just need to extend all of your tests from this class.

Answer:

Had the same issue. I did run a regex replace to fix getMock() deprecated entries.

->getMock\(([^)]+)\) replaced with ->getMockBuilder($1)->getMock()

Hope that helps