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

时间:2018-05-15 16:00:57

标签: magento phpunit magento2

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

  

致命错误:声明   Magento的\框架\ TestFramework \单位\监听\ ReplaceObjectManager :: startTest(PHPUnit的\框架\测试   $ test)必须兼容   PHPUnit_Framework_TestListener :: startTest(PHPUnit_Framework_Test的   $ test)in   /Applications/MAMP/htdocs/mag221/vendor/magento/framework/TestFramework/Unit/Listener/ReplaceObjectManager.php   第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。

知道可能导致这种情况的原因吗?

1 个答案:

答案 0 :(得分:1)

首先创建HelloMessage类\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!';
    }
}

创建UnitTest模型\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中添加UnitTest
<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