diff options
| author | Jan-Hendrik Willms <tleilax+studip@gmail.com> | 2024-05-16 11:05:47 +0000 |
|---|---|---|
| committer | David Siegfried <david.siegfried@uni-vechta.de> | 2024-05-16 11:05:47 +0000 |
| commit | efaeea07319c63be2f2c6a8bd076e4de8ac8f11d (patch) | |
| tree | b6c4f4f976cee1e156f8f9dd0fec39a9f09e6242 /tests | |
| parent | bb2278c43c4992c2cc0fc2d468ad98a395e13962 (diff) | |
relocate flexi, fixes #4101
Closes #4101
Merge request studip/studip!2977
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/functional/_bootstrap.php | 4 | ||||
| -rw-r--r-- | tests/jsonapi/BlubberThreadsCreateTest.php | 2 | ||||
| -rw-r--r-- | tests/jsonapi/BlubberThreadsIndexTest.php | 3 | ||||
| -rw-r--r-- | tests/jsonapi/BlubberThreadsShowTest.php | 2 | ||||
| -rw-r--r-- | tests/jsonapi/ConsultationHelper.php | 5 | ||||
| -rw-r--r-- | tests/jsonapi/_bootstrap.php | 1 | ||||
| -rw-r--r-- | tests/unit/_bootstrap.php | 6 | ||||
| -rw-r--r-- | tests/unit/lib/classes/StudipFileloaderTest.php | 2 | ||||
| -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 | ||||
| -rw-r--r-- | tests/unit/varstream.php | 106 |
15 files changed, 568 insertions, 53 deletions
diff --git a/tests/functional/_bootstrap.php b/tests/functional/_bootstrap.php index 8a9125b..1745a1a 100644 --- a/tests/functional/_bootstrap.php +++ b/tests/functional/_bootstrap.php @@ -34,6 +34,7 @@ StudipAutoloader::addAutoloadPath($STUDIP_BASE_PATH . '/lib/classes/cache'); StudipAutoloader::addAutoloadPath($STUDIP_BASE_PATH . '/lib/classes/cache', 'Studip'); StudipAutoloader::addAutoloadPath($STUDIP_BASE_PATH . '/lib/exceptions'); StudipAutoloader::addAutoloadPath($STUDIP_BASE_PATH . '/lib/exceptions/resources'); +StudipAutoloader::addAutoloadPath($STUDIP_BASE_PATH . '/lib/flexi', 'Flexi'); StudipAutoloader::addAutoloadPath($STUDIP_BASE_PATH . '/lib/filesystem'); StudipAutoloader::addAutoloadPath($STUDIP_BASE_PATH . '/lib/migrations'); StudipAutoloader::addAutoloadPath($STUDIP_BASE_PATH . '/lib/models'); @@ -64,8 +65,7 @@ require 'vendor/email_message/email_message.php'; require 'vendor/email_message/debug_message.php'; StudipMail::setDefaultTransporter(new debug_message_class()); -require_once 'vendor/flexi/lib/flexi.php'; -$GLOBALS['template_factory'] = new Flexi_TemplateFactory(dirname(dirname(__DIR__)) . '/templates'); +$GLOBALS['template_factory'] = new Flexi\Factory(dirname(dirname(__DIR__)) . '/templates'); // Disable caching to fallback to memory cache $GLOBALS['CACHING_ENABLE'] = false; diff --git a/tests/jsonapi/BlubberThreadsCreateTest.php b/tests/jsonapi/BlubberThreadsCreateTest.php index 17846f8..d2bdaea 100644 --- a/tests/jsonapi/BlubberThreadsCreateTest.php +++ b/tests/jsonapi/BlubberThreadsCreateTest.php @@ -22,7 +22,7 @@ class BlubberThreadsCreateTest extends \Codeception\Test\Unit // Create global template factory if neccessary $has_template_factory = isset($GLOBALS['template_factory']); if (!$has_template_factory) { - $GLOBALS['template_factory'] = new Flexi_TemplateFactory($GLOBALS['STUDIP_BASE_PATH'] . '/templates'); + $GLOBALS['template_factory'] = new Flexi\Factory($GLOBALS['STUDIP_BASE_PATH'] . '/templates'); } } diff --git a/tests/jsonapi/BlubberThreadsIndexTest.php b/tests/jsonapi/BlubberThreadsIndexTest.php index ab6d200..14173d3 100644 --- a/tests/jsonapi/BlubberThreadsIndexTest.php +++ b/tests/jsonapi/BlubberThreadsIndexTest.php @@ -1,6 +1,5 @@ <?php -use JsonApi\Errors\RecordNotFoundException; use JsonApi\Routes\Blubber\ThreadsIndex; require_once 'BlubberTestHelper.php'; @@ -21,7 +20,7 @@ class BlubberThreadsIndexTest extends \Codeception\Test\Unit // Create global template factory if neccessary $has_template_factory = isset($GLOBALS['template_factory']); if (!$has_template_factory) { - $GLOBALS['template_factory'] = new Flexi_TemplateFactory($GLOBALS['STUDIP_BASE_PATH'] . '/templates'); + $GLOBALS['template_factory'] = new Flexi\Factory($GLOBALS['STUDIP_BASE_PATH'] . '/templates'); } } diff --git a/tests/jsonapi/BlubberThreadsShowTest.php b/tests/jsonapi/BlubberThreadsShowTest.php index 932b768..875fc8c 100644 --- a/tests/jsonapi/BlubberThreadsShowTest.php +++ b/tests/jsonapi/BlubberThreadsShowTest.php @@ -23,7 +23,7 @@ class BlubberThreadsShowTest extends \Codeception\Test\Unit // Create global template factory if neccessary $has_template_factory = isset($GLOBALS['template_factory']); if (!$has_template_factory) { - $GLOBALS['template_factory'] = new Flexi_TemplateFactory($GLOBALS['STUDIP_BASE_PATH'] . '/templates'); + $GLOBALS['template_factory'] = new Flexi\Factory($GLOBALS['STUDIP_BASE_PATH'] . '/templates'); } } diff --git a/tests/jsonapi/ConsultationHelper.php b/tests/jsonapi/ConsultationHelper.php index a46cb47..673174e 100644 --- a/tests/jsonapi/ConsultationHelper.php +++ b/tests/jsonapi/ConsultationHelper.php @@ -3,9 +3,6 @@ use WoohooLabs\Yang\JsonApi\Response\JsonApiResponse; use WoohooLabs\Yang\JsonApi\Schema\Document; use WoohooLabs\Yang\JsonApi\Schema\Resource\ResourceObject; -// Required for consultation mailer -require_once 'vendor/flexi/flexi.php'; - trait ConsultationHelper { /** @@ -96,7 +93,7 @@ trait ConsultationHelper // Create global template factory if neccessary $has_template_factory = isset($GLOBALS['template_factory']); if (!$has_template_factory) { - $GLOBALS['template_factory'] = new Flexi_TemplateFactory($GLOBALS['STUDIP_BASE_PATH'] . '/templates'); + $GLOBALS['template_factory'] = new Flexi\Factory($GLOBALS['STUDIP_BASE_PATH'] . '/templates'); } $result = $this->tester->withPHPLib($credentials, $fn); diff --git a/tests/jsonapi/_bootstrap.php b/tests/jsonapi/_bootstrap.php index cb6df04..dea1c81 100644 --- a/tests/jsonapi/_bootstrap.php +++ b/tests/jsonapi/_bootstrap.php @@ -45,6 +45,7 @@ StudipAutoloader::addAutoloadPath($GLOBALS['STUDIP_BASE_PATH'].'/lib/models/cale StudipAutoloader::addAutoloadPath($GLOBALS['STUDIP_BASE_PATH'].'/lib/models/resources'); StudipAutoloader::addAutoloadPath($GLOBALS['STUDIP_BASE_PATH'].'/lib/classes'); StudipAutoloader::addAutoloadPath($GLOBALS['STUDIP_BASE_PATH'].'/lib/classes', 'Studip'); +StudipAutoloader::addAutoloadPath($GLOBALS['STUDIP_BASE_PATH'].'/lib/flexi', 'Flexi'); // Plugins StudipAutoloader::addAutoloadPath($GLOBALS['STUDIP_BASE_PATH'].'/lib/plugins/core'); diff --git a/tests/unit/_bootstrap.php b/tests/unit/_bootstrap.php index af95607..a1dbdd1 100644 --- a/tests/unit/_bootstrap.php +++ b/tests/unit/_bootstrap.php @@ -53,12 +53,12 @@ StudipAutoloader::addAutoloadPath('lib/activities', 'Studip\\Activity'); StudipAutoloader::addAutoloadPath('lib/models'); StudipAutoloader::addAutoloadPath('lib/classes'); StudipAutoloader::addAutoloadPath('lib/classes', 'Studip'); -StudipAutoloader::addAutoloadPath('lib/exTpl', 'exTpl'); -StudipAutoloader::addAutoloadPath('lib/classes/cache'); StudipAutoloader::addAutoloadPath('lib/classes/cache', 'Studip'); -StudipAutoloader::addAutoloadPath('lib/exceptions'); StudipAutoloader::addAutoloadPath('lib/classes/sidebar'); StudipAutoloader::addAutoloadPath('lib/classes/helpbar'); +StudipAutoloader::addAutoloadPath('lib/exTpl', 'exTpl'); +StudipAutoloader::addAutoloadPath('lib/exceptions'); +StudipAutoloader::addAutoloadPath('lib/flexi', 'Flexi'); StudipAutoloader::addAutoloadPath('lib/plugins/engine'); StudipAutoloader::addAutoloadPath('lib/plugins/core'); StudipAutoloader::addAutoloadPath('lib/plugins/db'); diff --git a/tests/unit/lib/classes/StudipFileloaderTest.php b/tests/unit/lib/classes/StudipFileloaderTest.php index 62c5e04..102aaac 100644 --- a/tests/unit/lib/classes/StudipFileloaderTest.php +++ b/tests/unit/lib/classes/StudipFileloaderTest.php @@ -20,7 +20,7 @@ class StudipFileloaderTestCase extends \Codeception\Test\Unit ]); if (!stream_wrapper_register('var', 'ArrayFileStream')) { - new Exception('Failed to register protocol'); + throw new Exception('Failed to register protocol'); } } diff --git a/tests/unit/lib/flexi/FactoryTest.php b/tests/unit/lib/flexi/FactoryTest.php new file mode 100644 index 0000000..2df9f6a --- /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 { + public 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()); + } +} diff --git a/tests/unit/varstream.php b/tests/unit/varstream.php index 9a3acb5..d2047c0 100644 --- a/tests/unit/varstream.php +++ b/tests/unit/varstream.php @@ -7,15 +7,17 @@ class ArrayFileStream private static $fs; - static function set_filesystem(array $fs) { + static function set_filesystem(array $fs) + { ArrayFileStream::$fs = $fs; } - private static function &get_element($path) { + private static function &get_element($path) + { $result =& ArrayFileStream::$fs; foreach (preg_split('/\//', $path, -1, PREG_SPLIT_NO_EMPTY) as $element) { if (!isset($result[$element])) { - $null = NULL; + $null = null; return $null; } $result =& $result[$element]; @@ -23,9 +25,10 @@ class ArrayFileStream return $result; } - private static function &get_file($path) { + private static function &get_file($path) + { $url = parse_url($path); - $file =& self::get_element($url['host'] . $url['path']); + $file =& self::get_element($url['host'] . ($url['path'] ?? '')); if (is_null($file)) { throw new Exception("file not found."); @@ -33,15 +36,18 @@ class ArrayFileStream return $file; } - public function stream_close() { + public function stream_close() + { # nothing to do } - public function stream_flush() { + public function stream_flush() + { # nothing to do } - public function stream_open($path, $mode, $options, $opened_path) { + public function stream_open($path, $mode, $options, $opened_path) + { try { $this->open_file =& self::get_file($path); $this->position = 0; @@ -51,37 +57,41 @@ class ArrayFileStream } } - public function stream_read($count) { + public function stream_read($count) + { $ret = mb_substr($this->open_file, $this->position, $count); $this->position += mb_strlen($ret); return $ret; } - public function stream_write($data) { - $left = mb_substr($this->open_file, 0, $this->position); + public function stream_write($data) + { + $left = mb_substr($this->open_file, 0, $this->position); $right = mb_substr($this->open_file, $this->position + mb_strlen($data)); $this->open_file = $left . $data . $right; $this->position += mb_strlen($data); return mb_strlen($data); } - public function stream_tell() { + public function stream_tell() + { return $this->position; } - public function stream_eof() { + public function stream_eof() + { return $this->position >= mb_strlen($this->open_file); } - public function stream_seek($offset, $whence) { + public function stream_seek($offset, $whence) + { switch ($whence) { case SEEK_SET: if ($offset < mb_strlen($this->open_file) && $offset >= 0) { $this->position = $offset; return true; - } - else { + } else { return false; } break; @@ -90,8 +100,7 @@ class ArrayFileStream if ($offset >= 0) { $this->position += $offset; return true; - } - else { + } else { return false; } break; @@ -100,8 +109,7 @@ class ArrayFileStream if (mb_strlen($this->open_file) + $offset >= 0) { $this->position = mb_strlen($this->open_file) + $offset; return true; - } - else { + } else { return false; } break; @@ -115,44 +123,61 @@ class ArrayFileStream { } - public function stream_stat() { - return array('size' => is_array($this->open_file) - ? sizeof($this->open_file) - : mb_strlen($this->open_file)); + public function stream_stat() + { + return [ + 'size' => is_array($this->open_file) + ? sizeof($this->open_file) + : mb_strlen($this->open_file), + ]; } - public function unlink($path) { + public function unlink($path) + { $parent =& self::get_file(dirname($path)); if (is_array($parent) && isset($parent[basename($path)])) { unset($parent[basename($path)]); - return TRUE; + return true; } - return FALSE; + return false; } - public function rename($path_from, $path_to) { + public function rename($path_from, $path_to) + { throw new Exception('not implemented yet'); } - public function mkdir($path, $mode, $options) { + public function mkdir($path, $mode, $options) + { throw new Exception('not implemented yet'); } - public function rmdir($path, $options) { + public function rmdir($path, $options) + { throw new Exception('not implemented yet'); } - public function dir_opendir($path, $options) { + public function dir_opendir($path, $options) + { throw new Exception('not implemented yet'); } - public function url_stat($path, $flags) { + public function url_stat($path, $flags) + { + try { + if (!self::get_file($path)) { + return false; + } + } catch (Exception $e) { + return false; + } + $time = time(); - $keys = array( + $keys = [ 'dev' => 0, 'ino' => 0, 'mode' => 33216, // chmod 700 @@ -161,25 +186,28 @@ class ArrayFileStream 'gid' => function_exists('posix_getgid') ? posix_getgid() : 0, 'rdev' => 0, 'size' => $flags & STREAM_URL_STAT_QUIET - ? @mb_strlen($this->open_file) : mb_strlen($this->open_file), + ? @mb_strlen($this->open_file) : mb_strlen($this->open_file), 'atime' => $time, 'mtime' => $time, 'ctime' => $time, 'blksize' => 0, - 'blocks' => 0 - ); + 'blocks' => 0, + ]; return array_merge(array_values($keys), $keys); } - public function dir_readdir() { + public function dir_readdir() + { throw new Exception('not implemented yet'); } - public function dir_rewinddir() { + public function dir_rewinddir() + { throw new Exception('not implemented yet'); } - public function dir_closedir() { + public function dir_closedir() + { throw new Exception('not implemented yet'); } } |
