MAPG-177 add tests for View\Parser
This commit is contained in:
parent
34f0c92eb5
commit
149003250c
144
tests/View/ParserTest.php
Normal file
144
tests/View/ParserTest.php
Normal 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();
|
||||
}
|
||||
}
|
22
tests/assets/views/view_complex.php
Normal file
22
tests/assets/views/view_complex.php
Normal 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
|
5
tests/assets/views/view_invalid_multiple_extends.php
Normal file
5
tests/assets/views/view_invalid_multiple_extends.php
Normal file
@ -0,0 +1,5 @@
|
||||
@extends(parent1)
|
||||
|
||||
@extends(parent2)
|
||||
|
||||
<div>Test HTML with multiple @extends</div>
|
@ -0,0 +1,6 @@
|
||||
@extends(parent)
|
||||
|
||||
@section(section1)
|
||||
@section(section2)
|
||||
|
||||
<div>Test HTML with opening @section before previous closed</div>
|
4
tests/assets/views/view_invalid_section_not_open.php
Normal file
4
tests/assets/views/view_invalid_section_not_open.php
Normal file
@ -0,0 +1,4 @@
|
||||
@extends(parent)
|
||||
|
||||
<div>Test HTML with @endsection when no section is open</div>
|
||||
@endsection
|
@ -0,0 +1,3 @@
|
||||
@section(section1)
|
||||
|
||||
<div>Test HTML with @section but without @extends</div>
|
4
tests/assets/views/view_with_assets.php
Normal file
4
tests/assets/views/view_with_assets.php
Normal file
@ -0,0 +1,4 @@
|
||||
@css(test.css)
|
||||
@js(test.js)
|
||||
@js(test_<?= $some['expression'] ?>)
|
||||
<div>Test HTML with @css and @js</div>
|
5
tests/assets/views/view_with_extends.php
Normal file
5
tests/assets/views/view_with_extends.php
Normal file
@ -0,0 +1,5 @@
|
||||
@extends(parent)
|
||||
|
||||
@section(section1)
|
||||
<div>Test HTML with @extends</div>
|
||||
@endsection
|
1
tests/assets/views/view_without_extends.php
Normal file
1
tests/assets/views/view_without_extends.php
Normal file
@ -0,0 +1 @@
|
||||
<div>Test HTML without @extends</div>
|
Loading…
Reference in New Issue
Block a user