Skip to content

Commit

Permalink
Add a facade to call Phug static methods
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Oct 18, 2017
1 parent 3090a03 commit 801c8a3
Show file tree
Hide file tree
Showing 17 changed files with 219 additions and 174 deletions.
3 changes: 3 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
<exclude>tests/features/issues.php</exclude>
<exclude>tests/features/exceptions.php</exclude>
</testsuite>
<testsuite name="Facade">
<file>tests/features/facade.php</file>
</testsuite>
<testsuite name="Compiler">
<directory suffix=".php">tests/nodes</directory>
<directory suffix=".php">tests/compiler</directory>
Expand Down
26 changes: 26 additions & 0 deletions src/Pug/Facade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Pug;

use Phug\Phug;

/**
* Class Pug\Pug.
*/
class Facade
{
/**
* Set Pug\Pug as the default Phug rendering engine then call static method through the Phug facade.
*
* @param string $name
* @param array $arguments
*
* @return mixed
*/
public static function __callStatic($name, $arguments)
{
Pug::init();

return call_user_func_array([Phug::class, $name], $arguments);
}
}
6 changes: 3 additions & 3 deletions tests/example/example.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,22 @@ protected function getExpectedFile($file)
*/
public function testIndex()
{
$this->assertSame($this->getExpectedFile('index'), $this->browse());
self::assertSame($this->getExpectedFile('index'), $this->browse());
}

/**
* Test server example login page
*/
public function testLogin()
{
$this->assertSame($this->getExpectedFile('login'), $this->browse('login'));
self::assertSame($this->getExpectedFile('login'), $this->browse('login'));
}

/**
* Test server example cities page
*/
public function testCities()
{
$this->assertSame($this->getExpectedFile('cities'), $this->browse('cities'));
self::assertSame($this->getExpectedFile('cities'), $this->browse('cities'));
}
}
26 changes: 13 additions & 13 deletions tests/features/cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ public function testStringInputCache()
'debug' => false,
'cache' => $dir
));
$this->assertSame(0, $pug->getCompilationsCount(), 'Should have done no compilations yet');
self::assertSame(0, $pug->getCompilationsCount(), 'Should have done no compilations yet');
$pug->render("header\n h1#foo Hello World!\nfooter");
$this->assertSame(1, $pug->getCompilationsCount(), 'Should have done 1 compilation');
self::assertSame(1, $pug->getCompilationsCount(), 'Should have done 1 compilation');
$pug->render("header\n h1#foo Hello World!\nfooter");
$this->assertSame(1, $pug->getCompilationsCount(), 'Should have done always 1 compilation because the code is cached');
self::assertSame(1, $pug->getCompilationsCount(), 'Should have done always 1 compilation because the code is cached');
$pug->render("header\n h1#foo Hello World2\nfooter");
$this->assertSame(2, $pug->getCompilationsCount(), 'Should have done always 2 compilations because the code changed');
self::assertSame(2, $pug->getCompilationsCount(), 'Should have done always 2 compilations because the code changed');
$this->emptyDirectory($dir);
}

