Skip to content

Commit

Permalink
First commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
ProklUng committed Apr 23, 2021
0 parents commit 518f5cc
Show file tree
Hide file tree
Showing 6 changed files with 388 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
/vendor/
composer.lock
30 changes: 30 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "proklung/bitrix-phpunit-testing-tools",
"description": "Bitrix PHPUNIT testing tools.",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Gavrilov Fedy",
"email": "[email protected]",
"homepage": "https://github.com/ProklUng"
}
],
"support": {
"issues": "https://github.com/ProklUng/bitrix.phpunit.testing.tools/issues",
"source": "https://github.com/ProklUng/bitrix.phpunit.testing.tools"
},
"autoload": {
"psr-4": {
"Prokl\\BitrixTestingTools\\": "src"
}
},
"require": {
"proklung/phpunit-testing-tools": "^1.0",
"symfony/dependency-injection": "^4.0 || ^5.0",
"symfony/http-kernel": "^4.0 || ^5.0",
"symfony/config": "~4|~5",
"symfony/framework-bundle": "^4.0 || ^5.0",
"sheerockoff/bitrix-ci": "^20.5"
}
}
8 changes: 8 additions & 0 deletions readme.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Кастомные инструменты для PHPUnit тестов под Битрикс

**INTERNAL**

## Установка

`composer require --dev proklung/bitrix-phpunit-testing-tools`

37 changes: 37 additions & 0 deletions src/Base/BitrixableTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Prokl\BitrixTestingTools\Base;

use Prokl\TestingTools\Base\BaseTestCase;

/**
* Class BitrixableTestCase
* @package Prokl\BitrixTestingTools\Base
*/
class BitrixableTestCase extends BaseTestCase
{
/**
* @inheritDoc
*/
protected function setUp(): void
{
parent::setUp();

putenv('MYSQL_HOST=localhost');
putenv('MYSQL_DATABASE=bitrix_ci');
putenv('MYSQL_USER=root');
putenv('MYSQL_PASSWORD=');

// \Sheerockoff\BitrixCi\Bootstrap::migrate();
\Sheerockoff\BitrixCi\Bootstrap::bootstrap();
}

/**
* @inheritDoc
*/
protected function tearDown(): void
{
parent::tearDown();
ob_get_clean(); // Битриксовые штучки-дрючки.
}
}
254 changes: 254 additions & 0 deletions src/Mockers/MockerBitrixBlocks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
<?php

namespace Prokl\BitrixTestingTools\Mockers;

use _CIBElement;
use CIBlockResult;
use Mockery;

