From 566bab036c29964ab4f67a125e7180b08fff83f0 Mon Sep 17 00:00:00 2001 From: Daniel Bannert Date: Tue, 11 Dec 2018 12:18:04 +0100 Subject: [PATCH] =?UTF-8?q?fixed=20automatic=20auto-scripts=20not=20to=20t?= =?UTF-8?q?hrow=20a=20exception=20if=20array=20is=20a=20n=E2=80=A6=20(#102?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit …umeric array | Q | A | --------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Related tickets | - | License | MIT | Doc PR | - --- src/Automatic/Automatic.php | 46 +++++++++++-------- tests/Automatic/AutomaticTest.php | 25 +++++++++- .../composer-with-numeric-scripts.json | 21 +++++++++ 3 files changed, 71 insertions(+), 21 deletions(-) create mode 100644 tests/Automatic/Fixture/composer-with-numeric-scripts.json diff --git a/src/Automatic/Automatic.php b/src/Automatic/Automatic.php index d5c8081d..746a1fac 100644 --- a/src/Automatic/Automatic.php +++ b/src/Automatic/Automatic.php @@ -656,36 +656,42 @@ public function onPostUpdate(Event $event, array $operations = []): void */ public function executeAutoScripts(Event $event): void { - $event->stopPropagation(); - // force reloading scripts as we might have added and removed during this run $json = new JsonFile(Factory::getComposerFile()); $jsonContents = $json->read(); - if (isset($jsonContents['scripts'][ScriptEvents::AUTO_SCRIPTS])) { - /** @var \Narrowspark\Automatic\ScriptExecutor $scriptExecutor */ - $scriptExecutor = $this->container->get(ScriptExecutor::class); + if (! isset($jsonContents['scripts'][ScriptEvents::AUTO_SCRIPTS])) { + $this->container->get(IOInterface::class)->write('No auto-scripts section was found under scripts', true, IOInterface::VERBOSE); - foreach ((array) $this->container->get(Lock::class)->get(ScriptExecutor::TYPE) as $extenders) { - foreach ($extenders as $class => $path) { - if (! \class_exists($class)) { - require_once $path; - } + return; + } - /** @var \Narrowspark\Automatic\Common\Contract\ScriptExtender $class */ - $reflectionClass = new ReflectionClass($class); + if (\in_array(true, \array_map('\is_numeric', \array_keys($jsonContents['scripts'][ScriptEvents::AUTO_SCRIPTS])), true)) { + return; + } - if ($reflectionClass->isInstantiable() && $reflectionClass->hasMethod('getType')) { - $scriptExecutor->add($class::getType(), $class); - } + $event->stopPropagation(); + + /** @var \Narrowspark\Automatic\ScriptExecutor $scriptExecutor */ + $scriptExecutor = $this->container->get(ScriptExecutor::class); + + foreach ((array) $this->container->get(Lock::class)->get(ScriptExecutor::TYPE) as $extenders) { + foreach ($extenders as $class => $path) { + if (! \class_exists($class)) { + require_once $path; } - } - foreach ($jsonContents['scripts'][ScriptEvents::AUTO_SCRIPTS] as $cmd => $type) { - $scriptExecutor->execute($type, $cmd); + /** @var \Narrowspark\Automatic\Common\Contract\ScriptExtender $class */ + $reflectionClass = new ReflectionClass($class); + + if ($reflectionClass->isInstantiable() && $reflectionClass->hasMethod('getType')) { + $scriptExecutor->add($class::getType(), $class); + } } - } else { - $this->container->get(IOInterface::class)->write('No auto-scripts section was found under scripts', true, IOInterface::VERBOSE); + } + + foreach ($jsonContents['scripts'][ScriptEvents::AUTO_SCRIPTS] as $cmd => $type) { + $scriptExecutor->execute($type, $cmd); } } diff --git a/tests/Automatic/AutomaticTest.php b/tests/Automatic/AutomaticTest.php index ab639e84..722b24a8 100644 --- a/tests/Automatic/AutomaticTest.php +++ b/tests/Automatic/AutomaticTest.php @@ -424,11 +424,34 @@ public function testExecuteAutoScripts(): void \putenv('COMPOSER'); } + public function testExecuteAutoScriptsWithNumericArray(): void + { + \putenv('COMPOSER=' . __DIR__ . '/Fixture/composer-with-numeric-scripts.json'); + + $eventMock = $this->mock(Event::class); + $eventMock->shouldReceive('stopPropagation') + ->never(); + + $containerMock = $this->mock(ContainerContract::class); + $containerMock->shouldReceive('get') + ->never() + ->with(ScriptExecutor::class); + $containerMock->shouldReceive('get') + ->never() + ->with(Lock::class); + + $this->automatic->setContainer($containerMock); + $this->automatic->executeAutoScripts($eventMock); + + \putenv('COMPOSER='); + \putenv('COMPOSER'); + } + public function testExecuteAutoScriptsWithoutScripts(): void { $eventMock = $this->mock(Event::class); $eventMock->shouldReceive('stopPropagation') - ->once(); + ->never(); $this->ioMock->shouldReceive('write') ->once() diff --git a/tests/Automatic/Fixture/composer-with-numeric-scripts.json b/tests/Automatic/Fixture/composer-with-numeric-scripts.json new file mode 100644 index 00000000..a53291fc --- /dev/null +++ b/tests/Automatic/Fixture/composer-with-numeric-scripts.json @@ -0,0 +1,21 @@ +{ + "name": "narrowspark/automatic", + "type": "composer-plugin", + "description": "Composer plugin for automatically project configuration and creation.", + "license": "MIT", + "config": { + "optimize-autoloader": true, + "preferred-install": "dist" + }, + "scripts": { + "auto-scripts": [ + "@php -r 'echo 1;'" + ], + "post-install-cmd": [ + "@auto-scripts" + ], + "post-update-cmd": [ + "@auto-scripts" + ] + } +}