1

I installed the conditional_fields module:

the tree is the following:

root project
  - phpunit.xml
  - web
    - modules
      - conditional_fields
        - tests
          - src
            - FunctionalJavascript
            - Unit
              - ConditionalFieldControllerTest.php

and this is the declaration of the php class I want to test:

<?php

namespace Drupal\Tests\conditional_fields\Unit;

use Drupal\Tests\UnitTestCase;

use Drupal\conditional_fields\Controller\ConditionalFieldController;

    /**
     * ConditionalFieldController units tests.
     *
     * In unit testing, there should be as few dependencies as possible.
     * We want the smallest number of moving parts to be interacting in
     * our test, or we won't be sure where the errors are, or whether our
     * tests passed by accident.
     *
     * @ingroup conditional_fields
     *
     * @group conditional_fields
     */
    class ConditionalFieldControllerTest extends UnitTestCase {
     ....
    }

In the phpunit.xml file I wrote this:

<?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="drupal-composer-project tests">
            <directory>web/modules/conditional_fields/tests/src/Unit</directory>
            <file>ConditionalFieldControllerTest</file>
        </testsuite>
    </testsuites>
</phpunit>

but I get this error:

Fatal error: Class 'Drupal\Tests\UnitTestCase' not found in C:\xampp\htdocs\drupaltest2\web\modules\conditional_fields\tests\src\Unit\ConditionalFieldControllerTest.php on line 21
  • I think you need to use the bootstrap file from phpunit.xml.dist at Drupal root folder not the vendor autoload – Ismail Cherri Sep 21 '18 at 17:26
  • I changed the path of the autoload file inside phpunuit.xml and I set it to web / autoload.php but the error remains – Alessio Frabotta Sep 22 '18 at 13:40
  • For accuracy web / autoload.php points to the autoload file inside the vendor folder, so it does not change anything – Alessio Frabotta Sep 22 '18 at 14:15
2

You must point in the phpunit.xml file to drupal's bootstrap.php file. See a working phpunit.xml file example for Drupal 8 here: http://cgit.drupalcode.org/permissions_by_term/tree/tests/phpunit.xml.dist

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.