d
Amit DhamuSoftware Engineer
 

Migrating to PHPUnit 8

3 minute read 00000 views

Coming from an old version of PHPUnit, let's see what the current supported version (8) deprecates, removes and what you need to change when writing your tests.

blacklist is now whitelist

<!-- Before -->
<blacklist>
    <directory>./config</directory>
</blacklist>

<!-- After -->
<whitelist>
    <exclude>
        <directory>./config</directory>
    </exclude>
</whitelist>

The library is now namespaced

As a rule of thumb, underscores can be replaced with a backslash

// Before
use PHPUnit_Framework_TestCase;
use PHPUnit_Framework_MockObject_MockObject;
use PHPUnit_Framework_MockObject_Stub_ConsecutiveCalls;
use PHPUnit_Framework_MockObject_InvocationMocker;

// After
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\MockObject\Stub\ConsecutiveCalls;
use PHPUnit\Framework\MockObject\InvocationMocker;
...etc

Extended methods like setUp and tearDown need to match their parent methods including return types

// Before
public function setUp()
public function tearDown()
public static function setUpBeforeClass()
public static function tearDownAfterClass()

// After
public function setUp(): void
public function tearDown(): void
public static function setUpBeforeClass(): void
public static function tearDownAfterClass(): void

You can't call getMock() with arguments, use getMockBuilder instead

// Before
$serviceMock = $this->getMock(
    'Service',
    array('update'),
    $params,
    '',
    false
);

// After
$serviceMock = $this->getMockBuilder('Service')
    ->setMethods(array('update'))
    ->setConstructorArgs($params)
    ->setMockClassName('')
    ->disableOriginalConstructor()
    ->getMock();
);

Don't use @expectedException and @expectedExceptionMessage docblocks or setExpectedException

// Before
/**
 * Test that an exception is thrown when an unsupported form type is specified
 *
 * @expectedException InvalidArgumentException
 * @expectedExceptionMessage Invalid!
 */
public function testInstantiateContactFormThrowsException()
{
    (new ContactForm('foo'));
}

public function testInstantiateContactFormThrowsException()
{
    $this->setExpectedException('InvalidArgumentException', 'Invalid!');
    (new ContactForm('foo'));
}

// After
/**
 * Test that an exception is thrown when an unsupported form type is specified
 */
public function testInstantiateContactFormThrowsException()
{
    $this->expectException('InvalidArgumentException');
    $this->expectExceptionMessage('Invalid!');
    (new ContactForm('foo'));
}

Don't use assertContains or assertNotContains on strings

// Before
$this->assertContains('needle', 'haystack');
$this->assertNotContains('needle', 'haystack');

// After
$this->assertStringContainsString('needle', 'haystack');
$this->assertStringNotContainsString('needle', 'haystack');

dataProviders can't start with the word 'test' - PHPUnit thinks it's a test

// Before
public function testDataProvider()
{
}

/**
 * @dataProvider testDataProvider
 */
public function testPayload()
{
}

// After
public function myDataProvider()
{
}

/**
 * @dataProvider myDataProvider
 */
public function testPayload()
{
}

assertNull

// Before
$this->assertNull(null, $iAmNull);

// After
$this->assertNull($iAmNull);

Assertions using attribute are removed

https://github.com/sebastianbergmann/phpunit/issues/3338

List of deprecated Assertions

  • assertArraySubset
  • assertInternalType
  • assertAttributeContains
  • assertAttributeNotContains
  • assertAttributeContainsOnly
  • assertAttributeNotContainsOnly
  • assertAttributeCount
  • assertAttributeNotCount
  • assertAttributeEquals
  • assertAttributeNotEquals
  • assertAttributeEmpty
  • assertAttributeNotEmpty
  • assertAttributeGreaterThan
  • assertAttributeGreaterThanOrEqual
  • assertAttributeLessThan
  • assertAttributeLessThanOrEqual
  • assertAttributeSame
  • assertAttributeNotSame
  • assertAttributeInstanceOf
  • assertAttributeNotInstanceOf
  • assertAttributeInternalType
  • assertNotInternalType
  • assertAttributeNotInternalType

Other deprecated methods from TestCase

  • attributeEqualTo
  • readAttribute
  • getStaticAttribute
  • getObjectAttribute