Skip to content

Commit

Permalink
Fixes template rendering with invalid named templates
Browse files Browse the repository at this point in the history
  • Loading branch information
jedrzejchalubek committed Apr 12, 2017
1 parent 5cf25b3 commit 374563a
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 46 deletions.
22 changes: 21 additions & 1 deletion src/Gin/Template/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,18 @@ public function getRelativePath()
*/
public function getFilename($extension = '.php')
{
// If template is named,
// return joined template names.
if ($this->isNamed()) {
return join('-', $this->file) . $extension;
}

// Use first template name, if template
// file is an array, but is not named.
if (is_array($this->file)) {
return "{$this->file[0]}{$extension}";
}

return "{$this->file}{$extension}";
}

Expand All @@ -129,7 +137,19 @@ public function getFilename($extension = '.php')
*/
public function isNamed()
{
return is_array($this->file);
// If file is not array, then template
// is not named for sure.
if (! is_array($this->file)) {
return false;
}

// Return false if template is named,
// but name is bool or null.
if (isset($this->file[1]) && is_bool($this->file[1]) || is_null($this->file[1])) {
return false;
}

return true;
}

/**
Expand Down
98 changes: 53 additions & 45 deletions tests/Gin/Template/TemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
use Brain\Monkey\Functions;
use Brain\Monkey\WP\Actions;
use Tonik\Gin\Foundation\Config;
use Tonik\Gin\Foundation\Exception\FileNotFoundException;
use Tonik\Gin\Template\Template;
use Tonik\Gin\Foundation\Exception\FileNotFoundException;

class TemplateTest extends TestCase
{
Expand All @@ -14,9 +14,9 @@ class TemplateTest extends TestCase
public function test_file_setter_and_getter()
{
$config = $this->getConfig();
$template = $this->getTemplate($config, 'sample-template');
$template = $this->getTemplate($config, 'sample_template');

$this->assertEquals($template->getFile(), 'sample-template');
$this->assertEquals($template->getFile(), 'sample_template');
}

/**
Expand All @@ -25,17 +25,9 @@ public function test_file_setter_and_getter()
public function test_absolute_path_getter()
{
$config = $this->getConfig();
$template = $this->getTemplate($config, 'sample-template');

$config->shouldReceive('offsetGet')
->with('paths')
->andReturn(['directory' => 'abs/path']);

$config->shouldReceive('offsetGet')
->with('directories')
->andReturn(['templates' => 'resources/templates']);
$template = $this->getTemplate($config, 'sample_template');

$this->assertEquals($template->getPath(), 'abs/path/resources/templates/sample-template.php');
$this->assertEquals($template->getPath(), 'abs/path/resources/templates/sample_template.php');
}

/**
Expand All @@ -44,13 +36,9 @@ public function test_absolute_path_getter()
public function test_relative_path_getter()
{
$config = $this->getConfig();
$template = $this->getTemplate($config, 'sample-template');
$template = $this->getTemplate($config, 'sample_template');

$config->shouldReceive('offsetGet')
->with('directories')
->andReturn(['templates' => 'resources/templates']);

$this->assertEquals($template->getRelativePath(), 'resources/templates/sample-template.php');
$this->assertEquals($template->getRelativePath(), 'resources/templates/sample_template.php');
}

/**
Expand All @@ -59,41 +47,69 @@ public function test_relative_path_getter()
public function it_should_throw_exception_on_render_if_file_is_no_located()
{
$config = $this->getConfig();
$template = $this->getTemplate($config, 'sample-template');
$template = $this->getTemplate($config, 'sample_template');

Functions::expect('locate_template')
->once()
->with('resources/templates/sample-template.php', false, false)
->with('resources/templates/sample_template.php', false, false)
->andReturn(false);

$config->shouldReceive('offsetGet')
->with('directories')
->andReturn(['templates' => 'resources/templates']);

$this->expectException(FileNotFoundException::class);

$template->render();
}

/**
* @test
*/
public function it_should_return_no_named_filename_when_template_name_is_not_valid()
{
$config = $this->getConfig();

$invalidNamedTemplate = $this->getTemplate($config, ['sample_template', false]);
$this->assertFalse($invalidNamedTemplate->isNamed());
$this->assertEquals('sample_template.php', $invalidNamedTemplate->getFilename());

$invalidNamedTemplate = $this->getTemplate($config, ['sample_template', null]);
$this->assertFalse($invalidNamedTemplate->isNamed());
$this->assertEquals('sample_template.php', $invalidNamedTemplate->getFilename());
}

/**
* @test
*/
public function it_should_return_named_filename_when_template_name_is_valid()
{
$config = $this->getConfig();

$namedTemplate = $this->getTemplate($config, ['sample_template', 'named']);
$this->assertTrue($namedTemplate->isNamed());
$this->assertEquals('sample_template-named.php', $namedTemplate->getFilename());

$namedTemplate = $this->getTemplate($config, ['sample_template', 'true']);
$this->assertTrue($namedTemplate->isNamed());
$this->assertEquals('sample_template-true.php', $namedTemplate->getFilename());

$namedTemplate = $this->getTemplate($config, ['sample_template', 'null']);
$this->assertTrue($namedTemplate->isNamed());
$this->assertEquals('sample_template-null.php', $namedTemplate->getFilename());
}

/**
* @test
*/
public function it_should_do_get_template_part_action_on_render_with_no_named_template()
{
$config = $this->getConfig();
$template = $this->getTemplate($config, 'sample-template');
$template = $this->getTemplate($config, 'sample_template');

Functions::expect('locate_template')
->twice()
->andReturn(true);

Actions::expectFired('get_template_part_sample-template')
Actions::expectFired('get_template_part_sample_template')
->once()
->with('sample-template', null);

$config->shouldReceive('offsetGet')
->with('directories')
->andReturn(['templates' => 'resources/templates']);
->with('sample_template', null);

$template->render();
}
Expand All @@ -104,19 +120,15 @@ public function it_should_do_get_template_part_action_on_render_with_no_named_te
public function it_should_do_get_template_part_action_on_render_with_named_template()
{
$config = $this->getConfig();
$template = $this->getTemplate($config, ['sample-template', 'named']);
$template = $this->getTemplate($config, ['sample_template', 'named']);

Functions::expect('locate_template')
->twice()
->andReturn(true);

Actions::expectFired('get_template_part_sample-template')
Actions::expectFired('get_template_part_sample_template')
->once()
->with('sample-template', 'named');

$config->shouldReceive('offsetGet')
->with('directories')
->andReturn(['templates' => 'resources/templates']);
->with('sample_template', 'named');

$template->render();
}
Expand All @@ -127,7 +139,7 @@ public function it_should_do_get_template_part_action_on_render_with_named_templ
public function it_should_set_up_context_to_the_query_var()
{
$config = $this->getConfig();
$template = $this->getTemplate($config, ['sample-template', 'named']);
$template = $this->getTemplate($config, ['sample_template', 'named']);

Functions::expect('locate_template')
->twice()
Expand All @@ -143,10 +155,6 @@ public function it_should_set_up_context_to_the_query_var()
->with('key2', 'value2')
->andReturn(null);

$config->shouldReceive('offsetGet')
->with('directories')
->andReturn(['templates' => 'resources/templates']);

$template->render([
'key1' => 'value1',
'key2' => 'value2'
Expand All @@ -155,7 +163,7 @@ public function it_should_set_up_context_to_the_query_var()

public function getConfig()
{
return Mockery::mock(Config::class, [
return new Config([
'paths' => [
'directory' => 'abs/path',
],
Expand Down

0 comments on commit 374563a

Please sign in to comment.