diff --git a/src/Utils/ErrorScreen.php b/src/Utils/ErrorScreen.php index 4ff43dd..81ae6f6 100644 --- a/src/Utils/ErrorScreen.php +++ b/src/Utils/ErrorScreen.php @@ -60,9 +60,10 @@ public function __construct( */ public function die(string $message = '') : ?bool { - if (defined('PHPUNIT_COMPOSER_INSTALL') && defined('__PHPUNIT_PHAR__')) { - echo $message; - return false; + if (defined('PHPUNIT_COMPOSER_INSTALL') || defined('__PHPUNIT_PHAR__')) { + throw new RuntimeException( + $message + ); } $content = $this->prepareErrorScreen($message); diff --git a/src/Utils/IgnoredAutowiringControllerParamsBag.php b/src/Utils/IgnoredAutowiringControllerParamsBag.php deleted file mode 100644 index b8ad54d..0000000 --- a/src/Utils/IgnoredAutowiringControllerParamsBag.php +++ /dev/null @@ -1,84 +0,0 @@ -getClassNames($className); - - $ignoredAutowiringClass = false; - if ($parentClasses) { - foreach ($parentClasses as $parentClass) { - if (in_array($parentClass, static::$ignoredClasses, true)) { - $ignoredAutowiringClass = true; - } - } - } - - return $ignoredAutowiringClass; - } - - /** - * Родительские классы. - * - * @param string $className Класс, подвергающийся обработке. - * - * @return array - * @throws ReflectionException Ошибки рефлексии. - * - * @since 08.11.2020 - */ - protected function getClassNames(string $className) : array - { - $ref = new ReflectionClass($className); - $parentRef = $ref->getParentClass(); - - return array_unique(array_merge( - [$className], - $ref->getInterfaceNames(), - $ref->getTraitNames(), - $parentRef ?$this->getClassNames($parentRef->getName()) : [] - )); - } -} diff --git a/tests/Cases/AppRequestTest.php b/tests/Cases/AppRequestTest.php new file mode 100644 index 0000000..7c6b44f --- /dev/null +++ b/tests/Cases/AppRequestTest.php @@ -0,0 +1,153 @@ +obTestObject = new AppRequest(); + } + + /** + * getDocumentRoot(). + * + * @return void + */ + public function testGetDocumentRoot() : void + { + $result = $this->obTestObject->getDocumentRoot(); + + $this->assertSame( + $_SERVER['DOCUMENT_ROOT'], + $result, + 'Неправильный DOCUMENT_ROOT.' + ); + } + + /** + * getHttpHost(). + * + * @return void + */ + public function testGetHttpHost() : void + { + $result = $this->obTestObject->getHttpHost(); + + $this->assertSame( + $_SERVER['HTTP_HOST'], + $result, + 'Неправильный HTTP_HOST.' + ); + } + + /** + * initGlobals(). + * + * @return void + * @throws ReflectionException + */ + public function testGetSuperglobal() : void + { + $backupGET = $_GET; + $_GET['test'] = 'Y'; + + PHPUnitUtils::callMethod( + $this->obTestObject, + 'initGlobals' + ); + + $this->assertSame( + 'Y', + $_GET['test'], + 'Суперглобалы не обработались.' + ); + + $_GET = $backupGET; + } + + /** + * initGlobals(). Empty values. + * + * @return void + * @throws ReflectionException + */ + public function testGetSuperglobalEmpty() : void + { + $backupGET = $_GET; + $_GET = null; + + PHPUnitUtils::callMethod( + $this->obTestObject, + 'initGlobals' + ); + + $this->assertIsArray( + $_GET, + 'Суперглобалы не обработались.' + ); + + $_GET = $backupGET; + } + + /** + * getRequestUri(). + * + * @return void + */ + public function testGetRequestUri() : void + { + $result = $this->obTestObject->getRequestUri(); + + $this->assertSame( + $_SERVER['REQUEST_URI'], + $result, + 'Неправильный REQUEST_URI.' + ); + } + + /** + * setServer(). + * + * @return void + */ + public function testSetServer() : void + { + $key = 'TEST.PHP.UNIT'; + $value = 'OK'; + + $this->obTestObject->setServer($key, $value); + + $request = $this->obTestObject->getRequest(); + + $this->assertSame( + $value, + $request->server->get($key), + 'Неправильная установка ключа $_REQUEST.' + ); + } +} diff --git a/tests/Cases/PostLoadingPasses/BootstrapServicesTest.php b/tests/Cases/PostLoadingPasses/BootstrapServicesTest.php new file mode 100644 index 0000000..1d91986 --- /dev/null +++ b/tests/Cases/PostLoadingPasses/BootstrapServicesTest.php @@ -0,0 +1,134 @@ +obTestObject = new BootstrapServices(); + } + + /** + * action(). Нормальный ход событий. + * + * @return void + * @throws Exception + */ + public function testAction(): void + { + $testContainerBuilder = $this->getTestContainer('service.bootstrap'); + + $result = $this->obTestObject->action( + $testContainerBuilder + ); + + $this->assertTrue( + $result, + 'Что-то пошло не так.' + ); + } + + /** + * action(). Нет обработчиков. Пустой parameterBag. + * + * @return void + * @throws Exception + */ + public function testActionNoListener(): void + { + $container = $this->getEmptyContainer(); + + $result = $this->obTestObject->action( + $container + ); + + $this->assertFalse( + $result, + 'Что-то пошло не так.' + ); + } + + /** + * Мок обработчика. + * + * @return mixed + */ + private function getStubService() + { + return new class { + public function addEvent(): void + { + } + }; + } + + /** + * Тестовый контейнер. + * + * @param string $serviceId ID сервиса. + * @param array $params Параметры. + * + * @return ContainerBuilder + */ + private function getTestContainer( + string $serviceId, + array $params = [ + ['event' => 'test'], + ] + ): ContainerBuilder { + $container = new ContainerBuilder(); + $container + ->register($serviceId, get_class($this->getStubService())) + ->setPublic(true); + + $container->setParameter('_bootstrap', [ + $serviceId => $params, + ]); + + $this->process($container); + + return $container; + } + + /** + * Пустой контейнер. + * + * @return ContainerBuilder + */ + private function getEmptyContainer() : ContainerBuilder + { + return new ContainerBuilder(); + } + + /** + * @param ContainerBuilder $container Контейнер. + */ + private function process(ContainerBuilder $container): void + { + (new RemoveUnusedDefinitionsPass())->process($container); + } +} diff --git a/tests/Cases/PostLoadingPasses/TwigExtensionApplyTest.php b/tests/Cases/PostLoadingPasses/TwigExtensionApplyTest.php new file mode 100644 index 0000000..82c66fa --- /dev/null +++ b/tests/Cases/PostLoadingPasses/TwigExtensionApplyTest.php @@ -0,0 +1,114 @@ +obTestObject = new TwigExtensionApply(); + } + + /** + * action(). Нормальный ход событий. + * + * @runInSeparateProcess + * @preserveGlobalState disabled + * + * @return void + */ + public function testActionNormal(): void + { + $testContainer = $this->getTestContainer('test.service', new class () { + }, true); + + $testContainer->set('twig.instance', new class { + public function addExtension($extension){} + }); + + $result = $this->obTestObject->action($testContainer); + + $this->assertTrue( + $result, + 'Процесс не прошел.' + ); + } + + /** + * action(). Нет сервиса. + * + * @runInSeparateProcess + * @preserveGlobalState disabled + * + * @return void + */ + public function testActionNoService(): void + { + $testContainer = $this->getTestContainer('test.service', new class () { + }, false); + + $result = $this->obTestObject->action($testContainer); + + $this->assertFalse( + $result, + 'Процесс не прошел.' + ); + } + + /** + * Тестовый контейнер. + * + * @param string $serviceId ID сервиса. + * @param mixed|null $object + * @param boolean $tagged + * + * @return ContainerBuilder + */ + private function getTestContainer( + string $serviceId, + $object = null, + bool $tagged = true + ): ContainerBuilder { + + $container = new ContainerBuilder(); + + if ($tagged) { + $container + ->register($serviceId, get_class($object)) + ->addTag('twig.extension') + ->setPublic(true); + + $container->setParameter( + '_twig_extension', + [$serviceId => []] + ); + } else { + $container + ->register($serviceId, get_class($object)) + ->setPublic(true); + } + + return $container; + } +} diff --git a/tests/Cases/ServiceProviderTest.php b/tests/Cases/ServiceProviderTest.php index 04a8da0..cdc3539 100644 --- a/tests/Cases/ServiceProviderTest.php +++ b/tests/Cases/ServiceProviderTest.php @@ -5,6 +5,7 @@ use Exception; use Prokl\BitrixTestingTools\Base\BitrixableTestCase; use Prokl\ServiceProvider\ServiceProvider; +use RuntimeException; /** * Class ServiceProviderTest @@ -61,6 +62,20 @@ public function testLoad() : void $this->assertTrue($container->has('test_service')); } + /** + * @return void + * @throws Exception + */ + public function testLoadInvalidConfigFile() : void + { + $_ENV['DEBUG'] = true; + + $this->expectException(RuntimeException::class); + $this->obTestObject = new ServiceProvider( + '/fake.yaml' + ); + } + /** * Компилируется ли контейнер? *