Skip to content

Commit e97fb92

Browse files
committed
Simplify task feedback in install steps
1 parent fb590fc commit e97fb92

File tree

2 files changed

+59
-18
lines changed

2 files changed

+59
-18
lines changed

src/Jade/JadeSymfonyEngine.php

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,16 @@ public function offsetUnset($name)
356356
unset($this->helpers[$name]);
357357
}
358358

359+
public static function proceedTask(&$flags, $io, $taskResult, $flag, $successMessage, $message)
360+
{
361+
if ($taskResult) {
362+
$flags |= $flag;
363+
$message = $successMessage;
364+
}
365+
366+
$io->write($message);
367+
}
368+
359369
public static function install($event, $dir = null)
360370
{
361371
/** @var \Composer\Script\Event $event */
@@ -387,6 +397,10 @@ public static function install($event, $dir = null)
387397
$addConfig = $io->askConfirmation('Would you like us to add automatically needed settings in your config.yml? [Y/N] ');
388398
$addBundle = $io->askConfirmation('Would you like us to add automatically the pug bundle in your AppKernel.php? [Y/N] ');
389399

400+
$proceedTask = function ($taskResult, $flag, $successMessage, $errorMessage) use (&$flags, $io) {
401+
static::proceedTask($flags, $io, $taskResult, $flag, $successMessage, $errorMessage);
402+
};
403+
390404
if ($addConfig) {
391405
$configFile = $dir . '/app/config/config.yml';
392406
$contents = @file_get_contents($configFile) ?: '';
@@ -397,12 +411,12 @@ public static function install($event, $dir = null)
397411
$contents = preg_replace('/^framework\s*:/m', "services:\n\$0", $contents);
398412
}
399413
$contents = preg_replace('/^services\s*:/m', "\$0$service", $contents);
400-
if (file_put_contents($configFile, $contents)) {
401-
$flags |= static::CONFIG_OK;
402-
$io->write('Engine service added in config.yml');
403-
} else {
404-
$io->write('Unable to add the engine service in config.yml');
405-
}
414+
$proceedTask(
415+
file_put_contents($configFile, $contents),
416+
static::CONFIG_OK,
417+
'Engine service added in config.yml',
418+
'Unable to add the engine service in config.yml'
419+
);
406420
} else {
407421
$flags |= static::CONFIG_OK;
408422
$io->write('templating.engine.pug setting in config.yml already exists.');
@@ -452,12 +466,12 @@ public static function install($event, $dir = null)
452466
}
453467
if ($proceeded) {
454468
$contents = implode("\n", $lines);
455-
if (file_put_contents($configFile, $contents)) {
456-
$flags |= static::ENGINE_OK;
457-
$io->write('Engine added to framework.templating.engines in config.yml');
458-
} else {
459-
$io->write('Unable to add the templating engine in framework.templating.engines in config.yml');
460-
}
469+
$proceedTask(
470+
file_put_contents($configFile, $contents),
471+
static::ENGINE_OK,
472+
'Engine added to framework.templating.engines in config.yml',
473+
'Unable to add the templating engine in framework.templating.engines in config.yml'
474+
);
461475
}
462476
} else {
463477
$io->write('framework entry not found in config.yml.');
@@ -473,12 +487,12 @@ public static function install($event, $dir = null)
473487
if (preg_match('/^[ \\t]*new\\s+Symfony\\\\Bundle\\\\FrameworkBundle\\\\FrameworkBundle\\(\\)/m', $contents)) {
474488
if (strpos($contents, $bundle) === false) {
475489
$contents = preg_replace('/^([ \\t]*)new\\s+Symfony\\\\Bundle\\\\FrameworkBundle\\\\FrameworkBundle\\(\\)/m', "\$0,\n\$1$bundle", $contents);
476-
if (file_put_contents($appFile, $contents)) {
477-
$flags |= static::KERNEL_OK;
478-
$io->write('Bundle added to AppKernel.php');
479-
} else {
480-
$io->write('Unable to add the bundle engine in AppKernel.php');
481-
}
490+
$proceedTask(
491+
file_put_contents($appFile, $contents),
492+
static::KERNEL_OK,
493+
'Bundle added to AppKernel.php',
494+
'Unable to add the bundle engine in AppKernel.php'
495+
);
482496
} else {
483497
$flags |= static::KERNEL_OK;
484498
$io->write('The bundle already exists in AppKernel.php');

tests/Pug/PugSymfonyBundle/Command/AssetsPublishCommandTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,20 @@
22

33
namespace Pug\Tests\PugSymfonyBundle\Command;
44

5+
use Jade\JadeSymfonyEngine;
56
use Pug\PugSymfonyBundle\Command\AssetsPublishCommand;
67
use Symfony\Bundle\FrameworkBundle\Console\Application;
78
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
89
use Symfony\Component\Console\Tester\CommandTester;
910

11+
class BadEngine extends JadeSymfonyEngine
12+
{
13+
public function getEngine()
14+
{
15+
return (object) array();
16+
}
17+
}
18+
1019
class AssetsPublishCommandTest extends KernelTestCase
1120
{
1221
public function setUp()
@@ -48,4 +57,22 @@ public function testCommand()
4857
$this->assertRegExp('/(Unknown\sfilter\supper|upper:\sFilter\sdoes\s?n[\'o]t\sexists)/', $output, 'filter.pug fails as the upper filter does not exists.');
4958
$this->assertContains('filter.pug', $output, 'filter.pug fails as the upper filter does not exists.');
5059
}
60+
61+
/**
62+
* @expectedException \InvalidArgumentException
63+
* @expectedExceptionMessage Allowed pug engine are Jade\Jade, Pug\Pug or Phug\Renderer, stdClass given.
64+
*/
65+
public function testCommandException()
66+
{
67+
$application = new Application(self::$kernel);
68+
self::$kernel->getContainer()->set('templating.engine.pug', new BadEngine(self::$kernel));
69+
$application->add(new AssetsPublishCommand());
70+
71+
$command = $application->find('assets:publish');
72+
$commandTester = new CommandTester($command);
73+
$commandTester->execute([
74+
'command' => $command->getName(),
75+
'--env' => 'prod',
76+
]);
77+
}
5178
}

0 commit comments

Comments
 (0)