19 votes
Accepted

SymfonyTestsListener does not exist on PHP Unit Test

Had a similar issue: Solved it by adding the "symfony/phpunit-bridge": "^3.4.3" library.
Dragu Alexandru's user avatar
13 votes
Accepted

"\Drupal::$container is not initialized yet. \Drupal::setContainer() must be called with a real container."

You need to mock the services you will be using in your test, so that they return values. You can do that as follows: public function testDefaultBehavior() { $some_resolver = $this->...
Jaypan's user avatar
  • 17.5k
10 votes
Accepted

Unit Testing Mock Guzzle Response

Guzzle includes a mock handler that lets you provide a mock HTTP response. Docs are at http://docs.guzzlephp.org/en/latest/testing.html. Here's how you might use it in your unit test: use GuzzleHttp\...
ksenzee's user avatar
  • 613
10 votes

How to create a unit test for a class which uses a trait?

Following on Bedir's answer, here is an example. I have a test that is using the class DurationService. DurationService uses the StringTranslationTrait as follows: class DurationService implements ...
Jaypan's user avatar
  • 17.5k
10 votes
Accepted

What to set up in a Kernel test to get custom content type to be available

For kernel tests you need to specify which configurations you want installed. For instance, you'll need something like this in your test's setUp() method: $this->installConfig(['my_custom_module'])...
jhedstrom's user avatar
  • 5,043
10 votes
Accepted

How to mock a node field value for phpunit?

The following should work using PHPUnit Mock Objects as in your example. First mock FieldItemListInterface __get magic method. $fieldDescMock = $this->getMockBuilder('\Drupal\Core\Field\...
mradcliffe's user avatar
  • 5,917
9 votes

How do I dump variables to the screen when running PHPUnit tests?

Recent versions of PHPUnit swallow output. To get around this, you need to run with the --debug flag, eg $ ../vendor/bin/phpunit --debug modules/migrate/tests/src/Unit/process/FormatDateTest.php ...
mpdonadio's user avatar
  • 38.1k
9 votes
Accepted

how to unit test an http client service with DI in drupal 8?

You are on the right track! Your tests should also never depend on calling out to external services. You should build a list of responses and scenarios you expect from the external service and ensure ...
Sam Becker's user avatar
  • 2,082
9 votes
Accepted

Inject service(s) into Drupal unit test?

If your new service is sufficiently independent (it doesn't have any or many of it own dependencies), then just create it the setUp(). And then if you have dependencies, you set up mocks (also called ...
mpdonadio's user avatar
  • 38.1k
8 votes
Accepted

Why is my unit test not detected?

Unit tests must have the following namespace: Drupal\Tests\dev_module\Unit. For complicated reasons, they also don't show up in the right group but they are all in the PHPUnit group. To run your ...
Berdir's user avatar
  • 83.2k
7 votes

Kernel Test: Programmatically create a vocabulary and add a field to it

You need to enable the field module so its entity types are present. Kernel test base does not enable required modules so the FieldConfig and FieldStorageConfig entity types don't exist until you ...
larowlan's user avatar
  • 1,740
7 votes
Accepted

Getting xdebug to work with BrowserTestBase functional tests?

BrowserTestBase and KernelTestBase use phpunit's 'run in separate process' feature which generates a new temporary php file from a template and then executes that. The output is provided as serialized ...
larowlan's user avatar
  • 1,740
7 votes
Accepted

How to connect database in UnitTestCase subclass?

See https://www.drupal.org/phpunit for better documentation. There are 4 test base classes: UnitTestBase, pure unit tests where everything but your code has to be mocked and only supports classes ...
Berdir's user avatar
  • 83.2k
7 votes
Accepted

How to create a unit test for a class which uses a trait?

StringTranslationTrait also has a setStringTranslation() method so you can inject a string translation mock for exactly that purpose.
Berdir's user avatar
  • 83.2k
7 votes
Accepted

No schema for my_module.global error on unit test

Turns out that it is related to the my_module.schema.yml. Looks like every YAML file under my_module/config/install must have a corresponding schema in the my_module.schema.yml file in order to set up ...
Ynhockey's user avatar
  • 430
7 votes
Accepted

Mocking Settings (settings.php) within Unit tests

At first glance Settings appears to be read-only, however the constructor is quite forgiving in its parameters. We can construct a fake settings structure like so: $site_settings = [ '...
CChiste's user avatar
  • 111
7 votes
Accepted

PHPUnit throws error: \Drupal\Tests\DocumentElement class not found

Finally got to the bottom of this! The package drupal/core-dev is required. composer require --dev drupal/core-dev This installs the package as a development dependency.
naomi's user avatar
  • 1,225
6 votes
Accepted

Writing unit tests for a method that calls static methods from another class

You can't mock static calls directly. A relatively easy way to fix this is what core uses in quite some places to mock functions that haven't been converted to objects/services yet. You create a ...
Berdir's user avatar
  • 83.2k
6 votes

Doing A/B testing

There are a few options for A/B testing a Drupal website, and all options will run the simple test requirements of this question (splitting traffic with specified percentages and keeping visitors in ...
mmowers's user avatar
  • 63
6 votes

How do I apply patch in vendor directory using composer?

You will need cweagans vendor to make composer able to parse the extra->patches lines. This line is wrong I think: "vendor/behat/web-api-extension" You always need the package name (without version) ...
ssibal's user avatar
  • 3,165
6 votes
Accepted

Injecting configuration into mock config object

Your mock of the Config object needs to also have a ::get method and return the appropriate value using your array. The core UnitTestCase class provides ::getConfigFactoryStub(array $configs), which ...
Christoph Burschka's user avatar
6 votes
Accepted

mkdir permission denied error when running Browser based test

Maybe the Permission problems section of Running PHPUnit tests can help you: Note: functional tests have to be invoked with a user in the same group as the web server user. You can either configure ...
Doc Bu's user avatar
  • 76
6 votes

Missing Schema on custom field only during PHPUnit Test

You only get that error during testing because the core testing base classes add a config schema checker during testing. You have 2 options, the first is the easiest, but is not recommended, and that ...
jhedstrom's user avatar
  • 5,043
5 votes

Is it possible to write test against the current database?

The testing environment is by default completely separated from your website, so module testing is in no way affected by the environment. However you might be able to switch the database. There is ...
Neograph734's user avatar
  • 8,419
5 votes

Kernel Test: Programmatically create a vocabulary and add a field to it

You should try FieldKernelTestBase instead of just KernelTestBase. There is a helper method called createFieldWithStorage who will do the job.
Rafael Caceres's user avatar
5 votes
Accepted

Why am i getting PDO exception while running the following test from console?

You can't create and save entities in a unit test. That's what kernel tests are for. You will also need to install the necessary entity schema then, make sure the necessary modules are enabled. One ...
Berdir's user avatar
  • 83.2k
5 votes
Accepted

How to visit a specific node using behat

You can do two things in this scenario: You can instruct it to click on "Accomodation Foo" (which should be the linked node title) on this page, to go to the node when the path is unknown. If you ...
Kevin's user avatar
  • 27.8k
5 votes

Is there any way to connect to a second (MySQL) database in a Kernel Base test?

I have figured it out. The setUp() method in my Kernel Base Test must look like that: /** * {@inheritdoc} */ protected function setUp() { parent::setUp(); $databases['second-database']['...
jepster's user avatar
  • 953
5 votes

Can I test code contained in module files?

Unit-testing procedural code In principle, you can unit test procedural code just as easily as code that is in a class, but you can't really mock it. Therefore, whether your tested code is in a class ...
Christoph Burschka's user avatar
5 votes

PHPUnitTest is missing the file_managed table?

Ugh... Forgot to install the entitySchema. This is how to do it in the setup method /** * {@inheritdoc} */ protected function setUp() { parent::setUp(); $this->installConfig('...
Justin Levi Winter's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible