Skip to content
This repository has been archived by the owner on Sep 10, 2019. It is now read-only.

Commit

Permalink
Adds Exception for the case a template file is not found
Browse files Browse the repository at this point in the history
  • Loading branch information
rathesDot committed Nov 2, 2017
1 parent bbbd789 commit 84ede8c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/Exceptions/TemplateNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
namespace Aheenam\Mozhi\Exceptions;

use Throwable;

class TemplateNotFoundException extends \Exception
{

public function __construct($message = "", $code = 0, Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}

}
5 changes: 5 additions & 0 deletions src/TemplateRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Aheenam\Mozhi;


use Aheenam\Mozhi\Exceptions\TemplateNotFoundException;
use Aheenam\Mozhi\Models\Page;

class TemplateRenderer
Expand Down Expand Up @@ -35,12 +36,16 @@ public function __construct(Page $page)
*
* @param array $data
* @return string
* @throws TemplateNotFoundException
*/
public function render($data = [])
{
$currentTheme = self::getCurrentTheme();
$template = $this->getTemplate();

if ( !view()->exists("theme::$currentTheme.$template") )
throw new TemplateNotFoundException("Template [$template] was not found in theme [$currentTheme]");

return view("theme::$currentTheme.$template", collect([
'meta' => $this->page->meta(),
'content' => $this->page->getParsedContent()
Expand Down
11 changes: 11 additions & 0 deletions tests/TemplateRendererTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Aheenam\Mozhi\Test;

use Aheenam\Mozhi\Exceptions\TemplateNotFoundException;
use Aheenam\Mozhi\RouteResolver;
use Aheenam\Mozhi\TemplateRenderer;
use Spatie\Snapshots\MatchesSnapshots;
Expand Down Expand Up @@ -38,6 +39,16 @@ public function it_returns_default_if_no_template_is_defined ()
$this->assertEquals('page', $template);
}

/** @test */
public function it_throws_an_exception_if_view_does_not_exists()
{
$this->expectException(TemplateNotFoundException::class);

$page = (new RouteResolver)->getPageByRoute('/no-view');

$this->assertMatchesSnapshot((new TemplateRenderer($page))->render());
}

/** @test */
public function it_renders_a_page_view ()
{
Expand Down
6 changes: 6 additions & 0 deletions tests/tmp/contents/no-view.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
title: demo
template: nothing
---

# No template

0 comments on commit 84ede8c

Please sign in to comment.