Skip to content
This repository has been archived by the owner on Mar 1, 2023. It is now read-only.

Commit

Permalink
fixed automatic auto-scripts not to throw a exception if array is a n… (
Browse files Browse the repository at this point in the history
#102)

…umeric array

| Q               | A
| --------------- | ---
| Bug fix?        | yes
| New feature?    | no
| BC breaks?      | no
| Deprecations?   | no
| Related tickets | -
| License         | MIT
| Doc PR          | -
  • Loading branch information
prisis authored Dec 11, 2018
1 parent 066bcad commit 566bab0
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 21 deletions.
46 changes: 26 additions & 20 deletions src/Automatic/Automatic.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
25 changes: 24 additions & 1 deletion tests/Automatic/AutomaticTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
21 changes: 21 additions & 0 deletions tests/Automatic/Fixture/composer-with-numeric-scripts.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
}

0 comments on commit 566bab0

Please sign in to comment.