如何在Drupal 8环境中为自定义模块运行PHPUnit测试

时间:2019-03-24 03:57:51

标签: phpunit drupal-8

我正在尝试为Drupal 8创建和运行PHPUnit测试。

我的顶级目录,位于composer.json。

Dockerfile          bootstrap.php           composer.lock           phpunit-examples        phpunit.xml.org         web
Jenkinsfile         checkstyle.xml          config              phpunit.xml         scripts
LICENSE             components          drush               phpunit.xml.dist        sonar-project.properties
README.md           composer.json           patches             phpunit.xml.dist.org        vendor

./ vendor / bin / phpunit --version Sebastian Bergmann和贡献者的PHPUnit 6.5.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/4.1/phpunit.xsd"
         backupGlobals="false"
         colors="true"
         bootstrap="vendor/autoload.php"
         verbose="true"
        >
    <testsuites>
    <testsuite name="unit">
      <file>./tests/TestSuites/UnitTestSuite.php</file>
    </testsuite>
    <testsuite name="kernel">
      <file>./tests/TestSuites/KernelTestSuite.php</file>
    </testsuite>
    <testsuite name="functional">
      <file>./tests/TestSuites/FunctionalTestSuite.php</file>
    </testsuite>
    <testsuite name="functional-javascript">
      <file>./tests/TestSuites/FunctionalJavascriptTestSuite.php</file>
    </testsuite>
  </testsuites>
</phpunit>

phpunit.xml

<phpunit
         bootstrap="bootstrap.php"
         colors="true"
         strict="true"
         verbose="true"
         beStrictAboutTestsThatDoNotTestAnything="true"
         beStrictAboutOutputDuringTests="true"
         beStrictAboutChangesToGlobalState="true"
         checkForUnintentionallyCoveredCode="false">

<testsuites>
  <testsuit name="Simple Example Test Suite">
   <directory>phpunit-examples/tests</directory>
  </testsuit>
</testsuites>
<php>
  <ini name="error_reporting" value="32767"/>
  <ini name="memory_limit" value="-1"/>
  <env name="SIMPLETEST_BASE_URL" value="http://drupal-8.localhost"/>
  <env name="SIMPLETEST_DB" value="mysql://drupal-8:drupal-8@localhost/drupal-8"/>
  <env name="BROWSERTEST_OUTPUT_DIRECTORY" value="/var/www/sites/default/simpletest"/>
  <includePath>phpunit-examples/src/</includePath>
</php>
</phpunit>

要测试的自定义代码: web / modules / custom / benefit / src / BenefitListBuilder.php

测试位于: web / modules / custom / benefit / tests / src / BenefitListBuilderTest.php

<?php declare(strict_types = 1);

namespace Drupal\Tests\benefit;

use Mockery;
use Mockery\MockInterface;
use PHPUnit\Framework\TestCase;
use Drupal\benefit\BenefitListBuilder;

/**
 * Test basic functionality of My Module.
 *
 * @group benefit
 */
class BenefitListBuilderTest extends UnitTestCase
{
    /** @var BenefitListBuilder */
    private $benefitListBuilder;

    protected function setUp()
    {
    $a = "var_a";
    $b = "var_b";
        $this->benefitListBuilder = new BenefitListBuilder($a,$b);
    }

    public function testMissing()
    {
        $this->fail('Test not yet implemented');
    }
}

现在,我尝试运行此测试:

$./vendor/bin/phpunit  web/modules/custom/benefit/tests/src/BenefitListBuilderTest.php
PHP Fatal error:  Class 'Drupal\Tests\benefit\UnitTestCase' not found in /Users/syedahmed/BG-REPOS/PHPUNITTEST-BenefitsAPI/BenefitsAPI/web/modules/custom/benefit/tests/src/BenefitListBuilderTest.php on line 15

Fatal error: Class 'Drupal\Tests\benefit\UnitTestCase' not found in /Users/syedahmed/BG-REPOS/PHPUNITTEST-BenefitsAPI/BenefitsAPI/web/modules/custom/benefit/tests/src/BenefitListBuilderTest.php on line 15

我尝试将测试移至web / modules / custom / benefit / tests / src / Unit / BenefitListBuilderTest.php下,但出现相同的错误。 如何获得测试以识别UnitTestCase的路径?

更新: 我已经在PHPStorm中设置了存储库,所以现在出现错误:

Error : Class 'Drupal\Tests\BenefitListBuilder' not found

1 个答案:

答案 0 :(得分:0)

如我所见,@ kamal,问题在于您正在按以下方式实例化测试:使用PHPUnit \ Framework \ TestCase;但您使用的是Drupal,因此应如下所示:使用Drupal \ Tests \ UnitTestCase;

我希望这会有所帮助。