0

I've managed to write some simple PHPUnit tests, but because the classes within my module have some methods that try to load entities (Content Types built via standard Drupal UI) and then do various thing with them, I am having trouble how to bridge this gap.

Currently when I run my tests most of the assertions pass as they are fairly simply but the one is giving me a warning:

The data provider specified for Drupal\Tests\lrgContentTypes\Unit\AuctionEventTest::testGetNodeFields is invalid. \Drupal::$container is not initialised yet. \Drupal::setContainer() must be called with a real container.

This has only occurred recently because I have now fleshed out the code for the entity class that the parent is trying to pass in the provider.

I am unsure where to go from here, the Drupal 8 documentation seems to be spotty and gives no clear guidance from a basics point of view.

Are there examples that show how to test methods that try to load a node via Node::load() and where entity reference revision objects are expected?

5
  • Drupal 8 docs are not going to tell you a lot about PHPUnit testing as its a PHP thing more than it is a Drupal thing and testing, in general, isn't really a PHP thing more than it is a programming discipline. My only guess here is that the objects here should be mocked and not depend on the entire system, and there is possibly an initialization error in the data provider function that is crashing the test. I think you should also be extending UnitTestCase and not TestCase.
    – Kevin
    Commented Nov 1, 2018 at 13:42
  • In fact these may be more functional tests than they are unit, if you are doing Node loads and other interactions.
    – Kevin
    Commented Nov 1, 2018 at 13:48
  • @Kevin, I have updated everything to use UnitTestCase. Also have started using Prophecy to mock the objects needed instead. I Thought Functional testing was for browser based stuff. I was following the video at: youtube.com/watch?v=nd7DUVNxTxE
    – John Cogan
    Commented Nov 1, 2018 at 15:11
  • Are you not interacting with the database and other services?
    – Kevin
    Commented Nov 1, 2018 at 15:13
  • @Kevin Not in the tests I've written so far, this is where I was stumbling because I had thought that there would be a way to grab data from the test development DB and run tests against that (Thought that was Kernal testing) but been told PHPUnit on its own requires mocking. So will be trying Jaypans recommendation today, see below.
    – John Cogan
    Commented Nov 2, 2018 at 10:25

1 Answer 1

0

If you are doing Unit tests (and not functional tests), you will need to mock the objects you want to use, in the setup of the test. You can read more here: https://www.drupal.org/docs/8/phpunit/mocking-entities-and-services-with-phpunit-and-mocks

If you are using Drupal functions, you should first try to find out if there is an OOP method that can be used to do the same thing - later versions of Drupal have tried to deprecate such functions, and provide OOP alternatives instead. An example is the Time (datetime.time) service, that replaces things like REQUEST_TIME.

If there is no OOP alternative, you can do the following. Imagine you are using db_query() (which, you shouldn't). Instead of using that function direct in your code, you can create a protected function as follows:

protected function dbQuery($query, array $args = [], array $options = []) {
  return db_query($query, $array, $options);
}

Next, for your text, you will create a class that extends the original class. In this test class, you override dbQuery() above:

protected function dbQuery($query, array $args = [], array $options = []) {
  return ['row1' => ['col1' => 'val1'], 'row2' => ['col2' => 'val2']];
}

This mocks a call to db_query by returning an array of values. The last step is to use this new class in your Unit tests, so that when dbQuery() is called, it uses your extended version that returns the array, instead of calling db_query() which is not available to the Unit test.

1
  • thanks I will look into trying your method out tomorrow, when I get back to the tests. Appreciate the time. As a side note (See comment above) I started attempting to use Prophecy to mock the objects.
    – John Cogan
    Commented Nov 1, 2018 at 15:14

Your Answer

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

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