-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
314 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 не запускался.' | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?php | ||
return [ | ||
Prokl\ServiceProvider\Tests\Fixtures\TestingBundle::class => ['all' => true] | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.