1
votes

I am trying to create and run PHPUnit tests for Drupal 8. Here are the details:

My top-level directory, where composer.json is located.

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 PHPUnit 6.5.14 by Sebastian Bergmann and contributors.

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>

custom code to be tested: web/modules/custom/benefit/src/BenefitListBuilder.php

test located at: 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');
    }
}

Now, i try to run just this test:

$./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

i tried moving the test under web/modules/custom/benefit/tests/src/Unit/BenefitListBuilderTest.php , but got same error. How do i get the test to recognize the Path for UnitTestCase?

Update: I have setup the repository in PHPStorm, so now i am getting error:

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

1 Answers

1
votes

As I see it, @kamal, the problem is you're instantiating the tests as this: use PHPUnit\Framework\TestCase; but you're using Drupal, so it should be as follows: use Drupal\Tests\UnitTestCase;

I hope this helps.