Skip to content

PHP 7 Syntax usage for Magento Contribution

Igor Miniailo edited this page Aug 19, 2017 · 11 revisions

At the beginning of this week, we've merged a pull request upgrading PHPUnit version to 6.2 https://github.com/magento/magento2/blob/develop/composer.json#L77

Along with that we've updated all the Unit Tests in our code base for Magento Open Source (Community Edition)

magento2 iminiailo$ find ./app/code/Magento/ -type f -name "*Test.php" | wc -l
    3069

and Magento Commerce (Enterprise Edition)

magento2ee iminiailo$ find ./app/code/Magento/ -type f -name "*Test.php" | wc -l
    1176

As you probably know PHPUnit 6 is Backward Incompatible release to 4.1 which was previously used by Magento 2. The list of most significant changes could be found here.

Main changes we were fixing in Magento 2 Unit test suit were:

  • PHPUnit_Framework_TestCase is now PHPUnit\Framework\TestCase PHPUnit's units of code are now namespaced. Among other things, this means that your test case classes now need to extend PHPUnit\Framework\TestCase instead of PHPUnit_Framework_TestCase. Thus, parent class for all Unit tests should be changed.
  • Deprecated APIs Removed Methods such as getMock(), getMockWithoutInvokingTheOriginalConstructor(), setExpectedException(), setExpectedExceptionRegExp(), or hasPerformedExpectationsOnOutput() that were deprecated before have now been removed from PHPUnit 6. createMock(), createConfiguredMock(), createPartialMock(), createTestProxy(), and getMockBuilder() should be used instead of getMock() and getMockWithoutInvokingTheOriginalConstructor(). And expectException() should be used instead of setExpectedException() (or the @expectException annotation).

Similar changes should be done by extension developers who cover their code with Unit Tests and relied on PHPUnit 4.1 APIs.

This Upgrade was the last step on a way to PHP 7 syntax support.

  1. Return types declaration
  2. Scalar type hinting

From this time Community Engineering team starts to accept PRs using PHP 7 syntax.

But because Magento 2.2.0 supports PHP 7 from version 7.0.* we don't support usage of:

  • Nullable types (which could be handy for our Data interfaces)
function testReturn(): ?string
{
    return null;
}

function test(?string $name)
{
    var_dump($name);
}
  • Void return type (which could be handy for our Commands, as they follow CQRS and return Void)
function testReturn(): void
{
    return;
}

As these features were added in PHP 7.1 release.

MSI Documentation:

  1. Technical Vision. Catalog Inventory
  2. Installation Guide
  3. List of Inventory APIs and their legacy analogs
  4. MSI Roadmap
  5. Known Issues in Order Lifecycle
  6. MSI User Guide
  7. DevDocs Documentation
  8. User Stories
  9. User Scenarios:
  10. Technical Designs:
  11. Admin UI
  12. MFTF Extension Tests
  13. Weekly MSI Demos
  14. Tutorials
Clone this wiki locally