/**
* Class MockerBitrixBlocks
* Мок битриксовых запросников к базе.
* @package Prokl\BitrixTestingTools\Mockers
*
* @since 17.12.2020
* @since 20.02.2021 Мок CIBlockSection.
*/
class MockerBitrixBlocks
{
/**
* @var string $srcClass Исходный класс.
*/
private $srcClass;

/**
* @var array $fixture Фикстура.
*/
private $fixture = [];

/**
* @var string $retrieverMethod Метод, извлекающий данные из базы.
*/
private $retrieverMethod = 'GetNext';

/**
* MockerBitrixBlocks constructor.
*
* @param string $srcClass Исходный класс.
*/
public function __construct(string $srcClass)
{
$this->srcClass = $srcClass;
}

/**
* Синтаксический сахар в виде статического конструктора. Мок GetNext.
*
* @param string $srcClass Исходный класс.
* @param array $fixture Фикстура.
*
* @return mixed
*/
public static function getNext(string $srcClass, array $fixture = [])
{
$self = new static($srcClass);
$self->setRetrieverMethod('GetNext')
->setFixture($fixture);

return $self->mock();
}

/**
* Синтаксический сахар в виде статического конструктора. Мок Fetch.
*
* @param string $srcClass Исходный класс.
* @param array $fixture Фикстура.
*
* @return mixed
*/
public static function fetch(string $srcClass, array $fixture = [])
{
$self = new static($srcClass);
$self->setRetrieverMethod('Fetch')
->setFixture($fixture);

return $self->mock();
}

/**
* Синтаксический сахар в виде статического конструктора. Мок GetNextElement.
*
* @param string $srcClass Исходный класс.
* @param array $fixture Фикстура.
*
* @return mixed
*/
public static function getNextElement(string $srcClass, array $fixture = [])
{
$self = new static($srcClass);
$self->setRetrieverMethod('GetNextElement')
->setFixture($fixture);

return $self->mock();
}

/**
* @param array $fixture
*
* @return $this
*/
public function setFixture(array $fixture): self
{
$this->fixture = $fixture;

return $this;
}

/**
* @param string $retrieverMethod Метод, извлекающий данные из базы.
*
* @return $this
*/
public function setRetrieverMethod(string $retrieverMethod): self
{
$this->retrieverMethod = $retrieverMethod;

return $this;
}

/**
* @return mixed
*/
public function mock()
{
return $this->getMockCIblockElement(
$this->fixture,
'GetList',
$this->retrieverMethod
);
}

/**
* @return mixed
*/
public function mockSection()
{
return $this->getMockCIblockSection(
$this->fixture,
'GetList',
$this->retrieverMethod
);
}

/**
* Мок CIBlockElement.
*
* @param string $method
* @param string $methodRetrieveData
* @param array $fixture
*
* @return mixed
*/
private function getMockCIblockElement(
array $fixture = [],
string $method = 'GetList',
string $methodRetrieveData = 'GetNext'
) {
$resultQuery = $this->getMockCIBlockResult($methodRetrieveData, $fixture);

return Mockery::mock(
$this->srcClass)
->makePartial()
->shouldReceive($method)
->andReturn(
$resultQuery
)
->getMock();
}

/**
* Мок CIBlockSection.
*
* @param string $method
* @param string $methodRetrieveData
* @param array $fixture
*
* @return mixed
*/
private function getMockCIblockSection(
array $fixture = [],
string $method = 'GetList',
string $methodRetrieveData = 'GetNext'
) {
$resultQuery = $this->getMockCIBlockResult($methodRetrieveData, $fixture);

return Mockery::mock(
$this->srcClass)
->makePartial()
->shouldReceive($method)
->andReturn(
$resultQuery
)
->getMock();
}

/**
* Мок CIBlockResult. Ответ CIBlockElement -> GetList.
*
* @param string $method
* @param array $fixture
*
* @return mixed
*/
private function getMockCIBlockResult(
string $method = 'GetNext',
array $fixture = []
) {
if ($method === 'GetNextElement') {
return $this->getMockCIBElement($fixture);
}

static $count = 0;

/**
* Возврат столько массивов, сколько есть в фикстуре.
* Последним должно вернуться false или null, иначе
* зацикливание.
*/
$mock = Mockery::mock(
CIBlockResult::class)
->shouldReceive($method)
->andReturnUsing(function () use ($fixture, &$count) {
if ($count >= count($fixture)) {
return false;
}

$result = !empty($fixture[$count]) ? $fixture[$count] : $fixture;

$count++;

return $result;
});

$count = 0;

return $mock->getMock();
}

/**
* Мок ответа CIBlockResult - GetNextElement (GetFields).
*
* @param array $fixture
*
* @return mixed
*/
private function getMockCIBElement(array $fixture = [])
{
$mock = Mockery::mock(
_CIBElement::class)
->shouldReceive('GetFields')
->andReturn($fixture);

return $mock->getMock();
}
}
56 changes: 56 additions & 0 deletions src/Mockers/MockerBitrixSeo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Prokl\BitrixTestingTools\Mockers;

use Mockery;

/**
* Class MockerBitrixSeo
* Мокирование SectionValues & ElementValues.
* @package Prokl\BitrixOgGraphBundle\Tests\Tools
*
* @since 20.02.2021
*/
class MockerBitrixSeo
{
/**
* @var array $fixture Фикстура.
*/
private $fixture;

/**
* MockerBitrixSeo constructor.
*
* @param array $fixture Фикстура.
*/
public function __construct(array $fixture = [])
{
$this->fixture = $fixture;
}

/**
* Мок SectionValues.
*
* @return void
*/
public function mockSectionValues() : void
{
$mock = Mockery::mock('overload:Bitrix\Iblock\InheritedProperty\SectionValues');

$mock->shouldReceive('queryValues')->andReturn($this->fixture);
$mock->shouldReceive('clearValues');
}

/**
* Мок ElementValues.
*
* @return void
*/
public function mockElementValues() : void
{
$mock = Mockery::mock('overload:Bitrix\Iblock\InheritedProperty\ElementValues');

$mock->shouldReceive('queryValues')->andReturn($this->fixture);
$mock->shouldReceive('clearValues');
}
}

0 comments on commit 518f5cc

Please sign in to comment.