Skip to content

Commit

Permalink
Тесты.
Browse files Browse the repository at this point in the history
  • Loading branch information
ProklUng committed Jun 2, 2021
1 parent 5cedd93 commit faea3e1
Show file tree
Hide file tree
Showing 6 changed files with 420 additions and 87 deletions.
7 changes: 4 additions & 3 deletions src/Utils/ErrorScreen.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,10 @@ public function __construct(
*/
public function die(string $message = '') : ?bool
{
if (defined('PHPUNIT_COMPOSER_INSTALL') && defined('__PHPUNIT_PHAR__')) {
echo $message;
return false;
if (defined('PHPUNIT_COMPOSER_INSTALL') || defined('__PHPUNIT_PHAR__')) {
throw new RuntimeException(
$message
);
}

$content = $this->prepareErrorScreen($message);
Expand Down
84 changes: 0 additions & 84 deletions src/Utils/IgnoredAutowiringControllerParamsBag.php

This file was deleted.

153 changes: 153 additions & 0 deletions tests/Cases/AppRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<?php

namespace Prokl\ServiceProvider\Tests\Cases;

use Prokl\ServiceProvider\Services\AppRequest;
use Prokl\TestingTools\Base\BaseTestCase;
use Prokl\TestingTools\Tools\PHPUnitUtils;
use ReflectionException;

/**
* Class AppRequest
* @package Prokl\ServiceProvider\Tests\Cases
* @coversDefaultClass AppRequest
*
* @since 24.12.2020 Новые тесты.
*/
class AppRequestTest extends BaseTestCase
{
/**
* @var AppRequest $obTestObject
*/
protected $obTestObject;

/**
* @inheritDoc
*/
protected function setUp(): void
{
parent::setUp();

$_SERVER['REQUEST_URI'] = '/';

$this->obTestObject = new AppRequest();
}

/**
* getDocumentRoot().
*
* @return void
*/
public function testGetDocumentRoot() : void
{
$result = $this->obTestObject->getDocumentRoot();

$this->assertSame(
$_SERVER['DOCUMENT_ROOT'],
$result,
'Неправильный DOCUMENT_ROOT.'
);
}

/**
* getHttpHost().
*
* @return void
*/
public function testGetHttpHost() : void
{
$result = $this->obTestObject->getHttpHost();

$this->assertSame(
$_SERVER['HTTP_HOST'],
$result,
'Неправильный HTTP_HOST.'
);
}

/**
* initGlobals().
*
* @return void
* @throws ReflectionException
*/
public function testGetSuperglobal() : void
{
$backupGET = $_GET;
$_GET['test'] = 'Y';

PHPUnitUtils::callMethod(
$this->obTestObject,
'initGlobals'
);

$this->assertSame(
'Y',
$_GET['test'],
'Суперглобалы не обработались.'
);

$_GET = $backupGET;
}

/**
* initGlobals(). Empty values.
*
* @return void
* @throws ReflectionException
*/
public function testGetSuperglobalEmpty() : void
{
$backupGET = $_GET;
$_GET = null;

PHPUnitUtils::callMethod(
$this->obTestObject,
'initGlobals'
);

$this->assertIsArray(
$_GET,
'Суперглобалы не обработались.'
);

$_GET = $backupGET;
}

/**
* getRequestUri().
*
* @return void
*/
public function testGetRequestUri() : void
{
$result = $this->obTestObject->getRequestUri();

$this->assertSame(
$_SERVER['REQUEST_URI'],
$result,
'Неправильный REQUEST_URI.'
);
}

/**
* setServer().
*
* @return void
*/
public function testSetServer() : void
{
$key = 'TEST.PHP.UNIT';
$value = 'OK';

$this->obTestObject->setServer($key, $value);

$request = $this->obTestObject->getRequest();

$this->assertSame(
$value,
$request->server->get($key),
'Неправильная установка ключа $_REQUEST.'
);
}
}
134 changes: 134 additions & 0 deletions tests/Cases/PostLoadingPasses/BootstrapServicesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php

namespace Prokl\ServiceProvider\Tests\Cases\PostLoadingPasses;

use Exception;
use Prokl\ServiceProvider\PostLoadingPass\BootstrapServices;
use Prokl\TestingTools\Base\BaseTestCase;
use Symfony\Component\DependencyInjection\Compiler\RemoveUnusedDefinitionsPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* Class BootstrapServicesTest
* @package Prokl\ServiceProvider\Tests\Cases\PostLoadingPasses
* @coversDefaultClass BootstrapServices
*
* @since 28.09.2020
*/
class BootstrapServicesTest extends BaseTestCase
{
/**
* @var BootstrapServices $obTestObject Тестируемый объект.
*/
protected $obTestObject;

/**
* @inheritDoc
*/
protected function setUp(): void
{
parent::setUp();

$this->obTestObject = new BootstrapServices();
}

/**
* action(). Нормальный ход событий.
*
* @return void
* @throws Exception
*/
public function testAction(): void
{
$testContainerBuilder = $this->getTestContainer('service.bootstrap');

$result = $this->obTestObject->action(
$testContainerBuilder
);

$this->assertTrue(
$result,
'Что-то пошло не так.'
);
}

/**
* action(). Нет обработчиков. Пустой parameterBag.
*
* @return void
* @throws Exception
*/
public function testActionNoListener(): void
{
$container = $this->getEmptyContainer();

$result = $this->obTestObject->action(
$container
);

$this->assertFalse(
$result,
'Что-то пошло не так.'
);
}

/**
* Мок обработчика.
*
* @return mixed
*/
private function getStubService()
{
return new class {
public function addEvent(): void
{
}
};
}

/**
* Тестовый контейнер.
*
* @param string $serviceId ID сервиса.
* @param array $params Параметры.
*
* @return ContainerBuilder
*/
private function getTestContainer(
string $serviceId,
array $params = [
['event' => 'test'],
]
): ContainerBuilder {
$container = new ContainerBuilder();
$container
->register($serviceId, get_class($this->getStubService()))
->setPublic(true);

$container->setParameter('_bootstrap', [
$serviceId => $params,
]);

$this->process($container);

return $container;
}

/**
* Пустой контейнер.
*
* @return ContainerBuilder
*/
private function getEmptyContainer() : ContainerBuilder
{
return new ContainerBuilder();
}

/**
* @param ContainerBuilder $container Контейнер.
*/
private function process(ContainerBuilder $container): void
{
(new RemoveUnusedDefinitionsPass())->process($container);
}
}
Loading

0 comments on commit faea3e1

Please sign in to comment.