Skip to content

Commit

Permalink
Merge pull request #13 from fullpipe/add-test
Browse files Browse the repository at this point in the history
Add more tests
  • Loading branch information
fullpipe authored Dec 27, 2019
2 parents fb56bf1 + 542aca9 commit b9585d4
Show file tree
Hide file tree
Showing 11 changed files with 228 additions and 37 deletions.
44 changes: 44 additions & 0 deletions .php_cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

$finder = PhpCsFixer\Finder::create()
->exclude('tests')
->in([__DIR__ . '/src']);

return PhpCsFixer\Config::create()
->setRules([
'@PSR1' => true,
'@PSR2' => true,
'@Symfony' => true,
'@DoctrineAnnotation' => true,
'psr4' => true,
'array_syntax' => ['syntax' => 'short'],
'mb_str_functions' => true,
'no_null_property_initialization' => true,
'no_php4_constructor' => true,
'no_short_echo_tag' => true,
'no_useless_else' => true,
'no_useless_return' => true,
'ordered_imports' => true,
'strict_comparison' => false,
'strict_param' => false,
'native_function_invocation' => true,
'phpdoc_add_missing_param_annotation' => true,
'phpdoc_types_order' => true,
'phpdoc_order' => true,
'no_superfluous_phpdoc_tags' => [
'allow_mixed' => true,
],
'method_argument_space' => [
'on_multiline' => 'ensure_fully_multiline',
],
'explicit_string_variable' => true,
'simple_to_complex_string_variable' => true,
// extra
'modernize_types_casting' => true,
'method_chaining_indentation' => true,
'linebreak_after_opening_tag' => true,
])
->setRiskyAllowed(true)
->setFinder($finder)
->setUsingCache(false)
;
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
language: php
php:
- '7.2'
- '7.3'

before_script:
- composer install
8 changes: 2 additions & 6 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
colors="true"
backupGlobals="false"
backupStaticAttributes="false"
timeoutForLargeTests="900">
<?xml version="1.0" encoding="UTF-8" ?>
<phpunit bootstrap="vendor/autoload.php" colors="true" backupGlobals="false" backupStaticAttributes="false" timeoutForLargeTests="900">
<testsuites>
<testsuite name="Tests">
<directory suffix="Test.php">tests</directory>
Expand Down
47 changes: 26 additions & 21 deletions src/Fullpipe/TwigWebpackExtension/TokenParser/EntryTokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,58 +9,63 @@

