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 1a1e4c8 commit c1bd6ba
Show file tree
Hide file tree
Showing 13 changed files with 314 additions and 19 deletions.
10 changes: 0 additions & 10 deletions src/Framework/SymfonyCompilerPassBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@ class SymfonyCompilerPassBag extends AbstractSymfonyCompilerPassBag
* @var array $standartCompilerPasses Пассы Symfony.
*/
protected $standartCompilerPasses = [
[
'pass' => ControllerArgumentValueResolverPass::class,
],
[
'pass' => RegisterControllerArgumentLocatorsPass::class,
],
[
'pass' => RoutingResolverPass::class,
],
Expand All @@ -38,10 +32,6 @@ class SymfonyCompilerPassBag extends AbstractSymfonyCompilerPassBag
[
'pass' => PropertyInfoPass::class,
],
[
'pass' => RemoveEmptyControllerArgumentLocatorsPass::class,
'phase' => PassConfig::TYPE_BEFORE_REMOVING,
],
[
'pass' => AddConstraintValidatorsPass::class,
],
Expand Down
6 changes: 0 additions & 6 deletions src/Framework/SymfonyCompilerPassBagLight.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ class SymfonyCompilerPassBagLight extends AbstractSymfonyCompilerPassBag
* @var array $standartCompilerPasses Пассы Symfony.
*/
protected $standartCompilerPasses = [
[
'pass' => ControllerArgumentValueResolverPass::class,
],
[
'pass' => RegisterControllerArgumentLocatorsPass::class,
],
[
'pass' => RoutingResolverPass::class,
],
Expand Down
9 changes: 9 additions & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Prokl\ServiceProvider\Services\AppKernel;
use Prokl\ServiceProvider\Utils\ErrorScreen;
use Psr\Container\ContainerInterface as PsrContainerInterface;
use RuntimeException;
use Symfony\Bridge\ProxyManager\LazyProxy\PhpDumper\ProxyDumper;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Config\ConfigCache;
Expand Down Expand Up @@ -755,10 +756,18 @@ private function loadContainerConfig(string $fileName, ContainerBuilder $contain
* @throws Exception Ошибки контейнера.
*
* @since 06.11.2020
* @throws RuntimeException Когда директория с конфигами не существует.
*/
private function configureContainer(ContainerBuilder $container, LoaderInterface $loader): void
{
$confDir = $_SERVER['DOCUMENT_ROOT'] . $this->configDir;

if (!@file_exists($confDir)) {
throw new RuntimeException(
'Config directory ' . $confDir . ' not exist.'
);
}

$container->setParameter('container.dumper.inline_class_loader', true);

try {
Expand Down
15 changes: 12 additions & 3 deletions src/Services/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use LogicException;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
use Symfony\Component\HttpKernel\Kernel;

/**
Expand Down Expand Up @@ -63,7 +64,7 @@ public function getCacheDir(): string
{
$cachePath = $this->getProjectDir() . '/bitrix/cache/';
if (!@file_exists($cachePath)) {
@mkdir($cachePath);
@mkdir($cachePath, 0777, true);
}

return $cachePath;
Expand Down Expand Up @@ -199,11 +200,19 @@ public function registerContainerConfiguration(LoaderInterface $loader)
/**
* Регистрация бандла.
*
* @return iterable
* @return iterable|BundleInterface[]
*
* @since 02.06.2021 Если файл не существует - игнорим.
*/
public function registerBundles(): iterable
{
$contents = require $this->getProjectDir() . '/local/configs/bundles.php';
$bundleConfigPath = $this->getProjectDir() . '/local/configs/bundles.php';

if (!@file_exists($bundleConfigPath)) {
return [];
}

$contents = require $bundleConfigPath;

foreach ($contents as $class => $envs) {
if ($envs[$this->environment] ?? $envs['all'] ?? false) {
Expand Down
189 changes: 189 additions & 0 deletions tests/Cases/BundlesLoaderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
<?php

namespace Prokl\ServiceProvider\Tests\Cases;

use InvalidArgumentException;
use Prokl\ServiceProvider\Bundles\BundlesLoader;
use Prokl\ServiceProvider\Tests\Fixtures\DummyService;
use Prokl\ServiceProvider\Tests\Fixtures\TestingBundle;
use Prokl\TestingTools\Base\BaseTestCase;
use Prokl\TestingTools\Tools\PHPUnitUtils;
use ReflectionException;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* Class BundlesLoaderTest
* @package Prokl\ServiceProvider\Tests\Cases
*
* @since 01.06.2021
*/
class BundlesLoaderTest extends BaseTestCase
{
/**
* @var BundlesLoader $obTestObject
*/
protected $obTestObject;

/**
* @var ContainerBuilder $dummyContainer
*/
private $dummyContainer;

/**
* @inheritDoc
*/
protected function setUp(): void
{
parent::setUp();
$_SERVER['DOCUMENT_ROOT'] = __DIR__;
$_ENV['DEBUG'] = true;

$this->dummyContainer = new ContainerBuilder();
$this->obTestObject = new BundlesLoader(
$this->dummyContainer,
'/../Fixtures/bundles.php'
);
}

/**
* load(). Нормальный ход вещей.
*
* @return void
*/
public function testLoad() : void
{
$this->obTestObject->load();

$result = $this->obTestObject->bundles();

$this->assertCount(1, $result);
$this->assertSame('TestingBundle', array_key_first($result));
$this->assertInstanceOf(TestingBundle::class, $result['TestingBundle']);
}

/**
* load(). Несуществующий конфиг.
*
* @return void
*/
public function testLoadDefaultPath() : void
{
$this->obTestObject = new BundlesLoader(
$this->dummyContainer,
'/../Fixtures/fake.php' // Несуществующий конфиг
);

$this->obTestObject->load();

$result = $this->obTestObject->bundles();

$this->assertEmpty($result);
}

/**
* load(). Бандл без метода RegisterExtension.
*
* @return void
*/
public function testLoadWithoutRegisterExtension() : void
{
$this->obTestObject = new BundlesLoader(
$this->dummyContainer,
'/../Fixtures/invalid_bundles.php'
);

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Bundle TestingInvalidBundle dont have implemented getContainerExtension method.');

$this->obTestObject->load();
}

/**
* load(). Invalid class.
*
* @return void
*/
public function testLoadInvalidClass() : void
{
$this->obTestObject = new BundlesLoader(
$this->dummyContainer,
'/../Fixtures/fake_bundles.php'
);

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Bundle class Prokl\ServiceProvider\Tests\Fixtures\FakeBundle not exist.');

$this->obTestObject->load();
}

/**
* boot(). Проверяется, что в класс бандла загоняется полноценный контейнер.
* И, что у бандла вызван метод boot.
*
* @return void
* @throws ReflectionException Ошибки рефлексии.
*/
public function testBoot() : void
{
$this->obTestObject->load();

$this->dummyContainer->register('test.service', DummyService::class);
$this->obTestObject->boot($this->dummyContainer);

$result = $this->obTestObject->bundles();
$bundle = current($result);

$container = PHPUnitUtils::getProtectedProperty(
$bundle,
'container'
);

$this->assertTrue(
$container->has('test.service'),
'Контейнер не обработался до конца. Ожидаемого сервиса нет.'
);

$this->assertTrue(
$bundle->booted,
'Метод boot бандла не вызывался.'
);
}

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

$this->assertCount(1, $result);
$this->assertSame('TestingBundle', array_key_first($result));
$this->assertInstanceOf(TestingBundle::class, $result['TestingBundle']);
}

/**
* bootAfterCompilingContainer().
*
* @return void
*/
public function testBootAfterCompilingContainer() : void
{
$this->obTestObject->load();
$result = $this->obTestObject::getBundlesMap();
$bundle = get_class(current($result));

$this->dummyContainer->setParameter('kernel.bundles', [
$bundle
]);

$this->obTestObject::bootAfterCompilingContainer($this->dummyContainer);

$this->assertTrue(
$bundle::$booted_static,
'Метод boot не запускался.'
);
}
}
12 changes: 12 additions & 0 deletions tests/Fixtures/DummyService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Prokl\ServiceProvider\Tests\Fixtures;

/**
* Class DummyService
* @package Prokl\ServiceProvider\Tests\Fixtures
*/
class DummyService
{

}
34 changes: 34 additions & 0 deletions tests/Fixtures/TestingBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Prokl\ServiceProvider\Tests\Fixtures;

use Symfony\Component\HttpKernel\Bundle\Bundle;

/**
* Class TestingBundle
* @package Prokl\ServiceProvider\Tests\Fixtures
*/
class TestingBundle extends Bundle
{
public $booted = false;

public static $booted_static = false;

/**
* @inheritDoc
*/
public function getContainerExtension()
{
return new TestingBundleExtension();
}

/**
* @inheritDoc
*/
public function boot() : void
{
parent::boot();
$this->booted = true;
static::$booted_static = true;
}
}
23 changes: 23 additions & 0 deletions tests/Fixtures/TestingBundleExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Prokl\ServiceProvider\Tests\Fixtures;

use Exception;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

/**
* Class TestingBundleExtension
* @package Prokl\ServiceProvider\Tests\Fixtures
*/
class TestingBundleExtension extends Extension
{
/**
* @inheritDoc
* @throws Exception
*/
public function load(array $configs, ContainerBuilder $container) : void
{

}
}
14 changes: 14 additions & 0 deletions tests/Fixtures/TestingInvalidBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Prokl\ServiceProvider\Tests\Fixtures;

use Symfony\Component\HttpKernel\Bundle\Bundle;

/**
* Class TestingInvalidBundle
* @package Prokl\ServiceProvider\Tests\Fixtures
*/
class TestingInvalidBundle extends Bundle
{

}
4 changes: 4 additions & 0 deletions tests/Fixtures/bundles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
return [
Prokl\ServiceProvider\Tests\Fixtures\TestingBundle::class => ['all' => true]
];
9 changes: 9 additions & 0 deletions tests/Fixtures/config/test_container.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
services:
# конфигурация по умолчанию в *этом* файле
_defaults:
autowire: true
autoconfigure: true
public: true

test_service:
class: Prokl\ServiceProvider\Tests\Fixtures\DummyService
Loading

0 comments on commit c1bd6ba

Please sign in to comment.