Skip to content

Commit b4873ae

Browse files
committed
Unit tests for install command
1 parent da85a33 commit b4873ae

File tree

4 files changed

+273
-16
lines changed

4 files changed

+273
-16
lines changed

src/Jade/JadeSymfonyEngine.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ class JadeSymfonyEngine implements EngineInterface, \ArrayAccess
2020
protected $assets;
2121
protected $kernel;
2222

23+
const CONFIG_OK = 1;
24+
const ENGINE_OK = 2;
25+
const KERNEL_OK = 4;
26+
2327
public function __construct($kernel)
2428
{
2529
if (empty($kernel) || !($kernel instanceof Kernel)) {
@@ -290,7 +294,7 @@ public function offsetUnset($name)
290294
unset($this->helpers[$name]);
291295
}
292296

293-
public static function install($event)
297+
public static function install($event, $dir = null)
294298
{
295299
/** @var \Composer\Script\Event $event */
296300
$io = $event->getIO();
@@ -300,18 +304,14 @@ public static function install($event)
300304
return true;
301305
}
302306

303-
$dir = $baseDirectory . '/../../..';
307+
$dir = $dir ?: $baseDirectory . '/../../..';
304308

305309
$service = "\n templating.engine.pug:\n" .
306310
" class: Pug\PugSymfonyEngine\n" .
307311
" arguments: [\"@kernel\"]\n";
308312

309313
$bundle = 'new Pug\PugSymfonyBundle\PugSymfonyBundle()';
310314

311-
define('CONFIG_OK', 1);
312-
define('ENGINE_OK', 2);
313-
define('KERNEL_OK', 4);
314-
315315
$flags = 0;
316316
$addConfig = $io->askConfirmation('Would you like us to add automatically needed settings in your config.yml? [Y/N] ');
317317
$addBundle = $io->askConfirmation('Would you like us to add automatically the pug bundle in your AppKernel.php? [Y/N] ');
@@ -327,13 +327,13 @@ public static function install($event)
327327
}
328328
$contents = preg_replace('/^services\s*:/m', "\$0$service", $contents);
329329
if (file_put_contents($configFile, $contents)) {
330-
$flags |= CONFIG_OK;
330+
$flags |= static::CONFIG_OK;
331331
$io->write('Engine service added in config.yml');
332332
} else {
333333
$io->write('Unable to add the engine service in config.yml');
334334
}
335335
} else {
336-
$flags |= CONFIG_OK;
336+
$flags |= static::CONFIG_OK;
337337
$io->write('templating.engine.pug setting in config.yml already exists.');
338338
}
339339
$lines = explode("\n", $contents);
@@ -368,7 +368,7 @@ public static function install($event)
368368
break;
369369
}
370370
if (in_array('pug', $engines)) {
371-
$flags |= ENGINE_OK;
371+
$flags |= static::ENGINE_OK;
372372
$io->write('Pug engine already exist in framework.templating.engines in config.yml.');
373373

374374
break;
@@ -382,7 +382,7 @@ public static function install($event)
382382
if ($proceeded) {
383383
$contents = implode("\n", $lines);
384384
if (file_put_contents($configFile, $contents)) {
385-
$flags |= ENGINE_OK;
385+
$flags |= static::ENGINE_OK;
386386
$io->write('Engine added to framework.templating.engines in config.yml');
387387
} else {
388388
$io->write('Unable to add the templating engine in framework.templating.engines in config.yml');
@@ -392,7 +392,7 @@ public static function install($event)
392392
$io->write('framework entry not found in config.yml.');
393393
}
394394
} else {
395-
$flags |= CONFIG_OK | ENGINE_OK;
395+
$flags |= static::CONFIG_OK | static::ENGINE_OK;
396396
}
397397

398398
if ($addBundle) {
@@ -403,23 +403,23 @@ public static function install($event)
403403
if (strpos($contents, $bundle) === false) {
404404
$contents = preg_replace('/^([ \\t]*)new\\s+Symfony\\\\Bundle\\\\FrameworkBundle\\\\FrameworkBundle\\(\\)/m', "\$0,\n\$1$bundle", $contents);
405405
if (file_put_contents($appFile, $contents)) {
406-
$flags |= KERNEL_OK;
406+
$flags |= static::KERNEL_OK;
407407
$io->write('Bundle added to AppKernel.php');
408408
} else {
409409
$io->write('Unable to add the bundle engine in AppKernel.php');
410410
}
411411
} else {
412-
$flags |= KERNEL_OK;
412+
$flags |= static::KERNEL_OK;
413413
$io->write('The bundle already exists in AppKernel.php');
414414
}
415415
} else {
416416
$io->write('Sorry, AppKernel.php has a format we can\'t handle automatically.');
417417
}
418418
} else {
419-
$flags |= KERNEL_OK;
419+
$flags |= static::KERNEL_OK;
420420
}
421421

