From ce52888d4f9f28e95c89483f859cf36ac698434c Mon Sep 17 00:00:00 2001 From: RJ Garcia Date: Mon, 8 Jan 2018 18:12:50 -0800 Subject: [PATCH] Fix Tests - Fixed broken tests - Added new tests - Removed var-dumper dep - Removed v4 example and moved it into an integration test Signed-off-by: RJ Garcia --- composer.json | 3 +- example/v4/main.php | 17 -- phpunit.xml.dist | 20 -- .../RenderContext/RenderContextExtension.php | 2 +- src/Extension/RenderContext/func.php | 40 ---- src/Template.php | 2 +- .../fixtures/standard}/alerts.phtml | 0 .../fixtures/standard}/layouts/layout.phtml | 0 .../integration/fixtures/standard}/main.phtml | 0 .../partials/components/header.phtml | 0 .../fixtures/standard}/sidebar.phtml | 0 test/integration/plates.spec.php | 36 ++++ test/unit/extension-layout-sections.spec.php | 101 ++++++++++ test/unit/extension-path.spec.php | 81 ++++++++ test/unit/extension-render-context.spec.php | 150 +++++++++++++++ test/unit/func.spec.php | 174 ------------------ test/unit/render-template.spec.php | 88 ++++----- test/unit/resolve-data.spec.php | 23 --- test/unit/resolve-path.spec..php | 76 -------- test/unit/sections.spec.php | 34 ---- test/unit/template-context.spec.php | 28 --- test/unit/template-include.spec.php | 29 --- test/unit/template.spec.php | 52 +++++- test/unit/util.spec.php | 3 - 24 files changed, 449 insertions(+), 510 deletions(-) delete mode 100644 example/v4/main.php delete mode 100755 phpunit.xml.dist rename {example/v4/templates => test/integration/fixtures/standard}/alerts.phtml (100%) rename {example/v4/templates => test/integration/fixtures/standard}/layouts/layout.phtml (100%) rename {example/v4/templates => test/integration/fixtures/standard}/main.phtml (100%) rename {example/v4/templates => test/integration/fixtures/standard}/partials/components/header.phtml (100%) rename {example/v4/templates => test/integration/fixtures/standard}/sidebar.phtml (100%) create mode 100644 test/integration/plates.spec.php create mode 100644 test/unit/extension-layout-sections.spec.php create mode 100644 test/unit/extension-path.spec.php create mode 100644 test/unit/extension-render-context.spec.php delete mode 100644 test/unit/func.spec.php delete mode 100644 test/unit/resolve-data.spec.php delete mode 100644 test/unit/resolve-path.spec..php delete mode 100644 test/unit/sections.spec.php delete mode 100644 test/unit/template-context.spec.php delete mode 100644 test/unit/template-include.spec.php diff --git a/composer.json b/composer.json index e79a3e12..983c0b17 100644 --- a/composer.json +++ b/composer.json @@ -30,8 +30,7 @@ "peridot-php/leo": "^1.6", "peridot-php/peridot": "^1.19", "peridot-php/peridot-code-coverage-reporters": "^3.0", - "squizlabs/php_codesniffer": "~1.5", - "symfony/var-dumper": "^4.0" + "squizlabs/php_codesniffer": "~1.5" }, "autoload": { "psr-4": { diff --git a/example/v4/main.php b/example/v4/main.php deleted file mode 100644 index 6c6b9ac7..00000000 --- a/example/v4/main.php +++ /dev/null @@ -1,17 +0,0 @@ - __DIR__ . '/templates']); -$plates->addFolder('components', ['partials/components', '']); -try { - echo $plates->render('main', [ - 'name' => 'RJ & Emily', - 'sidebar_style' => 'simple', - ]); -} catch (Throwable $e) { - echo $e; -} - diff --git a/phpunit.xml.dist b/phpunit.xml.dist deleted file mode 100755 index aae18548..00000000 --- a/phpunit.xml.dist +++ /dev/null @@ -1,20 +0,0 @@ - - - - - tests/ - - - - - src/ - - - - - - - - - - diff --git a/src/Extension/RenderContext/RenderContextExtension.php b/src/Extension/RenderContext/RenderContextExtension.php index 277de580..89033d60 100644 --- a/src/Extension/RenderContext/RenderContextExtension.php +++ b/src/Extension/RenderContext/RenderContextExtension.php @@ -4,7 +4,7 @@ use League\Plates; -/** The render context extension provides a RenderContext object and functions to be used within the render context object */ +/** The render context extension provides a RenderContext object and functions to be used within the render context object. This RenderContext object is injected into the template data to allow usefulness in the templates. */ final class RenderContextExtension implements Plates\Extension { public function register(Plates\Engine $plates) { diff --git a/src/Extension/RenderContext/func.php b/src/Extension/RenderContext/func.php index f7041118..a3c41aac 100644 --- a/src/Extension/RenderContext/func.php +++ b/src/Extension/RenderContext/func.php @@ -5,46 +5,6 @@ use League\Plates; use League\Plates\Exception\FuncException; -function layoutFunc() { - return function(FuncArgs $args) { - list($name, $data) = $args->args; - - $layout = $args->template()->fork($name, $data ?: []); - $args->template()->with('layout', $layout->reference); - - return $layout; - }; -} - -function sectionFunc() { - return function(FuncArgs $args) { - list($name) = $args->args; - return $args->template()->get('sections')->get($name); - }; -} - -const START_APPEND = 0; -const START_PREPEND = 1; -const START_REPLACE = 2; - -/** Starts the output buffering for a section, update of 0 = replace, 1 = append, 2 = prepend */ -function startFunc($update = START_REPLACE) { - return startBufferFunc(function(FuncArgs $args) use ($update) { - return function($contents) use ($update, $args) { - $name = $args->args[0]; - $sections = $args->template()->get('sections'); - - if ($update === START_APPEND) { - $sections->append($name, $contents); - } else if ($update === START_PREPEND) { - $sections->prepend($name, $contents); - } else { - $sections->add($name, $contents); - } - }; - }); -} - function componentFunc($insert = null) { $insert = $insert ?: insertFunc(); return startBufferFunc(function(FuncArgs $args) use ($insert) { diff --git a/src/Template.php b/src/Template.php index adf74976..f0910132 100644 --- a/src/Template.php +++ b/src/Template.php @@ -60,7 +60,7 @@ public function withAddedAttributes(array $attributes) { } public function __clone() { - return new self($this->name, $this->data, $this->attributes, null, $this->parent); + $this->reference = new TemplateReference(); } /** Create a new template based off of this current one */ diff --git a/example/v4/templates/alerts.phtml b/test/integration/fixtures/standard/alerts.phtml similarity index 100% rename from example/v4/templates/alerts.phtml rename to test/integration/fixtures/standard/alerts.phtml diff --git a/example/v4/templates/layouts/layout.phtml b/test/integration/fixtures/standard/layouts/layout.phtml similarity index 100% rename from example/v4/templates/layouts/layout.phtml rename to test/integration/fixtures/standard/layouts/layout.phtml diff --git a/example/v4/templates/main.phtml b/test/integration/fixtures/standard/main.phtml similarity index 100% rename from example/v4/templates/main.phtml rename to test/integration/fixtures/standard/main.phtml diff --git a/example/v4/templates/partials/components/header.phtml b/test/integration/fixtures/standard/partials/components/header.phtml similarity index 100% rename from example/v4/templates/partials/components/header.phtml rename to test/integration/fixtures/standard/partials/components/header.phtml diff --git a/example/v4/templates/sidebar.phtml b/test/integration/fixtures/standard/sidebar.phtml similarity index 100% rename from example/v4/templates/sidebar.phtml rename to test/integration/fixtures/standard/sidebar.phtml diff --git a/test/integration/plates.spec.php b/test/integration/plates.spec.php new file mode 100644 index 00000000..da1054c7 --- /dev/null +++ b/test/integration/plates.spec.php @@ -0,0 +1,36 @@ + __DIR__ . '/fixtures/standard']); + $plates->addFolder('components', ['partials/components', '']); + $res = $plates->render('main', [ + 'name' => 'RJ & Emily', + 'sidebar_style' => 'simple', + ]); + $html = << + + + Profile - RJ & Emily + + +
+

Success!

+
+
+

Sorry...

+
+

Hi RJ & Emily

+ + + + + + +html; + expect($res)->equal($html); + }); +}); diff --git a/test/unit/extension-layout-sections.spec.php b/test/unit/extension-layout-sections.spec.php new file mode 100644 index 00000000..73404c71 --- /dev/null +++ b/test/unit/extension-layout-sections.spec.php @@ -0,0 +1,101 @@ + 'bar']); + $sections->add('foo', 'foobar'); + expect($sections->get('foo'))->equal('foobar'); + }); + it('can append a section', function() { + $sections = new Sections(['foo' => 'foo']); + $sections->append('foo', 'bar'); + expect($sections->get('foo'))->equal('foobar'); + }); + it('can prepend a section', function() { + $sections = new Sections(['foo' => 'bar']); + $sections->prepend('foo', 'foo'); + expect($sections->get('foo'))->equal('foobar'); + }); + it('can clear a section', function() { + $sections = new Sections(['foo' => 'bar']); + $sections->clear('foo'); + expect($sections->get('foo'))->equal(null); + }); + it('can get a section', function() { + $sections = new Sections(['foo' => 'bar']); + expect($sections->get('foo'))->equal('bar'); + }); + it('returns null if section does not exist', function() { + $sections = new Sections(); + expect($sections->get('foo'))->equal(null); + }); + }); + + describe('LayoutRenderTemplate', function() { + it('recursively renders a layout template if there is one', function() { + $rt = new MockRenderTemplate([ + 'main' => function($template) { + $template->with('layout', $template->fork('sub-layout')->reference); + return 'c'; + }, + 'sub-layout' => function($template) { + $template = $template->with('layout', $template->fork('layout')->reference); + $content = $template->get('sections')->get('content'); + return "b{$content}b"; + }, + 'layout' => function($template) { + return "a{$template->get('sections')->get('content')}a"; + }, + ]); + $rt = new LayoutRenderTemplate($rt); + expect($rt->renderTemplate(new Template('main', [], ['sections' => new Sections()])))->equal('abcba'); + }); + }); + + describe('Funcs', function() { + beforeEach(function() { + $this->render = new MockRenderTemplate([]); + $this->template = (new Template('', [], ['sections' => new Sections()]))->reference; + $this->args = new FuncArgs($this->render, $this->template, '', []); + }); + xdescribe('layoutFunc', function() { + it('forks a template and sets the layout'); + }); + xdescribe('sectionFunc', function() { + it('gets a section from the template sections'); + }); + describe('startFunc', function() { + $create_test = function($update_text, $update, $expected) { + it("can start buffering and {$update_text} the section", function() use ($update, $expected) { + ($this->template)()->get('sections')->add('foo', 'baz'); + startFunc($update)($this->args->withArgs(['foo'])); + echo "bar"; + endFunc()($this->args); + expect(($this->template)()->get('sections')->get('foo'))->equal($expected); + }); + }; + $create_test('replace', START_REPLACE, 'bar'); + $create_test('append', START_APPEND, 'bazbar'); + $create_test('prepend', START_PREPEND, 'barbaz'); + }); + }); +}); diff --git a/test/unit/extension-path.spec.php b/test/unit/extension-path.spec.php new file mode 100644 index 00000000..3a36ad7d --- /dev/null +++ b/test/unit/extension-path.spec.php @@ -0,0 +1,81 @@ +args = new ResolvePathArgs('', [], new Template('a')); + }); + + describe('idResolvePath', function() { + it('returns the name passed in', function() { + $path = stack([idResolvePath()])($this->args->withPath('foo')); + expect($path)->equal('foo'); + }); + }); + describe('relativeResolvePath', function() { + beforeEach(function() { + $this->args->template = new Template('a', [], [], null, (new Template('b', [], ['path' => 'foo/b.phtml']))->reference); + $this->resolve = stack([relativeResolvePath(), idResolvePath()]); + }); + it('resolves the name relative to the current_directory in context', function() { + $resolve = $this->resolve; + $path = $resolve($this->args->withPath('./bar')); + $parent_path = $resolve($this->args->withPath('../bar')); + expect([$path, $parent_path])->equal([ + joinPath(['foo', './bar']), + joinPath(['foo', '../bar']) + ]); + }); + it('does nothing if the path does not start with a relative path', function() { + $resolve = $this->resolve; + expect([ + $resolve($this->args->withPath('.../foo')), + $resolve($this->args->withPath('/foo')), + $resolve($this->args->withPath('foo')), + ])->equal([ + '.../foo', + '/foo', + 'foo', + ]); + }); + }); + describe('prefixResolvePath', function() { + beforeEach(function() { + $this->resolve = stack([prefixResolvePath(['/foo', '/bar'], function() { + return true; + }), idResolvePath()]); + }); + it('prefixes non absolute paths with a base path', function() { + $path = ($this->resolve)($this->args->withPath('bar')); + expect($path)->equal(joinPath(['/foo', 'bar'])); + }); + it('does nothing if path starts with /', function() { + $path = ($this->resolve)($this->args->withPath('/acme')); + expect($path)->equal('/acme'); + }); + xit('applies the prefixes in order until it finds a valid path'); + xit('strips the prefix from absolute paths while finding the proper prefix'); + }); + describe('extResolvePath', function() { + it('appends an extension to the name', function() { + $resolve = stack([extResolvePath('bar'), idResolvePath()]); + $path = $resolve($this->args->withPath('foo')); + expect($path)->equal('foo.bar'); + }); + it('does not append the name if ext already exists', function() { + $resolve = stack([extResolvePath('bar'), idResolvePath()]); + $path = $resolve($this->args->withPath('foo.bar')); + expect($path)->equal('foo.bar'); + }); + }); +}); diff --git a/test/unit/extension-render-context.spec.php b/test/unit/extension-render-context.spec.php new file mode 100644 index 00000000..9121a35f --- /dev/null +++ b/test/unit/extension-render-context.spec.php @@ -0,0 +1,150 @@ +render = mock(RenderTemplate::class)->get(); + $this->template = (new Template('', []))->reference; + $this->args = new FuncArgs($this->render, $this->template, '', []); + }); + + describe('startBufferFunc', function() { + it('starts the output buffering and appends the buffer_stack', function() { + $args = $this->args; + $callback = function() {}; + + $cur_level = ob_get_level(); + startBufferFunc(function($passed_args) use ($args, $callback) { + expect($passed_args)->equal($args); + return $callback; + })($args); + expect($args->template()->get('buffer_stack'))->length(1); + expect($args->template()->get('buffer_stack')[0])->equal([ + $cur_level + 1, + $callback + ]); + ob_end_clean(); + }); + }); + describe('endFunc', function() { + it('throws an exception if no buffer_stack have been defined', function() { + $func = endFunc(); + expect(function() use ($func) { + $func($this->args); + })->throw(FuncException::class, 'Cannot end a section definition because no section has been started.'); + }); + it('throws an exception if the output buffering level does not match the section def', function() { + + startBufferFunc(function() { + return function() {}; + })($this->args); + + ob_end_clean(); + + $func = endFunc(); + expect(function() use ($func) { + $func($this->args); + })->throw(FuncException::class, 'Output buffering level does not match when section was started.'); + }); + it('cleans the buffer and calls the callback', function() { + $was_called = false; + startBufferFunc(function() use (&$was_called) { + return function() use (&$was_called) { + $was_called = true; + }; + })($this->args); + + expect($was_called)->false(); + + endFunc()($this->args); + expect($was_called)->true(); + expect($this->args->template()->get('buffer_stack'))->length(0); + }); + }); + describe('slotFunc', function() { + it('throws an exception if not component slot data is there', function() { + expect(function() { + slotFunc()($this->args); + })->throw(FuncException::class, 'Cannot call slot func outside of component definition.'); + }); + it('adds to the component slot data', function() { + $this->args->template()->with('component_slot_data', []); + slotFunc()($this->args->withArgs(['foo'])); + echo "bar"; + endFunc()($this->args); + expect($this->args->template()->get('component_slot_data')['foo']) + ->equal('bar'); + }); + }); + describe('componentFunc', function() { + it('inserts a partial while passing in buffered content', function() { + $insert_args = null; + componentFunc(function($args) use (&$insert_args) { + $insert_args = $args->args; + })($this->args->withArgs(['name', ['a' => 1]])); + $this->args->template()->attributes['component_slot_data']['b'] = 2; + echo "content"; + endFunc()($this->args); + expect($insert_args)->equal([ + 'name', + [ + 'a' => 1, + 'slot' => 'content', + 'b' => 2, + ] + ]); + }); + it('throws an exception if the nested component func is called', function() { + expect(function() { + componentFunc()($this->args->withArgs(['name', []])); + componentFunc()($this->args->withArgs(['name', []])); + })->throw(FuncException::class, 'Cannot nest component func calls.'); + }); + }); + xdescribe('insertFunc', function() { + it('forks a template and echos the rendered contents'); + }); + xdescribe('escapeFunc', function() { + it('it escapes the content with htmlspecialchars'); + }); + xdescribe('assertArgsFunc', function() { + it('throws an exception if the num of required args do not exist'); + it('passes through if there are enough args'); + it('appends null to the args to make up the amount of required and defaulted args'); + }); + xdescribe('aliasNameFunc', function() { + it('passes through if no aliases are matched'); + it('recursively aliases func names'); + }); + xdescribe('splitByNameFunc', function() { + it('allows stacks to be invoked by func name'); + it('passes through if no stack was registered per func name'); + }); + xdescribe('platesFunc', function() { + it('creates the default plates func stack'); + }); + }); +}); diff --git a/test/unit/func.spec.php b/test/unit/func.spec.php deleted file mode 100644 index 1ac30aae..00000000 --- a/test/unit/func.spec.php +++ /dev/null @@ -1,174 +0,0 @@ -render = mock(RenderTemplate::class)->get(); - $this->template = (new Template('', [], ['sections' => new Sections()]))->reference; - $this->args = new FuncArgs($this->render, $this->template, '', []); - }); - describe('layoutFunc', function() { - it('forks a template and sets the layout'); - }); - xdescribe('sectionFunc', function() { - it('gets a section from the template sections'); - }); - describe('startFunc', function() { - $create_test = function($update_text, $update, $expected) { - it("can start buffering and {$update_text} the section", function() use ($update, $expected) { - ($this->template)()->get('sections')->add('foo', 'baz'); - startFunc($update)($this->args->withArgs(['foo'])); - echo "bar"; - endFunc()($this->args); - expect(($this->template)()->get('sections')->get('foo'))->equal($expected); - }); - }; - $create_test('replace', START_REPLACE, 'bar'); - $create_test('append', START_APPEND, 'bazbar'); - $create_test('prepend', START_PREPEND, 'barbaz'); - }); - describe('startBufferFunc', function() { - it('starts the output buffering and appends the buffer_stack', function() { - $args = $this->args; - $callback = function() {}; - - $cur_level = ob_get_level(); - startBufferFunc(function($passed_args) use ($args, $callback) { - expect($passed_args)->equal($args); - return $callback; - })($args); - expect($args->template()->get('buffer_stack'))->length(1); - expect($args->template()->get('buffer_stack')[0])->equal([ - $cur_level + 1, - $callback - ]); - ob_end_clean(); - }); - }); - describe('endFunc', function() { - it('throws an exception if no buffer_stack have been defined', function() { - $func = endFunc(); - expect(function() use ($func) { - $func($this->args); - })->throw(FuncException::class, 'Cannot end a section definition because no section has been started.'); - }); - it('throws an exception if the output buffering level does not match the section def', function() { - - startBufferFunc(function() { - return function() {}; - })($this->args); - - ob_end_clean(); - - $func = endFunc(); - expect(function() use ($func) { - $func($this->args); - })->throw(FuncException::class, 'Output buffering level does not match when section was started.'); - }); - it('cleans the buffer and calls the callback', function() { - $was_called = false; - startBufferFunc(function() use (&$was_called) { - return function() use (&$was_called) { - $was_called = true; - }; - })($this->args); - - expect($was_called)->false(); - - endFunc()($this->args); - expect($was_called)->true(); - expect($this->args->template()->get('buffer_stack'))->length(0); - }); - }); - describe('slotFunc', function() { - it('throws an exception if not component slot data is there', function() { - expect(function() { - slotFunc()($this->args); - })->throw(FuncException::class, 'Cannot call slot func outside of component definition.'); - }); - it('adds to the component slot data', function() { - $this->args->template()->with('component_slot_data', []); - slotFunc()($this->args->withArgs(['foo'])); - echo "bar"; - endFunc()($this->args); - expect($this->args->template()->get('component_slot_data')['foo']) - ->equal('bar'); - }); - }); - describe('componentFunc', function() { - it('inserts a partial while passing in buffered content', function() { - $insert_args = null; - componentFunc(function($args) use (&$insert_args) { - $insert_args = $args->args; - })($this->args->withArgs(['name', ['a' => 1]])); - $this->args->template()->attributes['component_slot_data']['b'] = 2; - echo "content"; - endFunc()($this->args); - expect($insert_args)->equal([ - 'name', - [ - 'a' => 1, - 'slot' => 'content', - 'b' => 2, - ] - ]); - }); - it('throws an exception if the nested component func is called', function() { - expect(function() { - componentFunc()($this->args->withArgs(['name', []])); - componentFunc()($this->args->withArgs(['name', []])); - })->throw(FuncException::class, 'Cannot nest component func calls.'); - }); - }); - xdescribe('insertFunc', function() { - it('forks a template and echos the rendered contents'); - }); - xdescribe('escapeFunc', function() { - it('it escapes the content with htmlspecialchars'); - }); - xdescribe('assertArgsFunc', function() { - it('throws an exception if the num of required args do not exist'); - it('passes through if there are enough args'); - it('appends null to the args to make up the amount of required and defaulted args'); - }); - xdescribe('aliasNameFunc', function() { - it('passes through if no aliases are matched'); - it('recursively aliases func names'); - }); - xdescribe('splitByNameFunc', function() { - it('allows stacks to be invoked by func name'); - it('passes through if no stack was registered per func name'); - }); - xdescribe('platesFunc', function() { - it('creates the default plates func stack'); - }); -}); diff --git a/test/unit/render-template.spec.php b/test/unit/render-template.spec.php index 9726d964..b1006f8b 100644 --- a/test/unit/render-template.spec.php +++ b/test/unit/render-template.spec.php @@ -1,74 +1,48 @@ compose = function($template) { - return $template->withAddedData([ - 'this' => $template->reference, - 'render' => $template->attributes['render'] - ])->withAddedContext(['path' => $template->name]); - }; + describe('MockRenderTemplate', function() { + it('calls a mock render function based off of the template name', function() { + $rt = new MockRenderTemplate([ + 'a' => function($t) { + return 'a-contents'; + } + ]); + expect($rt->renderTemplate(new Template('a')))->equal('a-contents'); + }); + it('throws an exception if a mock render does not exist', function() { + expect(function() { + $rt = new MockRenderTemplate([]); + $rt->renderTemplate(new Template('a')); + })->throw(RenderTemplateException::class); + }); }); describe('FileSystemRenderTemplate', function() { - it('it renders a template into string', function() { - $rt = new FileSystemRenderTemplate( - Template\mockInclude([ - 'foo' => function($path, $data) { - $content = "{$path}-{$data['a']}"; - return $content . "-" . $data['render']->renderTemplate($data['this']()->fork( - 'bar', - ['a' => 2] - )); - }, - 'bar' => function($path, $data) { - return "{$path}-{$data['a']}"; - } - ]) - ); - $rt = new ComposeRenderTemplate($rt, $this->compose); - $t = new Template('foo', ['a' => 1], ['render' => $rt]); - expect($rt->renderTemplate($t))->equal('foo-1-bar-2'); + it('it delegates rendering to the matched renderer according to the render set', function() { + $rt = new FileSystemRenderTemplate([ + [matchStub(false), new MockRenderTemplate(['a' => function() { return 'a'; }])], + [matchStub(true), new MockRenderTemplate(['a' => function() { return 'b'; }])], + ]); + + expect($rt->renderTemplate(new Template('a')))->equal('b'); }); - }); - describe('LayoutRenderTemplate', function() { - it('recursively renders a layout template if there is one', function() { - $rt = new FileSystemRenderTemplate( - Template\mockInclude([ - 'main' => function($path, $data) { - $tpl = $data['this']->template; - $tpl = $tpl->with('layout', $tpl->fork('sub-layout')->reference); - return 'c'; - }, - 'sub-layout' => function($path, $data) { - $tpl = $data['this']->template; - $tpl = $tpl->with('layout', $tpl->fork('layout')->reference); - $content = $tpl->get('sections')->get('content'); - return "b{$content}b"; - }, - 'layout' => function($path, $data) { - $content = $data['this']()->get('sections')->get('content'); - return "a{$content}a"; - }, - ]) - ); - $rt = new LayoutRenderTemplate($rt); - $rt = new ComposeRenderTemplate($rt, $this->compose); - $res = $rt->renderTemplate(new Template('main', [], ['render' => $rt])); - expect($res)->equal('abcba'); + it('throws an exception if no renderer is available', function() { + expect(function() { + $rt = new FileSystemRenderTemplate([]); + $rt->renderTemplate(new Template('a')); + })->throw(RenderTemplateException::class); }); - xit('initializes the sections on templates being rendered'); }); }); diff --git a/test/unit/resolve-data.spec.php b/test/unit/resolve-data.spec.php deleted file mode 100644 index c183fb5d..00000000 --- a/test/unit/resolve-data.spec.php +++ /dev/null @@ -1,23 +0,0 @@ -args = new ResolveDataArgs([], new Template('')); - }); - - describe('idResolveData', function() { - it('returns the data passed in', function() { - $res = idResolveData()($this->args->withData([1,2,3])); - expect($res)->equal([1,2,3]); - }); - }); -}); diff --git a/test/unit/resolve-path.spec..php b/test/unit/resolve-path.spec..php deleted file mode 100644 index 070ddb8d..00000000 --- a/test/unit/resolve-path.spec..php +++ /dev/null @@ -1,76 +0,0 @@ -args = new ResolvePathArgs('', [], function() {}); - }); - - describe('idResolveName', function() { - it('returns the name passed in', function() { - $path = idResolveName()($this->args->withName('foo')); - expect($path)->equal('foo'); - }); - }); - describe('relativeResolveName', function() { - beforeEach(function() { - $this->args = $this->args->withContext(['current_directory' => 'foo']); - $this->resolve = stack([relativeResolveName(), idResolveName()]);; - }); - it('resolves the name relative to the current_directory in context', function() { - $resolve = $this->resolve; - $path = $resolve($this->args->withName('./bar')); - $parent_path = $resolve($this->args->withName('../bar')); - expect([$path, $parent_path])->equal([ - joinPath(['foo', './bar']), - joinPath(['foo', '../bar']) - ]); - }); - it('does nothing if the path does not start with a relative path', function() { - $resolve = $this->resolve; - expect([ - $resolve($this->args->withName('.../foo')), - $resolve($this->args->withName('/foo')), - $resolve($this->args->withName('foo')), - ])->equal([ - '.../foo', - '/foo', - 'foo', - ]); - }); - }); - describe('prefixResolveName', function() { - beforeEach(function() { - $this->resolve = stack([prefixResolveName('foo'), idResolveName()]); - }); - it('prefixes non absolute paths with a base path', function() { - $path = ($this->resolve)($this->args->withName('bar')); - expect($path)->equal(joinPath(['foo', 'bar'])); - }); - it('does nothing if path starts with /', function() { - $path = ($this->resolve)($this->args->withName('/bar')); - expect($path)->equal('/bar'); - }); - }); - describe('extResolveName', function() { - it('appends an extension to the name', function() { - $resolve = stack([extResolveName('bar'), idResolveName()]); - $path = $resolve($this->args->withName('foo')); - expect($path)->equal('foo.bar'); - }); - it('does not append the name if ext already exists', function() { - $resolve = stack([extResolveName('bar'), idResolveName()]); - $path = $resolve($this->args->withName('foo.bar')); - expect($path)->equal('foo.bar'); - }); - }); -}); diff --git a/test/unit/sections.spec.php b/test/unit/sections.spec.php deleted file mode 100644 index d8ab76a3..00000000 --- a/test/unit/sections.spec.php +++ /dev/null @@ -1,34 +0,0 @@ - 'bar']); - $sections->add('foo', 'foobar'); - expect($sections->get('foo'))->equal('foobar'); - }); - it('can append a section', function() { - $sections = new Sections(['foo' => 'foo']); - $sections->append('foo', 'bar'); - expect($sections->get('foo'))->equal('foobar'); - }); - it('can prepend a section', function() { - $sections = new Sections(['foo' => 'bar']); - $sections->prepend('foo', 'foo'); - expect($sections->get('foo'))->equal('foobar'); - }); - it('can clear a section', function() { - $sections = new Sections(['foo' => 'bar']); - $sections->clear('foo'); - expect($sections->get('foo'))->equal(null); - }); - it('can get a section', function() { - $sections = new Sections(['foo' => 'bar']); - expect($sections->get('foo'))->equal('bar'); - }); - it('returns null if section does not exist', function() { - $sections = new Sections(); - expect($sections->get('foo'))->equal(null); - }); -}); diff --git a/test/unit/template-context.spec.php b/test/unit/template-context.spec.php deleted file mode 100644 index 5d58e75a..00000000 --- a/test/unit/template-context.spec.php +++ /dev/null @@ -1,28 +0,0 @@ -equal($layout); - expect(Template\getLayout($layout))->equal(null); - }); -}); -describe('getSections', function() { - it('can get sections from a template', function() { - $t = new Template('main'); - $sections = new Template\Sections(); - Template\setSections($t, $sections); - expect(Template\getSections($t))->equal($sections); - }); - it('can initialize sections for a template if non exist', function() { - $t = new Template('main'); - expect(Template\getSections($t))->instanceof(Template\Sections::class); - expect(Template\getSections($t))->equal(Template\getSections($t)); - }); -}); diff --git a/test/unit/template-include.spec.php b/test/unit/template-include.spec.php deleted file mode 100644 index 6bf96f1f..00000000 --- a/test/unit/template-include.spec.php +++ /dev/null @@ -1,29 +0,0 @@ - 'foo']); - expect(trim($res))->equal("
foo
"); - }); -}); -describe('mockInclude', function() { - it('provides a simple mocking system for includes', function() { - $inc = Template\mockInclude(['foo' => function($path, $params) { - return $params['v']; - }]); - expect($inc('foo', ['v' => 'bar']))->equal('bar'); - }); - it('throws an exception if no mock is found for path', function() { - $inc = Template\mockInclude([]); - expect(function() use ($inc) { - $inc('foo', []); - })->to->throw(PlatesException::class); - }); -}); diff --git a/test/unit/template.spec.php b/test/unit/template.spec.php index 56658abb..54aa243f 100644 --- a/test/unit/template.spec.php +++ b/test/unit/template.spec.php @@ -15,17 +15,24 @@ $t = new Template('a', [1,2]); expect($t->data)->equal([1,2]); }); - it('can get the template context', function() { + it('can get the template attributes', function() { $t = new Template('a', [], [2,3]); expect($t->attributes)->equal([2,3]); }); - it('can fork a template', function() { + it('can fork a child template that has a reference to parent', function() { $t = new Template('a', ['a' => 1, 'b' => 1], ['c' => 1]); $f = $t->fork('b', ['b' => 2], ['d' => 2]); - expect($f)->not->equal($t); + expect($f->parent->template)->equal($t); expect($f->name)->equal('b'); - expect($f->data)->equal(['a' => 1, 'b' => 2]); - expect($f->attributes)->equal(['c' => 1, 'd' => 2]); + expect($f->data)->equal(['b' => 2]); + expect($f->attributes)->equal(['d' => 2]); + }); + it('can clone a template', function() { + $t = new Template('a', ['a' => 1]); + $t1 = clone $t; + expect($t->reference)->not->equal($t1->reference); + expect($t->data)->equal($t1->data); + expect($t->name)->equal($t1->name); }); it('can keep a reference', function() { $t = new Template('a'); @@ -35,4 +42,39 @@ expect($t)->not->equal($t2); expect($t->reference->template)->equal($t2); }); + it('can return the dereferenced parent', function() { + $t = new Template('a'); + expect($t->parent())->null(); + expect($t->fork('b')->parent())->equal($t); + }); + it('can update the name', function() { + $t = new Template('a'); + $t1 = $t->withName('b'); + expect($t)->not->equal($t1); + expect($t1->name)->equal('b'); + }); + it('can update the data', function() { + $t = new Template('a'); + $t1 = $t->withData([1,2,3]); + expect($t)->not->equal($t1); + expect($t1->data)->equal([1,2,3]); + }); + it('can update and merge the data', function() { + $t = new Template('a', ['a' => 1, 'b' => 1]); + $t1 = $t->withAddedData(['a' => 2]); + expect($t)->not->equal($t1); + expect($t1->data)->equal(['a' => 2, 'b' => 1]); + }); + it('can update the attributes', function() { + $t = new Template('a'); + $t1 = $t->withAttributes([1,2,3]); + expect($t)->not->equal($t1); + expect($t1->attributes)->equal([1,2,3]); + }); + it('can update and merge the attributes', function() { + $t = new Template('a', [], ['a' => 1, 'b' => 1]); + $t1 = $t->withAddedAttributes(['a' => 2]); + expect($t)->not->equal($t1); + expect($t1->attributes)->equal(['a' => 2, 'b' => 1]); + }); }); diff --git a/test/unit/util.spec.php b/test/unit/util.spec.php index 4f5ac68e..3731de27 100644 --- a/test/unit/util.spec.php +++ b/test/unit/util.spec.php @@ -10,9 +10,6 @@ it('returns the argument given', function() { expect(Util\id()(1))->equal(1); }); - it ('can return multiple arguments if multi = true', function() { - expect(Util\id(true)(1, 2))->equal([1,2]); - }); }); describe('stack', function() { it('can stack a group of functions', function() {