From 1a5c8b3f7e56f9d7d5c70e60b85bc0313eee45c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20H=C5=AFla?= Date: Fri, 17 Jun 2016 15:01:05 +0200 Subject: [PATCH 1/2] tests: added RemoteStream to be able test non-local URLs offline (#93) --- tests/Utils/FileSystem.phpt | 7 +++++-- tests/bootstrap.php | 9 +++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/tests/Utils/FileSystem.phpt b/tests/Utils/FileSystem.phpt index 68871f2f3..81e99bf27 100644 --- a/tests/Utils/FileSystem.phpt +++ b/tests/Utils/FileSystem.phpt @@ -36,12 +36,14 @@ Assert::exception(function () { test(function () { // copy + Assert::false(stream_is_local('remote://example.com')); + FileSystem::write(TEMP_DIR . '/3/file', 'Hello'); FileSystem::copy(TEMP_DIR . '/3/file', TEMP_DIR . '/3/x/file'); Assert::same('Hello', file_get_contents(TEMP_DIR . '/3/x/file')); - FileSystem::copy('http://example.com', TEMP_DIR . '/3/x/y/file'); + FileSystem::copy('remote://example.com', TEMP_DIR . '/3/x/y/file'); Assert::true(is_file(TEMP_DIR . '/3/x/y/file')); FileSystem::write(TEMP_DIR . '/5/newfile', 'World'); @@ -52,7 +54,7 @@ test(function () { // copy Assert::same('Hello', file_get_contents(TEMP_DIR . '/3/x/file')); Assert::exception(function () { - FileSystem::copy('http://example.com', TEMP_DIR . '/3/x/file', FALSE); + FileSystem::copy('remote://example.com', TEMP_DIR . '/3/x/file', FALSE); }, 'Nette\InvalidStateException', "File or directory '%a%' already exists."); Assert::same('Hello', file_get_contents(TEMP_DIR . '/3/x/file')); @@ -131,4 +133,5 @@ test(function () { // isAbsolute Assert::true(FileSystem::isAbsolute('d:\file')); Assert::true(FileSystem::isAbsolute('D:\file')); Assert::true(FileSystem::isAbsolute('http://file')); + Assert::true(FileSystem::isAbsolute('remote://file')); }); diff --git a/tests/bootstrap.php b/tests/bootstrap.php index d5af950ff..156db5cb8 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -38,3 +38,12 @@ function test(\Closure $function) before(); $function(); } + + +class RemoteStream /* extends \streamWrapper */ +{ + public function stream_open() { return TRUE; } + public function url_stat() { return FALSE; } +} + +stream_wrapper_register('remote', 'RemoteStream', STREAM_IS_URL); From 66ceba196e4535ca3bf8f835db50d7e02339fdb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Tvrd=C3=ADk?= Date: Fri, 17 Jun 2016 15:13:24 +0200 Subject: [PATCH 2/2] Html: added addHtml() and addText() (#111) --- src/Utils/Html.php | 23 +++++++++++++++++++++++ tests/Utils/Html.children.phpt | 17 ++++++++++------- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/Utils/Html.php b/src/Utils/Html.php index 5d7640282..3d8be98c5 100644 --- a/src/Utils/Html.php +++ b/src/Utils/Html.php @@ -310,11 +310,34 @@ public function getText() * @return self */ public function add($child) + { + return $this->addHtml($child); + } + + + /** + * Adds new element's child. + * @param Html|string Html node or raw HTML string + * @return self + */ + public function addHtml($child) { return $this->insert(NULL, $child); } + /** + * Appends plain-text string to element content. + * @param string plain-text string + * @return self + */ + public function addText($text) + { + $text = htmlspecialchars($text, ENT_NOQUOTES, 'UTF-8'); + return $this->insert(NULL, $text); + } + + /** * Creates and adds a new Html child. * @param string elements's name diff --git a/tests/Utils/Html.children.phpt b/tests/Utils/Html.children.phpt index 39571f4dc..19f3163b5 100644 --- a/tests/Utils/Html.children.phpt +++ b/tests/Utils/Html.children.phpt @@ -14,7 +14,7 @@ require __DIR__ . '/../bootstrap.php'; test(function () { // add $el = Html::el('ul'); $el->create('li')->setText('one'); - $el->add(Html::el('li')->setText('two'))->class('hello'); + $el->addHtml(Html::el('li')->setText('two'))->class('hello'); Assert::same('', (string) $el); @@ -31,15 +31,18 @@ test(function () { // add test(function () { $el = Html::el(NULL); - $el->add(Html::el('p')->setText('one')); - $el->add(Html::el('p')->setText('two')); - Assert::same('

one

two

', (string) $el); + $el->addHtml(Html::el('p')->setText('one')); + $el->addText('

two

'); + $el->addHtml('

three

'); + Assert::same('

one

<p>two</p>

three

', (string) $el); // ==> Get child: - Assert::true(isset($el[1])); - Assert::same('

two

', (string) $el[1]); - Assert::false(isset($el[2])); + Assert::true(isset($el[0])); + Assert::same('

one

', (string) $el[0]); + Assert::same('<p>two</p>', (string) $el[1]); + Assert::same('

three

', (string) $el[2]); + Assert::false(isset($el[3])); });