|
| 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