INTERNAL
composer require --dev proklung/bitrix-phpunit-testing-tools
A package that uses this package as a dependency must have a section in composer.json (the migrations module needs to be installed where it needs to be):
"extra" : {
"installer-paths" : {
"vendor/sheerockoff/bitrix-ci/files/bitrix/modules/{$name}/" : [ " type:bitrix-module " ]
}
}
The base class for tests is BitrixableTestCase
. Launches Bitrix attached to the package and allows you to use its API in tests.
Database access parameters are defined in the setupDatabaseData
method of the BitrixableTestCase
base class.
If the database does not exist at the time of launch, it will be created.
Default:
protected function setupDatabaseData () : void
{
putenv ( ' MYSQL_HOST=localhost ' );
putenv ( ' MYSQL_DATABASE=bitrix_ci ' );
putenv ( ' MYSQL_USER=root ' );
putenv ( ' MYSQL_PASSWORD= ' );
}
Can be overridden in each specific test.
Instructions to reset the database before each test and load a new one.
Reset the database and load a custom database dump.
The path to the dump is specified in the getDumpPath
method of the test:
protected function getDumpPath () : string
{
return $ _SERVER [ ' DOCUMENT_ROOT ' ] . ' /Tests/dump/dump.sql ' ;
}
Only effective in combination with ResetDatabaseTrait
.
When using battle dumps, problems with license expiration usually arise. In this case, you have to manually replace the include.php
file in vendor/sheerockoff/bitrix-ci/files/bitrix/modules/main
.
Using sprint.option module migrations.
The path to the directory with migrations is specified in the getPathSprintMigrations
method of the test:
protected function getPathSprintMigrations () : string
{
return __DIR__ . ' ../../../../../../Tests/sprint_migrations/ ' ;
}
Due to the peculiarities of using the Bitrix CI assembly, you have to install the module cunningly - directly inside the assembly package (in the vendor/sheerockoff/bitrix-ci/files/bitrix/modules/sprint.option
folder) at the composer stage. A side effect is that if the assembly itself is updated, the migration module will go down the drain.
So far so.
Periodically, the database expires due to its expiration date (“... the trial version has expired...”). To fix it, you need to run any test with the ResetDatabaseTrait
trait, which recreates the database again.
Instruct to run migrations before each test.
Under the hood is a stripped-down version of the package, so migrations from it are also suitable. With one exception - migration does not inherit from the ArrilotBitrixMigrationsBaseMigrationsBitrixMigration
class, but from ArrilotBitrixMigrationsForkBaseMigrationsBitrixMigration
.
The path to the directory with migrations is specified in the getMigrationsDir
method of the test:
protected function getMigrationsDir () : string
{
return __DIR__ . ' /../migrations ' ;
}
The trait comes with a makeMigration
helper method for creating template migrations.
protected function makeMigration( string $ name , string $ template ) : void
Available templates:
Name | Description | Aliases |
---|---|---|
`default` | Clean default template | |
`add_iblock_type` | Adding an infoblock type | |
`add_iblock` | Adding an information block | |
`add_iblock_element_property` | Adding a property to an infoblock | `add_iblock_prop`, `add_iblock_element_prop`, `add_element_prop`, `add_element_property` |
`add_uf` | Adding UF properties | |
`query` | Arbitrary request to the database via the d7 API | |
`add_table` | Creating a table via the d7 API | `create_table` |
`delete_table` | Deleting a table via API d7 | `drop_table` |
Additional trait - CSVTrait
for importing CSV files (Bitrix export format) in migrations.
Once enabled, the test must implement the following methods:
getIblockCode()
- infoblock code;getImportDefinitionSections()
- definition of subsections. An array with the number of columns in the CSV file (IC_GROUP0, etc.);getImportDefinitionProperties()
- definition of properties. Array of the form [property code => column number in CSV];getCsvPath()
- path to the CSV file;Important! - the CSV file should not have the first line with column names.
From the package. Redesigned for private needs.
To help test component code, use the ProklBitrixTestingToolsInvokersComponentInvoker
class
Class methods:
__constructor($componentObject)
- initialization of the component launch object;init()
- initialization;setParams($params)
- sets parameters for launching the component under test;setArParams($params)
- sets arParams to run the component under test;setName(string $name)
- sets the name of the component("test.component");setTemplate($template)
- sets the component template("test.component");execute()
- launches the component for execution (the template is not used);getResultValue($name)
- returns the $arResult parameter by key $name;getArResult()
- returns the complete $arResult of the component's operation;getArResultCached()
- returns the cached part (via $this->__component) of $arResult after the component has run;getExecuteResult()
- returns the result of the component's operation when the return
operator is used in the component code.Example:
// ...
/**
* @label component
* @test
*/
public function useComponentInvoker () {
/** @var CBitrixComponent $componentObject */
$ component = new Prokl BitrixTestingTools Invokers ComponentInvoker ( $ componentObject );
$ component -> init ();
$ component -> setParams ( array ( " id " => 10 ));
$ component -> execute ();
$ this -> getAssert ()-> equal ( $ component -> getResultValue ( " id " ), 10 , " Результат не верен " );
}
You can test the result_modifier of a component template using an object of the ProklBitrixTestingToolsInvokersResultModifierInvoker
class.
Methods:
__construct($componentName, $template)
- object initialization, parameters are the same as the parameters of the CMain::IncludeComponent()
method;setArResult($arResult)
- artificially setting the result to be sent to the adapter;setArParams($params)
- sets arParams to run the component under test;execute()
- launch the adapter for execution;getArResult()
- returns the full $arResult
of the adapter's operation;getArResultCached()
- returns the cached part (via $this->__component) of $arResult after the component has run;getArResultValue($name)
- the value of the result of the adapter using the $name
key;Example:
/**
* @label component
* @test
*/
public function modifierForSomeTemplate () {
$ rm = new Prokl BitrixTestingTools Invokers ResultModifierInvoker ( " project:test.with.class " , " list " );
$ rm -> setArResult ( array ( " id " => 10 ));
$ rm -> execute ();
$ this -> getAssert ()-> equal ( $ rm -> getArResultValue ( " id " ), 10 , " Параметры не равны " );
}
ProklBitrixTestingToolsInvokersEventInvoker
class makes it easier to test event processing.
Methods:
__construct($module, $eventName)
- initialization of the event trigger object, $module - name of the event emission module, $eventName - event name;setExecuteParams($params)
- setting the event parameters in the form of an array, which will be passed to the event parameters;execute()
- event release;countOfHandlers()
- getting the number of event handlers;getEvent()
- getting an event object;Example:
// ...
/**
* @test
*/
public function handlersOfEventExist () {
$ eventInvoker = new Prokl BitrixTestingTools Invokers EventInvoker ( " main " , " OnPageStart " );
$ eventInvoker -> setExecuteParams ( array (
" IBLOCK_ID " => 12
));
$ eventInvoker -> execute ();
$ this -> getAssert ()-> asTrue ( $ eventInvoker -> countOfHandlers () > 1 );
}
goTo
method of the BitrixableTestCase
class. Emulates being on a URL. Exposes everything related to the URL in the old core and D7.It also automatically replaces everything that is possible from super-globals like $_SERVER, $_POST, etc.
$ _GET [ ' test ' ] = ' OK ' ;
$ this -> goTo ( ' /test/ ' );
$ url = $ APPLICATION -> GetCurPage (); // $url = '/test/index.php'
$ request = Application:: getInstance ()-> getContext ()-> getRequest ();
$ uriString = $ request -> getRequestUri (); // $uriString = '/test/'
$ testGetParam = $ request -> getQuery ( ' test ' ); // $testGetParam = 'OK'