abstract class EntryTokenParser extends AbstractTokenParser
{
/**
* @var string
*/
protected $manifestFile;

/**
* @var string
*/
protected $publicPath;

abstract protected function type();
/**
* Entry extention.
*/
abstract protected function type(): string;

abstract protected function generateHtml($entryPath);
abstract protected function generateHtml(string $entryPath): string;

public function __construct($manifestFile, $publicPath)
public function __construct(string $manifestFile, string $publicPath)
{
$this->manifestFile = $manifestFile;
$this->publicPath = $publicPath;
}

/**
* {@inheritdoc}
*/
public function parse(Token $token)
{
$stream = $this->parser->getStream();
$entryName = $stream->expect(Token::STRING_TYPE)->getValue();
$stream->expect(Token::BLOCK_END_TYPE);

if (!file_exists($this->manifestFile)) {
throw new LoaderError(
'Webpack manifest file not exists.',
$token->getLine(),
$stream->getSourceContext()->getName()
);
if (!\file_exists($this->manifestFile)) {
throw new LoaderError('Webpack manifest file not exists.', $token->getLine(), $stream->getSourceContext());
}

$manifest = json_decode(file_get_contents($this->manifestFile), true);
$manifest = \json_decode(\file_get_contents($this->manifestFile), true);
$assets = [];

$manifestIndex = $entryName . '.' . $this->type();
$manifestIndex = $entryName.'.'.$this->type();

if (isset($manifest[$manifestIndex])) {
$entryPath = $this->publicPath . $manifest[$manifestIndex];
$entryPath = $this->publicPath.$manifest[$manifestIndex];

$assets[] = $this->generateHtml($entryPath);
} else {
throw new LoaderError(
'Webpack ' . $this->type() . ' entry ' . $entryName . ' not exists.',
$token->getLine(),
$stream->getSourceContext()->getName()
);
throw new LoaderError('Webpack '.$this->type().' entry '.$entryName.' not exists.', $token->getLine(), $stream->getSourceContext());
}

return new TextNode(implode('', $assets), $token->getLine());
return new TextNode(\implode('', $assets), $token->getLine());
}

/**
* @return string
* Get twig tag name.
*/
public function getTag()
public function getTag(): string
{
return 'webpack_entry_' . $this->type();
return 'webpack_entry_'.$this->type();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@

class EntryTokenParserCss extends EntryTokenParser
{
protected function type()
/**
* {@inheritdoc}
*/
protected function type(): string
{
return 'css';
}

protected function generateHtml($entryPath)
/**
* {@inheritdoc}
*/
protected function generateHtml(string $entryPath): string
{
return '<link type="text/css" href="' . $entryPath . '" rel="stylesheet">';
return '<link type="text/css" href="'.$entryPath.'" rel="stylesheet">';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@

class EntryTokenParserJs extends EntryTokenParser
{
protected function type()
/**
* {@inheritdoc}
*/
public function type(): string
{
return 'js';
}

protected function generateHtml($entryPath)
/**
* {@inheritdoc}
*/
protected function generateHtml(string $entryPath): string
{
return '<script type="text/javascript" src="' . $entryPath . '"></script>';
return '<script type="text/javascript" src="'.$entryPath.'"></script>';
}
}
15 changes: 13 additions & 2 deletions src/Fullpipe/TwigWebpackExtension/WebpackExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,22 @@

class WebpackExtension extends AbstractExtension
{
/**
* @var string
*/
protected $manifestFile;

/**
* @var string
*/
protected $publicPathJs;

/**
* @var string
*/
protected $publicPathCss;

public function __construct($manifestFile, $publicPathJs = '/js/', $publicPathCss = '/css/')
public function __construct(string $manifestFile, string $publicPathJs = '/js/', string $publicPathCss = '/css/')
{
$this->manifestFile = $manifestFile;
$this->publicPathJs = $publicPathJs;
Expand All @@ -28,7 +39,7 @@ public function getName()
}

/**
* @return array
* {@inheritdoc}
*/
public function getTokenParsers()
{
Expand Down
5 changes: 5 additions & 0 deletions tests/Resource/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"main.js": "main.js",
"main.css": "main.css",
"foo.png": "bar.png"
}
70 changes: 70 additions & 0 deletions tests/TokenParser/EntryTokenParserCssTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace tests\Fullpipe\TwigWebpackExtension\TokenParser;

use Fullpipe\TwigWebpackExtension\TokenParser\EntryTokenParser;
use Fullpipe\TwigWebpackExtension\TokenParser\EntryTokenParserCss;
use PHPUnit\Framework\TestCase;
use Twig\Environment;
use Twig\Error\LoaderError;
use Twig\Loader\LoaderInterface;
use Twig\Node\TextNode;
use Twig\Parser;
use Twig\Source;

class EntryTokenParserCssTest extends TestCase
{
public function testItIsAParser()
{
$this->assertInstanceOf(EntryTokenParser::class, new EntryTokenParserCss(__DIR__.'/../Resource/manifest.json', '/build/'));
}

public function testGenerate()
{
$env = $this->getEnv(__DIR__.'/../Resource/manifest.json', '/build/');
$parser = new Parser($env);
$source = new Source("{% webpack_entry_css 'main' %}", '');
$stream = $env->tokenize($source);

$expected = new TextNode('<link type="text/css" href="/build/main.css" rel="stylesheet">', 1);
$expected->setSourceContext($source);

$this->assertEquals(
$expected,
$parser->parse($stream)->getNode('body')->getNode('0')
);
}

public function testItThrowsExceptionIfNoManifest()
{
$this->expectException(LoaderError::class);

$env = $this->getEnv(__DIR__.'/../Resource/not_exists.json', '/build/');
$parser = new Parser($env);
$source = new Source("{% webpack_entry_css 'main' %}", '');
$stream = $env->tokenize($source);
$parser->parse($stream);
}

public function testItThrowsExceptionIfEntryNotExists()
{
$this->expectException(LoaderError::class);

$env = $this->getEnv(__DIR__.'/../Resource/manifest.json', '/build/');
$parser = new Parser($env);
$source = new Source("{% webpack_entry_css 'not_exists' %}", '');
$stream = $env->tokenize($source);
$parser->parse($stream);
}

private function getEnv(string $manifest, string $publicPath): Environment
{
$env = new Environment(
$this->getMockBuilder(LoaderInterface::class)->getMock(),
['cache' => false, 'autoescape' => false, 'optimizations' => 0]
);
$env->addTokenParser(new EntryTokenParserCss($manifest, $publicPath));

return $env;
}
}
41 changes: 41 additions & 0 deletions tests/TokenParser/EntryTokenParserJsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace tests\Fullpipe\TwigWebpackExtension\TokenParser;

use Fullpipe\TwigWebpackExtension\TokenParser\EntryTokenParserJs;
use PHPUnit\Framework\TestCase;
use Twig\Environment;
use Twig\Loader\LoaderInterface;
use Twig\Node\TextNode;
use Twig\Parser;
use Twig\Source;

class EntryTokenParserJsTest extends TestCase
{
public function testGenerate()
{
$env = $this->getEnv(__DIR__.'/../Resource/manifest.json', '/build/');
$parser = new Parser($env);
$source = new Source("{% webpack_entry_js 'main' %}", '');
$stream = $env->tokenize($source);

$expected = new TextNode('<script type="text/javascript" src="/build/main.js"></script>', 1);
$expected->setSourceContext($source);

$this->assertEquals(
$expected,
$parser->parse($stream)->getNode('body')->getNode('0')
);
}

private function getEnv(string $manifest, string $publicPath): Environment
{
$env = new Environment(
$this->getMockBuilder(LoaderInterface::class)->getMock(),
['cache' => false, 'autoescape' => false, 'optimizations' => 0]
);
$env->addTokenParser(new EntryTokenParserJs($manifest, $publicPath));

return $env;
}
}
4 changes: 2 additions & 2 deletions tests/WebpackExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ class WebpackExtensionTest extends TestCase
{
public function testGetName()
{
$extension = new WebpackExtension('manifest.json');
$extension = new WebpackExtension(__DIR__.'/Resource/manifest.json');
$this->assertEquals('fullpipe.extension.webpack', $extension->getName());
}

public function testGetFunctions()
{
$extension = new WebpackExtension('manifest.json');
$extension = new WebpackExtension(__DIR__.'/Resource/manifest.json');
$parsers = $extension->getTokenParsers();

$this->assertInstanceOf(EntryTokenParserJs::class, $parsers[0]);
Expand Down

0 comments on commit b9585d4

Please sign in to comment.