Skip to content
Snippets Groups Projects
Unverified Commit 688a08c1 authored by Thomas Flori's avatar Thomas Flori
Browse files

test handling of sections

parent c05dd9be
No related branches found
No related tags found
No related merge requests found
......@@ -37,6 +37,7 @@ class View
* The string representation of a view is it's rendered content
*
* @return string
* @codeCoverageIgnore Just a wrapper
*/
public function __toString()
{
......@@ -196,7 +197,7 @@ class View
*/
public function provide(string $name, string $content, bool $append = false)
{
$content = trim($content, "\t\n\r\0\x0b");
$content = trim($content, "\n\r\0\x0b");
if ($append && isset($this->sections[$name])) {
$this->sections[$name] .= $content;
} else {
......@@ -205,16 +206,16 @@ class View
}
/**
* Get the content of section $name or $default
* Get the content of section $name or $alternative
*
* @param string $name
* @param string|null $default
* @param string|null $alternative
* @return string
*/
public function section(string $name, string $default = null): string
public function section(string $name, string $alternative = ''): string
{
if (!isset($this->sections[$name])) {
return $default;
return $alternative;
}
return $this->sections[$name];
......
<?php
namespace Syna\Test\View;
use Syna\Factory;
use Syna\Test\TestCase;
use Mockery as m;
use Syna\ViewLocator;
class HandlingSectionsTest extends TestCase
{
/** @var m\Mock|Factory */
protected $factory;
protected function setUp()
{
parent::setUp();
$this->factory = m::mock(Factory::class, [new ViewLocator($this->templatePath)])->makePartial();
}
/** @test */
public function contentBetweenStartAndEndBelongsToSection()
{
$this->createTemplate('section.php', '<?php $v->start("foo") ?>Foo<?php $v->end() ?>');
$view = $this->factory->view('section');
$content = $view->render();
self::assertSame('', $content);
self::assertSame('Foo', $view->getSections()['foo']);
}
/** @test */
public function sectionNameContentIsNotAllowed()
{
$this->createTemplate('section.php', '<?php $v->start("content") ?>Foo<?php $v->end() ?>');
$view = $this->factory->view('section');
self::expectException(\LogicException::class);
self::expectExceptionMessage('The section name "content" is reserved.');
$view->render();
}
/** @test */
public function sectionsCanNotBeNested()
{
$this->createTemplate(
'section.php',
'<?php $v->start("foo") ?>
Foo
<?php $v->start("bar"); ?>
Bar
<?php $v->end() ?>
<?php $v->end() ?>'
);
$view = $this->factory->view('section');
self::expectException(\LogicException::class);
self::expectExceptionMessage('You cannot nest sections within other sections.');
$view->render();
}
/** @test */
public function simpleSectionsCanBeCreatedWithProvide()
{
$this->createTemplate('section.php', '<?php $v->provide("foo", "Foo") ?>');
$view = $this->factory->view('section');
$view->render();
self::assertSame('Foo', $view->getSections()['foo']);
}
/** @test */
public function sectionsAreTrimmedExceptSpacesAndTabs()
{
$this->createTemplate(
'section.php',
'<?php $v->start("foo") ?>' . PHP_EOL .
' Foo' . PHP_EOL .
'<?php $v->end() ?>'
);
$view = $this->factory->view('section');
$view->render();
self::assertSame(' Foo', $view->getSections()['foo']);
}
/** @test */
public function sectionsCanBeAccessedUsingSection()
{
$this->createTemplate('parent.php', '<?= $this->section("foo") ?>');
$view = $this->factory->view('parent');
$view->setSections(['foo' => 'Foo']);
$result = $view->render();
self::assertSame('Foo', $result);
}
/** @test */
public function undefinedSectionsAreEmpty()
{
$this->createTemplate('parent.php', '<?= $this->section("foo") ?>');
$view = $this->factory->view('parent');
$result = $view->render();
self::assertSame('', $result);
}
/** @test */
public function alternativeUsedForUndefinedSection()
{
$this->createTemplate('parent.php', '<?= $this->section("foo", "Unknown") ?>');
$view = $this->factory->view('parent');
$result = $view->render();
self::assertSame('Unknown', $result);
}
/** @test */
public function alternativeIsNotUsedForEmptySections()
{
$this->createTemplate('parent.php', '<?= $this->section("foo", "Unknown") ?>');
$view = $this->factory->view('parent');
$view->setSections(['foo' => '']);
$result = $view->render();
self::assertSame('', $result);
}
/** @test */
public function sectionContentCanBeAppended()
{
$this->createTemplate(
'section.php',
'<?php $v->provide("foo", "Bar", true) ?>' . PHP_EOL .
'<?php $v->start("foo", true) ?>Baz<?php $v->end() ?>'
);
$view = $this->factory->view('section');
$view->setSections(['foo' => 'Foo']);
$view->render();
self::assertSame('FooBarBaz', $view->getSections()['foo']);
}
/** @test */
public function sectionHasToBeStartedBeforeEnding()
{
$this->createTemplate('section.php', '<?php $v->end() ?>');
$view = $this->factory->view('section');
self::expectException(\LogicException::class);
self::expectExceptionMessage('You must start a section before you can end it.');
$view->render();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment