Setting up unit testing with magento2 docker

This post is being edited and refined. It's not complete yet. Changes may occur in the next few days, as I test this out and refine it, and improve the formatting of this post.

If you followed my tutorial to set up your docker container, and now you wish to run Magento2 unit tests, you might notice that running PHPUnit gives an error that it cannot connect to the amqp server, database server, or elasticsearch server.

To resolve these issues, and get magento phpunit up and running follow these steps, assuming you have followed my tutorial.

Open up docker-compose.yml and add the following lines:

rabbitmq:
  image: rabbitmq
  networks:
    - <project_name>-network
  ports: 
   - 5672:5672

Then run from your console 

docker-compose up

Now enter the docker database CLI and run the following commands:

mysql -u root -p

Use as password at the password prompt: root

In the database prompt, run the following commands

CREATE DATABASE magento_integration_tests;
GRANT ALL ON magento_integration_tests.* TO 'magento2_test_user'@'%' IDENTIFIED BY 'test_me_now';

Now go to your docker/src folder on your local disk where your Magento installation is present.

Open the file docker/src/dev/tests/integration/etc/install-config-mysql.php.dist

Change the contents to 

<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

return [
  'db-host' => 'db',
  'db-user' => 'magento2_test_user',
  'db-password' => 'test_me_now',
  'db-name' => 'magento_integration_tests',
  'db-prefix' => '',
  'backend-frontname' => 'backend',
  'search-engine' => 'elasticsearch7',
  'elasticsearch-host' => 'elasticsearch',
  'elasticsearch-port' => 9200,
  'admin-user' => \Magento\TestFramework\Bootstrap::ADMIN_NAME,
  'admin-password' => \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD,
  'admin-email' => \Magento\TestFramework\Bootstrap::ADMIN_EMAIL,
  'admin-firstname' => \Magento\TestFramework\Bootstrap::ADMIN_FIRSTNAME,
  'admin-lastname' => \Magento\TestFramework\Bootstrap::ADMIN_LASTNAME,
  'amqp-host' => 'rabbitmq',
  'amqp-port' => '5672',
  'amqp-user' => 'guest',
  'amqp-password' => 'guest',
];

What is changed is that the server names, that were all localhost here, are changed to their respective entries in docker-compose.yml. So the database server is db, elasticsearch is elasticsearch, amqp is rabbitmq. Also, the database user and password are changed

Save the file, and open up a CLI to your server.

Run the following command to start up the testing framework

vendor/bin/phpunit -c dev/tests/integration/phpunit.xml.dist

And then the full and complete test suite will fire up, which will most likely run out of the 2GB of allocated memory.

I am only interested in running tests in my custom test suites, and not in the 12486 tests Magento has for its own software, so this is not a great problem for me to run into.



Comments

Popular Posts