Expand All @@ -111,14 +111,14 @@ public function testFileCache()
'debug' => false,
'cache' => $dir
));
$this->assertSame(0, $pug->getCompilationsCount(), 'Should have done no compilations yet');
self::assertSame(0, $pug->getCompilationsCount(), 'Should have done no compilations yet');
$pug->renderFile($test);
$this->assertSame(1, $pug->getCompilationsCount(), 'Should have done 1 compilation');
self::assertSame(1, $pug->getCompilationsCount(), 'Should have done 1 compilation');
$pug->renderFile($test);
$this->assertSame(1, $pug->getCompilationsCount(), 'Should have done always 1 compilation because the code is cached');
self::assertSame(1, $pug->getCompilationsCount(), 'Should have done always 1 compilation because the code is cached');
file_put_contents($test, "header\n h1#foo Hello World2\nfooter");
$pug->renderFile($test);
$this->assertSame(2, $pug->getCompilationsCount(), 'Should have done always 2 compilations because the code changed');
self::assertSame(2, $pug->getCompilationsCount(), 'Should have done always 2 compilations because the code changed');
$this->emptyDirectory($dir);
if (file_exists($test)) {
unlink($test);
Expand Down Expand Up @@ -197,11 +197,11 @@ private function cacheSystem($keepBaseName)
}, array_filter(scandir($cacheDirectory), function ($file) {
return substr($file, -4) === '.php';
})));
$this->assertSame(1, count($phpFiles), 'The cached file should now exist.');
self::assertSame(1, count($phpFiles), 'The cached file should now exist.');
$cachedFile = realpath($phpFiles[0]);
$this->assertFalse(!$cachedFile, 'The cached file should now exist.');
self::assertFalse(!$cachedFile, 'The cached file should now exist.');
$containsBase = strpos($cachedFile, $base) !== false;
$this->assertTrue($containsBase === $keepBaseName, 'The cached file name should contains base name if keepBaseName is true.');
self::assertTrue($containsBase === $keepBaseName, 'The cached file name should contains base name if keepBaseName is true.');
if (file_exists($cachedFile)) {
unlink($cachedFile);
}
Expand Down Expand Up @@ -258,7 +258,7 @@ public function testCacheDirectory()
$this->emptyDirectory($cacheDirectory);
rmdir($cacheDirectory);

$this->assertSame($expectedCount, $success + $errors, 'Each .pug file in the directory to cache should generate a success or an error.');
$this->assertSame($success, $filesCount, 'Each file successfully cached should be in the cache directory.');
self::assertSame($expectedCount, $success + $errors, 'Each .pug file in the directory to cache should generate a success or an error.');
self::assertSame($success, $filesCount, 'Each file successfully cached should be in the cache directory.');
}
}
10 changes: 5 additions & 5 deletions tests/features/expressionLanguage.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ public function testJsExpression()
]);

$actual = trim($pug->render("- a = 2\n- b = 4\n- c = b * a\np=a * b\np=c"));
$this->assertSame('<p>8</p><p>8</p>', $actual);
self::assertSame('<p>8</p><p>8</p>', $actual);

$actual = trim($pug->render("- a = 2\n- b = 4\np=a + b"));
$this->assertSame('<p>6</p>', $actual);
self::assertSame('<p>6</p>', $actual);

$actual = trim($pug->render("- a = '2'\n- b = 4\np=a + b"));
$this->assertSame('<p>24</p>', $actual);
self::assertSame('<p>24</p>', $actual);

$actual = trim($pug->render("mixin test\n div&attributes(attributes)\nbody\n +test()(class='test')"));
$this->assertSame('<body><div class="test"></div></body>', $actual);
self::assertSame('<body><div class="test"></div></body>', $actual);
}

public function testJsLanguageOptions()
Expand All @@ -39,6 +39,6 @@ public function testJsLanguageOptions()
$actual = trim($pug->render('=a.ho', [
'a' => 'hi '
]));
$this->assertSame('hi ho', $actual);
self::assertSame('hi ho', $actual);
}
}
16 changes: 16 additions & 0 deletions tests/features/facade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

use Pug\Facade as Pug;

class FacadeTest extends PHPUnit_Framework_TestCase
{
public function testFacade()
{
$html = Pug::render('p=foo', ['foo' => 'bar']);
self::assertSame('<p>bar</p>', $html);

Pug::share('pi', 3.14);
$html = Pug::render('=2 * pi');
self::assertSame('6.28', $html);
}
}
54 changes: 27 additions & 27 deletions tests/features/filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ public function testFilter()
$pug = new Pug([
'debug' => true,
]);
$this->assertSame("<?php\nfoo\n?>", call_user_func($pug->getFilter('php'), 'foo'));
$this->assertFalse($pug->hasFilter('text'));
self::assertSame("<?php\nfoo\n?>", call_user_func($pug->getFilter('php'), 'foo'));
self::assertFalse($pug->hasFilter('text'));
$pug->filter('text', function($code) {
return strip_tags($code);
});
$this->assertTrue($pug->hasFilter('text'));
self::assertTrue($pug->hasFilter('text'));
$actual = $pug->render('
div
p
Expand All @@ -46,7 +46,7 @@ public function testFilter()
$expected = str_replace(' ', '', $expected);
$actual = str_replace([' ', "\n"], '', $actual);

$this->assertSame($expected, $actual, 'Custom filter');
self::assertSame($expected, $actual, 'Custom filter');
}

/**
Expand All @@ -59,7 +59,7 @@ public function testNonCallableFilter()
$pug = new Pug([
'debug' => true,
]);
$this->assertFalse($pug->hasFilter('bar'));
self::assertFalse($pug->hasFilter('bar'));
$pug->filter('bar', 'nonexists');
}

Expand All @@ -72,23 +72,23 @@ public function testFilterAutoload()
'debug' => true,
'filterAutoLoad' => false,
]);
$this->assertFalse($pug->hasFilter('foo-bar'));
self::assertFalse($pug->hasFilter('foo-bar'));
spl_autoload_register(function ($name) {
$name = explode('\\', $name);
$file = __DIR__ . '/../lib/' . end($name) . 'Filter.php';
if (file_exists($file)) {
include_once $file;
}
});
$this->assertFalse($pug->hasFilter('foo-bar'));
$this->assertSame($pug->getFilter('foo-bar'), null);
self::assertFalse($pug->hasFilter('foo-bar'));
self::assertSame($pug->getFilter('foo-bar'), null);

$pug = new Pug([
'debug' => true,
'filterAutoLoad' => true,
]);
$this->assertTrue($pug->hasFilter('foo-bar'));
$this->assertSame('I\'M SO TALL :)', call_user_func($pug->getFilter('foo-bar'), 'I\'m so small :('));
self::assertTrue($pug->hasFilter('foo-bar'));
self::assertSame('I\'M SO TALL :)', call_user_func($pug->getFilter('foo-bar'), 'I\'m so small :('));
$actual = $pug->render('
div
p
Expand All @@ -97,7 +97,7 @@ public function testFilterAutoload()
');
$expected = '<div><p>I\'M SO TALL :)</p></div>';

$this->assertSame(preg_replace('`\s`', '', $expected), preg_replace('`\s`', '', $actual), 'Autoloaded filter');
self::assertSame(preg_replace('`\s`', '', $expected), preg_replace('`\s`', '', $actual), 'Autoloaded filter');
}

/**
Expand All @@ -110,7 +110,7 @@ public function testFilterAutoloadWhenClassDoNotExist()
$pug = new Pug([
'debug' => true,
]);
$this->assertFalse($pug->hasFilter('bar-foo'));
self::assertFalse($pug->hasFilter('bar-foo'));
$pug->render('
div
p
Expand Down Expand Up @@ -139,13 +139,13 @@ public function testInlineFilter()
');
$expected = '/^<h1>BAR-\s+foo\s+-BAR<\/h1>$/';

$this->assertRegExp($expected, $actual, 'One-line filter');
self::assertRegExp($expected, $actual, 'One-line filter');

$actual = $pug->render('h1 BAR-#[:lower FOO]-BAR');

$expected = '/^<h1>BAR-\s+foo\s+-BAR<\/h1>$/';

$this->assertRegExp($expected, $actual, 'In-line filter');
self::assertRegExp($expected, $actual, 'In-line filter');
}

/**
Expand All @@ -165,13 +165,13 @@ public function testParseMethod()
');
$expected = '/^<h1>BAR-\s+foo\s+-BAR<\/h1>$/';

$this->assertRegExp($expected, $actual, 'One-line filter');
self::assertRegExp($expected, $actual, 'One-line filter');

$actual = $pug->render('h1 BAR-#[:lower FOO]-BAR');

$expected = '/^<h1>BAR-\s+foo\s+-BAR<\/h1>$/';

$this->assertRegExp($expected, $actual, 'In-line filter');
self::assertRegExp($expected, $actual, 'In-line filter');
}

/**
Expand All @@ -181,7 +181,7 @@ public function testCData()
{
$filter = new \Pug\Filter\Cdata();

$this->assertSame("<![CDATA[\nfoo\n]]>", $filter('foo'));
self::assertSame("<![CDATA[\nfoo\n]]>", $filter('foo'));
}

/**
Expand All @@ -192,7 +192,7 @@ public function testParseAutoload()
include_once __DIR__ . '/../lib/AutoloadParseFilter.php';
$pug = new Pug();

$this->assertSame('foobar', $pug->render(':autoload-parse-filter'));
self::assertSame('foobar', $pug->render(':autoload-parse-filter'));
}

/**
Expand All @@ -202,7 +202,7 @@ public function testWrapInTag()
{
$filter = new SpecialScript();

$this->assertSame('<script>foo</script>', $filter->pugInvoke('foo'));
self::assertSame('<script>foo</script>', $filter->pugInvoke('foo'));
}

/**
Expand All @@ -223,7 +223,7 @@ public function testPhpFilterWithoutPrettyprint()
');
$expected = '/^<h1>BAR-\s+42\s+-BAR<\/h1>$/';

$this->assertRegExp($expected, $actual, 'Block filter');
self::assertRegExp($expected, $actual, 'Block filter');

$actual = $pug->render('
h1
Expand All @@ -234,7 +234,7 @@ public function testPhpFilterWithoutPrettyprint()
');
$expected = '<h1><span>BAR-</span>42<span>-BAR</span></h1>';

$this->assertSame($expected, $actual, 'Block filter and span');
self::assertSame($expected, $actual, 'Block filter and span');

$actual = $pug->render('
h1
Expand All @@ -244,13 +244,13 @@ public function testPhpFilterWithoutPrettyprint()
');
$expected = '/^<h1>BAR-\s+42\s+-BAR<\/h1>$/';

$this->assertRegExp($expected, $actual, 'One-line filter');
self::assertRegExp($expected, $actual, 'One-line filter');

$actual = $pug->render('h1 BAR-#[:php echo 6 * 7]-BAR');

$expected = '/^<h1>BAR-\s+42\s+-BAR<\/h1>$/';

$this->assertRegExp($expected, $actual, 'In-line filter');
self::assertRegExp($expected, $actual, 'In-line filter');
}

/**
Expand All @@ -272,7 +272,7 @@ public function testPhpFilterWithPrettyprint()
'));
$expected = '/^<h1>BAR-\s+42\s+-BAR<\/h1>$/';

$this->assertRegExp($expected, $actual, 'Block filter');
self::assertRegExp($expected, $actual, 'Block filter');

$actual = trim($pug->render('
h1
Expand All @@ -283,7 +283,7 @@ public function testPhpFilterWithPrettyprint()
'));
$expected = '/^<h1><span>BAR-<\/span>42<span>-BAR<\/span><\/h1>$/';

$this->assertRegExp($expected, $actual, 'Block filter and span');
self::assertRegExp($expected, $actual, 'Block filter and span');

$actual = trim($pug->render('
h1
Expand All @@ -293,11 +293,11 @@ public function testPhpFilterWithPrettyprint()
'));
$expected = '/^<h1>BAR-\s+42\s+-BAR<\/h1>$/';

$this->assertRegExp($expected, $actual, 'One-line filter');
self::assertRegExp($expected, $actual, 'One-line filter');

$actual = trim($pug->render('h1 BAR-#[:php echo 6 * 7]-BAR'));
$expected = '/^<h1>BAR-\s+42\s+-BAR<\/h1>$/';

$this->assertRegExp($expected, $actual, 'In-line filter');
self::assertRegExp($expected, $actual, 'In-line filter');
}
}
Loading

0 comments on commit 801c8a3

Please sign in to comment.