MAPG-177 add tests for View\Parser

This commit is contained in:
Bence Pőcze 2020-06-28 22:55:15 +02:00
parent 34f0c92eb5
commit 149003250c
Signed by: bence
GPG Key ID: AA52B11A3269D1C1
9 changed files with 194 additions and 0 deletions

144
tests/View/ParserTest.php Normal file
View File

@ -0,0 +1,144 @@
<?php namespace MapGuesser\Tests\View\Parser;
use MapGuesser\View\ParsedFragment;
use MapGuesser\View\Parser;
use PHPUnit\Framework\TestCase;
final class ParserTest extends TestCase
{
const TEST_VIEWS_PATH = __DIR__ . '/../assets/views';
public function testCanParseViewWithoutExtends(): void
{
$file = realpath(self::TEST_VIEWS_PATH . '/view_without_extends.php');
$parser = new Parser($file);
$fragment = $parser->parse();
$expected = new ParsedFragment(
null,
[],
[],
[],
['', '']
);
$this->assertEquals($expected, $fragment);
}
public function testCanParseViewWithAssets(): void
{
$file = realpath(self::TEST_VIEWS_PATH . '/view_with_assets.php');
$parser = new Parser($file);
$fragment = $parser->parse();
$expected = new ParsedFragment(
null,
[
'test.css'
],
[
'test.js',
'test_<?= $some[\'expression\'] ?>'
],
[],
['', '']
);
$this->assertEquals($expected, $fragment);
}
public function testCanParseViewWithExtends(): void
{
$file = realpath(self::TEST_VIEWS_PATH . '/view_with_extends.php');
$parser = new Parser($file);
$fragment = $parser->parse();
$expected = new ParsedFragment(
'parent',
[],
[],
[
'section1' => '<div>Test HTML with @extends</div>' . "\n"
],
['', '']
);
$this->assertEquals($expected, $fragment);
}
public function testCanParseComplexView(): void
{
$file = realpath(self::TEST_VIEWS_PATH . '/view_complex.php');
$parser = new Parser($file);
$fragment = $parser->parse();
$expected = new ParsedFragment(
'parent',
[
'test1.css'
],
[
'test1.js',
'test2_<?= $some[\'expression\'] ?>'
],
[
'section1' => '<div>Test HTML with @extends - section 1</div>' . "\n",
'section2' => '<div>Test HTML with @extends - section 2</div>' . "\n"
],
[
'<?php phpinfo() ?>' . "\n",
'<?php $a = \'test_string\' ?>' . "\n" . 'EXTRA' . "\n",
]
);
$this->assertEquals($expected, $fragment);
}
public function testFailsIfMultipleExtendsGiven(): void
{
$file = realpath(self::TEST_VIEWS_PATH . '/view_invalid_multiple_extends.php');
$parser = new Parser($file);
$this->expectExceptionMessage('Error in file ' . $file . ' in line 3 - There is already an \'@extends\' declared.');
$parser->parse();
}
public function testFailsIfSectionWithoutExtendsGiven(): void
{
$file = realpath(self::TEST_VIEWS_PATH . '/view_invalid_section_without_extends.php');
$parser = new Parser($file);
$this->expectExceptionMessage('Error in file ' . $file . ' in line 1 - \'@section\' has no meaning if view extends nothing.');
$parser->parse();
}
public function testFailsIfOpeningSectionBeforePreviousClosed(): void
{
$file = realpath(self::TEST_VIEWS_PATH . '/view_invalid_multiple_sections_open.php');
$parser = new Parser($file);
$this->expectExceptionMessage('Parse error in file ' . $file . ' in line 4 - A \'@section\' is already open (no \'@endsection\' found).');
$parser->parse();
}
public function testFailsIfClosingSectionWhenNoSectionIsOpen(): void
{
$file = realpath(self::TEST_VIEWS_PATH . '/view_invalid_section_not_open.php');
$parser = new Parser($file);
$this->expectExceptionMessage('Parse error in file ' . $file . ' in line 4 - Cannot end section until no \'@section\' is open.');
$parser->parse();
}
}

View File

@ -0,0 +1,22 @@
@css(test1.css)
@js(test1.js)
@js(test2_<?= $some['expression'] ?>)
@extra
<?php phpinfo() ?>
@endextra
@extends(parent)
@section(section1)
<div>Test HTML with @extends - section 1</div>
@endsection
@section(section2)
<div>Test HTML with @extends - section 2</div>
@endsection
@extra
<?php $a = 'test_string' ?>
EXTRA
@endextra

View File

@ -0,0 +1,5 @@
@extends(parent1)
@extends(parent2)
<div>Test HTML with multiple @extends</div>

View File

@ -0,0 +1,6 @@
@extends(parent)
@section(section1)
@section(section2)
<div>Test HTML with opening @section before previous closed</div>

View File

@ -0,0 +1,4 @@
@extends(parent)
<div>Test HTML with @endsection when no section is open</div>
@endsection

View File

@ -0,0 +1,3 @@
@section(section1)
<div>Test HTML with @section but without @extends</div>

View File

@ -0,0 +1,4 @@
@css(test.css)
@js(test.js)
@js(test_<?= $some['expression'] ?>)
<div>Test HTML with @css and @js</div>

View File

@ -0,0 +1,5 @@
@extends(parent)
@section(section1)
<div>Test HTML with @extends</div>
@endsection

View File

@ -0,0 +1 @@
<div>Test HTML without @extends</div>