0

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

  • 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 Nov 1 '18 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 Nov 1 '18 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 Nov 1 '18 at 15:11
  • Are you not interacting with the database and other services? – Kevin Nov 1 '18 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 Nov 2 '18 at 10:25
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.

  • 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 Nov 1 '18 at 15:14

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.