422-
if (($flags & KERNEL_OK) && ($flags & CONFIG_OK) && ($flags & ENGINE_OK)) {
422+
if (($flags & static::KERNEL_OK) && ($flags & static::CONFIG_OK) && ($flags & static::ENGINE_OK)) {
423423
touch($baseDirectory . '/installed');
424424
}
425425

tests/Pug/CaptureIO.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace Pug\Tests;
4+
5+
use Composer\IO\NullIO;
6+
7+
class CaptureIO extends NullIO
8+
{
9+
protected $lastMsgs = [];
10+
protected $errored;
11+
protected $interactive = false;
12+
protected $permissive = false;
13+
14+
public function reset()
15+
{
16+
$this->lastMsgs = [];
17+
}
18+
19+
public function setInteractive($interactive)
20+
{
21+
$this->interactive = $interactive;
22+
}
23+
24+
public function setPermissive($permissive)
25+
{
26+
$this->permissive = $permissive;
27+
}
28+
29+
public function askConfirmation($question, $default = true)
30+
{
31+
return $this->permissive;
32+
}
33+
34+
public function isInteractive()
35+
{
36+
return $this->interactive;
37+
}
38+
39+
public function write($msg, $newline = true, $verbosity = self::NORMAL)
40+
{
41+
$this->lastMsgs[] = $msg;
42+
$this->errored = false;
43+
}
44+
45+
public function writeError($msg, $newline = true, $verbosity = self::NORMAL)
46+
{
47+
$this->lastMsgs[] = $msg;
48+
$this->errored = true;
49+
}
50+
51+
public function isErrored()
52+
{
53+
return $this->errored;
54+
}
55+
56+
public function getLastOutput()
57+
{
58+
return $this->lastMsgs;
59+
}
60+
}

tests/Pug/PugSymfonyEngineTest.php

Lines changed: 182 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Pug\Tests;
44

5+
use Composer\Composer;
6+
use Composer\Script\Event;
57
use Jade\Compiler;
68
use Jade\Filter\AbstractFilter;
79
use Jade\Nodes\Filter;
@@ -10,7 +12,6 @@
1012
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
1113
use Symfony\Bundle\SecurityBundle\Templating\Helper\LogoutUrlHelper as BaseLogoutUrlHelper;
1214
use Symfony\Component\Filesystem\Filesystem;
13-
use Symfony\Component\HttpKernel\Kernel;
1415
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage as BaseTokenStorage;
1516
use Symfony\Component\Security\Http\Logout\LogoutUrlGenerator as BaseLogoutUrlGenerator;
1617

@@ -306,4 +307,184 @@ public function testIssue11BackgroundImage()
306307

307308
self::assertSame('<div style="background-image: url(foo);" class="slide"></div>', $html);
308309
}
310+
311+
/**
312+
* @group install
313+
*/
314+
public function testInstall()
315+
{
316+
include_once __DIR__ . '/CaptureIO.php';
317+
$io = new CaptureIO();
318+
$composer = new Composer();
319+
$installedFile = __DIR__ . '/../../installed';
320+
touch($installedFile);
321+
322+
self::assertTrue(PugSymfonyEngine::install(new Event('install', $composer, $io)));
323+
324+
unlink($installedFile);
325+
$io->setInteractive(true);
326+
327+
self::assertTrue(PugSymfonyEngine::install(new Event('install', $composer, $io)));
328+
self::assertTrue(file_exists($installedFile));
329+
330+
unlink($installedFile);
331+
$io->setPermissive(true);
332+
$io->reset();
333+
$dir = sys_get_temp_dir() . '/pug-temp';
334+
$fs = new Filesystem();
335+
$fs->remove($dir);
336+
337+
self::assertTrue(PugSymfonyEngine::install(new Event('install', $composer, $io), $dir));
338+
self::assertSame([
339+
'framework entry not found in config.yml.',
340+
'Sorry, AppKernel.php has a format we can\'t handle automatically.',
341+
], $io->getLastOutput());
342+
clearstatcache();
343+
self::assertFalse(file_exists($installedFile));
344+
345+
foreach (['/app/config/config.yml', '/app/AppKernel.php'] as $file) {
346+
$fs->copy(__DIR__ . '/../project' . $file, $dir . $file);
347+
}
348+
$io->reset();
349+
350+
self::assertTrue(PugSymfonyEngine::install(new Event('install', $composer, $io), $dir));
351+
self::assertSame([
352+
'templating.engine.pug setting in config.yml already exists.',
353+
'Pug engine already exist in framework.templating.engines in config.yml.',
354+
'The bundle already exists in AppKernel.php',
355+
], $io->getLastOutput());
356+
clearstatcache();
357+
self::assertTrue(file_exists($installedFile));
358+
359+
unlink($installedFile);
360+
file_put_contents($dir . '/app/config/config.yml', str_replace(
361+
['pug', 'services:'],
362+
['x', 'x:'],
363+
file_get_contents($dir . '/app/config/config.yml')
364+
));
365+
file_put_contents($dir . '/app/AppKernel.php', str_replace(
366+
'Pug',
367+
'X',
368+
file_get_contents($dir . '/app/AppKernel.php')
369+
));
370+
$io->reset();
371+
372+
self::assertTrue(PugSymfonyEngine::install(new Event('install', $composer, $io), $dir));
373+
self::assertSame([
374+
'Engine service added in config.yml',
375+
'Engine added to framework.templating.engines in config.yml',
376+
'Bundle added to AppKernel.php',
377+
], $io->getLastOutput());
378+
self::assertContains(
379+
'Pug\PugSymfonyBundle\PugSymfonyBundle()',
380+
file_get_contents($dir . '/app/AppKernel.php')
381+
);
382+
self::assertContains(
383+
'templating.engine.pug',
384+
file_get_contents($dir . '/app/config/config.yml')
385+
);
386+
clearstatcache();
387+
self::assertTrue(file_exists($installedFile));
388+
389+
$io->reset();
390+
unlink($installedFile);
391+
file_put_contents($dir . '/app/config/config.yml', implode("\n", [
392+
'foo:',
393+
' bar: biz',
394+
'framework:',
395+
' bar1: biz',
396+
' templating:',
397+
' bar2: biz',
398+
' engines:',
399+
' - pug',
400+
' - php',
401+
' bar3: biz',
402+
' bar4: biz',
403+
'bar: biz',
404+
]));
405+
self::assertTrue(PugSymfonyEngine::install(new Event('install', $composer, $io), $dir));
406+
self::assertSame([
407+
'Engine service added in config.yml',
408+
'Automatic engine adding is only possible if framework.templating.engines is a '.
409+
'one-line setting in config.yml.',
410+
'The bundle already exists in AppKernel.php',
411+
], $io->getLastOutput());
412+
clearstatcache();
413+
self::assertFalse(file_exists($installedFile));
414+
415+
file_put_contents($dir . '/app/config/config.yml', implode("\n", [
416+
'foo:',
417+
' bar: biz',
418+
'framework:',
419+
' bar1: biz',
420+
' templating:',
421+
' bar2: biz',
422+
' templating:',
423+
' bar2: biz',
424+
' engines: ["twig","php"]',
425+
' bar3: biz',
426+
' engines: ["twig","php"]',
427+
' bar4: biz',
428+
'bar: biz',
429+
]));
430+
PugSymfonyEngine::install(new Event('install', $composer, $io), $dir);
431+
self::assertSame(implode("\n", [
432+
'foo:',
433+
' bar: biz',
434+
'services:',
435+
' templating.engine.pug:',
436+
' class: Pug\PugSymfonyEngine',
437+
' arguments: ["@kernel"]',
438+
'',
439+
'framework:',
440+
' bar1: biz',
441+
' templating:',
442+
' bar2: biz',
443+
' templating:',
444+
' bar2: biz',
445+
' engines: ["pug","twig","php"]',
446+
' bar3: biz',
447+
' engines: ["twig","php"]',
448+
' bar4: biz',
449+
'bar: biz',
450+
]), file_get_contents($dir . '/app/config/config.yml'));
451+
self::assertTrue(file_exists($installedFile));
452+
unlink($installedFile);
453+
454+
file_put_contents($dir . '/app/config/config.yml', implode("\n", [
455+
'foo:',
456+
' bar: biz',
457+
'framework:',
458+
' bar1: biz',
459+
' templating:',
460+
' bar2: biz',
461+
' templating:',
462+
' bar2: biz',
463+
' bar3: biz',
464+
'bar:',
465+
' engines: ["twig","php"]',
466+
' bar4: biz',
467+
]));
468+
PugSymfonyEngine::install(new Event('install', $composer, $io), $dir);
469+
self::assertSame(implode("\n", [
470+
'foo:',
471+
' bar: biz',
472+
'services:',
473+
' templating.engine.pug:',
474+
' class: Pug\PugSymfonyEngine',
475+
' arguments: ["@kernel"]',
476+
'',
477+
'framework:',
478+
' bar1: biz',
479+
' templating:',
480+
' bar2: biz',
481+
' templating:',
482+
' bar2: biz',
483+
' bar3: biz',
484+
'bar:',
485+
' engines: ["twig","php"]',
486+
' bar4: biz',
487+
]), file_get_contents($dir . '/app/config/config.yml'));
488+
self::assertFalse(file_exists($installedFile));
489+
}
309490
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
framework:
2+
secret: secret
3+
test: ~
4+
router: { resource: "%kernel.root_dir%/routing.yml" }
5+
form: true
6+
csrf_protection: true
7+
validation: { enable_annotations: true }
8+
templating:
9+
engines: ['pug', 'php']
10+
session:
11+
storage_id: session.storage.filesystem
12+
13+
services:
14+
templating.engine.pug:
15+
class: Pug\PugSymfonyEngine
16+
arguments: ["@kernel"]

0 commit comments

Comments
 (0)