From 23b1891273137b3875ac9bdaab7747e283e1940d Mon Sep 17 00:00:00 2001 From: Giuseppe Mazzapica Date: Mon, 6 Jul 2015 03:04:17 +0200 Subject: [PATCH] Fix tests mess, remove tests --- tests/_files/assets/foo.css | 0 tests/_files/assets/foo.jpg | 0 tests/_files/assets/foo.js | 0 tests/_files/bar/foo.php | 0 tests/_files/bar/second.php | 16 - tests/_files/foo/alias.php | 1 - tests/_files/foo/bar.inc | 2 - tests/_files/foo/blocks.php | 11 - tests/_files/foo/double.tpl.php | 1 - tests/_files/foo/foo.php | 2 - tests/_files/foo/main.php | 14 - tests/_files/stubs.php | 49 --- tests/_files/templates/expected.html | 113 ------ tests/_files/templates/extended.php | 33 -- tests/_files/templates/final-template.php | 25 -- tests/_files/templates/layout.php | 90 ----- tests/_files/templates/partials/partial-2.php | 6 - tests/_files/templates/partials/partial.php | 10 - tests/boot.php | 23 -- tests/src/TestCase.php | 94 ----- tests/src/TestCaseFunctional.php | 50 --- tests/src/Unit/EngineTest.php | 361 ------------------ tests/src/functional/AdvRenderTest.php | 116 ------ tests/src/functional/BlocksTest.php | 33 -- tests/src/functional/EngineTest.php | 150 -------- tests/src/functional/SimpleRenderTest.php | 81 ---- tests/src/unit/Blocks/BlockTest.php | 53 --- tests/src/unit/Blocks/BlocksTest.php | 167 -------- tests/src/unit/Blocks/FactoryTest.php | 42 -- tests/src/unit/Context/CollectionTest.php | 105 ----- tests/src/unit/Context/GlobalContextTest.php | 30 -- tests/src/unit/Context/RegexContextTest.php | 54 --- tests/src/unit/Context/SearchContextTest.php | 48 --- tests/src/unit/Extension/AuraHtmlTest.php | 99 ----- tests/src/unit/Extension/BlocksTest.php | 63 --- tests/src/unit/Extension/FiltersTest.php | 162 -------- tests/src/unit/Extension/HelpersTest.php | 262 ------------- tests/src/unit/Extension/LinksTest.php | 222 ----------- tests/src/unit/Extension/UriTest.php | 113 ------ tests/src/unit/Extension/WalkerTest.php | 140 ------- tests/src/unit/Kernel/ArraizeTest.php | 235 ------------ tests/src/unit/Kernel/CommandTest.php | 172 --------- tests/src/unit/Kernel/EscaperTest.php | 212 ---------- tests/src/unit/Kernel/EventsTest.php | 31 -- tests/src/unit/Section/FactoryTest.php | 75 ---- tests/src/unit/Section/SectionTest.php | 113 ------ tests/src/unit/Template/AliasTest.php | 66 ---- tests/src/unit/Template/FactoryTest.php | 92 ----- tests/src/unit/Template/FinderTest.php | 154 -------- tests/src/unit/Template/StackTest.php | 43 --- tests/src/unit/Template/TemplateTest.php | 267 ------------- 51 files changed, 4301 deletions(-) delete mode 100644 tests/_files/assets/foo.css delete mode 100644 tests/_files/assets/foo.jpg delete mode 100644 tests/_files/assets/foo.js delete mode 100644 tests/_files/bar/foo.php delete mode 100644 tests/_files/bar/second.php delete mode 100644 tests/_files/foo/alias.php delete mode 100644 tests/_files/foo/bar.inc delete mode 100644 tests/_files/foo/blocks.php delete mode 100644 tests/_files/foo/double.tpl.php delete mode 100644 tests/_files/foo/foo.php delete mode 100644 tests/_files/foo/main.php delete mode 100644 tests/_files/stubs.php delete mode 100644 tests/_files/templates/expected.html delete mode 100644 tests/_files/templates/extended.php delete mode 100644 tests/_files/templates/final-template.php delete mode 100644 tests/_files/templates/layout.php delete mode 100644 tests/_files/templates/partials/partial-2.php delete mode 100644 tests/_files/templates/partials/partial.php delete mode 100644 tests/boot.php delete mode 100644 tests/src/TestCase.php delete mode 100644 tests/src/TestCaseFunctional.php delete mode 100644 tests/src/Unit/EngineTest.php delete mode 100644 tests/src/functional/AdvRenderTest.php delete mode 100644 tests/src/functional/BlocksTest.php delete mode 100644 tests/src/functional/EngineTest.php delete mode 100644 tests/src/functional/SimpleRenderTest.php delete mode 100644 tests/src/unit/Blocks/BlockTest.php delete mode 100644 tests/src/unit/Blocks/BlocksTest.php delete mode 100644 tests/src/unit/Blocks/FactoryTest.php delete mode 100644 tests/src/unit/Context/CollectionTest.php delete mode 100644 tests/src/unit/Context/GlobalContextTest.php delete mode 100644 tests/src/unit/Context/RegexContextTest.php delete mode 100644 tests/src/unit/Context/SearchContextTest.php delete mode 100644 tests/src/unit/Extension/AuraHtmlTest.php delete mode 100644 tests/src/unit/Extension/BlocksTest.php delete mode 100644 tests/src/unit/Extension/FiltersTest.php delete mode 100644 tests/src/unit/Extension/HelpersTest.php delete mode 100644 tests/src/unit/Extension/LinksTest.php delete mode 100644 tests/src/unit/Extension/UriTest.php delete mode 100644 tests/src/unit/Extension/WalkerTest.php delete mode 100644 tests/src/unit/Kernel/ArraizeTest.php delete mode 100644 tests/src/unit/Kernel/CommandTest.php delete mode 100644 tests/src/unit/Kernel/EscaperTest.php delete mode 100644 tests/src/unit/Kernel/EventsTest.php delete mode 100644 tests/src/unit/Section/FactoryTest.php delete mode 100644 tests/src/unit/Section/SectionTest.php delete mode 100644 tests/src/unit/Template/AliasTest.php delete mode 100644 tests/src/unit/Template/FactoryTest.php delete mode 100644 tests/src/unit/Template/FinderTest.php delete mode 100644 tests/src/unit/Template/StackTest.php delete mode 100644 tests/src/unit/Template/TemplateTest.php diff --git a/tests/_files/assets/foo.css b/tests/_files/assets/foo.css deleted file mode 100644 index e69de29..0000000 diff --git a/tests/_files/assets/foo.jpg b/tests/_files/assets/foo.jpg deleted file mode 100644 index e69de29..0000000 diff --git a/tests/_files/assets/foo.js b/tests/_files/assets/foo.js deleted file mode 100644 index e69de29..0000000 diff --git a/tests/_files/bar/foo.php b/tests/_files/bar/foo.php deleted file mode 100644 index e69de29..0000000 diff --git a/tests/_files/bar/second.php b/tests/_files/bar/second.php deleted file mode 100644 index 88dc436..0000000 --- a/tests/_files/bar/second.php +++ /dev/null @@ -1,16 +0,0 @@ -layout('main', ['foo' => 'Bar!']) ?> - -section('one') ?> -World foo ?> -stop() ?> - -section('two') ?> -I Win -replace() ?> - -Buffalo Bill - -section('three') ?> -MAN -stop(); diff --git a/tests/_files/foo/alias.php b/tests/_files/foo/alias.php deleted file mode 100644 index 8251678..0000000 --- a/tests/_files/foo/alias.php +++ /dev/null @@ -1 +0,0 @@ -foo ?> \ No newline at end of file diff --git a/tests/_files/foo/bar.inc b/tests/_files/foo/bar.inc deleted file mode 100644 index 3d6365d..0000000 --- a/tests/_files/foo/bar.inc +++ /dev/null @@ -1,2 +0,0 @@ -data()); diff --git a/tests/_files/foo/blocks.php b/tests/_files/foo/blocks.php deleted file mode 100644 index d822e67..0000000 --- a/tests/_files/foo/blocks.php +++ /dev/null @@ -1,11 +0,0 @@ -block('spaceless') ?> - block('wrap', '
', '
') ?> - block('spaceless') ?> - - endblock('spaceless') ?> - endblock('wrap') ?> -endblock('spaceless') ?> diff --git a/tests/_files/foo/double.tpl.php b/tests/_files/foo/double.tpl.php deleted file mode 100644 index 8a41cc6..0000000 --- a/tests/_files/foo/double.tpl.php +++ /dev/null @@ -1 +0,0 @@ -I have 2 extensions diff --git a/tests/_files/foo/foo.php b/tests/_files/foo/foo.php deleted file mode 100644 index b8be172..0000000 --- a/tests/_files/foo/foo.php +++ /dev/null @@ -1,2 +0,0 @@ -data()); diff --git a/tests/_files/foo/main.php b/tests/_files/foo/main.php deleted file mode 100644 index a3c7316..0000000 --- a/tests/_files/foo/main.php +++ /dev/null @@ -1,14 +0,0 @@ -section('one') ?> -Hello foo.' ' ?> -stop() ?> - -Alone - -section('two') ?> -NO -stop() ?> - -section('three') ?> -YES -stop(); diff --git a/tests/_files/stubs.php b/tests/_files/stubs.php deleted file mode 100644 index 78f0919..0000000 --- a/tests/_files/stubs.php +++ /dev/null @@ -1,49 +0,0 @@ -value = $value; - } -} - -class ToArray extends Value -{ - public function toArray() - { - return ['toarray' => (array) $this->value]; - } -} - -class AsArray extends Value -{ - public function asArray() - { - return ['asarray' => (array) $this->value]; - } -} - -class Json implements JsonSerializable -{ - public function jsonSerialize() - { - return 'I am JSON'; - } -} - -class Target extends Value -{ -} - -class Transformer -{ - public function transform($object) - { - return is_object($object) ? ['transformed' => get_object_vars($object)] : false; - } -} diff --git a/tests/_files/templates/expected.html b/tests/_files/templates/expected.html deleted file mode 100644 index e7939fd..0000000 --- a/tests/_files/templates/expected.html +++ /dev/null @@ -1,113 +0,0 @@ - - - - - Foil is Awesome! - - - - - -
- -
- -
- -

Deep var test: Lorem Ipsum Dolor

- -

Default test: I am a default.

- -

__get test: TEST ME!

- -

Raw test: This is a strong tag html content

- -

Autoescape test: <strong>This is a strong tag html content</strong>

- -

Filter test: I AM LOWERCASE

- -

Advanced test: HELLO WORLD!

- -
- -
- Here something for you. -
- -
-

OVERRIDE a section defined in a PARTIAL

- -

- I am a partial. And this: "TEST ME!" is a var I share with template, but - this "I am a partial var!" is a specific partial var. -

- -
- -
-

- I am another partial. - This: "I am a partial var too." is a specific partial var. - This: "TEST ME!" is a var I share with template. - A shared var skipped: "Foil is Awesome!". -

-
- -
-

Should BE SUPPLYED by layout.

-
- -
- I am here. -
- -
- -

MAIN LAYOUT, first section

- -
- -

Should REPLACE layout first-child layout section.

- -
- -

MAIN LAYOUT, first section after child

- -

Should BE APPENDED to layout first section.

- -
- -
- -

Should REPLACE layout second section.

- -
-

Extended layout third section.

-

Should BE APPENDED to extended layout third section.

-
- -
- -
- -

EXTENDED LAYOUT, Out of any section, output a deep var Another deep var.

- -
- -

- I am another partial. - This: "!!!" is a specific partial var. - This: "TEST ME!" is a var I share with template. - A shared var skipped: "". -

- -

FINAL TEMPLATE, Out of any section, ouput a var TEST ME!

- -
-
- - - \ No newline at end of file diff --git a/tests/_files/templates/extended.php b/tests/_files/templates/extended.php deleted file mode 100644 index 391c8fd..0000000 --- a/tests/_files/templates/extended.php +++ /dev/null @@ -1,33 +0,0 @@ -layout('layout') ?> - -

EXTENDED LAYOUT, Out of any section, output a deep var v('a.var.in.extended') ?>

- -section('first') ?> - -

Should BE APPENDED to layout first section.

- -append() // first ?> - -section('first-child') ?> - -

Should REPLACE layout first-child layout section.

- -replace() // first-child ?> - -section('second') ?> - -

Should REPLACE layout second section.

- -
- - section('third') ?> -

Extended layout third section.

- append() // third ?> - -
- -replace(); // second ?> - -
- buffer() ?> -
diff --git a/tests/_files/templates/final-template.php b/tests/_files/templates/final-template.php deleted file mode 100644 index 4a7049d..0000000 --- a/tests/_files/templates/final-template.php +++ /dev/null @@ -1,25 +0,0 @@ -layout('extended') ?> - -section('a-section') ?> - -

Should BE SUPPLYED by layout.

- -stop() ?> - - -section('third') ?> - -

Should BE APPENDED to extended layout third section.

- -append() // third ?> - - -section('another-section') ?> - -

OVERRIDE a section defined in a PARTIAL

- -replace() // another-section ?> - -insert('partials/partial-2', ['a_partial_var' => '"!!!"'], ['test_me']) ?> - -

FINAL TEMPLATE, Out of any section, ouput a var v('test_me') ?>

diff --git a/tests/_files/templates/layout.php b/tests/_files/templates/layout.php deleted file mode 100644 index 7150a08..0000000 --- a/tests/_files/templates/layout.php +++ /dev/null @@ -1,90 +0,0 @@ - - - - - <?= $this->v('title', 'Default Title') ?> - - - - - - ww('menu', '
', '
  • %s
  • ') ?> - - wwif('menu', false, '
    %s
    ', '
  • %s
  • ') ?> - -
    - -

    Deep var test: v('a.pretty.deep.var') ?>

    - -

    Default test: v('i_do_not_exist', 'I am a default.') ?>

    - -

    __get test: test_me ?>

    - -

    Raw test: raw('html_content') ?>

    - -

    Autoescape test: html_content ?>

    - -

    Filter test: f('uppercase|reverse', 'lowercase') ?>

    - -

    Advanced test: v('a.var.0|uppercase|reverse') ?>

    - -
    - -
    - returnSomething() ?> -
    - -
    - insert('partials/partial', ['a_partial_var' => '"I am a partial var!"']) ?> -
    - -
    - raw('i_do_not_exist', $this->insert('partials/partial-2', ['a_partial_var' => '"I am a partial var too."'])) ?> -
    - -
    - supply('a-section') ?> -
    - -
    - supply('a-non-existent-section', 'I am here.') ?> -
    - -
    - - section('first') ?> - -

    MAIN LAYOUT, first section

    - -
    - - section('first-child') ?> - -

    MAIN LAYOUT, first child section

    - - stop() // first-child ?> - -
    - -

    MAIN LAYOUT, first section after child

    - - stop() // first ?> - -
    - -
    - - section('second') ?> - -

    MAIN LAYOUT, second section

    - - stop() // second ?> - -
    - -
    - buffer() ?> -
    - - - diff --git a/tests/_files/templates/partials/partial-2.php b/tests/_files/templates/partials/partial-2.php deleted file mode 100644 index 6fcd717..0000000 --- a/tests/_files/templates/partials/partial-2.php +++ /dev/null @@ -1,6 +0,0 @@ -

    - I am another partial. - This: v('a_partial_var') ?> is a specific partial var. - This: "test_me ?>" is a var I share with template. - A shared var skipped: "v('title') ?>". -

    diff --git a/tests/_files/templates/partials/partial.php b/tests/_files/templates/partials/partial.php deleted file mode 100644 index b788c1e..0000000 --- a/tests/_files/templates/partials/partial.php +++ /dev/null @@ -1,10 +0,0 @@ -section('another-section') ?> - -

    A SECTION defined in a PARTIAL

    - -stop() ?> - -

    - I am a partial. And this: "test_me ?>" is a var I share with template, but - this v('a_partial_var') ?> is a specific partial var. -

    diff --git a/tests/boot.php b/tests/boot.php deleted file mode 100644 index 1edf178..0000000 --- a/tests/boot.php +++ /dev/null @@ -1,23 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -$vendor = dirname(dirname(__FILE__)).'/vendor/'; - -if (! realpath($vendor)) { - die('Please install via Composer before running tests.'); -} - -require_once $vendor.'antecedent/patchwork/Patchwork.php'; -require_once $vendor.'autoload.php'; -require_once $vendor.'phpunit/phpunit/src/Framework/Assert/Functions.php'; - -putenv('FOIL_TESTS_BASEPATH='.__DIR__); - -unset($vendor); diff --git a/tests/src/TestCase.php b/tests/src/TestCase.php deleted file mode 100644 index 4fd229b..0000000 --- a/tests/src/TestCase.php +++ /dev/null @@ -1,94 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ -namespace Foil\Tests; - -use PHPUnit_Framework_TestCase; -use Brain\Monkey; -use Closure; -use InvalidArgumentException; -use LogicException; - -/** - * @author Giuseppe Mazzapica - * @package foil\foil - * @license http://opensource.org/licenses/MIT MIT - */ -class TestCase extends PHPUnit_Framework_TestCase -{ - protected function setUp() - { - parent::setUp(); - Monkey::setUp(); - } - - protected function tearDown() - { - Monkey::tearDown(); - parent::tearDown(); - } - - /** - * @param callable $closure - * @param object $object - * @param array $args - * @return mixed - */ - protected function bindClosure(Closure $closure, $object, array $args = []) - { - /** @var \Closure $closure */ - /** @noinspection PhpUndefinedMethodInspection */ - $closure = Closure::bind($closure, $object, get_class($object)); - - return call_user_func_array($closure, $args); - } - - /** - * @param string $property - * @param $object - * @return mixed - */ - protected function accessPrivateProperty($property, $object) - { - if (! is_string($property) || ! is_object($object)) { - throw new InvalidArgumentException( - __METHOD__.' needs a valid property name and a valid object.' - ); - } - - return $this->bindClosure(function ($property) { - if (! isset($this->$property)) { - throw new LogicException( - "{$property} is not a set on the object." - ); - } - - return $this->$property; - }, $object, [$property]); - } - - /** - * @param string $property - * @param mixed $value - * @param object $object - * @return mixed - */ - protected function setPrivateProperty($property, $value, $object) - { - if (! is_string($property) || ! is_object($object)) { - throw new InvalidArgumentException( - __METHOD__.' needs a valid property name and a valid object.' - ); - } - - return $this->bindClosure(function ($property, $value) { - $this->$property = $value; - }, $object, [$property, $value]); - } -} diff --git a/tests/src/TestCaseFunctional.php b/tests/src/TestCaseFunctional.php deleted file mode 100644 index 53e3281..0000000 --- a/tests/src/TestCaseFunctional.php +++ /dev/null @@ -1,50 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ -namespace Foil\Tests; - -use Foil\Foil; - -/** - * @author Giuseppe Mazzapica - * @package foil\foil - * @license http://opensource.org/licenses/MIT MIT - */ -class TestCaseFunctional extends TestCase -{ - /** - * @var \Foil\Engine - */ - protected $engine; - - /** - * @var \Pimple\Container - */ - protected $container; - - /** - * @param array $options - */ - public function initFoil(array $options = []) - { - $base = realpath(getenv('FOIL_TESTS_BASEPATH')).DIRECTORY_SEPARATOR; - $options = array_merge( - [ - 'folders' => [ - 'foo' => $base.implode(DIRECTORY_SEPARATOR, ['_files', 'foo']), - 'bar' => $base.implode(DIRECTORY_SEPARATOR, ['_files', 'bar']), - ], - ], - $options - ); - $app = Foil::boot($options); - $this->container = $this->accessPrivateProperty('container', $app); - $this->engine = $app->engine(); - } -} diff --git a/tests/src/Unit/EngineTest.php b/tests/src/Unit/EngineTest.php deleted file mode 100644 index 8cfddd4..0000000 --- a/tests/src/Unit/EngineTest.php +++ /dev/null @@ -1,361 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Foil\Tests\Unit; - -use Foil\Engine; -use Foil\Template\Finder; -use Foil\Tests\TestCase; -use Foil\Kernel\Events; -use Mockery; - -/** - * @author Giuseppe Mazzapica - * @license http://opensource.org/licenses/MIT MIT - * @package Foil - */ -class EngineTest extends TestCase -{ - /** - * @param \Foil\Kernel\Events $events - * @param \Foil\Template\Finder $finder - * @param string $render - * @return \Foil\Engine - */ - private function getEngine(Events $events = null, Finder $finder = null, $render = '') - { - /** @var \Foil\Template\Stack|\Mockery\MockInterface $stack */ - $stack = Mockery::mock('Foil\Template\Stack'); - - /** @var \Foil\Template\Finder|\Mockery\MockInterface $finder */ - $finder = $finder ?: Mockery::mock('Foil\Template\Finder'); - if (is_null($events)) { - /** @var \Foil\Kernel\Events|\Mockery\MockInterface $events */ - $events = Mockery::mock('Foil\Kernel\Events'); - $events - ->shouldReceive('fire') - ->with(Mockery::type('string'), Mockery::type('array')) - ->andReturnNull(); - $events - ->shouldReceive('on') - ->with(Mockery::type('string'), Mockery::type('Closure')) - ->andReturnNull(); - } - - $engine = new Engine($stack, $finder, $events); - - if ($render) { - /** @var \Foil\Template\Stack|\Mockery\MockInterface $template */ - $template = Mockery::mock('Foil\Template\Template'); - $template - ->shouldReceive('render') - ->with(Mockery::type('array')) - ->andReturn($render); - $stack - ->shouldReceive('factory') - ->with(__FILE__, $engine, Mockery::any()) - ->andReturn($template); - - $events - ->shouldReceive('fire') - ->once() - ->with('f.template.render', $template, Mockery::type('array')) - ->andReturnNull(); - $events - ->shouldReceive('fire') - ->once() - ->with('f.template.renderered', $template, $render) - ->andReturnNull(); - } - - return $engine; - } - - /** - * @expectedException \LogicException - */ - public function testCallFailsIfUnsafeFunction() - { - $engine = $this->getEngine(); - $engine->foo(); - } - - public function testCall() - { - /** @var \Foil\Kernel\Events|\Mockery\MockInterface $events */ - $events = Mockery::mock('Foil\Kernel\Events'); - $events - ->shouldReceive('fire') - ->once() - ->with('f.engine.call', 'useData', [['foo' => 'foo']]); - $engine = $this->getEngine($events); - - assertSame($engine, $engine->useData(['foo' => 'foo'])); - } - - public function testFireCallOnEvents() - { - /** @var \Foil\Kernel\Events|\Mockery\MockInterface $events */ - $events = Mockery::mock('Foil\Kernel\Events'); - $events - ->shouldReceive('fire') - ->once() - ->with('foo', 'bar', 'baz'); - $engine = $this->getEngine($events); - $engine->fire('foo', 'bar', 'baz'); - } - - public function testStatusIdleOnStart() - { - $engine = $this->getEngine(); - assertSame(Engine::STATUS_IDLE, $engine->status()); - } - - public function testLoadExtension() - { - /** @var \Foil\Contracts\ExtensionInterface $extension */ - $extension = Mockery::mock('Foil\Contracts\ExtensionInterface'); - /** @var \Foil\Kernel\Events|\Mockery\MockInterface $events */ - $events = Mockery::mock('Foil\Kernel\Events'); - $events - ->shouldReceive('fire') - ->once() - ->with('f.extension.load', $extension, ['foo' => 'foo'], false); - $engine = $this->getEngine($events); - - assertSame($engine, $engine->loadExtension($extension, ['foo' => 'foo'])); - } - - public function testRegisterFilter() - { - $filter = function () { - return true; - }; - /** @var \Foil\Kernel\Events|\Mockery\MockInterface $events */ - $events = Mockery::mock('Foil\Kernel\Events'); - $events - ->shouldReceive('fire') - ->once() - ->with('f.filter.register', 'foo', $filter); - $engine = $this->getEngine($events); - - assertSame($engine, $engine->registerFilter('foo', $filter)); - } - - public function testRegisterFunction() - { - $function = function () { - return true; - }; - /** @var \Foil\Kernel\Events|\Mockery\MockInterface $events */ - $events = Mockery::mock('Foil\Kernel\Events'); - $events - ->shouldReceive('fire') - ->once() - ->with('f.function.register', 'foo', $function, true); - $engine = $this->getEngine($events); - - assertSame($engine, $engine->registerFunction('foo', $function, true)); - } - - public function testRegisterBlock() - { - $block = function () { - return true; - }; - /** @var \Foil\Kernel\Events|\Mockery\MockInterface $events */ - $events = Mockery::mock('Foil\Kernel\Events'); - $events - ->shouldReceive('fire') - ->once() - ->with('f.block.register', 'foo', $block); - $engine = $this->getEngine($events); - - assertSame($engine, $engine->registerBlock('foo', $block)); - } - - public function testSetFolders() - { - /** @var \Foil\Template\Finder|\Mockery\MockInterface $finder */ - $finder = Mockery::mock('Foil\Template\Finder'); - $finder - ->shouldReceive('in') - ->once() - ->with(['foo', 'bar'], true) - ->andReturnNull(); - $engine = $this->getEngine(null, $finder); - - assertSame($engine, $engine->setFolders(['foo', 'bar'])); - } - - public function testAddFolderNoName() - { - /** @var \Foil\Template\Finder|\Mockery\MockInterface $finder */ - $finder = Mockery::mock('Foil\Template\Finder'); - $finder - ->shouldReceive('in') - ->once() - ->with(['foo']) - ->andReturnNull(); - $engine = $this->getEngine(null, $finder); - - assertSame($engine, $engine->addFolder('foo')); - } - - public function testAddFolderName() - { - /** @var \Foil\Template\Finder|\Mockery\MockInterface $finder */ - $finder = Mockery::mock('Foil\Template\Finder'); - $finder - ->shouldReceive('in') - ->once() - ->with(['name' => 'foo']) - ->andReturnNull(); - $engine = $this->getEngine(null, $finder); - - assertSame($engine, $engine->addFolder('foo', 'name')); - } - - public function testFindCallOnFinder() - { - /** @var \Foil\Template\Finder|\Mockery\MockInterface $finder */ - $finder = Mockery::mock('Foil\Template\Finder'); - $finder - ->shouldReceive('find') - ->once() - ->with('foo') - ->andReturn('/foo.php'); - $engine = $this->getEngine(null, $finder); - - assertSame('/foo.php', $engine->find('foo', 'meh')); - } - - public function testRenderFile() - { - $engine = $this->getEngine(null, null, 'Rendered!'); - assertSame('Rendered!', $engine->render(__FILE__)); - } - - public function testRender() - { - /** @var \Foil\Template\Finder|\Mockery\MockInterface $finder */ - $finder = Mockery::mock('Foil\Template\Finder'); - $finder - ->shouldReceive('find') - ->once() - ->with('foo') - ->andReturn(__FILE__); - $engine = $this->getEngine(null, $finder, 'Rendered!'); - assertSame('Rendered!', $engine->render('foo')); - } - - /** - * @expectedException \RuntimeException - */ - public function testRenderFailsIfTemplateNotFound() - { - /** @var \Foil\Template\Finder|\Mockery\MockInterface $finder */ - $finder = Mockery::mock('Foil\Template\Finder'); - $finder - ->shouldReceive('find') - ->once() - ->with('foo') - ->andReturn(false); - $engine = $this->getEngine(null, $finder); - $engine->render('foo'); - } - - public function testRenderTemplate() - { - $engine = $this->getEngine(null, null, 'Rendered!'); - assertSame('Rendered!', $engine->renderTemplate(__FILE__)); - } - - /** - * @expectedException \InvalidArgumentException - */ - public function testRenderSectionFailsIfBadSection() - { - $engine = $this->getEngine(); - $engine->renderSection('foo', true); - } - - public function testRenderSectionSingle() - { - /** @var \Foil\Kernel\Events|\Mockery\MockInterface $events */ - $events = Mockery::mock('Foil\Kernel\Events'); - $events - ->shouldReceive('on') - ->once() - ->with('f.sections.content', Mockery::type('Closure')) - ->andReturnNull(); - $events - ->shouldReceive('on') - ->with(Mockery::type('string'), Mockery::type('Closure')) - ->andReturnNull(); - $events - ->shouldReceive('removeListener') - ->once() - ->with('f.sections.content', Mockery::type('Closure')) - ->andReturnNull(); - - $engine = $this->getEngine($events, null, 'foo'); - - assertSame('', $engine->renderSection(__FILE__, 'foo')); - } - - public function testRenderSectionMulti() - { - /** @var \Foil\Kernel\Events|\Mockery\MockInterface $events */ - $events = Mockery::mock('Foil\Kernel\Events'); - $events - ->shouldReceive('on') - ->once() - ->with('f.sections.content', Mockery::type('Closure')) - ->andReturnNull(); - $events - ->shouldReceive('on') - ->with(Mockery::type('string'), Mockery::type('Closure')) - ->andReturnNull(); - $events - ->shouldReceive('removeListener') - ->once() - ->with('f.sections.content', Mockery::type('Closure')) - ->andReturnNull(); - - $engine = $this->getEngine($events, null, 'foo'); - - assertSame(['foo' => '', 'bar' => ''], $engine->renderSection(__FILE__, ['foo', 'bar'])); - } - - public function testRenderSections() - { - /** @var \Foil\Kernel\Events|\Mockery\MockInterface $events */ - $events = Mockery::mock('Foil\Kernel\Events'); - $events - ->shouldReceive('on') - ->once() - ->with('f.sections.content', Mockery::type('Closure')) - ->andReturnNull(); - $events - ->shouldReceive('on') - ->with(Mockery::type('string'), Mockery::type('Closure')) - ->andReturnNull(); - $events - ->shouldReceive('removeListener') - ->once() - ->with('f.sections.content', Mockery::type('Closure')) - ->andReturnNull(); - - $engine = $this->getEngine($events, null, 'foo'); - - assertSame([], $engine->renderSections(__FILE__)); - } -} diff --git a/tests/src/functional/AdvRenderTest.php b/tests/src/functional/AdvRenderTest.php deleted file mode 100644 index b8de476..0000000 --- a/tests/src/functional/AdvRenderTest.php +++ /dev/null @@ -1,116 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ -namespace Foil\Tests\Functional; - -use Foil\Tests\TestCaseFunctional; -use Foil\Context\SearchContext; -use Foil\Context\RegexContext; - -/** - * @author Giuseppe Mazzapica - * @package foil\foil - * @license http://opensource.org/licenses/MIT MIT - */ -class AdvRenderTest extends TestCaseFunctional -{ - private $templates_path; - - public function setUp() - { - parent::setUp(); - $this->initFoil(); - $base = realpath(getenv('FOIL_TESTS_BASEPATH')).DIRECTORY_SEPARATOR; - $this->templates_path = $base.implode(DIRECTORY_SEPARATOR, ['_files', 'templates']); - - $this->engine->addFolder($this->templates_path); - } - - /** - * @coversNothing - */ - public function testRender() - { - $this->registerCallbacks(); - $this->provideGlobalContext(); - $this->provideSearchContext(); - $this->provideRegexContext(); - $expected = file_get_contents($this->templates_path.'/expected.html'); - $render = $this->engine->render('final-template', ['test_me' => 'TEST ME!']); - assertSame($this->normalize($expected), $this->normalize($render)); - } - - private function registerCallbacks() - { - $this->engine->registerFunction('returnSomething', function () { - return 'Here something for you.'; - }); - $this->engine->registerFilter('uppercase', function ($string) { - return strtoupper($string); - }); - $this->engine->registerFilter('reverse', function ($string) { - return strrev($string); - }); - } - - private function provideGlobalContext() - { - $this->engine->useData([ - 'title' => 'Global Title', // should be overridded - 'menu' => [ - ['http://www.example.com', 'First Item'], - ['#', 'Second Item'], - ], - ]); - } - - private function provideSearchContext() - { - $context = new SearchContext('final', [ - 'a' => [ - 'var' => [ - '!dlrow olleh', - 'in' => [ - 'extended' => 'Another deep var.', - ], - ], - 'pretty' => [ - 'deep' => [ - 'var' => 'Lorem Ipsum Dolor', - ], - ], - ] - ]); - $context_failed = new SearchContext('foo', ['i_do_not_exist' => 'NOT SHOULD BE HERE']); - $this->engine->useContext($context); - $this->engine->useContext($context_failed); - $this->engine->useContext('final', ['lowercase' => 'esacrewol ma i']); - } - - private function provideRegexContext() - { - $context = new RegexContext('/-template\.php/'); - $context->setData([ - 'html_content' => 'This is a strong tag html content', - ]); - $context_failed = new RegexContext('/[0-9]+/', ['i_do_not_exist' => 'NOT SHOULD BE HERE']); - $this->engine->useContext($context); - $this->engine->useContext($context_failed); - $this->engine->useContext('/\.php$/', ['title' => 'Foil is Awesome!', ], true); - } - - /** - * @param $html - * @return string - */ - private function normalize($html) - { - return preg_replace('~[\s]+~s', '', $html); - } -} diff --git a/tests/src/functional/BlocksTest.php b/tests/src/functional/BlocksTest.php deleted file mode 100644 index 6c801e7..0000000 --- a/tests/src/functional/BlocksTest.php +++ /dev/null @@ -1,33 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ -namespace Foil\Tests\Functional; - -use Foil\Tests\TestCaseFunctional; - -/** - * @author Giuseppe Mazzapica - * @package foil\foil - * @license http://opensource.org/licenses/MIT MIT - */ -class BlocksTest extends TestCaseFunctional -{ - /** - * @coversNothing - */ - public function testBlock() - { - $this->initFoil(); - $this->engine->registerBlock('wrap', function ($output, $before, $later) { - return $before.$output.$later; - }); - $render = $this->engine->render('foo::blocks'); - assertSame('
    • a
    • a
    • a
    ', $render); - } -} diff --git a/tests/src/functional/EngineTest.php b/tests/src/functional/EngineTest.php deleted file mode 100644 index 859feb1..0000000 --- a/tests/src/functional/EngineTest.php +++ /dev/null @@ -1,150 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ -namespace Foil\Tests\Functional; - -use Foil\Tests\TestCaseFunctional; -use Foil\Context\SearchContext; -use Foil\Context\RegexContext; -use Mockery; - -/** - * @author Giuseppe Mazzapica - * @package foil\foil - * @license http://opensource.org/licenses/MIT MIT - */ -class EngineTest extends TestCaseFunctional -{ - public function setUp() - { - parent::setUp(); - $this->initFoil(); - } - - /** - * @coversNothing - */ - public function testInit() - { - // $this->engine is set in `TestCaseFunctional::setUp()` - assertInstanceOf('Foil\Engine', $this->engine); - } - - /** - * @coversNothing - */ - public function testUseData() - { - $this->engine->useData(['foo' => 'bar'], 'foo', 'bar'); - /** @var \Foil\Context\Collection $context */ - $context = $this->container['context']; - $context->accept('foo'); - assertSame(['foo' => 'bar'], $context->provide()); - } - - /** - * @coversNothing - */ - public function testContext() - { - $this->engine - ->useContext(new SearchContext('foo', ['search_1' => 1])) - ->useContext(new RegexContext('/^foo/', ['regex_1' => 1])) - ->useContext('fo', ['search_2' => 2]) - ->useContext('/[\w]+/', ['regex_2' => 2], true) - ->useContext('no', ['search_3' => 3]) - ->useContext('/[0-9]+/', ['regex_3' => 3], true); - /** @var \Foil\Context\Collection $context */ - $context = $this->container['context']; - $context->accept('foo'); - $expected = [ - 'search_1' => 1, - 'regex_1' => 1, - 'search_2' => 2, - 'regex_2' => 2, - ]; - assertSame($expected, $context->provide()); - } - - /** - * @coversNothing - */ - public function testRegisterExtension() - { - $e = Mockery::mock('Foil\Contracts\ExtensionInterface'); - $func = function ($a, $b = '') { - return $a.$b; - }; - $filter = function ($a, $args = []) { - return $a.$args[0]; - }; - $e->shouldReceive('setup')->with([])->andReturnNull(); - $e->shouldReceive('provideFilters')->andReturn(['foo' => $filter]); - $e->shouldReceive('provideFunctions')->andReturn(['foo' => $func]); - $this->engine->loadExtension($e); - - /** @var \Foil\Kernel\Command $command */ - $command = $this->container['command']; - assertSame('Hello!', $command->run('foo', 'Hello', '!')); - assertSame('Hello!', $command->filter('foo', 'Hello', ['!'])); - } - - /** - * @coversNothing - */ - public function testRegisterFunction() - { - $cb = function ($a, $b = '') { - echo 'Hello!'; - - return $a.$b; - }; - $this->engine->registerFunction('foo', $cb); - $expected = htmlentities('Hello', ENT_QUOTES, 'UTF-8', false); - /** @var \Foil\Kernel\Command $command */ - $command = $this->container['command']; - assertSame($expected, $command->run('foo', 'Hello', '')); - } - - /** - * @coversNothing - */ - public function testRegisterFilter() - { - $cb = function ($original, $a, $b) { - return $original.$a.$b; - }; - $this->engine->registerFilter('foo', $cb); - /** @var \Foil\Kernel\Command $command */ - $command = $this->container['command']; - assertSame('Hello!!', $command->filter('foo', 'Hello', ['!', '!'])); - } - - /** - * @coversNothing - */ - public function testRender() - { - assertSame('foo,bar', $this->engine->render('foo', ['foo', 'bar'])); - } - - /** - * @coversNothing - */ - public function testRenderSection() - { - $section = $this->engine->renderSection('main', 'three'); - $this->initFoil(); - $sections = $this->engine->renderSection('main', ['two', 'three', 'meh']); - $expected = ['two' => 'NO', 'three' => 'YES', 'meh' => '']; - - assertSame('YES', trim($section)); - assertSame($expected, array_map('trim', $sections)); - } -} diff --git a/tests/src/functional/SimpleRenderTest.php b/tests/src/functional/SimpleRenderTest.php deleted file mode 100644 index ecb1777..0000000 --- a/tests/src/functional/SimpleRenderTest.php +++ /dev/null @@ -1,81 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ -namespace Foil\Tests\Functional; - -use Foil\Tests\TestCaseFunctional; -use Foil\Template\Template; - -/** - * @author Giuseppe Mazzapica - * @package foil\foil - * @license http://opensource.org/licenses/MIT MIT - */ -class SimpleRenderTest extends TestCaseFunctional -{ - /** - * @coversNothing - */ - public function testSimpleRender() - { - $this->initFoil(); - $render = preg_replace('/[\s]+/', ' ', $this->engine->render('main')); - assertSame('Hello Alone NO YES', $render); - } - - /** - * @coversNothing - */ - public function testSectionEvent() - { - $this->initFoil(); - $section = ''; - /** @var \Foil\Kernel\Events $events */ - $events = $this->container['events']; - $events->on( - 'f.sections.content', - function ($name, $content) use (&$section) { - $name === 'three' and $section = trim(preg_replace('/[\s]+/', ' ', $content)); - } - ); - $this->expectOutputString(''); - $this->engine->render('second'); - - assertSame('YES MAN', $section); - } - - /** - * @coversNothing - */ - public function testLayoutRender() - { - $this->initFoil(); - $buffer = ''; - /** @var \Foil\Kernel\Events $events */ - $events = $this->container['events']; - $events->on( - 'f.template.renderlayout', - function ($layout, Template $template) use (&$buffer) { - $buffer = $template->buffer(); - } - ); - $render = preg_replace('/[\s]+/', ' ', $this->engine->render('second', ['foo' => 'Foo!'])); - assertSame('Hello Bar! World Foo! Alone I Win YES MAN', $render); - assertSame('Buffalo Bill', trim($buffer)); - } - - /** - * @coversNothing - */ - public function testSimpleRenderDoubleExt() - { - $this->initFoil(['ext' => 'tpl.php']); - assertSame('I have 2 extensions', trim($this->engine->render('double'))); - } -} diff --git a/tests/src/unit/Blocks/BlockTest.php b/tests/src/unit/Blocks/BlockTest.php deleted file mode 100644 index 5efd9f2..0000000 --- a/tests/src/unit/Blocks/BlockTest.php +++ /dev/null @@ -1,53 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Foil\Tests\Unit\Blocks; - -use Foil\Blocks\Block; -use Foil\Tests\TestCase; - -/** - * @author Giuseppe Mazzapica - * @license http://opensource.org/licenses/MIT MIT - * @package Foil - */ -class BlockTest extends TestCase -{ - public function testOpen() - { - $block = new Block('strtoupper'); - $block->open(); - echo 'Foo!'; - $data = ob_get_clean(); - $this->expectOutputString(''); - assertSame('Foo!', $data); - } - - /** - * @expectedException \LogicException - */ - public function testCloseFailsIfNotOpened() - { - $block = new Block('strtolower'); - $block->open(); - $block->close(); - $block->close(); - } - - public function testOpenClose() - { - $block = new Block('str_repeat'); - $block->open([2]); - echo 'Hello'; - $data = $block->close(); - $this->expectOutputString(''); - assertSame('HelloHello', $data); - } -} diff --git a/tests/src/unit/Blocks/BlocksTest.php b/tests/src/unit/Blocks/BlocksTest.php deleted file mode 100644 index 880596b..0000000 --- a/tests/src/unit/Blocks/BlocksTest.php +++ /dev/null @@ -1,167 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ -namespace Foil\Tests\Unit\Blocks; - -use Foil\Tests\TestCase; -use Foil\Blocks\Blocks; -use Mockery; - -/** - * @author Giuseppe Mazzapica - * @package foil\foil - * @license http://opensource.org/licenses/MIT MIT - */ -class BlocksTest extends TestCase -{ - /** - * @expectedException \InvalidArgumentException - */ - public function testAddFailsIfBadName() - { - /** @var \Foil\Blocks\Factory $factory */ - $factory = Mockery::mock('Foil\Blocks\Factory'); - - $blocks = new Blocks($factory); - $blocks->add(true, function ($val) { - return $val; - }); - } - - public function testAdd() - { - /** @var \Foil\Blocks\Factory|\Mockery\MockInterface $factory */ - $factory = Mockery::mock('Foil\Blocks\Factory'); - /** @var \Foil\Blocks\Block|\Mockery\MockInterface $block */ - $block = Mockery::mock('Foil\Blocks\Block'); - - $func = function () { - return 'foo'; - }; - - $factory->shouldReceive('factory') - ->once() - ->with($func, null) - ->andReturn($block); - - $blocks = new Blocks($factory); - $blocks->add('foo', $func); - - /** @var array $added */ - $added = $this->accessPrivateProperty('blocks', $blocks); - - assertArrayHasKey('foo', $added); - assertSame($block, $added['foo']); - } - - /** - * @expectedException \InvalidArgumentException - */ - public function testOpenFailsIfBadName() - { - /** @var \Foil\Blocks\Factory|\Mockery\MockInterface $factory */ - $factory = Mockery::mock('Foil\Blocks\Factory'); - $blocks = new Blocks($factory); - $blocks->open(true); - } - - /** - * @expectedException \InvalidArgumentException - */ - public function testOpenFailIfNotRegisteredBlock() - { - /** @var \Foil\Blocks\Factory|\Mockery\MockInterface $factory */ - $factory = Mockery::mock('Foil\Blocks\Factory'); - $blocks = new Blocks($factory); - $blocks->open('foo'); - } - - public function testOpen() - { - /** @var \Foil\Blocks\Factory|\Mockery\MockInterface $factory */ - $factory = Mockery::mock('Foil\Blocks\Factory'); - - /** @var \Foil\Blocks\Block|\Mockery\MockInterface $block */ - $block = Mockery::mock('Foil\Blocks\Block'); - $block->shouldReceive('open') - ->with(['foo', 'bar']) - ->once() - ->andReturnNull(); - - $blocks = new Blocks($factory); - - $this->bindClosure(function ($block) { - /** @noinspection PhpUndefinedFieldInspection */ - $this->blocks['foo'] = $block; - }, $blocks, [$block]); - - $blocks->open('foo', ['foo', 'bar']); - - /** @var \SplStack $buffers */ - $buffers = $this->accessPrivateProperty('buffers', $blocks); - - assertInstanceOf('\SplStack', $buffers); - $buffer = $buffers->pop(); - - assertInternalType('array', $buffer); - assertSame($block, $buffer[0]); - assertSame('foo', $buffer[1]); - } - - /** - * @expectedException \LogicException - */ - public function testCloseFailsIfNoBuffers() - { - /** @var \Foil\Blocks\Factory|\Mockery\MockInterface $factory */ - $factory = Mockery::mock('Foil\Blocks\Factory'); - $blocks = new Blocks($factory); - $blocks->close(); - } - - /** - * @expectedException \InvalidArgumentException - */ - public function testCloseFailsIfBadBuffer() - { - /** @var \Foil\Blocks\Factory|\Mockery\MockInterface $factory */ - $factory = Mockery::mock('Foil\Blocks\Factory'); - $blocks = new Blocks($factory); - - /** @var \Foil\Blocks\Block|\Mockery\MockInterface $block */ - $block = Mockery::mock('Foil\Blocks\Block'); - $block->shouldReceive('open')->andReturnNull(); - - $this->bindClosure(function ($block) { - /** @noinspection PhpUndefinedFieldInspection */ - $this->blocks = ['foo' => $block, 'bar' => $block]; - }, $blocks, [$block]); - - $blocks->open('foo'); - $blocks->close('bar'); - } - - public function testClose() - { - /** @var \Foil\Blocks\Factory|\Mockery\MockInterface $factory */ - $factory = Mockery::mock('Foil\Blocks\Factory'); - /** @var \Foil\Blocks\Block|\Mockery\MockInterface $block */ - $block = Mockery::mock('Foil\Blocks\Block'); - $block->shouldReceive('close')->once()->andReturn('Closed!'); - - $blocks = new Blocks($factory); - - $this->bindClosure(function ($block) { - /** @noinspection PhpUndefinedFieldInspection */ - $this->buffers->push([$block, 'foo']); - }, $blocks, [$block]); - - assertSame('Closed!', $blocks->close('foo')); - } -} diff --git a/tests/src/unit/Blocks/FactoryTest.php b/tests/src/unit/Blocks/FactoryTest.php deleted file mode 100644 index a59a5a8..0000000 --- a/tests/src/unit/Blocks/FactoryTest.php +++ /dev/null @@ -1,42 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Foil\Tests\Unit\Blocks; - -use Foil\Tests\TestCase; -use Foil\Blocks\Factory; -use Mockery; - -/** - * @author Giuseppe Mazzapica - * @license http://opensource.org/licenses/MIT MIT - * @package Foil - */ -class FactoryTest extends TestCase -{ - public function testFactory() - { - $factory = new Factory(); - $f1 = $factory->factory('strtolower'); - $f2 = $factory->factory('strtolower', 12); - $f3 = $factory->factory('strtolower', __CLASS__); - $mock = Mockery::mock('Foil\Contracts\BlockInterface'); - $f4 = $factory->factory('strtolower', get_class($mock)); - - assertInstanceOf(Factory::CONTRACT, $f1); - assertInstanceOf(Factory::CONTRACT, $f2); - assertInstanceOf(Factory::CONTRACT, $f3); - assertInstanceOf(Factory::CONTRACT, $f4); - assertInstanceOf(Factory::DEFAULT_CLASS, $f1); - assertInstanceOf(Factory::DEFAULT_CLASS, $f2); - assertInstanceOf(Factory::DEFAULT_CLASS, $f3); - assertInstanceOf(get_class($mock), $f4); - } -} diff --git a/tests/src/unit/Context/CollectionTest.php b/tests/src/unit/Context/CollectionTest.php deleted file mode 100644 index 03f38ca..0000000 --- a/tests/src/unit/Context/CollectionTest.php +++ /dev/null @@ -1,105 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ -namespace Foil\Tests\Unit\Context; - -use Foil\Context\Collection; -use Foil\Tests\TestCase; -use Mockery; - -/** - * @author Giuseppe Mazzapica - * @package foil\foil - * @license http://opensource.org/licenses/MIT MIT - */ -class CollectionTest extends TestCase -{ - /** - * @return \Foil\Context\Collection - */ - private function getCollection() - { - /** @var \Foil\Kernel\Events|\Mockery\MockInterface $events */ - $events = Mockery::mock('Foil\Kernel\Events'); - $events->shouldReceive('fire')->andReturnNull(); - $collection = new Collection($events); - $collection->setData(['foo' => 'bar']); - - return $collection; - } - - /** - * @param $data - * @param bool $accept - * @return \Mockery\MockInterface|\Foil\Contracts\ContextInterface - */ - private function getContextMocked($data, $accept = true) - { - $con = Mockery::mock('Foil\Contracts\ContextInterface'); - $con->shouldReceive('accept')->once()->with('foo')->andReturn($accept); - $con->shouldReceive('provide')->withNoArgs()->andReturn($data); - - return $con; - } - - public function testAccept() - { - $collection = $this->getCollection(); - assertTrue($collection->accept('template')); - assertFalse($collection->accept(1)); - $collection->disallow(); - assertFalse($collection->accept('template')); - } - - public function testAddHasRemove() - { - /** @var \Foil\Contracts\ContextInterface $context */ - $context = Mockery::mock('Foil\Contracts\ContextInterface'); - $clone = clone $context; - $collection = $this->getCollection(); - $collection->add($context); - assertTrue($collection->has($context)); - assertFalse($collection->has($clone)); - $collection->remove($context); - assertFalse($collection->has($context)); - } - - public function testProvideReturnDataIfNoTemplate() - { - $c = $this->getCollection(); - assertSame(['foo' => 'bar'], $c->provide()); - } - - public function testProvideNoStorage() - { - $collection = $this->getCollection(); - $this->setPrivateProperty('template', 'foo', $collection); - - assertSame(['foo' => 'bar'], $collection->provide()); - } - - public function testProvide() - { - $con1 = $this->getContextMocked(['one' => 'one']); - $con2 = $this->getContextMocked(['two' => 'two']); - $con3 = $this->getContextMocked(['foo' => 'baz']); - $con4 = $this->getContextMocked(['x' => 'x'], false); - $collection = $this->getCollection(); - $collection->add($con1); - $collection->add($con2); - $collection->add($con3); - $collection->add($con4); - $this->bindClosure(function () { - /** @noinspection PhpUndefinedFieldInspection */ - $this->template = 'foo'; - }, $collection); - - assertSame(['foo' => 'baz', 'one' => 'one', 'two' => 'two'], $collection->provide()); - } -} diff --git a/tests/src/unit/Context/GlobalContextTest.php b/tests/src/unit/Context/GlobalContextTest.php deleted file mode 100644 index dda4dc8..0000000 --- a/tests/src/unit/Context/GlobalContextTest.php +++ /dev/null @@ -1,30 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ -namespace Foil\Tests\Unit\Context; - -use Foil\Tests\TestCase; -use Foil\Context\GlobalContext; - -/** - * @author Giuseppe Mazzapica - * @package foil\foil - * @license http://opensource.org/licenses/MIT MIT - */ -class GlobalContextTest extends TestCase -{ - public function testAccept() - { - $c = new GlobalContext(['foo' => 'bar']); - assertTrue($c->accept('foo')); - assertTrue($c->accept('fooo')); - assertFalse($c->accept(false)); - assertSame(['foo' => 'bar'], $c->provide()); - } -} diff --git a/tests/src/unit/Context/RegexContextTest.php b/tests/src/unit/Context/RegexContextTest.php deleted file mode 100644 index e2d7f55..0000000 --- a/tests/src/unit/Context/RegexContextTest.php +++ /dev/null @@ -1,54 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ -namespace Foil\Tests\Unit\Context; - -use Foil\Tests\TestCase; -use Foil\Context\RegexContext; - -/** - * @author Giuseppe Mazzapica - * @package foil\foil - * @license http://opensource.org/licenses/MIT MIT - * - */ -class RegexContextTest extends TestCase -{ - /** - * @expectedException \InvalidArgumentException - */ - public function testConstructorFailsIfBadArgument() - { - new RegexContext(['|^f[\w-]+\.php$|'], ['foo' => 'bar']); - } - - /** - * @expectedException \InvalidArgumentException - */ - public function testAcceptFailsIfBadTemplate() - { - $context = new RegexContext('/.+/'); - $context->accept(1); - } - - public function testAccept() - { - $c = new RegexContext('|^f[\w-]+\.php$|', ['foo' => 'bar']); - assertFalse($c->accept('/path/to/foo.php')); - assertFalse($c->accept('path/to/fo-o.php')); - assertFalse($c->accept('path/to/foo.phtml')); - assertFalse($c->accept('path/to/f.oo.php')); - $c->acceptBasename(); - assertTrue($c->accept('/path/to/foo.php')); - assertTrue($c->accept('path/to/fo-o.php')); - assertFalse($c->accept('path/to/foo.phtml')); - assertFalse($c->accept('path/to/f.oo.php')); - assertSame(['foo' => 'bar'], $c->provide()); - } -} diff --git a/tests/src/unit/Context/SearchContextTest.php b/tests/src/unit/Context/SearchContextTest.php deleted file mode 100644 index bd5d043..0000000 --- a/tests/src/unit/Context/SearchContextTest.php +++ /dev/null @@ -1,48 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ -namespace Foil\Tests\Unit\Context; - -use Foil\Tests\TestCase; -use Foil\Context\SearchContext; - -/** - * @author Giuseppe Mazzapica - * @package foil\foil - * @license http://opensource.org/licenses/MIT MIT - */ -class SearchContextTest extends TestCase -{ - /** - * @expectedException \InvalidArgumentException - */ - public function testConstructorFailsIfBadNeedle() - { - new SearchContext(1); - } - - /** - * @expectedException \InvalidArgumentException - */ - public function testAcceptFailsIfBadTemplate() - { - $c = new SearchContext('foo\bar'); - $c->accept(true); - } - - public function testAccept() - { - $c = new SearchContext('foo\bar', ['foo' => 'bar']); - assertTrue($c->accept('foo\bar.php')); - assertTrue($c->accept('foo\bar\baz.php')); - assertFalse($c->accept('foo')); - assertFalse($c->accept('bar')); - assertSame(['foo' => 'bar'], $c->provide()); - } -} diff --git a/tests/src/unit/Extension/AuraHtmlTest.php b/tests/src/unit/Extension/AuraHtmlTest.php deleted file mode 100644 index 06ac9b6..0000000 --- a/tests/src/unit/Extension/AuraHtmlTest.php +++ /dev/null @@ -1,99 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Foil\Tests\Unit\Extension; - -use Foil\Tests\TestCase; -use Foil\Extensions\AuraHtml; -use Aura\Html\HelperLocatorFactory; - -/** - * @author Giuseppe Mazzapica - * @license http://opensource.org/licenses/MIT MIT - * @package Foil - */ -class AuraHtmlTest extends TestCase -{ - private function sut($register_tag_functions = false) - { - return new AuraHtml((new HelperLocatorFactory())->newInstance(), $register_tag_functions); - } - - public function testHtml() - { - $aura = $this->sut(); - $expected = '<b>"foo"</b>'; - $html = $aura->html( - 'anchor', - 'http://example.com', - '"foo"', - ['class' => ['foo', 'bar']] - ); - assertSame($expected, $html); - } - - public function testHtmlEscapeAttr() - { - $aura = $this->sut(); - $expected = 'foo'; - $html = $aura->html( - 'anchor', - 'http://example.com', - 'foo', - ['class' => ['\'foo\'', '"bar"', '']] - ); - assertSame($expected, $html); - } - - public function testInput() - { - $aura = $this->sut(); - $data = [ - 'name' => 'foo', - 'value' => 'y', - 'attribs' => [ - 'label' => 'Check me', - 'value' => 'y', - 'value_unchecked' => '0', - ], - ]; - $expected = - ''; - assertSame($expected, trim($aura->html('checkbox', $data))); - } - - public function testHtmlInput() - { - $aura = $this->sut(); - $data = [ - 'type' => 'checkbox', - 'name' => 'foo', - 'value' => 'y', - 'attribs' => [ - 'label' => 'Check me', - 'value' => 'y', - 'value_unchecked' => '0', - ], - ]; - $expected = - ''; - assertSame($expected, trim($aura->html('input', $data))); - } - - public function testTags() - { - $functions = $this->sut(true)->provideFunctions(); - $expected = 'hello.jpg'; - $img = call_user_func($functions['img'], '/images/hello.jpg', ['id' => 'image-id']); - assertSame($expected, trim($img)); - } -} diff --git a/tests/src/unit/Extension/BlocksTest.php b/tests/src/unit/Extension/BlocksTest.php deleted file mode 100644 index a4d2220..0000000 --- a/tests/src/unit/Extension/BlocksTest.php +++ /dev/null @@ -1,63 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ -namespace Foil\Tests\Unit\Extension; - -use Foil\Extensions\Blocks; -use Foil\Tests\TestCase; -use Mockery; - -/** - * @author Giuseppe Mazzapica - * @package foil\foil - * @license http://opensource.org/licenses/MIT MIT - */ -class BlocksTest extends TestCase -{ - public function testProvideFunctions() - { - /** @var \Foil\Blocks\Blocks $blocks */ - $blocks = Mockery::mock('Foil\Blocks\Blocks'); - $extension = new Blocks($blocks); - $functions = $extension->provideFunctions(); - assertInternalType('array', $functions); - assertArrayHasKey('block', $functions); - assertArrayHasKey('endblock', $functions); - assertInternalType('callable', $functions['block']); - assertInternalType('callable', $functions['endblock']); - } - - public function testBlock() - { - /** @var \Foil\Blocks\Blocks|\Mockery\MockInterface $blocks */ - $blocks = Mockery::mock('Foil\Blocks\Blocks'); - $blocks->shouldReceive('open') - ->once() - ->with('foo', [1, 'bar']) - ->andReturnNull(); - $extension = new Blocks($blocks); - $functions = $extension->provideFunctions(); - $block = $functions['block']; - $block('foo', 1, 'bar'); - } - - public function testEndBlock() - { - /** @var \Foil\Blocks\Blocks|\Mockery\MockInterface $blocks */ - $blocks = Mockery::mock('Foil\Blocks\Blocks'); - $blocks->shouldReceive('close') - ->once() - ->andReturn('Works!'); - $extension = new Blocks($blocks); - $functions = $extension->provideFunctions(); - $block = $functions['endblock']; - $this->expectOutputString('Works!'); - $block(); - } -} diff --git a/tests/src/unit/Extension/FiltersTest.php b/tests/src/unit/Extension/FiltersTest.php deleted file mode 100644 index ead16b8..0000000 --- a/tests/src/unit/Extension/FiltersTest.php +++ /dev/null @@ -1,162 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ -namespace Foil\Tests\Unit\Extension; - -use Foil\Tests\TestCase; -use Foil\Extensions\Filters; - -/** - * @author Giuseppe Mazzapica - * @package foil\foil - * @license http://opensource.org/licenses/MIT MIT - */ -class FiltersTest extends TestCase -{ - /** - * @expectedException \InvalidArgumentException - */ - public function testFirstFailsIfBadArgs() - { - $filters = new Filters(); - $filters->first(true); - } - - public function testFirst() - { - $filters = new Filters(); - assertSame('foo', $filters->first(['foo', 'bar'])); - assertSame('foo', $filters->first(['a' => 'foo', 'foo' => 'bar'])); - assertSame('F', $filters->first('Foo')); - } - /** - * @expectedException \InvalidArgumentException - */ - public function testLastFailsIfBadArgs() - { - $filters = new Filters(); - $filters->last(true); - } - - public function testLast() - { - $filters = new Filters(); - assertSame('bar', $filters->last(['foo', 'bar'])); - assertSame('bar', $filters->last(['a' => 'foo', 'foo' => 'bar'])); - assertSame('o', $filters->last('Foo')); - } - - /** - * @expectedException \InvalidArgumentException - */ - public function testChunkFailsInNoNumber() - { - $filters = new Filters(); - $filters->chunk([], '3'); - } - - public function testChunk() - { - $filters = new Filters(); - $data = ['a', 'b', 'c', 'd', 'e', 'f', 'g']; - $three = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g']]; - $threeFilled = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'fill', 'fill']]; - $dataFilled = [['a', 'b', 'c', 'd', 'e', 'f', 'g', 'fill', 'fill', 'fill']]; - $dataAssoc = ['a' => 'A', 'b' => 'B', 'c' => 'C', 'd' => 'D', 'e' => 'E', 'f' => 'F']; - $threeAssoc = [['A', 'B', 'C'], ['D', 'E', 'F']]; - $datAssocFilled = [['A', 'B', 'C', 'D', 'E', 'F', 'fill', 'fill', 'fill', 'fill']]; - assertSame($three, $filters->chunk($data, 3)); - assertSame([$data], $filters->chunk($data, 7)); - assertSame([$data], $filters->chunk($data, 10)); - assertSame($threeFilled, $filters->chunk($data, 3, 'fill')); - assertSame([$data], $filters->chunk($data, 7, 'fill')); - assertSame($dataFilled, $filters->chunk($data, 10, 'fill')); - assertSame($threeAssoc, $filters->chunk($dataAssoc, 3)); - assertSame([array_values($dataAssoc)], $filters->chunk($dataAssoc, 10)); - assertSame($threeAssoc, $filters->chunk($dataAssoc, 3, 'fill')); - assertSame($datAssocFilled, $filters->chunk($dataAssoc, 10, 'fill')); - } - - /** - * @expectedException \InvalidArgumentException - */ - public function testIsFirstFailsIfBadArgs() - { - $filters = new Filters(); - $filters->isFirst(true, ''); - } - - public function testIsFirst() - { - $filters = new Filters(); - $data = ['a', 'b', 'c', 'd', 'e', 'f', 'g']; - $data_assoc = ['a' => 'A', 'b' => 'B', 'c' => 'C', 'd' => 'D', 'e' => 'E', 'f' => 'F']; - $str = 'ABCDEFGHIJKL'; - assertTrue($filters->isFirst($data, 'a')); - assertTrue($filters->isFirst($data_assoc, 'A')); - assertTrue($filters->isFirst($str, 'A')); - assertFalse($filters->isFirst($data, 'c')); - assertFalse($filters->isFirst($data_assoc, 'a')); - assertFalse($filters->isFirst($str, 'a')); - } - - /** - * @expectedException \InvalidArgumentException - */ - public function testIsLastFailsIfBadArgs() - { - $filters = new Filters(); - $filters->isLast(true, ''); - } - - public function testIsLast() - { - $filters = new Filters(); - $data = ['a', 'b', 'c', 'd', 'e', 'f', 'g']; - $data_assoc = ['a' => 'A', 'b' => 'B', 'c' => 'C', 'd' => 'D', 'e' => 'E', 'f' => 'F']; - $str = 'ABCDEFGHIJKL'; - assertTrue($filters->isLast($data, 'g')); - assertTrue($filters->isLast($data_assoc, 'F')); - assertTrue($filters->isLast($str, 'L')); - assertFalse($filters->isLast($data, 'f')); - assertFalse($filters->isLast($data_assoc, 'f')); - assertFalse($filters->isLast($str, 'l')); - } - - public function testIndex() - { - $filters = new Filters(); - $data = ['a', 'b', 'c', 'd', 'e', 'f']; - $data_assoc = ['a' => 'A', 'b' => 'B', 'c' => 'C', 'd' => 'D', 'e' => 'E', 'f' => 'F']; - assertSame(1, $filters->index($data, 'a')); - assertSame(-1, $filters->index($data, 'A')); - assertSame(1, $filters->index($data_assoc, 'A')); - assertSame(-1, $filters->index($data_assoc, 'H')); - assertTrue($filters->index($data, 'a', 1)); - assertFalse($filters->index($data, 'a', 0)); - assertFalse($filters->index($data_assoc, 'A', 0)); - assertFalse($filters->index($data_assoc, 'A', 'a')); - } - - public function testIndex0() - { - $filters = new Filters(); - $data = ['a', 'b', 'c', 'd', 'e', 'f']; - $data_assoc = ['a' => 'A', 'b' => 'B', 'c' => 'C', 'd' => 'D', 'e' => 'E', 'f' => 'F']; - assertSame(0, $filters->index0($data, 'a')); - assertSame(-1, $filters->index0($data, 'A')); - assertSame(0, $filters->index0($data_assoc, 'A')); - assertSame(-1, $filters->index0($data_assoc, 'H')); - assertTrue($filters->index0($data, 'a', 0)); - assertTrue($filters->index0($data_assoc, 'A', 0)); - assertFalse($filters->index0($data, 'a', 1)); - assertFalse($filters->index0($data_assoc, 'A', 1)); - assertFalse($filters->index0($data_assoc, 'A', 'a')); - } -} diff --git a/tests/src/unit/Extension/HelpersTest.php b/tests/src/unit/Extension/HelpersTest.php deleted file mode 100644 index b579510..0000000 --- a/tests/src/unit/Extension/HelpersTest.php +++ /dev/null @@ -1,262 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ -namespace Foil\Tests\Unit\Extension; - -use Aura\Html\Escaper\AttrEscaper; -use Aura\Html\Escaper\HtmlEscaper; -use Foil\Tests\TestCase; -use Foil\Extensions\Helpers; -use Mockery; - -/** - * @author Giuseppe Mazzapica - * @package foil\foil - * @license http://opensource.org/licenses/MIT MIT - */ -class HelpersTest extends TestCase -{ - /** - * @param array $data - * @param bool|string $strict - * @param bool $autoescape - * @return \Foil\Extensions\Helpers - */ - private function getHelpers($data = [], $strict = false, $autoescape = true) - { - /** @var \Foil\Contracts\TemplateInterface|\Mockery\MockInterface $template */ - $template = Mockery::mock('Foil\Contracts\TemplateInterface'); - $template->shouldReceive('data')->andReturn($data); - $template - ->shouldReceive('filter') - ->with(Mockery::type('array'), Mockery::any()) - ->andReturnUsing(function (array $filters, $data) { - return $data.' + '.implode(',', $filters); - }); - - /** @var \Foil\Template\Stack|\Mockery\MockInterface $stack */ - $stack = Mockery::mock('\Foil\Template\Stack'); - $stack->shouldReceive('template')->andReturn($template); - - /** @var \Foil\Kernel\Escaper|\Mockery\MockInterface $escaper */ - $escaper = Mockery::mock('\Foil\Kernel\Escaper'); - $escaper - ->shouldReceive('escape') - ->with(Mockery::any(), Mockery::anyOf('html', 'js', 'css'), Mockery::any()) - ->andReturnUsing(function ($data) { - if (is_string($data)) { - return (new HtmlEscaper())->__invoke($data); - } elseif (is_array($data)) { - return array_map(new HtmlEscaper(), $data); - } - - return $data; - }); - $escaper - ->shouldReceive('escape') - ->with(Mockery::any(), 'attr', Mockery::any()) - ->andReturnUsing(function ($data) { - return (new AttrEscaper(new HtmlEscaper()))->__invoke($data); - }); - $escaper - ->shouldReceive('decode') - ->andReturnUsing(function ($data) { - if (is_string($data)) { - return html_entity_decode($data); - } elseif (is_array($data)) { - return array_map('html_entity_decode', $data); - } - - return $data; - }); - - $helpers = new Helpers( - $escaper, - [ - 'autoescape' => $autoescape, - 'strict_variables' => $strict - ] - ); - $helpers->setStack($stack); - - return $helpers; - } - - public function testProvideFilters() - { - $helpers = $this->getHelpers(); - $filters = $helpers->provideFilters(); - foreach ($filters as $filter) { - assertInternalType('callable', $filter); - } - } - - public function testProvideFunctions() - { - $helpers = $this->getHelpers(); - $filters = $helpers->provideFunctions(); - foreach ($filters as $filter) { - assertInternalType('callable', $filter); - } - } - - public function testVariableEscape() - { - $helpers = $this->getHelpers(['foo' => '

    foo

    ']); - assertSame(htmlentities('

    foo

    '), $helpers->variable('foo')); - } - - public function testVariableNoEscape() - { - $helpers = $this->getHelpers(['foo' => '

    foo

    '], true, false); - assertSame('

    foo

    ', $helpers->variable('foo')); - } - - public function testEscape() - { - $helpers = $this->getHelpers(['foo' => '

    foo

    ']); - assertSame(htmlentities('

    foo

    '), $helpers->escape('foo')); - assertSame(htmlentities('

    foo

    '), $helpers->escape('bar', '

    foo

    ')); - } - - public function testEscapeJs() - { - $helpers = $this->getHelpers(['foo' => '

    foo

    ']); - assertSame(htmlentities('

    foo

    '), $helpers->escapeJs('foo')); - assertSame(htmlentities('

    foo

    '), $helpers->escapeJs('bar', '

    foo

    ')); - } - - public function testEscapeCss() - { - $helpers = $this->getHelpers(['foo' => '

    foo

    ']); - assertSame(htmlentities('

    foo

    '), $helpers->escapeCss('foo')); - assertSame(htmlentities('

    foo

    '), $helpers->escapeCss('bar', '

    foo

    ')); - } - - public function testEscapeAttr() - { - $helpers = $this->getHelpers(['foo' => ['class' => ['foescapeAttr('foo')); - } - - public function testDecode() - { - $helpers = $this->getHelpers(['foo' => htmlentities('

    foo

    ')]); - assertSame('

    foo

    ', $helpers->decode('foo')); - } - - public function testEntities() - { - $helpers = $this->getHelpers(); - assertSame(htmlentities('

    foo

    '), $helpers->entities('

    foo

    ')); - } - - public function testDecodeEntities() - { - $helpers = $this->getHelpers(); - assertSame('

    foo

    ', $helpers->decodeEntities(htmlentities('

    foo

    '))); - } - - public function testRawDefault() - { - $helpers1 = $this->getHelpers(); - assertSame('foo', $helpers1->raw('some', 'foo')); - $helpers2 = $this->getHelpers(['foo' => 'bar']); - assertSame('foo', $helpers2->raw('some', 'foo')); - } - - public function testRawDefaultClosureEcho() - { - $output = $this->getHelpers()->raw('some', function () { - echo "I'm loving it!"; - }); - assertSame("I'm loving it!", $output); - } - - public function testRawDefaultClosureReturn() - { - $output = $this->getHelpers()->raw('some', function () { - return "I'm loving it!"; - }); - assertSame("I'm loving it!", $output); - } - - public function testRaw() - { - $helpers1 = $this->getHelpers(['some' => 'bar']); - assertSame('bar', $helpers1->raw('some', 'foo')); - $data = [ - 'foo' => [ - 'bar' => [ - 'baz' => [ - 'some' => 'Deep!', - ], - ], - ], - ]; - $helpers2 = $this->getHelpers($data); - assertSame('Deep! + f1,f2', $helpers2->raw('foo.bar.baz.some|f1|f2', 'foo')); - } - - public function testRawFilters() - { - $data = [ - 'foo' => [ - 'bar' => [ - 'baz' => [ - 'some' => 'Deep!', - ], - ], - ], - ]; - $helpers = $this->getHelpers($data); - assertSame('Deep! + f1,f2', $helpers->raw('foo.bar.baz.some', 'foo', 'f1|f2')); - } - - /** - * @expectedException \RuntimeException - */ - public function testStrictVariablesException() - { - $helpers = $this->getHelpers([], true); - $helpers->getIn(['foo' => 'bar'], 'bar.baz', true); - } - - /** - * @expectedException \PHPUnit_Framework_Error_Notice - */ - public function testStrictVariablesNotice() - { - $helpers = $this->getHelpers([], 'notice'); - $helpers->getIn(['foo' => 'bar'], 'bar.baz', true); - } - - public function testAsArray() - { - $helpers = $this->getHelpers([ - 'string' => '

    foo

    ', - 'object' => (object) ['id' => 'foo'], - ]); - assertSame([htmlentities('

    foo

    ')], $helpers->asArray('string')); - assertSame(['id' => 'foo'], $helpers->asArray('object')); - } - - public function testAsArrayRaw() - { - $helpers = $this->getHelpers(['object' => (object) ['id' => '

    foo

    ']]); - assertSame(['id' => '

    foo

    '], $helpers->asArrayRaw('object')); - } - - public function testIfNot() - { - $helpers = $this->getHelpers(['foo' => 'foo', 'bar' => 'bar']); - assertSame('Yes', $helpers->ifNot('baz', 'Yes')); - assertSame('', $helpers->ifNot('foo', 'Yes')); - } -} diff --git a/tests/src/unit/Extension/LinksTest.php b/tests/src/unit/Extension/LinksTest.php deleted file mode 100644 index dfbe94e..0000000 --- a/tests/src/unit/Extension/LinksTest.php +++ /dev/null @@ -1,222 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ -namespace Foil\Tests\Unit\Extension; - -use Foil\Tests\TestCase; -use Foil\Extensions\Links; - -/** - * @author Giuseppe Mazzapica - * @package foil\foil - * @license http://opensource.org/licenses/MIT MIT - */ -class LinksTest extends TestCase -{ - public function testProvideFunctions() - { - $links = new Links(); - $functions = $links->provideFunctions(); - foreach ($functions as $function) { - assertInternalType('callable', $function); - } - } - - public function testLinkNoDomainNoDirs() - { - $links = new Links(); - assertSame('/foo/bar', $links->link('foo/bar/', 'foo', 'https')); - } - - public function testLinkDomainNoDirs() - { - $links1 = new Links(); - $links1->setup([ - 'host' => 'www.example.com', - 'assets_host' => 'static.example.com', - 'assets_path' => 'path/to/', - ]); - $links2 = new Links(); - $links2->setup(['host' => 'http://example.com']); - assertSame('https://www.example.com/foo/bar', $links1->link('foo/bar/', 'foo', 'https')); - assertSame('http://example.com/foo/bar', $links2->link('foo/bar/', 'foo', 'bar')); - } - - public function testLinkNoDomainDirs() - { - $links = new Links(); - $links->setup([ - 'urls' => ['pdf' => '/url/to/pdf/', 'doc' => 'url/to/doc'], - ]); - assertSame('/url/to/pdf/foo.pdf', $links->link('foo.pdf', 'pdf')); - assertSame('/url/to/doc/foo.doc', $links->link('foo.doc', 'doc')); - } - - public function testLinkDomainDirs() - { - $links = new Links(); - $links->setup([ - 'urls' => ['pdf' => '/url/to/pdf/'], - 'host' => 'example.com', - 'assets_host' => 'static.example.com', - ]); - assertSame('http://example.com/url/to/pdf/foo.pdf', $links->link('foo.pdf', 'pdf')); - assertSame('https://example.com/url/to/pdf/foo.pdf', $links->link('foo.pdf', 'pdf', 'https')); - } - - public function testAssetNothing() - { - $links = new Links(); - assertSame('/foo.js', $links->asset('foo.js')); - } - - public function testAssetOnlyUrl() - { - $links = new Links(); - $links->setup(['assets_url' => '/assets/']); - assertSame('/assets/foo.js', $links->asset('foo.js')); - } - - public function testAssetOnlyDomain() - { - $links = new Links(); - $links->setup(['host' => 'example.com']); - $links2 = new Links(); - $links2->setup(['host' => 'http://example.com']); - $links3 = new Links(); - $links3->setup(['assets_host' => 'static.example.com']); - $links4 = new Links(); - $links4->setup(['host' => 'auto']); - assertSame('http://example.com/foo.js', $links->asset('foo.js')); - assertSame('https://example.com/foo.js', $links->asset('foo.js', 'https')); - assertSame('//example.com/foo.js', $links->asset('foo.js', false)); - assertSame('http://example.com/foo.js', $links2->asset('foo.js')); - assertSame('https://example.com/foo.js', $links2->asset('foo.js', 'https')); - assertSame('//example.com/foo.js', $links2->asset('foo.js', false)); - assertSame('http://static.example.com/foo.js', $links3->asset('foo.js')); - assertSame('https://static.example.com/foo.js', $links3->asset('foo.js', 'https')); - assertSame('//static.example.com/foo.js', $links3->asset('foo.js', false)); - // in tests filter_input(INPUT_SERVER, 'SERVER_NAME') return NULL - assertSame('/foo.js', $links4->asset('foo.js')); - } - - public function testAssetOnlyCache() - { - $path = getenv('FOIL_TESTS_BASEPATH').'/_files/assets'; - $links1 = new Links(); - $links1->setup(['assets_path' => $path, 'cache_bust' => true]); - $links2 = new Links(); - $links2->setup(['assets_path' => $path, 'cache_bust' => 'images']); - $links3 = new Links(); - $links3->setup(['assets_path' => $path, 'cache_bust' => 'scripts']); - $links4 = new Links(); - $links4->setup(['assets_path' => $path, 'cache_bust' => 'styles']); - $links5 = new Links(); - $links5->setup(['assets_path' => $path, 'cache_bust' => ['js', '.jpg']]); - // timestamp in 10 chars is from 09 Sept 2001 to 20 Nov 2286 - assertSame(1, preg_match('|^/foo\.[0-9]{10}\.js$|', $links1->asset('foo.js'))); - assertSame(1, preg_match('|^/foo\.[0-9]{10}\.jpg$|', $links1->asset('foo.jpg'))); - assertSame(1, preg_match('|^/foo\.[0-9]{10}\.css$|', $links1->asset('foo.css'))); - assertSame(0, preg_match('|^/foo\.[0-9]{10}\.js$|', $links2->asset('foo.js'))); - assertSame(1, preg_match('|^/foo\.[0-9]{10}\.jpg$|', $links2->asset('foo.jpg'))); - assertSame(0, preg_match('|^/foo\.[0-9]{10}\.css$|', $links2->asset('foo.css'))); - assertSame(1, preg_match('|^/foo\.[0-9]{10}\.js$|', $links3->asset('foo.js'))); - assertSame(0, preg_match('|^/foo\.[0-9]{10}\.jpg$|', $links3->asset('foo.jpg'))); - assertSame(0, preg_match('|^/foo\.[0-9]{10}\.css$|', $links3->asset('foo.css'))); - assertSame(0, preg_match('|^/foo\.[0-9]{10}\.js$|', $links4->asset('foo.js'))); - assertSame(0, preg_match('|^/foo\.[0-9]{10}\.jpg$|', $links4->asset('foo.jpg'))); - assertSame(1, preg_match('|^/foo\.[0-9]{10}\.css$|', $links4->asset('foo.css'))); - assertSame(1, preg_match('|^/foo\.[0-9]{10}\.js$|', $links5->asset('foo.js'))); - assertSame(1, preg_match('|^/foo\.[0-9]{10}\.jpg$|', $links5->asset('foo.jpg'))); - assertSame(0, preg_match('|^/foo\.[0-9]{10}\.css$|', $links5->asset('foo.css'))); - } - - public function testAssetDomainAndCache() - { - $links1 = new Links(); - $links1->setup([ - 'assets_path' => getenv('FOIL_TESTS_BASEPATH').'/_files/assets', - 'cache_bust' => ['js', '.jpg'], - 'host' => 'foo.example.com', - ]); - $links2 = new Links(); - $links2->setup([ - 'assets_path' => getenv('FOIL_TESTS_BASEPATH').'/_files/assets', - 'cache_bust' => ['js', '.jpg'], - 'host' => 'foo.example.com', - 'assets_host' => 'static.example.com', - ]); - $regex1 = '^http://foo.example.com/foo\.[0-9]{10}\.'; - $regex2 = '^http://static.example.com/foo\.[0-9]{10}\.'; - assertSame(1, preg_match("|{$regex1}js$|", $links1->asset('foo.js'))); - assertSame(1, preg_match("|{$regex1}jpg$|", $links1->asset('foo.jpg'))); - assertSame('http://foo.example.com/foo.css', $links1->asset('foo.css')); - assertSame(1, preg_match("|{$regex2}js$|", $links2->asset('foo.js'))); - assertSame(1, preg_match("|{$regex2}jpg$|", $links2->asset('foo.jpg'))); - assertSame('http://static.example.com/foo.css', $links2->asset('foo.css')); - } - - public function testAssetDomainAndUrl() - { - $links1 = new Links(); - $links1->setup([ - 'assets_url' => '/assets/', - 'host' => 'foo.example.com', - ]); - $links2 = new Links(); - $links2->setup([ - 'assets_url' => '/assets/', - 'host' => 'foo.example.com', - 'assets_host' => 'static.example.com', - ]); - assertSame('http://foo.example.com/assets/foo.js', $links1->asset('foo.js')); - assertSame('http://static.example.com/assets/foo.js', $links2->asset('foo.js')); - } - - public function testAssetCacheAndUrl() - { - $links = new Links(); - $links->setup([ - 'assets_path' => getenv('FOIL_TESTS_BASEPATH').'/_files/assets', - 'cache_bust' => ['js', '.jpg'], - 'assets_url' => '/assets/', - ]); - $regex = '^/assets/foo\.[0-9]{10}\.'; - assertSame(1, preg_match("|{$regex}js$|", $links->asset('foo.js'))); - assertSame(1, preg_match("|{$regex}jpg$|", $links->asset('foo.jpg'))); - assertSame('/assets/foo.css', $links->asset('foo.css')); - } - - public function testAssetDomainAndCacheAndUrl() - { - $links1 = new Links(); - $links1->setup([ - 'host' => 'foo.example.com', - 'assets_path' => getenv('FOIL_TESTS_BASEPATH').'/_files/assets', - 'cache_bust' => ['js', '.jpg'], - 'assets_url' => '/assets/', - ]); - $regex1 = '^http://foo.example.com/assets/foo\.[0-9]{10}\.'; - $links2 = new Links(); - $links2->setup([ - 'host' => 'foo.example.com', - 'assets_host' => 'static.example.com', - 'assets_path' => getenv('FOIL_TESTS_BASEPATH').'/_files/assets', - 'cache_bust' => ['js', '.jpg'], - 'assets_url' => '/assets/', - ]); - $regex2 = '^http://static.example.com/assets/foo\.[0-9]{10}\.'; - assertSame(1, preg_match("|{$regex1}js$|", $links1->asset('foo.js'))); - assertSame(1, preg_match("|{$regex1}jpg$|", $links1->asset('foo.jpg'))); - assertSame('http://foo.example.com/assets/foo.css', $links1->asset('foo.css')); - assertSame(1, preg_match("|{$regex2}js$|", $links2->asset('foo.js'))); - assertSame(1, preg_match("|{$regex2}jpg$|", $links2->asset('foo.jpg'))); - assertSame('http://static.example.com/assets/foo.css', $links2->asset('foo.css')); - } -} diff --git a/tests/src/unit/Extension/UriTest.php b/tests/src/unit/Extension/UriTest.php deleted file mode 100644 index 9540de3..0000000 --- a/tests/src/unit/Extension/UriTest.php +++ /dev/null @@ -1,113 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ -namespace Foil\Tests\Unit\Extension; - -use Foil\Tests\TestCase; -use Foil\Extensions\Uri; - -/** - * @author Giuseppe Mazzapica - * @package foil\foil - * @license http://opensource.org/licenses/MIT MIT - */ -class UriTest extends TestCase -{ - public function testHome() - { - $u = new Uri(); - $u->setup(); - assertSame('Yes', $u->is('/', 'Yes', 'No')); - assertSame('Yes', $u->is([1, '/'], 'Yes', 'No')); - } - - public function testIsWithArray() - { - $u = new Uri(); - $u->setup(['pathinfo' => '/foo/bar/baz']); - assertSame('Yes', $u->is([2, 'bar'], 'Yes', 'No')); - assertTrue($u->is([2, 'bar'])); - assertSame('Yes', $u->is([3, '/baz'], 'Yes', 'No')); - assertTrue($u->is([3, '/baz'])); - assertSame('No', $u->is([1, '/baz'], 'Yes', 'No')); - assertSame('', $u->is([2, '/baz'])); - } - - public function testIsWithHomeAndArray() - { - $u = new Uri(); - $u->setup(['pathinfo' => '/foo/bar/baz', 'home' => '/foo/bar']); - assertSame('Yes', $u->is([1, 'baz'], 'Yes', 'No')); - assertTrue($u->is([1, 'baz'])); - assertSame('No', $u->is([3, 'baz'], 'Yes', 'No')); - assertSame('', $u->is([3, '/baz'])); - } - - public function testIsWithString() - { - $u = new Uri(); - $u->setup(['pathinfo' => '/foo/bar/baz']); - assertSame('Yes', $u->is('/foo/bar/baz', 'Yes', 'No')); - assertTrue($u->is('foo/bar/baz/')); - assertSame('No', $u->is('/foo/bar/baz/foo', 'Yes', 'No')); - assertSame('', $u->is('foo/bar')); - } - - public function testIsWithHomeAndString() - { - $u = new Uri(); - $u->setup(['pathinfo' => '/foo/bar/baz', 'home' => '/foo/bar']); - assertSame('Yes', $u->is('baz', 'Yes', 'No')); - assertTrue($u->is('/baz/')); - assertSame('No', $u->is('/foo/bar/baz', 'Yes', 'No')); - assertSame('', $u->is('foo/bar/baz')); - } - - public function testHas() - { - $u = new Uri(); - $u->setup(['pathinfo' => '/foo/bar/baz']); - assertSame('Yes', $u->has('/foo', 'Yes', 'No')); - assertTrue($u->has('/foo/bar/')); - assertSame('No', $u->has('/foo/foo', 'Yes', 'No')); - assertSame('', $u->has('foo/baz/bar')); - } - - public function testHasWithHome() - { - $u = new Uri(); - $u->setup(['pathinfo' => '/foo/bar/baz', 'home' => '/foo']); - assertSame('Yes', $u->has('/bar', 'Yes', 'No')); - assertTrue($u->has('bar/baz')); - assertSame('No', $u->has('foo/bar', 'Yes', 'No')); - assertSame('', $u->has('foo/bar/baz')); - } - - public function testMatch() - { - $u = new Uri(); - $u->setup(['pathinfo' => '/foo/bar/baz']); - assertSame('Yes', $u->match('foo', 'Yes', 'No')); - assertSame('Yes', $u->match('^foo', 'Yes', 'No')); - assertTrue($u->match('.+bar')); - assertSame('No', $u->match('^bar', 'Yes', 'No')); - assertSame('', $u->match('[0-9]')); - } - - public function testMatchWithHome() - { - $u = new Uri(); - $u->setup(['pathinfo' => '/foo/bar/baz', 'home' => '/foo']); - assertSame('Yes', $u->match('bar', 'Yes', 'No')); - assertSame('Yes', $u->match('^bar', 'Yes', 'No')); - assertTrue($u->match('.*baz')); - assertSame('No', $u->match('^foo', 'Yes', 'No')); - assertSame('', $u->match('foo')); - } -} diff --git a/tests/src/unit/Extension/WalkerTest.php b/tests/src/unit/Extension/WalkerTest.php deleted file mode 100644 index 942166a..0000000 --- a/tests/src/unit/Extension/WalkerTest.php +++ /dev/null @@ -1,140 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ -namespace Foil\Tests\Unit\Extension; - -use Foil\Tests\TestCase; -use Foil\Extensions\Walker; -use Mockery; -use Foil; -use ArrayIterator; - -/** - * @author Giuseppe Mazzapica - * @package foil\foil - * @license http://opensource.org/licenses/MIT MIT - */ -class WalkerTest extends TestCase -{ - /** - * @param bool $autoescape - * @return \Foil\Extensions\Walker|\Mockery\MockInterface - */ - private function getWalker($autoescape = true) - { - /** @var \Foil\Kernel\Command $command */ - $command = Mockery::mock('Foil\Kernel\Command'); - /** @var \Foil\Kernel\Escaper|\Mockery\MockInterface $escaper */ - $escaper = Mockery::mock('\Foil\Kernel\Escaper'); - $escaper->shouldReceive('escape')->andReturnUsing(function ($data) { - if (is_string($data)) { - return htmlentities($data); - } elseif (is_array($data)) { - return array_map('htmlentities', $data); - } - - return ''; - }); - $options = ['autoescape' => $autoescape]; - - return new Walker($command, $escaper, $options); - } - - public function testWalkArray() - { - $w = $this->getWalker(); - $expected = '
  • A - foo - bar
  • B - foo - bar
  • C - foo - bar
  • '; - assertSame($expected, $w->walk(['A', 'B', 'C'], '
  • %s %s %s
  • ', '- foo', '- bar')); - } - - public function testWalkArrayEscape() - { - $w = $this->getWalker(); - $expected = '
  • "A" <foo>
  • "B" <foo>
  • '; - assertSame($expected, $w->walk(['"A"', '"B"'], '
  • %s %s
  • ', '')); - } - - public function testWalkArrayArray() - { - $w = $this->getWalker(); - $expected = "

    Tom is 30 years old and comes from London

    " - ."

    Dick is 33 years old and comes from New York

    " - ."

    Harry is 25 years old and comes from Berlin

    "; - $data = [['Tom', 'London', 30], ['Dick', 'New York', 33], ['Harry', 'Berlin', 25]]; - assertSame($expected, $w->walk($data, '

    %1$s is %3$d years old and comes from %2$s

    ')); - } - - public function testWalkIterator() - { - $w = $this->getWalker(); - $expected = '
  • "A" foo
  • "B" foo
  • "C" foo
  • '; - $it = new ArrayIterator(['"A"', '"B"', '"C"']); - assertSame($expected, $w->walk($it, '
  • %s %s
  • ', 'foo')); - } - - public function testWalkIfBool() - { - $w = $this->getWalker(); - $expected = '
  • A - foo
  • B - foo
  • C - foo
  • '; - assertSame($expected, $w->walkIf(['A', 'B', 'C'], true, '
  • %s %s
  • ', '- foo')); - assertSame('', $w->walkIf(['A', 'B', 'C'], false, '
  • %s %s
  • ', '- foo')); - } - - public function testWalkIfCallback() - { - $w = $this->getWalker(); - $expected = '
  • A - foo
  • B - foo
  • C - foo
  • '; - assertSame( - $expected, - $w->walkIf(['A', 'B', 'C'], function () { - return 1 === 1; - }, '
  • %s %s
  • ', '- foo') - ); - assertSame( - '', - $w->walkIf(['A', 'B', 'C'], function () { - return 1 === 2; - }, '
  • %s %s
  • ', '- foo') - ); - } - - public function testWalkWrap() - { - $w = $this->getWalker(); - $expected = '
    • A - foo
    • B - foo
    • C - foo
    '; - assertSame( - $expected, - $w->walkWrap(['A', 'B', 'C'], '
      %s
    ', '
  • %s %s
  • ', '- foo') - ); - } - - public function testWalkWrapDefault() - { - $w = $this->getWalker(); - $expected = '
  • A
  • B
  • C
  • '; - assertSame( - $expected, - $w->walkWrap(['A', 'B', 'C'], '', '
  • %s
  • ') - ); - } - - public function testWalkWrapIf() - { - $w = $this->getWalker(); - $expected = '
    • A - foo
    • B - foo
    • C - foo
    '; - assertSame( - $expected, - $w->walkWrapIf(['A', 'B', 'C'], true, '
      %s
    ', '
  • %s %s
  • ', '- foo') - ); - assertSame( - '', - $w->walkWrapIf(['A', 'B', 'C'], false, '
      %s
    ', '
  • %s %s
  • ', '- foo') - ); - } -} diff --git a/tests/src/unit/Kernel/ArraizeTest.php b/tests/src/unit/Kernel/ArraizeTest.php deleted file mode 100644 index 332aa63..0000000 --- a/tests/src/unit/Kernel/ArraizeTest.php +++ /dev/null @@ -1,235 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ -namespace Foil\Tests\Unit\Kernel; - -use Foil\Tests\TestCase; -use Aura\Html\Escaper\HtmlEscaper; -use Foil\Tests\ToArray; -use Foil\Tests\AsArray; -use Foil\Tests\Json; -use Foil\Tests\Value; -use Foil\Tests\Target; - -/** - * @author Giuseppe Mazzapica - * @package foil\foil - * @license http://opensource.org/licenses/MIT MIT - */ -class ArraizeTest extends TestCase -{ - private function e($var) - { - return call_user_func(new HtmlEscaper(), $var); - } - - public function testScalars() - { - assertSame(['foo bar'], \Foil\arraize('foo bar')); - assertSame([$this->e('foo bar')], \Foil\arraize('foo bar', true)); - assertSame([1], \Foil\arraize(1)); - assertSame([true], \Foil\arraize(true)); - assertSame([(string) 1], \Foil\arraize(1, true, [], true)); - assertSame([(string) true], \Foil\arraize(true, true, [], true)); - assertSame([], \Foil\arraize(null)); - assertSame([], \Foil\arraize(false)); - assertSame([], \Foil\arraize('')); - } - - public function testArray() - { - $data = [ - 'lev1' => [ - 'lev1.1' => 'lev1.1', - 'lev1.2' => true, - 'lev1.3' => ['foo', 'bar', 'baz'], - ], - 'lev2' => [ - 'lev2.2' => [ - 'lev2.2.1' => ['a' => 'a', 'b' => 'b'], - 'lev2.2.2' => [1, true, null], - ], - ], - ]; - $esc = [ - 'lev1' => [ - 'lev1.1' => $this->e('lev1.1'), - 'lev1.2' => true, - 'lev1.3' => [ - $this->e('foo'), - $this->e('bar'), - $this->e('baz'), - ], - ], - 'lev2' => [ - 'lev2.2' => [ - 'lev2.2.1' => ['a' => $this->e('a'), 'b' => $this->e('b')], - 'lev2.2.2' => [1, true, null], - ], - ], - ]; - $str = [ - 'lev1' => [ - 'lev1.1' => 'lev1.1', - 'lev1.2' => '1', - 'lev1.3' => ['foo', 'bar', 'baz'], - ], - 'lev2' => [ - 'lev2.2' => [ - 'lev2.2.1' => ['a' => 'a', 'b' => 'b'], - 'lev2.2.2' => ['1', '1', ''], - ], - ], - ]; - $escstr = [ - 'lev1' => [ - 'lev1.1' => $this->e('lev1.1'), - 'lev1.2' => '1', - 'lev1.3' => [ - $this->e('foo'), - $this->e('bar'), - $this->e('baz'), - ], - ], - 'lev2' => [ - 'lev2.2' => [ - 'lev2.2.1' => ['a' => $this->e('a'), 'b' => $this->e('b')], - 'lev2.2.2' => ['1', '1', ''], - ], - ], - ]; - assertSame($data, \Foil\arraize($data)); - assertSame($esc, \Foil\arraize($data, true)); - assertSame($str, \Foil\arraize($data, false, [], true)); - assertSame($escstr, \Foil\arraize($data, true, [], true)); - } - - public function testStorage() - { - $storage = new \SplObjectStorage(); - foreach (range('a', 'c') as $i) { - $object = new \stdClass(); - $object->$i = "{$i}"; - $storage->attach($object); - } - $expected = [ - ['a' => $this->e('a')], - ['b' => $this->e('b')], - ['c' => $this->e('c')], - ]; - assertSame($expected, \Foil\arraize($storage, true)); - } - - public function testMix() - { - $storage1 = new \SplObjectStorage(); - $storage1->attach(new \stdClass()); - $storage2 = new \SplObjectStorage(); - $storage2->attach((object) ['foo' => 'bar']); - $data = [ - 'lev1' => new \ArrayIterator([ - 'lev1.1' => 'lev1.1', - 'lev1.2' => true, - 'lev1.3' => ['foo', 'bar', 'baz'], - ]), - 'lev2' => [ - 'lev2.2' => [ - 'lev2.2.1' => (object) ['a' => 'a', 'b' => 'b'], - 'lev2.2.2' => [1, true, null], - ], - ], - 'storage1' => $storage1, - 'string', - 33, - null, - 'storage2' => $storage2, - true, - ]; - $expected = [ - 'lev1' => [ - 'lev1.1' => $this->e('lev1.1'), - 'lev1.2' => '1', - 'lev1.3' => [ - $this->e('foo'), - $this->e('bar'), - $this->e('baz'), - ], - ], - 'lev2' => [ - 'lev2.2' => [ - 'lev2.2.1' => ['a' => $this->e('a'), 'b' => $this->e('b')], - 'lev2.2.2' => ['1', '1', ''], - ], - ], - 'storage1' => [[]], - 'string', - '33', - '', - 'storage2' => [['foo' => $this->e('bar')]], - '1', - ]; - assertSame($expected, \Foil\arraize($data, true, [], true)); - } - - public function testObjectToArray() - { - // this file uses contain the object used to test object "arraization" - // see that file to understand what happen here - require getenv('FOIL_TESTS_BASEPATH').'/_files/stubs.php'; - - $cb = function ($object) { - return is_object($object) ? ['callbacked' => get_object_vars($object)] : false; - }; - $trasformers = [ - 'Foil\\Tests\\Value' => 'Foil\\Tests\\Transformer', - 'Foil\\Tests\\Target' => $cb, - ]; - $storage = new \SplObjectStorage(); - $storage->attach(new \stdClass()); - $data = [ - 'to' => new ToArray('To'), - 'as' => new AsArray(['as1' => ['as1.1' => ['as1.1.1' => 'Deep!']]]), - 'json' => new Json(), - 'value' => new Value(['a' => 'a', 'b' => 'b']), - 'target' => new Target(['c' => 'c', 'd' => 'd']), - 'storage' => $storage, - ]; - $expected = [ - 'to' => [ - 'toarray' => [$this->e('To')], - ], - 'as' => [ - 'asarray' => [ - 'as1' => [ - 'as1.1' => ['as1.1.1' => $this->e('Deep!')], - ], - ], - ], - 'json' => [$this->e('I am JSON')], - 'value' => [ - 'transformed' => [ - 'value' => [ - 'a' => $this->e('a'), - 'b' => $this->e('b'), - ], - ], - ], - 'target' => [ - 'callbacked' => [ - 'value' => [ - 'c' => $this->e('c'), - 'd' => $this->e('d'), - ], - ], - ], - 'storage' => [[]], - ]; - assertSame($expected, \Foil\arraize($data, true, $trasformers)); - } -} diff --git a/tests/src/unit/Kernel/CommandTest.php b/tests/src/unit/Kernel/CommandTest.php deleted file mode 100644 index 126d9dd..0000000 --- a/tests/src/unit/Kernel/CommandTest.php +++ /dev/null @@ -1,172 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ -namespace Foil\Tests\Unit\Kernel; - -use Foil\Tests\TestCase; -use Foil\Kernel\Command; -use Aura\Html\Escaper\HtmlEscaper; -use Mockery; - -/** - * @author Giuseppe Mazzapica - * @package foil\foil - * @license http://opensource.org/licenses/MIT MIT - */ -class CommandTest extends TestCase -{ - /** - * @return \Foil\Contracts\EscaperInterface $escaper - */ - private function escaper() - { - $escaper = Mockery::mock('Foil\Contracts\EscaperInterface'); - $escaper->shouldReceive('escape')->andReturnUsing(function ($var) { - if (is_object($var)) { - return method_exists($var, '__toString') - ? (new HtmlEscaper())->__invoke($var->__toString()) - : ''; - } - - return is_array($var) - ? array_map(new HtmlEscaper(), $var) - : (new HtmlEscaper())->__invoke($var); - }); - - return $escaper; - } - - /** - * @expectedException \InvalidArgumentException - */ - public function testRunFailsIfBadFunctionName() - { - $c = new Command($this->escaper()); - $this->bindClosure(function () { - /** @noinspection PhpUndefinedMethodInspection */ - $this->execute([], [], []); - }, $c); - } - - /** - * @expectedException \LogicException - */ - public function testRunFailsIfBadFunctionNotExists() - { - $c = new Command($this->escaper()); - $this->bindClosure(function () { - /** @noinspection PhpUndefinedMethodInspection */ - $this->execute('goodbye', [], []); - }, $c); - } - - public function testFunctionNoEcho() - { - $test = function ($str) { - echo $str; - }; - $c = new Command($this->escaper()); - $c->registerFunctions(['hello' => $test]); - $this->expectOutputString(''); - assertSame('', $c->run('hello', 'Hello!')); - } - - public function testFunctionObject() - { - $test1 = function () { - return (object) ['foo' => 'bar']; - }; - $test2 = function () { - return 'foo'; - }; - $c = new Command($this->escaper()); - $c->registerFunctions(['one' => $test1]); - $c->registerFunctions(['two' => $test2]); - assertSame('', $c->run('one')); - assertSame('foo', $c->run('two')); - } - - public function testFunctionEcho() - { - $test = function ($str) { - echo $str; - }; - $c = new Command($this->escaper()); - $c->registerFunctions(['block' => $test]); - $this->expectOutputString('Hello!'); - $c->run('block', 'Hello!'); - } - - public function testFunctionEscape() - { - $test = function ($str) { - return $str; - }; - $c = new Command($this->escaper()); - $c->registerFunctions(['hello' => $test]); - $expected = htmlentities('Hello!', ENT_QUOTES, 'UTF-8', false); - assertSame($expected, $c->run('hello', 'Hello!')); - } - - public function testFunctionNotEscape() - { - $test = function ($str) { - return $str; - }; - $c = new Command($this->escaper(), false); - $c->registerFunctions(['hello' => $test]); - assertSame('Hello!', $c->run('hello', 'Hello!')); - } - - public function testPreventCoreFunctionOverride() - { - $test1 = function () { - return 'A'; - }; - $test2 = function () { - return 'B'; - }; - $test3 = function () { - return 'C'; - }; - $test4 = function () { - return 'D'; - }; - $c = new Command($this->escaper()); - $c->registerFunctions(['t1' => $test1, 't2' => $test2]); - $c->registerFunctions(['t1' => $test3]); // should override - $c->lock(); - $c->registerFunctions(['t2' => $test4]); // override shouldn't be possible anymore - assertSame('C', $c->run('t1')); - assertSame('B', $c->run('t2')); - } - - public function testFunctionsSafe() - { - $func = function () { - return 'A'; - }; - $c = new Command($this->escaper()); - $c->registerFunctions(['a' => $func, 'b' => $func]); - $c->registerFunctions(['c' => $func, 'd' => $func], ['c']); - $c->registerFunctions(['e' => $func, 'f' => $func], ['e', 'f']); - $c->registerFunctions(['g' => $func, 'h' => $func], true); - assertSame(['c', 'e', 'f', 'g', 'h'], $this->accessPrivateProperty('safe', $c)); - } - - public function testFilters() - { - $test = function ($str) { - return strrev($str); - }; - $c = new Command($this->escaper()); - $c->registerFilters(['rev' => $test]); - assertSame('Foo', $c->filter('rev', 'ooF')); - } -} diff --git a/tests/src/unit/Kernel/EscaperTest.php b/tests/src/unit/Kernel/EscaperTest.php deleted file mode 100644 index e84d4ed..0000000 --- a/tests/src/unit/Kernel/EscaperTest.php +++ /dev/null @@ -1,212 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Foil\Tests\Unit\Kernel; - -use Foil\Tests\TestCase; -use Foil\Kernel\Escaper; -use Aura\Html\Escaper as AuraEscaper; -use Aura\Html\Escaper\HtmlEscaper; -use Aura\Html\Escaper\AttrEscaper; -use Aura\Html\Escaper\CssEscaper; -use Aura\Html\Escaper\JsEscaper; -use Mockery; - -/** - * @author Giuseppe Mazzapica - * @license http://opensource.org/licenses/MIT MIT - * @package Foil - */ -class EscaperTest extends TestCase -{ - /** - * @param mixed $data - * @param string $encoding - * @param string $strategy - * @return string - */ - private function e($data, $encoding = 'utf-8', $strategy = 'html') - { - $aura = $this->auraEscaper(); - $aura->setEncoding($encoding); - - return $aura->$strategy($data); - } - - /** - * @return \Aura\Html\Escaper - */ - private function auraEscaper() - { - return new AuraEscaper( - new HtmlEscaper(), - new AttrEscaper(new HtmlEscaper()), - new CssEscaper(), - new JsEscaper() - ); - } - - public function testHtmlDefaultStrategy() - { - $escaper = new Escaper($this->auraEscaper(), 'utf-8'); - assertSame($this->e('Foo'), $escaper->escape('Foo', 'mhe')); - } - - public function testUtf8() - { - $data = [ - 'int' => 1, - 'bool' => true, - 'string' => 'Foo', - 'array' => [ - 'array.string' => 'Foo', - 'array.array' => [ - 'array.array.string' => 'Foo', - 'array.array.array' => [ - 'array.array.array.string' => 'Foo', - ], - ], - ], - ]; - $expected = [ - 'int' => 1, - 'bool' => true, - 'string' => $this->e('Foo', 'utf-8'), - 'array' => [ - 'array.string' => $this->e('Foo', 'utf-8'), - 'array.array' => [ - 'array.array.string' => $this->e('Foo', 'utf-8'), - 'array.array.array' => [ - 'array.array.array.string' => $this->e('Foo', 'utf-8'), - ], - ], - ], - ]; - $escaper = new Escaper($this->auraEscaper(), 'utf-8'); - assertSame($expected, $escaper->escape($data)); - } - - public function testIso88591() - { - $data = [ - 'int' => 1, - 'bool' => true, - 'string' => 'Ñ', - 'array' => [ - 'array.string' => 'Ñ', - 'array.array' => [ - 'array.array.string' => 'Ñ', - 'array.array.array' => [ - 'array.array.array.string' => 'Ñ', - ], - ], - ], - ]; - $expected = [ - 'int' => 1, - 'bool' => true, - 'string' => $this->e('Ñ', 'iso-8859-1'), - 'array' => [ - 'array.string' => $this->e('Ñ', 'iso-8859-1'), - 'array.array' => [ - 'array.array.string' => $this->e('Ñ', 'iso-8859-1'), - 'array.array.array' => [ - 'array.array.array.string' => $this->e('Ñ', 'iso-8859-1'), - ], - ], - ], - ]; - $escaper = new Escaper($this->auraEscaper(), 'iso-8859-1'); - assertSame($expected, $escaper->escape($data)); - } - - public function testMultipleEncoding() - { - if (defined('HHVM_VERSION')) { - $this->markTestSkipped('HHVM do not yet support multi byte encoding.'); - } - $escaper = new Escaper($this->auraEscaper(), 'utf-8'); - $str = '体字'; - $big5 = $this->e($str, 'big5'); - $utf8 = $this->e($str, 'utf-8'); - - assertSame($utf8, $escaper->escape($str)); - assertNotSame($big5, $escaper->escape($str)); - assertSame($big5, $escaper->escape($str, 'html', 'big5')); - } - - public function testAttr() - { - $escaper = new Escaper($this->auraEscaper(), 'utf-8'); - $data = ['class' => ['foo', 'bar'], 'id' => 'foo']; - $expected = 'class="foo bar" id="foo"'; - - assertSame($expected, $escaper->escape($data, 'attr')); - } - - public function testObjectArr() - { - $escaper = new Escaper($this->auraEscaper(), 'utf-8'); - $data = (object) ['class' => ['foo', 'bar'], 'id' => 'foo']; - $expected = 'class="foo bar" id="foo"'; - - assertSame($expected, $escaper->escape($data, 'attr')); - } - - public function testObjectHtml() - { - $escaper = new Escaper($this->auraEscaper(), 'utf-8'); - $data = new \ArrayIterator(['a', 'c', 'd']); - $expected = array_map([$this, 'e'], ['a', 'c', 'd']); - - assertSame($expected, $escaper->escape($data)); - } - - public function testObjectString() - { - $mock = Mockery::mock(); - $mock->shouldReceive('__toString')->andReturn('

    P

    '); - - $escaper = new Escaper($this->auraEscaper(), 'utf-8'); - assertSame($this->e('

    P

    '), $escaper->escape($mock)); - } - - public function testDecode() - { - $str = '

    à""&àà体

    '; - $encode = $this->e($str); - - $escaper = new Escaper($this->auraEscaper(), 'utf-8'); - assertSame($str, $escaper->decode($encode)); - } - - public function testDecodeArray() - { - $arr = ['

    a

    ', '

    b

    ', '

    c

    ']; - $encode = array_map([$this, 'e'], $arr); - $encodeObj = new \ArrayIterator($encode); - - $escaper = new Escaper($this->auraEscaper(), 'utf-8'); - assertSame($arr, $escaper->decode($encode)); - assertSame($arr, $escaper->decode($encodeObj)); - } - - public function testDecodeObject() - { - $obj = (object) ['a' => '

    a

    ', 'b' => '

    b

    ']; - $objStr = Mockery::mock(); - $objStr->shouldReceive('__toString')->andReturn($this->e('

    a

    ')); - - $escaper = new Escaper($this->auraEscaper(), 'utf-8'); - - assertSame($obj, $escaper->decode($obj)); - assertSame('

    a

    ', $escaper->decode($objStr)); - } -} diff --git a/tests/src/unit/Kernel/EventsTest.php b/tests/src/unit/Kernel/EventsTest.php deleted file mode 100644 index cc18579..0000000 --- a/tests/src/unit/Kernel/EventsTest.php +++ /dev/null @@ -1,31 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ -namespace Foil\Tests\Unit\Kernel; - -use Foil\Tests\TestCase; -use Foil\Kernel\Events; - -/** - * @author Giuseppe Mazzapica - * @package foil\foil - * @license http://opensource.org/licenses/MIT MIT - */ -class EventsTest extends TestCase -{ - public function testFire() - { - $e = new Events(); - $e->on('echo', function ($str1, $str2) { - echo $str1.$str2; - }); - $this->expectOutputString('Hello!'); - $e->fire('echo', 'Hello', '!'); - } -} diff --git a/tests/src/unit/Section/FactoryTest.php b/tests/src/unit/Section/FactoryTest.php deleted file mode 100644 index 6e3bd92..0000000 --- a/tests/src/unit/Section/FactoryTest.php +++ /dev/null @@ -1,75 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ -namespace Foil\Tests\Unit\Section; - -use Foil\Tests\TestCase; -use Foil\Section\Factory; -use Foil\Contracts\SectionInterface as Section; -use ArrayObject; -use Mockery; - -/** - * @author Giuseppe Mazzapica - * @package foil\foil - * @license http://opensource.org/licenses/MIT MIT - */ -class FactoryTest extends TestCase -{ - /** - * @expectedException \InvalidArgumentException - */ - public function testFactoryFailsIfBadName() - { - $factory = new Factory(new ArrayObject()); - $factory->factory(true); - } - - public function testFactoryNoMode() - { - $factory = new Factory(new ArrayObject()); - $s_1 = $factory->factory('one'); - $s_2 = $factory->factory('two'); - $s_3 = $factory->factory('one'); - assertInstanceOf('Foil\Section\Section', $s_1); - assertInstanceOf('Foil\Section\Section', $s_2); - assertSame($s_1, $s_3); - assertFalse($s_2 === $s_3); - } - - public function testFactoryMode() - { - $factory = new Factory(new ArrayObject()); - $s_1 = $factory->factory('one', Section::MODE_APPEND); - $s_2 = $factory->factory('one', Section::MODE_REPLACE); - $s_3 = $factory->factory('two', Section::MODE_APPEND); - $s_4 = $factory->factory('two', Section::MODE_OUTPUT); - assertSame($s_1, $s_2); - assertSame(Section::MODE_REPLACE, $s_2->mode()); - assertSame($s_3, $s_4); - assertSame(Section::MODE_APPEND | Section::MODE_OUTPUT, $s_4->mode()); - } - - public function testCustomClass() - { - $class = get_class(Mockery::mock(Factory::DEFAULT_CONTRACT)); - $factory = new Factory(new ArrayObject()); - $section = $factory->factory('foo', Section::MODE_APPEND, $class); - assertInstanceOf($class, $section); - assertInstanceOf(Factory::DEFAULT_CONTRACT, $section); - assertNotInstanceOf(Factory::DEFAULT_CLASS, $section); - } - - public function testDefaultClassIfBadClass() - { - $factory = new Factory(new ArrayObject()); - $section = $factory->factory('foo', Section::MODE_APPEND, __CLASS__); - assertInstanceOf(Factory::DEFAULT_CLASS, $section); - } -} diff --git a/tests/src/unit/Section/SectionTest.php b/tests/src/unit/Section/SectionTest.php deleted file mode 100644 index 1f41319..0000000 --- a/tests/src/unit/Section/SectionTest.php +++ /dev/null @@ -1,113 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ -namespace Foil\Tests\Unit\Section; - -use Foil\Tests\TestCase; -use Foil\Section\Section; - -/** - * @author Giuseppe Mazzapica - * @package foil\foil - * @license http://opensource.org/licenses/MIT MIT - */ -class SectionTest extends TestCase -{ - /** - * @expectedException \LogicException - */ - public function testReplaceFailsIfNoStarted() - { - $section = new Section(false); - $section->replace(); - } - - public function testReplaceNoMode() - { - $this->expectOutputString(''); - $section = new Section(false); - $section->start(); - echo 'Lorem Ipsum Dolor Simet'; - $section->replace(); - assertSame('Lorem Ipsum Dolor Simet', $section->content()); - } - - public function testReplaceModeOutput() - { - $this->expectOutputString('Lorem Ipsum Dolor Simet'); - $section = new Section(Section::MODE_REPLACE | Section::MODE_OUTPUT); - $section->start(); - echo 'Lorem Ipsum Dolor Simet'; - $section->replace(); - assertSame('Lorem Ipsum Dolor Simet', $section->content()); - } - - /** - * @expectedException \LogicException - */ - public function testAppendFailsIfNoStarted() - { - $section = new Section(false); - $section->append(); - } - - public function testAppendModeReplace() - { - $this->expectOutputString(''); - $section = new Section(Section::MODE_REPLACE); - $section->start(); - echo 'Lorem Ipsum Dolor Simet'; - $section->append(); - assertSame('', $section->content()); - } - - public function testAppendNoMode() - { - $this->expectOutputString(''); - $section = new Section(false); - $section->start(); - echo 'Lorem Ipsum Dolor Simet'; - $section->append(); - assertSame('Lorem Ipsum Dolor Simet', $section->content()); - } - - public function testAppendModeOutput() - { - $this->expectOutputString('Lorem Ipsum Dolor Simet'); - $section = new Section(Section::MODE_APPEND | Section::MODE_OUTPUT); - $section->start(); - echo 'Lorem Ipsum Dolor Simet'; - $section->append(); - assertSame('Lorem Ipsum Dolor Simet', $section->content()); - } - - public function testStopAsAppend() - { - $this->expectOutputString(''); - $section = new Section(); - $section->start(); - echo 'Dolor Simet'; - $section->stop(); - $section->start(); - echo 'Lorem Ipsum '; - $section->stop(); - assertSame('Lorem Ipsum Dolor Simet', $section->content()); - } - - public function testStopAsReplace() - { - $this->expectOutputString(''); - $section = new Section(false, Section::MODE_REPLACE); - $section->start(); - echo 'Lorem Ipsum'; - $section->stop(); - assertSame(Section::MODE_REPLACE, $section->mode()); - assertSame('Lorem Ipsum', $section->content()); - } -} diff --git a/tests/src/unit/Template/AliasTest.php b/tests/src/unit/Template/AliasTest.php deleted file mode 100644 index fc78ca6..0000000 --- a/tests/src/unit/Template/AliasTest.php +++ /dev/null @@ -1,66 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Foil\Tests\Unit\Template; - -use Foil\Template\Alias; -use Foil\Tests\TestCase; - -/** - * @author Giuseppe Mazzapica - * @license http://opensource.org/licenses/MIT MIT - * @package Foil - */ -class AliasTest extends TestCase -{ - /** - * @expectedException \InvalidArgumentException - */ - public function testValidateInvalidNumber() - { - new Alias(1); - } - - /** - * @expectedException \InvalidArgumentException - */ - public function testValidateInvalidNumerical() - { - new Alias('1'); - } - - /** - * @expectedException \InvalidArgumentException - */ - public function testValidateInvalidHyphen() - { - new Alias('a-a'); - } - - /** - * @expectedException \InvalidArgumentException - */ - public function testValidateInvalidDot() - { - new Alias('a.a'); - } - - public function testToString() - { - $this->expectOutputString('foo'); - echo new Alias('foo'); - } - - public function testAsArrayKey() - { - $alias = new Alias('bar'); - assertSame(['bar' => $this], ["{$alias}" => $this]); - } -} diff --git a/tests/src/unit/Template/FactoryTest.php b/tests/src/unit/Template/FactoryTest.php deleted file mode 100644 index 01d259d..0000000 --- a/tests/src/unit/Template/FactoryTest.php +++ /dev/null @@ -1,92 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ -namespace Foil\Tests\Unit\Template; - -use Foil\Tests\TestCase; -use Foil\Template\Factory; -use ArrayObject; -use Mockery; - -/** - * @author Giuseppe Mazzapica - * @package foil\foil - * @license http://opensource.org/licenses/MIT MIT - */ -class FactoryTest extends TestCase -{ - /** - * @param string|bool $class - * @return \Foil\Template\Factory - */ - private function getTemplateFactory($class = false) - { - /** @var \Foil\Kernel\Command $command */ - $command = Mockery::mock('Foil\Kernel\Command'); - $options = ['template_class' => $class]; - - return new Factory(new ArrayObject(), new ArrayObject(), $command, $options); - } - - /** - * @expectedException \InvalidArgumentException - */ - public function testFactoryFailsIfBadName() - { - $factory = $this->getTemplateFactory(false); - /** @var \Foil\Engine $engine */ - $engine = Mockery::mock('Foil\Engine'); - $factory->factory([], $engine); - } - - public function testFactoryStandardClass() - { - $factory = $this->getTemplateFactory(false); - /** @var \Foil\Engine $engine */ - $engine = Mockery::mock('Foil\Engine'); - $instance = $factory->factory('one', $engine); - assertInstanceOf('Foil\Template\Template', $instance); - } - - public function testFactoryCustomDefaultClass() - { - $mock = Mockery::mock('Foil\\Contracts\\TemplateInterface'); - $class = get_class($mock); - $factory = $this->getTemplateFactory($class); - /** @var \Foil\Engine $engine */ - $engine = Mockery::mock('Foil\Engine'); - $instance = $factory->factory('one', $engine); - assertInstanceOf($class, $instance); - } - - public function testFactoryCustomClass() - { - $factory = $this->getTemplateFactory(); - /** @var \Foil\Engine $engine */ - $engine = Mockery::mock('Foil\Engine'); - $mock = Mockery::mock('Foil\\Contracts\\TemplateInterface'); - $class = get_class($mock); - $instance1 = $factory->factory('one', $engine, $class); - $instance2 = $factory->factory('two', $engine); - assertInstanceOf($class, $instance1); - assertInstanceOf('Foil\Template\Template', $instance2); - } - - public function testFactorySameInstance() - { - $factory = $this->getTemplateFactory(); - /** @var \Foil\Engine $engine */ - $engine = Mockery::mock('Foil\Engine'); - $instance1 = $factory->factory('one', $engine); - $instance2 = $factory->factory('two', $engine); - $instance3 = $factory->factory('one', $engine); - assertSame($instance1, $instance3); - assertNotSame($instance1, $instance2); - } -} diff --git a/tests/src/unit/Template/FinderTest.php b/tests/src/unit/Template/FinderTest.php deleted file mode 100644 index 95b83d7..0000000 --- a/tests/src/unit/Template/FinderTest.php +++ /dev/null @@ -1,154 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ -namespace Foil\Tests\Unit\Template; - -use Foil\Tests\TestCase; -use Foil\Template\Finder; - -/** - * @author Giuseppe Mazzapica - * @package foil\foil - * @license http://opensource.org/licenses/MIT MIT - */ -class FinderTest extends TestCase -{ - /** - * @param bool $named - * @return array - */ - private function finderDirectories($named = false) - { - $base = realpath(getenv('FOIL_TESTS_BASEPATH')); - $dirs = [ - $base.DIRECTORY_SEPARATOR.implode(DIRECTORY_SEPARATOR, ['_files', 'foo']), - $base.DIRECTORY_SEPARATOR.implode(DIRECTORY_SEPARATOR, ['_files', 'bar']), - ]; - - return (! $named) ? $dirs : array_combine(['foo', 'bar'], $dirs); - } - - /** - * @expectedException \InvalidArgumentException - */ - public function testInFailsIfBadDir() - { - $finder = new Finder(); - $finder->in(['foo']); - } - - public function testIn() - { - $finder1 = new Finder(); - $unnamed = $this->finderDirectories(); - $finder1->in($unnamed); - $finder2 = new Finder(); - $named = $this->finderDirectories(true); - $finder2->in($named); - $expected = ['_files.foo' => $unnamed[0], '_files.bar' => $unnamed[1]]; - assertSame($expected, $finder1->dirs()); - assertSame($named, $finder2->dirs()); - } - - public function testInEdit() - { - $dirs = $this->finderDirectories(true); - $finder = new Finder(); - $finder->in(['foo' => $dirs['foo']]); - assertSame(['foo' => $dirs['foo']], $finder->dirs()); - // add - $finder->in(['bar' => $dirs['bar']]); - assertSame($dirs, $finder->dirs()); - // edit - $finder->in(['bar' => $dirs['foo']]); - assertSame(['foo' => $dirs['foo'], 'bar' => $dirs['foo']], $finder->dirs()); - $finder->in(['foo' => $dirs['foo']]); // this do nothing - assertSame(['foo' => $dirs['foo'], 'bar' => $dirs['foo']], $finder->dirs()); - // reset - $finder->in(['foo' => $dirs['foo']], true); - assertSame(['foo' => $dirs['foo']], $finder->dirs()); - } - - /** - * @expectedException \InvalidArgumentException - */ - public function testFindFailsIfEmptyTemplate() - { - $finder = new Finder(); - $finder->find([]); - } - - /** - * @expectedException \InvalidArgumentException - */ - public function testFindFailsIfBadTemplate() - { - $finder = new Finder(); - $finder->find(true); - } - - public function testFindInDir() - { - $finder = new Finder(); - $dirs = $this->finderDirectories(true); - $finder->in($dirs); - $found = $finder->find('bar::foo'); - assertSame('foo.php', basename($found)); - assertSame(0, strpos($found, $dirs['bar'])); - } - - public function testFindInDirFalseIfNoTemplate() - { - $finder = new Finder(); - $dirs = $this->finderDirectories(true); - $finder->in($dirs); - assertFalse($finder->find('foo::second')); - } - - public function testFindInDirFalseIfNoDir() - { - $finder = new Finder(); - $dirs = $this->finderDirectories(true); - $finder->in($dirs); - assertFalse($finder->find('meh::second')); - } - - public function testFind() - { - $finder = new Finder(); - $finder->in($this->finderDirectories()); - assertSame('bar.inc', basename($finder->find('bar.inc'))); - assertSame('foo.php', basename($finder->find('foo'))); - } - - public function testFindWithExt() - { - $finder = new Finder('tpl.php'); - $finder->in($this->finderDirectories()); - assertSame('double.tpl.php', basename($finder->find('double'))); - } - - public function testFindMany() - { - $finder = new Finder(); - $dirs = $this->finderDirectories(true); - $finder->in($dirs); - assertSame('foo.php', basename($finder->find(['mhe', 'nope', 'foo']))); - } - - public function testFindManyInDirs() - { - $finder = new Finder(); - $dirs = $this->finderDirectories(true); - $finder->in($dirs); - $found = $finder->find(['foo::mhe', 'foo::second', 'bar::second']); - assertSame('second.php', basename($found)); - assertSame('bar', basename(dirname($found))); - } -} diff --git a/tests/src/unit/Template/StackTest.php b/tests/src/unit/Template/StackTest.php deleted file mode 100644 index 2715473..0000000 --- a/tests/src/unit/Template/StackTest.php +++ /dev/null @@ -1,43 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ -namespace Foil\Tests\Unit\Template; - -use Foil\Tests\TestCase; -use Foil\Template\Stack; -use Mockery; - -/** - * @author Giuseppe Mazzapica - * @package foil\foil - * @license http://opensource.org/licenses/MIT MIT - */ -class StackTest extends TestCase -{ - public function testFactory() - { - /** @var \Foil\Template\Factory|\Mockery\MockInterface $factory */ - $factory = Mockery::mock('Foil\Template\Factory'); - $factory->shouldReceive('factory')->andReturnValues(['foo', 'bar', 'baz']); - $stack = new Stack($factory); - /** @var \Foil\Engine $engine */ - $engine = Mockery::mock('Foil\Engine'); - $stack->factory('/path/to/foo', $engine); - $stack->factory('/path/to/bar', $engine); - $stack->factory('/path/to/baz', $engine); - assertSame('baz', $stack->template()); - assertSame(3, $stack->count()); - $stack->pop(); - assertSame('bar', $stack->template()); - assertSame(2, $stack->count()); - $stack->pop(); - assertSame('foo', $stack->template()); - assertSame(1, $stack->count()); - } -} diff --git a/tests/src/unit/Template/TemplateTest.php b/tests/src/unit/Template/TemplateTest.php deleted file mode 100644 index d52420d..0000000 --- a/tests/src/unit/Template/TemplateTest.php +++ /dev/null @@ -1,267 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ -namespace Foil\Tests\Template; - -use Foil\Tests\TestCase; -use Foil\Template\Template; -use Foil\Template\Alias; -use Mockery; -use ArrayObject; - -/** - * @author Giuseppe Mazzapica - * @package foil\foil - * @license http://opensource.org/licenses/MIT MIT - */ -class TemplateTest extends TestCase -{ - public function testCall() - { - /** @var \Foil\Engine $engine */ - $engine = Mockery::mock('Foil\Engine'); - /** @var \Foil\Kernel\Command|\Mockery\MockInterface $command */ - $command = Mockery::mock('Foil\Kernel\Command'); - $command->shouldReceive('run')->with('foo', 'bar')->once()->andReturn('Foo!'); - - $template = new Template('/path', new ArrayObject(), $engine, $command); - - assertSame('Foo!', $template->foo('bar')); - } - - public function testFilter() - { - /** @var \Foil\Engine $engine */ - $engine = Mockery::mock('Foil\Engine'); - - /** @var \Foil\Kernel\Command|\Mockery\MockInterface $command */ - $command = Mockery::mock('Foil\Kernel\Command'); - $command->shouldReceive('filter') - ->with(Mockery::type('string'), Mockery::any(), []) - ->andReturnValues(['foo', 'bar', null, 'baz']); - - $template = new Template('/path', new ArrayObject(), $engine, $command); - assertSame('baz', $template->filter('first|foo|bar|baz', 'Lorem Ipsum')); - } - - public function testFilterArgs() - { - /** @var \Foil\Engine $engine */ - $engine = Mockery::mock('Foil\Engine'); - - /** @var \Foil\Kernel\Command|\Mockery\MockInterface $command */ - $command = Mockery::mock('Foil\Kernel\Command'); - $command->shouldReceive('filter') - ->once() - ->with('first', 'Lorem Ipsum', ['foo']) - ->andReturn('Lorem!'); - $command->shouldReceive('filter') - ->once() - ->with('last', 'Lorem!', ['bar']) - ->andReturn('Ipsum!'); - - $template = new Template('/path', new ArrayObject(), $engine, $command); - - $filter = $template->filter('first|last', 'Lorem Ipsum', [['foo'], ['bar']]); - - assertSame('Ipsum!', $filter); - } - - public function testRenderNoLayout() - { - // the file foo.php contains the code `echo implode(',', $this->data());` - $base = realpath(getenv('FOIL_TESTS_BASEPATH')).DIRECTORY_SEPARATOR; - $path = $base.implode(DIRECTORY_SEPARATOR, ['_files', 'foo', 'foo.php']); - - /** @var \Foil\Engine|\Mockery\MockInterface $engine */ - $engine = Mockery::mock('Foil\Engine'); - /** @var \Foil\Kernel\Command|\Mockery\MockInterface $command */ - $command = Mockery::mock('Foil\Kernel\Command'); - - $template = new Template($path, new ArrayObject(), $engine, $command); - - $command->shouldReceive('filter') - ->with(Mockery::type('string'), Mockery::any(), []) - ->andReturnValues(['foo', 'bar', null, 'baz']); - - $engine->shouldReceive('fire') - ->with('f.template.prerender', $template) - ->once() - ->andReturnNull(); - - $engine->shouldReceive('fire') - ->with('f.template.rendered', $template) - ->once() - ->andReturnNull(); - - $render = $template->render(['foo', 'bar']); - assertSame('foo,bar', $render); - } - - /** - * @expectedException \InvalidArgumentException - */ - public function testLayoutFailsIfBadFile() - { - /** @var \Foil\Engine|\Mockery\MockInterface $engine */ - $engine = Mockery::mock('Foil\Engine'); - $engine->shouldReceive('find')->with('foo')->once()->andReturn(false); - /** @var \Foil\Kernel\Command|\Mockery\MockInterface $command */ - $command = Mockery::mock('Foil\Kernel\Command'); - $template = new Template('/path', new ArrayObject(), $engine, $command); - $template->layout('foo'); - } - - public function testRenderLayout() - { - $base = realpath(getenv('FOIL_TESTS_BASEPATH')).DIRECTORY_SEPARATOR; - // the file foo.php contains the code `echo implode(',', $this->data());` - $path = $base.implode(DIRECTORY_SEPARATOR, ['_files', 'foo', 'foo.php']); - // the file foo.php contains the code `echo implode('|', $this->data());` - $layout = $base.implode(DIRECTORY_SEPARATOR, ['_files', 'foo', 'bar.inc']); - - /** @var \Foil\Engine|\Mockery\MockInterface $engine */ - $engine = Mockery::mock('Foil\Engine'); - /** @var \Foil\Kernel\Command|\Mockery\MockInterface $command */ - $command = Mockery::mock('Foil\Kernel\Command'); - - $template = new Template($path, new ArrayObject(), $engine, $command); - - $engine->shouldReceive('fire') - ->with('f.template.prerender', $template) - ->once() - ->andReturnNull(); - - $engine->shouldReceive('fire') - ->with('f.template.layout', $layout, $template) - ->once() - ->andReturnNull(); - - $engine->shouldReceive('fire') - ->with('f.template.renderlayout', $layout, $template) - ->once() - ->andReturnNull(); - - $engine->shouldReceive('fire') - ->with('f.template.rendered', $template) - ->once() - ->andReturnNull(); - - $engine->shouldReceive('find') - ->with('bar.inc') - ->once() - ->andReturn($layout); - - $template->layout('bar.inc'); - assertSame('foo|bar', $template->render(['foo', 'bar'])); - assertSame('foo,bar', $template->lastBuffer()); - } - - public function testSupply() - { - /** @var \Foil\Engine|\Mockery\MockInterface $engine */ - $engine = Mockery::mock('Foil\Engine'); - /** @var \Foil\Kernel\Command|\Mockery\MockInterface $command */ - $command = Mockery::mock('Foil\Kernel\Command'); - $section = Mockery::mock('Foil\Section\Section'); - $section->shouldReceive('content')->once()->andReturn('Ok!'); - $template = new Template('/path', new ArrayObject(['foo' => $section]), $engine, $command); - - assertSame('Ok!', $template->supply('foo')); - } - - public function testSupplyDefaultString() - { - /** @var \Foil\Engine|\Mockery\MockInterface $engine */ - $engine = Mockery::mock('Foil\Engine'); - /** @var \Foil\Kernel\Command|\Mockery\MockInterface $command */ - $command = Mockery::mock('Foil\Kernel\Command'); - $template = new Template('/path', new ArrayObject(), $engine, $command); - - assertSame('Ok!', $template->supply('foo', 'Ok!')); - } - - public function testSupplyDefaultCallable() - { - /** @var \Foil\Engine|\Mockery\MockInterface $engine */ - $engine = Mockery::mock('Foil\Engine'); - /** @var \Foil\Kernel\Command|\Mockery\MockInterface $command */ - $command = Mockery::mock('Foil\Kernel\Command'); - $template = new Template('/path', new ArrayObject(), $engine, $command); - - assertSame('Ok!', $template->supply('foo', function ($section, $tmpl) use ($template) { - assertSame('foo', $section); - assertSame($template, $tmpl); - - return 'Ok!'; - })); - } - - public function testInsert() - { - /** @var \Foil\Engine|\Mockery\MockInterface $engine */ - $engine = Mockery::mock('Foil\Engine'); - /** @var \Foil\Kernel\Command|\Mockery\MockInterface $command */ - $command = Mockery::mock('Foil\Kernel\Command'); - - $template = new Template('/path', new ArrayObject(), $engine, $command); - - $engine->shouldReceive('fire') - ->once() - ->with('f.template.prepartial', 'foo', ['foo' => 'foo'], $template) - ->andReturnNull(); - $engine->shouldReceive('fire') - ->once() - ->with('f.template.afterpartial', $template) - ->andReturnNull(); - - $engine->shouldReceive('render') - ->once() - ->with('foo', ['foo' => 'foo']) - ->andReturn('Ok!'); - - assertSame('Ok!', $template->insert('foo', ['foo' => 'foo'])); - } - - public function testInsertIfDoNothingIfFileNotExists() - { - /** @var \Foil\Engine|\Mockery\MockInterface $engine */ - $engine = Mockery::mock('Foil\Engine'); - $engine->shouldReceive('find')->with('foo')->once()->andReturn(false); - /** @var \Foil\Kernel\Command|\Mockery\MockInterface $command */ - $command = Mockery::mock('Foil\Kernel\Command'); - $template = new Template('/path', new ArrayObject(), $engine, $command); - - assertSame('', $template->insertif('foo')); - } - - public function testAlias() - { - /** @var \Foil\Engine $engine */ - $engine = Mockery::mock('Foil\Engine'); - /** @var \Foil\Kernel\Command|\Mockery\MockInterface $command */ - $command = Mockery::mock('Foil\Kernel\Command'); - $command->shouldReceive('run') - ->with('v', 'foo') - ->andReturn('Foo!'); - - $template = new Template('/path', new ArrayObject(), $engine, $command); - - $template->alias(new Alias('T')); - - $file = realpath(getenv('FOIL_TESTS_BASEPATH').'/_files/foo/alias.php'); - - $this->bindClosure(function ($file) { - /** @noinspection PhpUndefinedMethodInspection */ - $this->collect($file); - }, $template, [$file]); - - assertSame('Foo!', $template->buffer()); - } -}