5

Using DrupalUnitTestCase to unit test a Drupal module, fails. I probably forget something.

The test runs fine untill I create an instance of some class:

$foo = new FooBar();

In that case, Drupal decides to do some magic and attempts to call the database, in order to find some file in its registry.

 Test PDOStatement->execute() failed: <em class="placeholder">PDOException</em>: SQLSTATE[42S02]: Base table  [error]
or view not found: 1146 Table &#039;td_development.simpletest50921registry&#039; doesn&#039;t exist: SELECT
filename FROM {registry} WHERE name = :name AND type = :type; Array
(
    [:name] =&gt; FooBar
    [:type] =&gt; interface
)

DrupalUnitTestCase, as opposed to DrupalWebTestCase do not set up a database, by design. So the reason why this fails is clear.

However, I don't want Drupal to go looking in a database when all I want is to create some instance. How to avoid Drupal looking up the file in its registry?

1
  • To those lost souls like me - this use case/error applies (sadly) to ENTITIES and ENTITY CONTROLLERS as well. Tagging for the benefit of the interwebz. Also added relevant information to the accepted (@Berdir) answer. Dec 11, 2013 at 1:48

2 Answers 2

5

You probably can't.

The possibilities of using UnitTestCase as the parent class are very limited. As soon as you do anything that requires the database (and creating a new class does because the autoload features of Drupal 7 depend on the database), you have to use WebTestCase.

The only thing that might work is explicitly including all files that are required for that class to work. Because the autoload is only called if the class does not exist yet (could also be a class that your class uses or depends on). But that is relatively fragile and you will always have to include all these files manually in the correct order, which means that your unit tests depend on the inner workings of your class. Which isn't nice either.

1
  • When including manually on setUp(), Drupal indeed no longer needs a database to create an intance. However, it fails a little further down with a DatabaseConnectionNotDefinedException. I guess it simply boils down to "don't use unittests, use webtests instead". Thanks.
    – berkes
    May 18, 2011 at 15:25
0

You can also try this

spl_autoload_register('your_function');
if (function_exists('drupal_autoload_class')) {
  spl_autoload_unregister('drupal_autoload_class');
  spl_autoload_register('drupal_autoload_class');
  spl_autoload_unregister('drupal_autoload_interface');
  spl_autoload_register('drupal_autoload_interface');
}

This will move the Drupal autoload to the bottom and solve the problem.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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