Skip to content

Commit faea3e1

Browse files
committed
Тесты.
1 parent 5cedd93 commit faea3e1

File tree

6 files changed

+420
-87
lines changed

6 files changed

+420
-87
lines changed

src/Utils/ErrorScreen.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,10 @@ public function __construct(
6060
*/
6161
public function die(string $message = '') : ?bool
6262
{
63-
if (defined('PHPUNIT_COMPOSER_INSTALL') && defined('__PHPUNIT_PHAR__')) {
64-
echo $message;
65-
return false;
63+
if (defined('PHPUNIT_COMPOSER_INSTALL') || defined('__PHPUNIT_PHAR__')) {
64+
throw new RuntimeException(
65+
$message
66+
);
6667
}
6768

6869
$content = $this->prepareErrorScreen($message);

src/Utils/IgnoredAutowiringControllerParamsBag.php

Lines changed: 0 additions & 84 deletions
This file was deleted.

tests/Cases/AppRequestTest.php

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
<?php
2+
3+
namespace Prokl\ServiceProvider\Tests\Cases;
4+
5+
use Prokl\ServiceProvider\Services\AppRequest;
6+
use Prokl\TestingTools\Base\BaseTestCase;
7+
use Prokl\TestingTools\Tools\PHPUnitUtils;
8+
use ReflectionException;
9+
10+
/**
11+
* Class AppRequest
12+
* @package Prokl\ServiceProvider\Tests\Cases
13+
* @coversDefaultClass AppRequest
14+
*
15+
* @since 24.12.2020 Новые тесты.
16+
*/
17+
class AppRequestTest extends BaseTestCase
18+
{
19+
/**
20+
* @var AppRequest $obTestObject
21+
*/
22+
protected $obTestObject;
23+
24+
/**
25+
* @inheritDoc
26+
*/
27+
protected function setUp(): void
28+
{
29+
parent::setUp();
30+
31+
$_SERVER['REQUEST_URI'] = '/';
32+
33+
$this->obTestObject = new AppRequest();
34+
}
35+
36+
/**
37+
* getDocumentRoot().
38+
*
39+
* @return void
40+
*/
41+
public function testGetDocumentRoot() : void
42+
{
43+
$result = $this->obTestObject->getDocumentRoot();
44+
45+
$this->assertSame(
46+
$_SERVER['DOCUMENT_ROOT'],
47+
$result,
48+
'Неправильный DOCUMENT_ROOT.'
49+
);
50+
}
51+
52+
/**
53+
* getHttpHost().
54+
*
55+
* @return void
56+
*/
57+
public function testGetHttpHost() : void
58+
{
59+
$result = $this->obTestObject->getHttpHost();
60+
61+
$this->assertSame(
62+
$_SERVER['HTTP_HOST'],
63+
$result,
64+
'Неправильный HTTP_HOST.'
65+
);
66+
}
67+
68+
/**
69+
* initGlobals().
70+
*
71+
* @return void
72+
* @throws ReflectionException
73+
*/
74+
public function testGetSuperglobal() : void
75+
{
76+
$backupGET = $_GET;
77+
$_GET['test'] = 'Y';
78+
79+
PHPUnitUtils::callMethod(
80+
$this->obTestObject,
81+
'initGlobals'
82+
);
83+
84+
$this->assertSame(
85+
'Y',
86+
$_GET['test'],
87+
'Суперглобалы не обработались.'
88+
);
89+
90+
$_GET = $backupGET;
91+
}
92+
93+
/**
94+
* initGlobals(). Empty values.
95+
*
96+
* @return void
97+
* @throws ReflectionException
98+
*/
99+
public function testGetSuperglobalEmpty() : void
100+
{
101+
$backupGET = $_GET;
102+
$_GET = null;
103+
104+
PHPUnitUtils::callMethod(
105+
$this->obTestObject,
106+
'initGlobals'
107+
);
108+
109+
$this->assertIsArray(
110+
$_GET,
111+
'Суперглобалы не обработались.'
112+
);
113+
114+
$_GET = $backupGET;
115+
}
116+
117+
/**
118+
* getRequestUri().
119+
*
120+
* @return void
121+
*/
122+
public function testGetRequestUri() : void
123+
{
124+
$result = $this->obTestObject->getRequestUri();
125+
126+
$this->assertSame(
127+
$_SERVER['REQUEST_URI'],
128+
$result,
129+
'Неправильный REQUEST_URI.'
130+
);
131+
}
132+
133+
/**
134+
* setServer().
135+
*
136+
* @return void
137+
*/
138+
public function testSetServer() : void
139+
{
140+
$key = 'TEST.PHP.UNIT';
141+
$value = 'OK';
142+
143+
$this->obTestObject->setServer($key, $value);
144+
145+
$request = $this->obTestObject->getRequest();
146+
147+
$this->assertSame(
148+
$value,
149+
$request->server->get($key),
150+
'Неправильная установка ключа $_REQUEST.'
151+
);
152+
}
153+
}
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<?php
2+
3+
namespace Prokl\ServiceProvider\Tests\Cases\PostLoadingPasses;
4+
5+
use Exception;
6+
use Prokl\ServiceProvider\PostLoadingPass\BootstrapServices;
7+
use Prokl\TestingTools\Base\BaseTestCase;
8+
use Symfony\Component\DependencyInjection\Compiler\RemoveUnusedDefinitionsPass;
9+
use Symfony\Component\DependencyInjection\ContainerBuilder;
10+
11+
/**
12+
* Class BootstrapServicesTest
13+
* @package Prokl\ServiceProvider\Tests\Cases\PostLoadingPasses
14+
* @coversDefaultClass BootstrapServices
15+
*
16+
* @since 28.09.2020
17+
*/
18+
class BootstrapServicesTest extends BaseTestCase
19+
{
20+
/**
21+
* @var BootstrapServices $obTestObject Тестируемый объект.
22+
*/
23+
protected $obTestObject;
24+
25+
/**
26+
* @inheritDoc
27+
*/
28+
protected function setUp(): void
29+
{
30+
parent::setUp();
31+
32+
$this->obTestObject = new BootstrapServices();
33+
}
34+
35+
/**
36+
* action(). Нормальный ход событий.
37+
*
38+
* @return void
39+
* @throws Exception
40+
*/
41+
public function testAction(): void
42+
{
43+
$testContainerBuilder = $this->getTestContainer('service.bootstrap');
44+
45+
$result = $this->obTestObject->action(
46+
$testContainerBuilder
47+
);
48+
49+
$this->assertTrue(
50+
$result,
51+
'Что-то пошло не так.'
52+
);
53+
}
54+
55+
/**
56+
* action(). Нет обработчиков. Пустой parameterBag.
57+
*
58+
* @return void
59+
* @throws Exception
60+
*/
61+
public function testActionNoListener(): void
62+
{
63+
$container = $this->getEmptyContainer();
64+
65+
$result = $this->obTestObject->action(
66+
$container
67+
);
68+
69+
$this->assertFalse(
70+
$result,
71+
'Что-то пошло не так.'
72+
);
73+
}
74+
75+
/**
76+
* Мок обработчика.
77+
*
78+
* @return mixed
79+
*/
80+
private function getStubService()
81+
{
82+
return new class {
83+
public function addEvent(): void
84+
{
85+
}
86+
};
87+
}
88+
89+
/**
90+
* Тестовый контейнер.
91+
*
92+
* @param string $serviceId ID сервиса.
93+
* @param array $params Параметры.
94+
*
95+
* @return ContainerBuilder
96+
*/
97+
private function getTestContainer(
98+
string $serviceId,
99+
array $params = [
100+
['event' => 'test'],
101+
]
102+
): ContainerBuilder {
103+
$container = new ContainerBuilder();
104+
$container
105+
->register($serviceId, get_class($this->getStubService()))
106+
->setPublic(true);
107+
108+
$container->setParameter('_bootstrap', [
109+
$serviceId => $params,
110+
]);
111+
112+
$this->process($container);
113+
114+
return $container;
115+
}
116+
117+
/**
118+
* Пустой контейнер.
119+
*
120+
* @return ContainerBuilder
121+
*/
122+
private function getEmptyContainer() : ContainerBuilder
123+
{
124+
return new ContainerBuilder();
125+
}
126+
127+
/**
128+
* @param ContainerBuilder $container Контейнер.
129+
*/
130+
private function process(ContainerBuilder $container): void
131+
{
132+
(new RemoveUnusedDefinitionsPass())->process($container);
133+
}
134+
}

0 commit comments

Comments
 (0)