尝试 运行 PHPUnit Magento 2 测试时出错

Error trying to run PHPUnit Magento 2 test

我创建了一个简单的 hello world Magento 2 测试,当我尝试 运行 测试时出现以下错误:

Fatal error: Declaration of Magento\Framework\TestFramework\Unit\Listener\ReplaceObjectManager::startTest(PHPUnit\Framework\Test $test) must be compatible with PHPUnit_Framework_TestListener::startTest(PHPUnit_Framework_Test $test) in /Applications/MAMP/htdocs/mag221/vendor/magento/framework/TestFramework/Unit/Listener/ReplaceObjectManager.php on line 14

这是phpunit.xml.dist

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.2/phpunit.xsd" colors="true" beStrictAboutTestsThatDoNotTestAnything="false" bootstrap="./framework/bootstrap.php">
   <testsuite name="Magento Unit Tests">
      <directory suffix="Test.php">../../../TestStore/Checkout/HelloMessage/Test/Unit</directory>
   </testsuite>
   <php>
      <ini name="date.timezone" value="America/Los_Angeles" />
      <ini name="xdebug.max_nesting_level" value="200" />
      <ini name="memory_limit" value="-1" />
   </php>
   <filter>
      <whitelist addUncoveredFilesFromWhiteList="true">
         <directory suffix=".php">../../../app/code/*</directory>
         <directory suffix=".php">../../../lib/internal/Magento</directory>
         <directory suffix=".php">../../../setup/src/*</directory>
         <exclude>
            <directory>../../../app/code/*/*/Test</directory>
            <directory>../../../lib/internal/*/*/Test</directory>
            <directory>../../../lib/internal/*/*/*/Test</directory>
            <directory>../../../setup/src/*/*/Test</directory>
         </exclude>
      </whitelist>
   </filter>
   <listeners>
      <listener class="Magento\Framework\TestFramework\Unit\Listener\ReplaceObjectManager" />
   </listeners>
   <logging />
</phpunit>

HelloMessageTest.php

<?php
namespace TestStore\HelloMessage;
use TestStore\HelloMessage;



class HelloMessageTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @var HelloMessage
     */
    protected $helloMessage;

    public function setUp()
    {
        $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
        $this->helloMessage = $objectManager->getObject('Magestore\HelloMagento\Model\HelloMessage');
        $this->expectedMessage = 'Hello Magento 2! We will change the world!';
    }

    public function testGetMessage()
    {
         $this->assertEquals($this->expectedMessage, $this->helloMessage->getMessage());
    }
}

我正在使用 PHP 7.1.12,Magento 2.2.1。

知道是什么原因造成的吗?

首先创建HelloMessageclass\app\code\TestStore\Hello\Model\HelloMessage.php

<?php
namespace TestStore\Hello\Model;

class HelloMessage
{
    public function getMessage()
    {
        return 'Hello Magento 2! We will change the world!';
    }
}

创建单元测试模型\app\code\TestStore\Hello\Test\Unit\Model\HelloMessageTest.php

<?php
namespace TestStore\Hello\Test\Unit\Model;

use TestStore\Hello\Model\HelloMessage;

class HelloMessageTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @var HelloMessage
     */
    protected $helloMessage;

    public function setUp()
    {
        $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
        $this->helloMessage = $objectManager->getObject('TestStore\Hello\Model\HelloMessage');
        $this->expectedMessage = 'Hello Magento 2! We will change the world!';
    }

    public function testGetMessage()
    {
         $this->assertEquals($this->expectedMessage, $this->helloMessage->getMessage());
    }
}

现在,您必须通过

phpunit.xml中添加单元测试
<testsuite name="Magento Unit Tests">
    <directory suffix="Test.php">../../../app/code/TestStore/Hello/Test/Unit</directory>
</testsuite>

然后只需 运行 从控制台在 magento 文件夹下进行单元测试

作为参考,您还可以查看 How to write Unit Test in Magento 2