Change record status: 
Project: 
Introduced in branch: 
8.4.x
Introduced in version: 
8.4.4
Description: 

getMock() is removed in PHPUnit 6 and createMock() doesn't exist in PHPUnit 4.8, the use of getMock() in any PHPUnit based tests has been deprecated in favour of createMock() or getMockBuilder().

See https://github.com/sebastianbergmann/phpunit/wiki/Release-Announcement-f...

To smooth over the transition to PHPUnit 6 and later, a forward and backward compatibility layer is provided in the form of \Drupal\Tests\PhpunitCompatibilityTrait. This will allow the use of createMock while running PHPUnit 4, and maintain backward compatibility when moving to PHPUnit 6 and later.

Before:

$this->entityTypeManager = $this->getMock(EntityTypeManagerInterface::class);

After:

$this->entityTypeManager = $this->createMock(EntityTypeManagerInterface::class);
Impacts: 
Module developers

Comments

eric.chenchao’s picture

Use getMockBuilder to the do the partial mock after getMock is deprecated.

For example I have a class called JwtService. I want to test this class but mock createJwtBuilder method of it. I can do

$mocked_jwt_service = $this->getMockBuilder(JwtService::class)
      ->setMethods(['createJwtBuilder'])
      ->setConstructorArgs($arguments)   // Arguments used in JwtService constructor
      ->getMock();

Check more examples here:
https://phpunit.de/manual/6.5/en/test-doubles.html#test-doubles.mock-obj...