2

Trying to port a Drupal 7 module to 9 with accompanying test and getting this error immediately when running it:

PHP Fatal error:  Type of Drupal\KernelTests\Core\Entity\EntityKernelTestBase::$modules must be array (as in class Drupal\KernelTests\KernelTestBase) in /var/www/html/docroot/core/tests/Drupal/KernelTests/Core/Entity/EntityKernelTestBase.php on line 12

The test:

class MyModuleTest extends EntityKernelTestBase {

  use NodeCreationTrait;

  /**
   * @var \Drupal\mymodule\Query\YearsQuery
   */
  protected $query;

  /**
   * @var array $modules
   */
  protected static $modules = [
    'node',
    'mymodule',
    'mymodule_test',
  ];

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    ...

I checked around some of the core classes - the $modules declaration looks the same. Note that I am using PHPUnit 9.

21
  • It's probably the access modifier - all core tests use public rather than protected
    – Clive
    Mar 2 at 19:29
  • I get it with either one. I saw core tests use protected as well, for example MessageEntityTest.
    – Kevin
    Mar 2 at 19:31
  • Oh yeah I see it, interestingly none of those declare the array type. Does public static $modules work? The class you're inheriting from declares $modules as public, maybe the trouble is being caused by the change to the visibility and/or type
    – Clive
    Mar 2 at 19:33
  • Yep, tried all those combinations.
    – Kevin
    Mar 2 at 19:36
  • 1
    @anonymous I was looking at the wrong codebase (D8), where it's public - I definitely wasn't suggesting it should be public, they were just debug suggestions to see if the modifier was causing it :) But as you obviously feel strongly about it I'll take that on board, cheers for the tip!
    – Clive
    Mar 2 at 20:17
0

From KernelTestBase:

 /**
  * Modules to enable.
  *
  * The test runner will merge the $modules lists from this class, the class
  * it extends, and so on up the class hierarchy. It is not necessary to
  * include modules in your list that a parent class has already declared.
  *
  * @see \Drupal\Tests\KernelTestBase::enableModules()
  * @see \Drupal\Tests\KernelTestBase::bootKernel()
  *
  * @var array
  */
 protected static $modules = [];

You'll need to match this declaration in your own code (though you can add to the array).

2
  • That is what I have, and is not working. It errors immediately.
    – Kevin
    Mar 2 at 19:48
  • 1
    That error cannot match the code you've shown, as the error says modules is not an array, and you have defined it as an array. This means your problem lies elsewhere, such as debugging the wrong file, debugging the right file but on the wrong environment, having something clogged in a cache somewhere etc.
    – Jaypan
    Mar 2 at 21:34
0

Looks like my problem stemmed entirely from using the wrong KernelTestBase include. My IDE suggested KernelTestBase as I was typing, and I did not look at the path:

use Drupal\Tests\token\Kernel\KernelTestBase;

vs

use Drupal\KernelTests\KernelTestBase;

send coffee.

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.