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

  • Tutorial
Hello colleagues,

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


It should be noted right away that the task was to run tests both locally, where development is underway, and remotely on test and project sites. And also, to run tests remotely, ssh access to the server with rights to /usr/bin/phpor wherever you have installed the PHP interpreter on the server is required.

The following is something like a step-by-step instruction:

Install and configure PHPUnit


PHPUnit is installed into the project as dependency through Composer.

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


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

folder : Now, in detail, on the contents of the files.
File/local/tests/phpunit.xml.dist
./

Here we indicate the path to the bootstrap file in the current folder and set the colors parameter to true. We also set the path “test-take” for the current folder (local), so that PHPUnit considers all files ending with * 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;
}

Here a slightly “shortened” file is connected /bitrix/modules/main/include.php, I cut out the verification and launch of agents and authorization from it (for example, without this, the tests would not start with the html code of the authorization form on the box portal where authorization is required).
Below is defined a function initBitrixCore()that explicitly saves the database resource in the property of the global $ DB object (without this, it often disappears with an error in the CDatabase :: ForSql () method ) and also saves the identifier of the super administrator of the system in the session.
The code can be taken from GitHub at this link

Configure PhpStorm for testing


The configuration of the IDE itself is not particularly different from that described in the official documentation . The bottom line is that we need to add the 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 . There, click on "+" and select "By Remote Interpreter". Select the newly added interpreter. In the PHPUnit Library blockselect "Use custom autoloader". In “Path to script” you need to specify the file autoloader.phpthat Composer creates. And in the Test Runner block we indicate the path to the /local/tests/phpunit.xml.dist


next step. 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 setting. Now, when you click on the green “Run” arrow button, a similar area should appear:


Now we recall the requirement to run tests on a test remote site, local and combat. This task, as described in the official documentation article above, is solved by adding the necessary interpreters and PHPUnit settings to them. Then the interpreter is changed in the project settings and the same green arrow “Run” will run the same tests, but on a different server (it’s convenient to watch “didn’t break anything” after the project was transferred).

The fact that running PHPUnit tests over SSH on a remote machine with PhpStorm does not require a special Run / Debug configuration means that we can easily switch where unit tests are run. By switching the project's PHP interpreter through Project Settings | PHP, we can run PHP Unit tests either local or remote without having to reconfigure anything else.


I will be glad to hear comments or recommendations in the comments, since there are few materials on Bitrix unit testing and it is quite possible that there is a more beautiful way. Thank you and good luck in testing!

UPD: Thanks to BusteR27 , who suggested how to bypass authorization correctly. Updated bootstrap.php on GitHub

Also popular now: