Skip to content

Commit

Permalink
CustomDumpTrait. Позволяет задать собственный дамп БД для тестов.
Browse files Browse the repository at this point in the history
  • Loading branch information
ProklUng committed Apr 24, 2021
1 parent df03249 commit 862bcbc
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 10 deletions.
53 changes: 43 additions & 10 deletions src/Base/BitrixableTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Prokl\BitrixTestingTools\Base;

use Prokl\BitrixTestingTools\Migrator;
use Prokl\BitrixTestingTools\Helpers\ClassUtils;
use Prokl\BitrixTestingTools\Helpers\Database;
use Prokl\BitrixTestingTools\Traits\CustomDumpTrait;
use Prokl\BitrixTestingTools\Traits\ResetDatabaseTrait;
use Prokl\TestingTools\Base\BaseTestCase;
use Sheerockoff\BitrixCi\Bootstrap;
Expand Down Expand Up @@ -33,12 +35,12 @@ protected function setUp(): void
if ($this->dropBase) {
$dbManager->dropBase();
$dbManager->createDatabaseIfNotExist();
Bootstrap::migrate();
$this->migrateDatabase();
} else {
$dbManager->createDatabaseIfNotExist();

if ($dbManager->hasEmptyBase()) {
Bootstrap::migrate();
$this->migrateDatabase();
}
}

Expand All @@ -52,14 +54,7 @@ protected function tearDown(): void
{
parent::tearDown();

// Битриксовые штучки-дрючки с буфером.
while (ob_get_level()) {
ob_end_clean();
}

if ($GLOBALS['APPLICATION']) {
$GLOBALS['APPLICATION']->RestartBuffer();
}
$this->clearBitrixBuffer();

if ($this->dropBase) {
$dbManager = $this->getDbManager();
Expand All @@ -80,6 +75,32 @@ protected function setupDatabaseData() : void
putenv('MYSQL_PASSWORD=');
}

/**
* Битриксовые штучки-дрючки с буфером.
*
* @return void
*/
private function clearBitrixBuffer() : void
{
while (ob_get_level()) {
ob_end_clean();
}

if ($GLOBALS['APPLICATION']) {
$GLOBALS['APPLICATION']->RestartBuffer();
}
}

/**
* Загрузить дамп - кастомный или нативный.
*
* @return void
*/
private function migrateDatabase() : void
{
$this->useCustomDump() ? Migrator::migrate($this->getDumpPath()) : Bootstrap::migrate();
}

/**
* Экземпляр менеджера БД.
*
Expand All @@ -106,4 +127,16 @@ private function needDropBase() : bool

return in_array(ResetDatabaseTrait::class, $traits, true);
}

/**
* Использовать ли кастомный дамп. Признак - трэйт CustomDumpTrait.
*
* @return boolean
*/
private function useCustomDump() : bool
{
$traits = ClassUtils::class_uses_recursive($this);

return in_array(CustomDumpTrait::class, $traits, true);
}
}
42 changes: 42 additions & 0 deletions src/Migrator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Prokl\BitrixTestingTools;

use InvalidArgumentException;
use Sheerockoff\BitrixCi\SqlDump;

/**
* Class Migrator
* Декоратор к мигратору дампов (позволяет загружать свои).
* @package Prokl\BitrixTestingTools
*
* @since 24.04.2021
*/
class Migrator
{
/**
* @param string $pathDump Путь к дампу базы.
*
* @return void
*/
public static function migrate(string $pathDump) : void
{
$db = mysqli_connect(
getenv('MYSQL_HOST', true) ?: getenv('MYSQL_HOST'),
getenv('MYSQL_USER', true) ?: getenv('MYSQL_USER'),
getenv('MYSQL_PASSWORD', true) ?: getenv('MYSQL_PASSWORD'),
getenv('MYSQL_DATABASE', true) ?: getenv('MYSQL_DATABASE')
);

if (!$db) {
throw new InvalidArgumentException('Mysql connection error.');
}

$sqlDump = new SqlDump($pathDump);
foreach ($sqlDump->parse() as $query) {
mysqli_query($db, $query);
}

mysqli_close($db);
}
}
22 changes: 22 additions & 0 deletions src/Traits/CustomDumpTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Prokl\BitrixTestingTools\Traits;

/**
* Trait CustomDumpTrait
* Использовать свой дамп базы для тестов. Применяется в сочетании с трэйтом ResetDatabaseTrait.
* @package Prokl\BitrixTestingTools\Traits
*
* @since 24.04.2021
*/
trait CustomDumpTrait
{
/**
* Путь к кастомному дампу БД.
* @return string
*/
protected function getDumpPath() : string
{
return '';
}
}

0 comments on commit 862bcbc

Please sign in to comment.