Skip to content

Commit

Permalink
Merge pull request #193 from thephpleague/fix-tests
Browse files Browse the repository at this point in the history
Fix Tests
  • Loading branch information
ragboyjr committed Jan 9, 2018
2 parents 17868f2 + ce52888 commit 91af3e1
Show file tree
Hide file tree
Showing 24 changed files with 449 additions and 510 deletions.
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
17 changes: 0 additions & 17 deletions example/v4/main.php

This file was deleted.

20 changes: 0 additions & 20 deletions phpunit.xml.dist

This file was deleted.

2 changes: 1 addition & 1 deletion src/Extension/RenderContext/RenderContextExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
40 changes: 0 additions & 40 deletions src/Extension/RenderContext/func.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
36 changes: 36 additions & 0 deletions test/integration/plates.spec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

use League\Plates\Engine;

describe('Plates', function() {
it('can render templates with layouts, sections, relative paths, absolute paths, and folders', function() {
$plates = new Engine(['base_dir' => __DIR__ . '/fixtures/standard']);
$plates->addFolder('components', ['partials/components', '']);
$res = $plates->render('main', [
'name' => 'RJ & Emily',
'sidebar_style' => 'simple',
]);
$html = <<<html
<!DOCTYPE html>
<html>
<head>
<title>Profile - RJ & Emily</title>
</head>
<body>
<div class="alert success">
<p>Success!</p>
</div>
<div class="alert error">
<p>Sorry...</p>
</div>
<h1>Hi RJ &amp; Emily</h1>
<div id="sidebar" class="simple"></div>
<script type="text/javascript" id="main"></script>
<script type="text/javascript" id="sidebar"></script>
</body>
</html>
html;
expect($res)->equal($html);
});
});
101 changes: 101 additions & 0 deletions test/unit/extension-layout-sections.spec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

use League\Plates\Extension\LayoutSections\{
LayoutRenderTemplate,
Sections
};
use League\Plates\{
Template,
RenderTemplate\MockRenderTemplate,
Extension\RenderContext\FuncArgs
};
use const League\Plates\Extension\LayoutSections\{
START_APPEND,
START_PREPEND,
START_REPLACE
};
use function League\Plates\Extension\LayoutSections\{startFunc};
use function League\Plates\Extension\RenderContext\{endFunc};


describe('Extension\LayoutSections', function() {
describe('Sections', function() {
it('can add a section', function() {
$sections = new Sections(['foo' => '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');
});
});
});
81 changes: 81 additions & 0 deletions test/unit/extension-path.spec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

use League\Plates\Extension\Path\ResolvePathArgs;
use League\Plates\Template;
use function League\Plates\{
Extension\Path\idResolvePath,
Extension\Path\relativeResolvePath,
Extension\Path\prefixResolvePath,
Extension\Path\extResolvePath,
Util\joinPath,
Util\stack
};

describe('Extension\Path', function() {
beforeEach(function() {
$this->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');
});
});
});

0 comments on commit 91af3e1

Please sign in to comment.