PHPUnit Testing when a custom class contains drupal calls and references other classes Ok, I am brand new to TDD within the Drupal environment but we have been asked to try write tests where possible for our custom and/or utility/support modules.

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:

1) 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.

Is there anywhere that gives a definitive set of examples to show how to test methods that try to load a node via Node::load and where Entity Reference Revision objects are expected?

I will post my classes and test classes below in case its relevant. 

1) AuctionEventTest class

https://pastebin.com/vkaiPfUD

2) AuctionLotTest class

https://pastebin.com/ptUmF8UJ

3) AuctionEvent Class (Auction Event content type utility class)

https://pastebin.com/NCDy34z2

4) AuctionLot Class (Auction Lot Entity Reference Revision util class)

https://pastebin.com/mmZ0dh1i
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' =&gt; ['col1' =&gt; 'val1'], 'row2' =&gt; ['col2' =&gt; '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.