Skip to content

Commit c1bd6ba

Browse files
committed
Тесты
1 parent 1a1e4c8 commit c1bd6ba

13 files changed

+314
-19
lines changed

src/Framework/SymfonyCompilerPassBag.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,6 @@ class SymfonyCompilerPassBag extends AbstractSymfonyCompilerPassBag
2323
* @var array $standartCompilerPasses Пассы Symfony.
2424
*/
2525
protected $standartCompilerPasses = [
26-
[
27-
'pass' => ControllerArgumentValueResolverPass::class,
28-
],
29-
[
30-
'pass' => RegisterControllerArgumentLocatorsPass::class,
31-
],
3226
[
3327
'pass' => RoutingResolverPass::class,
3428
],
@@ -38,10 +32,6 @@ class SymfonyCompilerPassBag extends AbstractSymfonyCompilerPassBag
3832
[
3933
'pass' => PropertyInfoPass::class,
4034
],
41-
[
42-
'pass' => RemoveEmptyControllerArgumentLocatorsPass::class,
43-
'phase' => PassConfig::TYPE_BEFORE_REMOVING,
44-
],
4535
[
4636
'pass' => AddConstraintValidatorsPass::class,
4737
],

src/Framework/SymfonyCompilerPassBagLight.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,6 @@ class SymfonyCompilerPassBagLight extends AbstractSymfonyCompilerPassBag
2121
* @var array $standartCompilerPasses Пассы Symfony.
2222
*/
2323
protected $standartCompilerPasses = [
24-
[
25-
'pass' => ControllerArgumentValueResolverPass::class,
26-
],
27-
[
28-
'pass' => RegisterControllerArgumentLocatorsPass::class,
29-
],
3024
[
3125
'pass' => RoutingResolverPass::class,
3226
],

src/ServiceProvider.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Prokl\ServiceProvider\Services\AppKernel;
1212
use Prokl\ServiceProvider\Utils\ErrorScreen;
1313
use Psr\Container\ContainerInterface as PsrContainerInterface;
14+
use RuntimeException;
1415
use Symfony\Bridge\ProxyManager\LazyProxy\PhpDumper\ProxyDumper;
1516
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1617
use Symfony\Component\Config\ConfigCache;
@@ -755,10 +756,18 @@ private function loadContainerConfig(string $fileName, ContainerBuilder $contain
755756
* @throws Exception Ошибки контейнера.
756757
*
757758
* @since 06.11.2020
759+
* @throws RuntimeException Когда директория с конфигами не существует.
758760
*/
759761
private function configureContainer(ContainerBuilder $container, LoaderInterface $loader): void
760762
{
761763
$confDir = $_SERVER['DOCUMENT_ROOT'] . $this->configDir;
764+
765+
if (!@file_exists($confDir)) {
766+
throw new RuntimeException(
767+
'Config directory ' . $confDir . ' not exist.'
768+
);
769+
}
770+
762771
$container->setParameter('container.dumper.inline_class_loader', true);
763772

764773
try {

src/Services/AppKernel.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use LogicException;
99
use Symfony\Component\Config\Loader\LoaderInterface;
1010
use Symfony\Component\DependencyInjection\ContainerInterface;
11+
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
1112
use Symfony\Component\HttpKernel\Kernel;
1213

1314
/**
@@ -63,7 +64,7 @@ public function getCacheDir(): string
6364
{
6465
$cachePath = $this->getProjectDir() . '/bitrix/cache/';
6566
if (!@file_exists($cachePath)) {
66-
@mkdir($cachePath);
67+
@mkdir($cachePath, 0777, true);
6768
}
6869

6970
return $cachePath;
@@ -199,11 +200,19 @@ public function registerContainerConfiguration(LoaderInterface $loader)
199200
/**
200201
* Регистрация бандла.
201202
*
202-
* @return iterable
203+
* @return iterable|BundleInterface[]
204+
*
205+
* @since 02.06.2021 Если файл не существует - игнорим.
203206
*/
204207
public function registerBundles(): iterable
205208
{
206-
$contents = require $this->getProjectDir() . '/local/configs/bundles.php';
209+
$bundleConfigPath = $this->getProjectDir() . '/local/configs/bundles.php';
210+
211+
if (!@file_exists($bundleConfigPath)) {
212+
return [];
213+
}
214+
215+
$contents = require $bundleConfigPath;
207216

208217
foreach ($contents as $class => $envs) {
209218
if ($envs[$this->environment] ?? $envs['all'] ?? false) {

tests/Cases/BundlesLoaderTest.php

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
<?php
2+
3+
namespace Prokl\ServiceProvider\Tests\Cases;
4+
5+
use InvalidArgumentException;
6+
use Prokl\ServiceProvider\Bundles\BundlesLoader;
7+
use Prokl\ServiceProvider\Tests\Fixtures\DummyService;
8+
use Prokl\ServiceProvider\Tests\Fixtures\TestingBundle;
9+
use Prokl\TestingTools\Base\BaseTestCase;
10+
use Prokl\TestingTools\Tools\PHPUnitUtils;
11+
use ReflectionException;
12+
use Symfony\Component\DependencyInjection\ContainerBuilder;
13+
14+
/**
15+
* Class BundlesLoaderTest
16+
* @package Prokl\ServiceProvider\Tests\Cases
17+
*
18+
* @since 01.06.2021
19+
*/
20+
class BundlesLoaderTest extends BaseTestCase
21+
{
22+
/**
23+
* @var BundlesLoader $obTestObject
24+
*/
25+
protected $obTestObject;
26+
27+
/**
28+
* @var ContainerBuilder $dummyContainer
29+
*/
30+
private $dummyContainer;
31+
32+
/**
33+
* @inheritDoc
34+
*/
35+
protected function setUp(): void
36+
{
37+
parent::setUp();
38+
$_SERVER['DOCUMENT_ROOT'] = __DIR__;
39+
$_ENV['DEBUG'] = true;
40+
41+
$this->dummyContainer = new ContainerBuilder();
42+
$this->obTestObject = new BundlesLoader(
43+
$this->dummyContainer,
44+
'/../Fixtures/bundles.php'
45+
);
46+
}
47+
48+
/**
49+
* load(). Нормальный ход вещей.
50+
*
51+
* @return void
52+
*/
53+
public function testLoad() : void
54+
{
55+
$this->obTestObject->load();
56+
57+
$result = $this->obTestObject->bundles();
58+
59+
$this->assertCount(1, $result);
60+
$this->assertSame('TestingBundle', array_key_first($result));
61+
$this->assertInstanceOf(TestingBundle::class, $result['TestingBundle']);
62+
}
63+
64+
/**
65+
* load(). Несуществующий конфиг.
66+
*
67+
* @return void
68+
*/
69+
public function testLoadDefaultPath() : void
70+
{
71+
$this->obTestObject = new BundlesLoader(
72+
$this->dummyContainer,
73+
'/../Fixtures/fake.php' // Несуществующий конфиг
74+
);
75+
76+
$this->obTestObject->load();
77+
78+
$result = $this->obTestObject->bundles();
79+
80+
$this->assertEmpty($result);
81+
}
82+
83+
/**
84+
* load(). Бандл без метода RegisterExtension.
85+
*
86+
* @return void
87+
*/
88+
public function testLoadWithoutRegisterExtension() : void
89+
{
90+
$this->obTestObject = new BundlesLoader(
91+
$this->dummyContainer,
92+
'/../Fixtures/invalid_bundles.php'
93+
);
94+
95+
$this->expectException(InvalidArgumentException::class);
96+
$this->expectExceptionMessage('Bundle TestingInvalidBundle dont have implemented getContainerExtension method.');
97+
98+
$this->obTestObject->load();
99+
}
100+
101+
/**
102+
* load(). Invalid class.
103+
*
104+
* @return void
105+
*/
106+
public function testLoadInvalidClass() : void
107+
{
108+
$this->obTestObject = new BundlesLoader(
109+
$this->dummyContainer,
110+
'/../Fixtures/fake_bundles.php'
111+
);
112+
113+
$this->expectException(InvalidArgumentException::class);
114+
$this->expectExceptionMessage('Bundle class Prokl\ServiceProvider\Tests\Fixtures\FakeBundle not exist.');
115+
116+
$this->obTestObject->load();
117+
}
118+
119+
/**
120+
* boot(). Проверяется, что в класс бандла загоняется полноценный контейнер.
121+
* И, что у бандла вызван метод boot.
122+
*
123+
* @return void
124+
* @throws ReflectionException Ошибки рефлексии.
125+
*/
126+
public function testBoot() : void
127+
{
128+
$this->obTestObject->load();
129+
130+
$this->dummyContainer->register('test.service', DummyService::class);
131+
$this->obTestObject->boot($this->dummyContainer);
132+
133+
$result = $this->obTestObject->bundles();
134+
$bundle = current($result);
135+
136+
$container = PHPUnitUtils::getProtectedProperty(
137+
$bundle,
138+
'container'
139+
);
140+
141+
$this->assertTrue(
142+
$container->has('test.service'),
143+
'Контейнер не обработался до конца. Ожидаемого сервиса нет.'
144+
);
145+
146+
$this->assertTrue(
147+
$bundle->booted,
148+
'Метод boot бандла не вызывался.'
149+
);
150+
}
151+
152+
/**
153+
* getBundlesMap().
154+
*
155+
* @return void
156+
*/
157+
public function testGetBundlesMap() : void
158+
{
159+
$this->obTestObject->load();
160+
$result = $this->obTestObject::getBundlesMap();
161+
162+
$this->assertCount(1, $result);
163+
$this->assertSame('TestingBundle', array_key_first($result));
164+
$this->assertInstanceOf(TestingBundle::class, $result['TestingBundle']);
165+
}
166+
167+
/**
168+
* bootAfterCompilingContainer().
169+
*
170+
* @return void
171+
*/
172+
public function testBootAfterCompilingContainer() : void
173+
{
174+
$this->obTestObject->load();
175+
$result = $this->obTestObject::getBundlesMap();
176+
$bundle = get_class(current($result));
177+
178+
$this->dummyContainer->setParameter('kernel.bundles', [
179+
$bundle
180+
]);
181+
182+
$this->obTestObject::bootAfterCompilingContainer($this->dummyContainer);
183+
184+
$this->assertTrue(
185+
$bundle::$booted_static,
186+
'Метод boot не запускался.'
187+
);
188+
}
189+
}

tests/Fixtures/DummyService.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Prokl\ServiceProvider\Tests\Fixtures;
4+
5+
/**
6+
* Class DummyService
7+
* @package Prokl\ServiceProvider\Tests\Fixtures
8+
*/
9+
class DummyService
10+
{
11+
12+
}

tests/Fixtures/TestingBundle.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Prokl\ServiceProvider\Tests\Fixtures;
4+
5+
use Symfony\Component\HttpKernel\Bundle\Bundle;
6+
7+
/**
8+
* Class TestingBundle
9+
* @package Prokl\ServiceProvider\Tests\Fixtures
10+
*/
11+
class TestingBundle extends Bundle
12+
{
13+
public $booted = false;
14+
15+
public static $booted_static = false;
16+
17+
/**
18+
* @inheritDoc
19+
*/
20+
public function getContainerExtension()
21+
{
22+
return new TestingBundleExtension();
23+
}
24+
25+
/**
26+
* @inheritDoc
27+
*/
28+
public function boot() : void
29+
{
30+
parent::boot();
31+
$this->booted = true;
32+
static::$booted_static = true;
33+
}
34+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Prokl\ServiceProvider\Tests\Fixtures;
4+
5+
use Exception;
6+
use Symfony\Component\DependencyInjection\ContainerBuilder;
7+
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
8+
9+
/**
10+
* Class TestingBundleExtension
11+
* @package Prokl\ServiceProvider\Tests\Fixtures
12+
*/
13+
class TestingBundleExtension extends Extension
14+
{
15+
/**
16+
* @inheritDoc
17+
* @throws Exception
18+
*/
19+
public function load(array $configs, ContainerBuilder $container) : void
20+
{
21+
22+
}
23+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Prokl\ServiceProvider\Tests\Fixtures;
4+
5+
use Symfony\Component\HttpKernel\Bundle\Bundle;
6+
7+
/**
8+
* Class TestingInvalidBundle
9+
* @package Prokl\ServiceProvider\Tests\Fixtures
10+
*/
11+
class TestingInvalidBundle extends Bundle
12+
{
13+
14+
}

tests/Fixtures/bundles.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
return [
3+
Prokl\ServiceProvider\Tests\Fixtures\TestingBundle::class => ['all' => true]
4+
];

0 commit comments

Comments
 (0)