Skip to content

Commit 5cedd93

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

File tree

4 files changed

+150
-7
lines changed

4 files changed

+150
-7
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@
4646
"proklung/base-exception": "^1.0",
4747
"symfony/psr-http-message-bridge": "^2.1",
4848
"nyholm/psr7": "^1.4",
49-
"guzzlehttp/psr7": "^1.8"
49+
"guzzlehttp/psr7": "^1.8",
50+
"vlucas/phpdotenv": "3.* || 4.*"
5051
},
5152
"require-dev": {
5253
"proklung/bitrix-phpunit-testing-tools": "^1.1"

src/Bundles/BundlesLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function __construct(
5454
) {
5555
$configPath = $configPath ?: self::PATH_BUNDLES_CONFIG;
5656

57-
if (@file_exists($_SERVER['DOCUMENT_ROOT'] . $configPath)) {
57+
if (file_exists($_SERVER['DOCUMENT_ROOT'] . $configPath)) {
5858
$this->bundles = require $_SERVER['DOCUMENT_ROOT'] . $configPath;
5959
}
6060

src/ServiceProvider.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Prokl\ServiceProvider\Framework\SymfonyCompilerPassBag;
1111
use Prokl\ServiceProvider\Services\AppKernel;
1212
use Prokl\ServiceProvider\Utils\ErrorScreen;
13+
use Psr\Container\ContainerInterface;
1314
use Psr\Container\ContainerInterface as PsrContainerInterface;
1415
use RuntimeException;
1516
use Symfony\Bridge\ProxyManager\LazyProxy\PhpDumper\ProxyDumper;
@@ -180,9 +181,7 @@ public function __construct(
180181
$this->environment = $_ENV['DEBUG'] ? 'dev' : 'prod';
181182
$this->debug = (bool)$_ENV['DEBUG'];
182183

183-
$this->errorHandler = new ErrorScreen(
184-
new CMain()
185-
);
184+
$this->errorHandler = new ErrorScreen(new CMain());
186185

187186
$this->filesystem = new Filesystem();
188187

@@ -234,9 +233,9 @@ public function get(string $id)
234233
/**
235234
* Контейнер.
236235
*
237-
* @return ContainerBuilder
236+
* @return ContainerInterface
238237
*/
239-
public function container(): ContainerBuilder
238+
public function container() : ContainerInterface
240239
{
241240
return static::$containerBuilder ?: $this->initContainer($this->filename);
242241
}
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
<?php
2+
3+
namespace Prokl\ServiceProvider\Tests\Cases;
4+
5+
use Exception;
6+
use Prokl\BitrixTestingTools\Base\BitrixableTestCase;
7+
use Prokl\ServiceProvider\ServiceProvider;
8+
9+
/**
10+
* Class ServiceProviderTest
11+
* @package Prokl\ServiceProvider\Tests
12+
*
13+
* @since 02.06.2021
14+
*/
15+
class ServiceProviderTest extends BitrixableTestCase
16+
{
17+
/**
18+
* @var ServiceProvider
19+
*/
20+
protected $obTestObject;
21+
22+
/**
23+
* @var string $pathYamlConfig Путь к конфигу.
24+
*/
25+
private $pathYamlConfig = '../Fixtures/config/test_container.yaml';
26+
27+
/**
28+
* @inheritDoc
29+
*/
30+
protected function setUp() : void
31+
{
32+
parent::setUp();
33+
34+
$this->rrmdir($_SERVER['DOCUMENT_ROOT'] . '/bitrix/cache/s1/containers');
35+
$_SERVER['DOCUMENT_ROOT'] = __DIR__;
36+
}
37+
38+
/**
39+
* @inheritDoc
40+
*/
41+
protected function tearDown() : void
42+
{
43+
parent::tearDown();
44+
}
45+
46+
/**
47+
* @return void
48+
* @throws Exception
49+
*/
50+
public function testLoad() : void
51+
{
52+
$_ENV['DEBUG'] = true;
53+
54+
$this->obTestObject = new ServiceProvider(
55+
$this->pathYamlConfig
56+
);
57+
58+
$container = $this->obTestObject->container();
59+
60+
$this->assertTrue($container->has('kernel'));
61+
$this->assertTrue($container->has('test_service'));
62+
}
63+
64+
/**
65+
* Компилируется ли контейнер?
66+
*
67+
* @return void
68+
* @throws Exception
69+
*/
70+
public function testLoadProd() : void
71+
{
72+
$_ENV['DEBUG'] = false;
73+
74+
$this->obTestObject = new ServiceProvider(
75+
$this->pathYamlConfig,
76+
);
77+
78+
$container = $this->obTestObject->container();
79+
80+
$this->assertTrue($container->has('kernel'));
81+
$this->assertTrue($container->has('test_service'));
82+
$this->assertTrue(file_exists($_SERVER['DOCUMENT_ROOT'] . '/bitrix/cache/s1/containers'));
83+
84+
$this->rrmdir($_SERVER['DOCUMENT_ROOT'] . '/bitrix/cache/s1/containers');
85+
}
86+
87+
/**
88+
* Грузятся ли бандлы?
89+
*
90+
* @return void
91+
* @throws Exception
92+
*/
93+
public function testLoadBundles() : void
94+
{
95+
$_ENV['DEBUG'] = true;
96+
97+
$this->obTestObject = new ServiceProvider(
98+
$this->pathYamlConfig,
99+
'/../Fixtures/bundles.php'
100+
);
101+
102+
$container = $this->obTestObject->container();
103+
104+
$this->assertTrue($container->has('kernel'));
105+
$this->assertTrue($container->has('test_service'));
106+
107+
$bundles = $container->getParameter('kernel.bundles');
108+
109+
$this->assertSame(
110+
['TestingBundle' => 'Prokl\ServiceProvider\Tests\Fixtures\TestingBundle'],
111+
$bundles,
112+
'Бандл не загрузился.'
113+
);
114+
115+
$bundlesMeta = $container->getParameter('kernel.bundles_metadata');
116+
$this->assertNotEmpty($bundlesMeta);
117+
}
118+
119+
/**
120+
* Рекурсивно удалить папку со всем файлами и папками.
121+
*
122+
* @param string $dir Директория.
123+
*
124+
* @return void
125+
*/
126+
private function rrmdir(string $dir) : void
127+
{
128+
if (is_dir($dir)) {
129+
$objects = scandir($dir);
130+
foreach ($objects as $object) {
131+
if ($object !== '.' && $object !== '..') {
132+
if (filetype($dir. '/' .$object) === 'dir') {
133+
$this->rrmdir($dir . '/' . $object);
134+
} else {
135+
unlink($dir. '/' . $object);
136+
}
137+
}
138+
}
139+
reset($objects);
140+
rmdir($dir);
141+
}
142+
}
143+
}

0 commit comments

Comments
 (0)