diff options
Diffstat (limited to 'tests/unit/lib/flexi')
| -rw-r--r-- | tests/unit/lib/flexi/FactoryTest.php | 119 | ||||
| -rw-r--r-- | tests/unit/lib/flexi/PHPTemplatePartialBugTest.php | 45 | ||||
| -rw-r--r-- | tests/unit/lib/flexi/PHPTemplateTest.php | 136 | ||||
| -rw-r--r-- | tests/unit/lib/flexi/TemplateEmptyTest.php | 44 | ||||
| -rw-r--r-- | tests/unit/lib/flexi/TemplateMagicMethodsTest.php | 78 | ||||
| -rw-r--r-- | tests/unit/lib/flexi/TemplateTest.php | 68 |
6 files changed, 490 insertions, 0 deletions
diff --git a/tests/unit/lib/flexi/FactoryTest.php b/tests/unit/lib/flexi/FactoryTest.php new file mode 100644 index 0000000..f8b9b87 --- /dev/null +++ b/tests/unit/lib/flexi/FactoryTest.php @@ -0,0 +1,119 @@ +<?php + +use Flexi\Factory; +use Flexi\TemplateNotFoundException; +use Flexi\PhpTemplate; + +final class FactoryTestCase extends \Codeception\Test\Unit +{ + private Factory $factory; + + public function setUp(): void + { + $this->setUpFS(); + + $this->factory = new Factory('var://templates'); + } + + public function tearDown(): void + { + unset($this->factory); + stream_wrapper_unregister('var'); + } + + public function setUpFS(): void + { + ArrayFileStream::set_filesystem([ + 'templates' => [ + 'foo.php' => 'some content', + 'baz.unknown' => 'some content', + 'multiplebasenames' => [ + 'foo.txt' => 'there is no matching template class', + 'foo.php' => 'some content', + 'bar.txt' => 'there is no matching template class', + ], + 'baz.known-ext' => 'some content', + ], + ]); + if (!stream_wrapper_register('var', ArrayFileStream::class)) { + die('Failed to register protocol'); + } + } + + public function testShouldCreateFactory() + { + $factory = new Factory('.'); + $this->assertNotNull($factory); + } + + public function testShouldCreateFactoryUsingPath() + { + $path = 'var://'; + $factory = new Factory($path); + $this->assertNotNull($factory); + } + + public function testShouldOpenTemplateUsingRelativePath() + { + $foo = $this->factory->open('foo'); + $this->assertNotNull($foo); + } + + public function testShouldOpenTemplateUsingAbsolutePath() + { + $foo = $this->factory->open('var://templates/foo'); + $this->assertNotNull($foo); + } + + public function testShouldThrowAnExceptionOpeningAMissingTemplateWithoutFileExtension() + { + $this->expectException(TemplateNotFoundException::class); + $this->factory->open('bar'); + } + + public function testShouldThrowAnExceptionOpeningAMissingTemplateWithFileExtension() + { + $this->expectException(TemplateNotFoundException::class); + $this->factory->open('bar.php'); + } + + public function testShouldOpenTemplateUsingExtension() + { + $this->assertInstanceOf( + PhpTemplate::class, + $this->factory->open('foo.php') + ); + } + + public function testShouldThrowAnExceptionWhenOpeningATemplateWithUnknownExtension() + { + $this->expectException(TemplateNotFoundException::class); + $this->factory->open('baz'); + } + + public function testShouldThrowAnExceptionOpeningATemplateInANonExistingDirectory() + { + $this->expectException(TemplateNotFoundException::class); + $this->factory->open('doesnotexist/foo'); + } + + public function testShouldSearchForASupportedTemplate() + { + $this->assertInstanceOf( + PhpTemplate::class, + $this->factory->open('multiplebasenames/foo') + ); + } + + public function testShouldRespondToAddedHandlers() + { + $handler = new class('', $this->factory) extends Flexi\Template { + protected function _render(): string + { + return ''; + } + }; + $this->factory->add_handler('known-ext', $handler::class); + $this->factory->open('baz.known-ext'); + } +} diff --git a/tests/unit/lib/flexi/PHPTemplatePartialBugTest.php b/tests/unit/lib/flexi/PHPTemplatePartialBugTest.php new file mode 100644 index 0000000..ef265cf --- /dev/null +++ b/tests/unit/lib/flexi/PHPTemplatePartialBugTest.php @@ -0,0 +1,45 @@ +<?php + +use Flexi\Factory; + +final class PhpTemplatePartialBugTestCase extends Codeception\Test\Unit +{ + public function setUp(): void + { + $this->setUpFS(); + $this->factory = new Factory('var://templates/'); + } + + public function tearDown(): void + { + unset($this->factory); + + stream_wrapper_unregister("var"); + } + + public function setUpFS(): void + { + ArrayFileStream::set_filesystem([ + 'templates' => [ + 'layout.php' => + '<? $do_not_echo_this = $this->render_partial_collection("partial", range(1, 5));' . + 'echo $content_for_layout;', + 'partial.php' => + 'partial', + 'template.php' => + 'template', + ] + ]); + if (!stream_wrapper_register('var', ArrayFileStream::class)) { + die('Failed to register protocol'); + } + } + + public function testPartialBug() + { + $template = $this->factory->open('template'); + $template->set_layout('layout'); + $result = $template->render(); + $this->assertEquals($result, "template"); + } +} diff --git a/tests/unit/lib/flexi/PHPTemplateTest.php b/tests/unit/lib/flexi/PHPTemplateTest.php new file mode 100644 index 0000000..5dfd247 --- /dev/null +++ b/tests/unit/lib/flexi/PHPTemplateTest.php @@ -0,0 +1,136 @@ +<?php + +use Flexi\Factory; +use Flexi\TemplateNotFoundException; + +final class PhpTemplateTestCase extends Codeception\Test\Unit +{ + private Factory $factory; + + public function setUp(): void + { + $this->setUpFS(); + $this->factory = new Factory('var://templates/'); + } + + + public function tearDown(): void + { + unset($this->factory); + + stream_wrapper_unregister('var'); + } + + public function setUpFS() + { + ArrayFileStream::set_filesystem([ + 'templates' => [ + 'foo_using_partial.php' => + 'Hello, <?= $this->render_partial("foos_partial") ?>!', + + 'foos_partial.php' => + '<h1><?= $whom ?> at <?= $when ?></h1>', + + 'foo_with_partial_collection.php' => + '[<?= $this->render_partial_collection("item", $items, "spacer") ?>]', + + 'item.php' => + '"<?= $item ?>"', + + 'spacer.php' => + ', ', + + 'attributes.php' => + '<? foreach (get_defined_vars() as $name => $value) : ?>' . + '<?= $name ?><?= $value ?>' . + '<? endforeach ?>', + + 'foo.php' => + 'Hello, <?= $whom ?>!', + + 'layout.php' => + '[<?= $content_for_layout ?>]', + ] + ]); + if (!stream_wrapper_register('var', ArrayFileStream::class)) { + die('Failed to register protocol'); + } + } + + public function testRenderPartial() + { + $template = $this->factory->open('foo_using_partial'); + $template->set_attribute('whom', 'bar'); + $this->assertEquals( + 'Hello, <h1>bar at now</h1>!', + $template->render(['when' => 'now']) + ); + } + + public function testRenderPartialCollection() + { + $template = $this->factory->open('foo_with_partial_collection'); + $result = $template->render_partial_collection( + 'item', + range(1, 3), + 'spacer' + ); + $this->assertEquals('"1", "2", "3"', $result); + } + + public function testShouldOverrideAttributesWithThosePassedToRender() + { + $template = $this->factory->open('attributes'); + $template->set_attribute('foo', 'baz'); + + $template->render(['foo' => 'bar']); + $this->assertEquals('bar', $template->get_attribute('foo')); + + $template->render(); + $this->assertEquals('bar', $template->get_attribute('foo')); + } + + public function testRenderWithoutLayout() + { + $foo = $this->factory->open('foo'); + $foo->set_attribute('whom', 'bar'); + $this->assertEquals('Hello, bar!', $foo->render()); + } + + public function testRenderWithLayout() + { + $foo = $this->factory->open('foo'); + $foo->set_attribute('whom', 'bar'); + $foo->set_layout('layout'); + $out = $foo->render(); + $this->assertEquals('[Hello, bar!]', $out); + } + + public function testRenderWithLayoutInline() + { + $this->assertEquals( + '[Hello, bar!]', + $this->factory->render('foo', ['whom' => 'bar'], 'layout') + ); + } + + public function testRenderWithMissingLayout() + { + $foo = $this->factory->open('foo'); + $this->expectException(TemplateNotFoundException::class); + $foo->set_layout('nosuchlayout'); + } + + public function testRenderWithAttributes() + { + $foo = $this->factory->open('foo'); + $foo->set_attribute('whom', 'bar'); + $foo->set_layout('layout'); + $foo_out = $foo->render(); + + $bar = $this->factory->open('foo'); + $bar_out = $bar->render(['whom' => 'bar'], 'layout'); + + $this->assertEquals($foo_out, $bar_out); + } +} diff --git a/tests/unit/lib/flexi/TemplateEmptyTest.php b/tests/unit/lib/flexi/TemplateEmptyTest.php new file mode 100644 index 0000000..c5ebd79 --- /dev/null +++ b/tests/unit/lib/flexi/TemplateEmptyTest.php @@ -0,0 +1,44 @@ +<?php + +use Flexi\Factory; +use Flexi\Template; + +final class TemplateEmptyTestCase extends \Codeception\Test\Unit +{ + private Factory $factory; + + public function setUp(): void + { + $this->factory = $this->make(Factory::class, [ + 'open' => $this->make(Template::class), + ]); + } + + public function tearDown(): void + { + unset($this->factory); + } + + public function testShouldHaveNoAttributes() + { + $template = $this->factory->open(''); + $this->assertCount(0, $template->get_attributes()); + } + + public function testShouldNotBeEmptyAfterSettingAnAttribute() + { + $template = $this->factory->open(''); + $template->set_attribute('foo', 'bar'); + $this->assertNotEmpty($template->get_attributes()); + } + + public function testShouldBeEmptyAfterClear() + { + $template = $this->factory->open('foo'); + + $this->assertEmpty($template->get_attributes()); + + $template->clear_attributes(); + $this->assertEmpty($template->get_attributes()); + } +} diff --git a/tests/unit/lib/flexi/TemplateMagicMethodsTest.php b/tests/unit/lib/flexi/TemplateMagicMethodsTest.php new file mode 100644 index 0000000..ad2690a --- /dev/null +++ b/tests/unit/lib/flexi/TemplateMagicMethodsTest.php @@ -0,0 +1,78 @@ +<?php + +use Flexi\Factory; +use Flexi\Template; + +final class TemplateMagicMethodsTestCase extends \Codeception\Test\Unit +{ + private Factory $factory; + + public function setUp(): void + { + $this->factory = $this->make(Factory::class, [ + 'open' => $this->make(Template::class), + ]); + $this->template = $this->factory->open(''); + } + + public function tearDown(): void + { + unset($this->factory); + unset($this->template); + } + + public function testShouldSetAnAttributeUsingTheMagicMethods() + { + $this->template->foo = 'bar'; + $this->assertEquals('bar', $this->template->get_attribute('foo')); + } + + public function testShouldNotSetAProtectedMemberFieldAsAnAttribute() + { + $this->template->layout = 'bar'; + $this->assertEquals('bar', $this->template->layout); + $this->assertNotEquals('bar', $this->template->get_layout()); + } + + public function testShouldOverwriteAnAttribute() + { + $this->template->set_attribute('foo', 'bar'); + $this->template->foo = 'baz'; + $this->assertEquals('baz', $this->template->get_attribute('foo')); + } + + public function testShouldReturnAnExistingAttributeUsingTheMagicMethods() + { + $this->template->set_attribute('foo', 'bar'); + $this->assertEquals('bar', $this->template->foo); + } + + public function testShouldReturnNullForANonExistingAttributeUsingTheMagicMethods() + { + $this->assertNull($this->template->foo); + } + + public function testShouldUnsetAnAttributeUsingTheMagicMethods() + { + $this->template->foo = 'bar'; + unset($this->template->foo); + $this->assertNull($this->template->foo); + } + + public function testShouldReturnNullOnUnsettingANonAttribute() + { + unset($this->template->foo); + $this->assertNull($this->template->foo); + } + + public function testShouldReturnTrueOnIssetForAnAttribute() + { + $this->template->foo = 'bar'; + $this->assertTrue(isset($this->template->foo)); + } + + public function testShouldReturnFalseOnIssetForANonExistingAttribute() + { + $this->assertFalse(isset($this->template->foo)); + } +} diff --git a/tests/unit/lib/flexi/TemplateTest.php b/tests/unit/lib/flexi/TemplateTest.php new file mode 100644 index 0000000..95e9145 --- /dev/null +++ b/tests/unit/lib/flexi/TemplateTest.php @@ -0,0 +1,68 @@ +<?php + +use Flexi\Factory; +use Flexi\Template; + +final class TemplateTestCase extends \Codeception\Test\Unit +{ + private Factory $factory; + + public function setUp(): void + { + $this->factory = $this->make(Factory::class, [ + 'open' => $this->make(Template::class), + ]); + } + + public function tearDown(): void + { + unset($this->factory); + } + + public function testShouldReturnAPreviouslySetAttribute() + { + $template = $this->factory->open('foo'); + $template->set_attribute('whom', 'bar'); + $this->assertEquals('bar', $template->get_attribute('whom')); + } + + public function testShouldReturnPreviouslySetAttributes() + { + $template = $this->factory->open('foo'); + $template->set_attributes(['whom' => 'bar', 'foo' => 'baz']); + + $attributes = $template->get_attributes(); + $this->assertIsArray($attributes); + $this->assertCount(2, $attributes); + $this->assertEquals('bar', $attributes['whom']); + $this->assertEquals('baz', $attributes['foo']); + } + + public function testShouldMergeAttributesWithSetAttributes() + { + $template = $this->factory->open('foo'); + $template->set_attributes(['a' => 1, 'b' => 2]); + + $this->assertCount(2, $template->get_attributes()); + $this->assertEquals(1, $template->get_attribute('a')); + $this->assertEquals(2, $template->get_attribute('b')); + + $template->set_attributes(['b' => 8, 'c' => 9]); + + $this->assertCount(3, $template->get_attributes()); + $this->assertEquals(1, $template->get_attribute('a')); + $this->assertEquals(8, $template->get_attribute('b')); + $this->assertEquals(9, $template->get_attribute('c')); + } + + public function testShouldBeEmptyAfterClear() + { + $template = $this->factory->open('foo'); + + $template->set_attributes(['a' => 1, 'b' => 2]); + $this->assertNotEmpty($template->get_attributes()); + + $template->clear_attributes(); + $this->assertCount(0, $template->get_attributes()); + } +} |
