📜 ⬆️ ⬇️

We start PHPUnit tests for the project on 1C-Bitrix

Hello colleagues,

In this post I want to show my way of running unit tests on Bitrix projects. When unit testing such projects, there is a known problem of initializing the system kernel. Below I will show how I managed to solve it.


Immediately it is worth noting that the task was to run tests both locally, where development is underway, and remotely on test prod and prod sites of projects. And also, to run tests remotely, you must have ssh access to the server with permissions to /usr/bin/php or where you have a PHP interpreter installed on the server.
')
The following is a kind of step-by-step instruction:

Install and configure PHPUnit


PHPUnit is installed in the project as dependency through Composer.

File / /local/composer/composer.json
 { "require-dev": { "phpunit/phpunit": "4.5.*" } } 


Next, in the daddy / local / tests / the PHPUnit xml configuration file, the bootstrap and the Bitrix kernel initialization file are added:
image

Now detailed on the contents of the files.
File / /local/tests/phpunit.xml.dist
 <phpunit bootstrap="bootstrap.php" colors="true"> <testsuites> <testsuite name="My Test Suit"> <directory>./</directory> </testsuite> </testsuites> </phpunit> 

Here we specify the path to the bootstrap file in the current folder and set the colors parameter to true . We also set the path “test shake” for the current folder (local), so that PHPUnit will count all files with the ending * Test.php as files with test classes.

File /local/tests/bootstrap.php
 // bitrix/modules/main/include.php with no authorizing and Agents execution require_once "main_include_no_permission.php"; function initBitrixCore() { // manual saving of DB resource global $DB; $app = \Bitrix\Main\Application::getInstance(); $con = $app->getConnection(); $DB->db_Conn = $con->getResource(); // "authorizing" as admin $_SESSION["SESS_AUTH"]["USER_ID"] = 1; } 

A slightly “shortened” file /bitrix/modules/main/include.php , I cut out checking and launching agents and authorization from it (for example, without this, the start of tests disappeared with the html-code of the authorization form on the boxed portal, where authorization is required).
Below, the initBitrixCore() function is initBitrixCore() , which explicitly saves the database resource to the global object property $ DB (without this, it often disappears with an error in the CDatabase :: ForSql () method), and also saves the super administrator ID to the session.
The code can be picked up from GitHub via this link.

Setting up phpstorm for testing


Configuring the IDE itself is not much different from that described in the official documentation . The bottom line is that we need to add a remote PHP interpreter on the remote server, configure PHPUnit in the IDE settings, and add the PHPUnit configuration to Run \ Debug Configuration.

Add a remote interpreter:
Settings - Languages ​​& Frameworks - PHP


Add a remote interpreter


In the settings that appeared, I selected “SSH Credentials” and entered access to the server with the rights described at the beginning of the post.

Next, go to Settings - Languages ​​& Frameworks - PHP - PHPUnit . They click on the "+" and choose "By Remote Interpreter". Select the newly added interpreter. In the PHPUnit Library block, select “Use custom autoloader”. In "Path to script" you need to specify the file autoloader.php , which creates Composer. And in the Test Runner block, specify the path to /local/tests/phpunit.xml.dist


The next step is to create the PHPUnit configuration in:

In the window that appears it is important to add the usual PHPUnit and not PHPUnit on Server. In its settings in the Test Runner block you need to select "Defined in the configuration file"

This is actually the whole setup. Now, when you click on the green “Run” arrow button, a similar area should appear:


Now we remember about the requirement to run tests on a remote test site, local and military. This task, as described in the official documentation article above, is solved by adding the necessary interpreters and PHPUnit settings to them. Then, in the project settings, the interpreter changes and the same green arrow “Run” runs the same tests, but on a different server (it’s convenient to watch “it’s not broken” after project transfer).

If you’re running, you’ll be able to switch to a new one. By switching the project's PHP interpreter through Project Settings | PHP, we can run the PHPUnit tests to test anything else else.


I would be happy to hear comments or recommendations in the comments, since there is little material on the unit testing of Beatrix and it is quite possible that there is a more beautiful way. Thanks and good luck with your testing!

UPD: Thanks to BusteR27 , who suggested how to get around authorization correctly. The bootstrap.php file has been updated on GitHub

Source: https://habr.com/ru/post/253561/


All Articles