Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upgrading PHPUnit from 4.8 to 5.5

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?

like image 268
Umbrella Avatar asked Aug 22 '16 18:08

Umbrella


2 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.

like image 135
jaro1989 Avatar answered Oct 04 '22 00:10

jaro1989


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

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

Hope that helps

like image 31
Amadu Bah Avatar answered Oct 04 '22 00:10

Amadu Bah