|
| 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 | +} |
0 commit comments