Upgrading PHPUnit from 4.8 to 5.5

We upgraded our systems to PHP7.0. This required us to upgrade PHPUnit to 5.5, since 4.8 does not support PHP7. This leads to the following error, which sets a crash in phpunit in our CI

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

Now it looks like we need to touch 1200+ unit tests to reorganize how we build our layouts.

Is there a way to suppress this warning or quickly convert our use of getMock to createMock , which seems to work differently so that the global find / replace does not shorten it?

+7
php unit-testing phpunit
source share
3 answers

You can create an additional test class TestAdapter that extends 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 your tests from this class.

+6
source share

The following works for my old tests ...

 /** * Returns a mock object for the specified class. * * This method is a temporary solution to provide backward compatibility for tests that are still using the old * (4.8) getMock() method. * We should update the code and remove this method but for now this is good enough. * * * @param string $originalClassName Name of the class to mock. * @param array|null $methods When provided, only methods whose names are in the array * are replaced with a configurable test double. The behavior * of the other methods is not changed. * Providing null means that no methods will be replaced. * @param array $arguments Parameters to pass to the original class' constructor. * @param string $mockClassName Class name for the generated test double class. * @param bool $callOriginalConstructor Can be used to disable the call to the original class' constructor. * @param bool $callOriginalClone Can be used to disable the call to the original class' clone constructor. * @param bool $callAutoload Can be used to disable __autoload() during the generation of the test double class. * @param bool $cloneArguments * @param bool $callOriginalMethods * @param object $proxyTarget * * @return \PHPUnit_Framework_MockObject_MockObject * * @throws \Exception */ public function getMock($originalClassName, $methods = array(), array $arguments = array(), $mockClassName = '', $callOriginalConstructor = true, $callOriginalClone = true, $callAutoload = true, $cloneArguments = false, $callOriginalMethods = false, $proxyTarget = null) { $builder = $this->getMockBuilder($originalClassName); if (is_array($methods)) { $builder->setMethods($methods); } if (is_array($arguments)) { $builder->setConstructorArgs($arguments); } $callOriginalConstructor ? $builder->enableOriginalConstructor() : $builder->disableOriginalConstructor(); $callOriginalClone ? $builder->enableOriginalClone() : $builder->disableOriginalClone(); $callAutoload ? $builder->enableAutoload() : $builder->disableAutoload(); $cloneArguments ? $builder->enableOriginalClone() : $builder->disableOriginalClone(); $callOriginalMethods ? $builder->enableProxyingToOriginalMethods() : $builder->disableProxyingToOriginalMethods(); if ($mockClassName) { $builder->setMockClassName($mockClassName); } if ($proxyTarget) { $builder->setProxyTarget($proxyTarget); } $mockObject = $builder->getMock(); return $mockObject; } 
0
source share

There was the same problem. I did a regex replacement to fix the obsolete getMock() entries.

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

hope this helps

0
source share

All Articles