From ffb839e8e1789b0037d1340498407f050f456a78 Mon Sep 17 00:00:00 2001 From: Khoubza Younes Date: Wed, 1 Feb 2023 23:50:39 +0100 Subject: [PATCH] test: add html presenter tests --- .../Response/Presenter/HtmlPresenterTest.php | 113 ++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 tests/Prime/Response/Presenter/HtmlPresenterTest.php diff --git a/tests/Prime/Response/Presenter/HtmlPresenterTest.php b/tests/Prime/Response/Presenter/HtmlPresenterTest.php new file mode 100644 index 00000000..247fa95b --- /dev/null +++ b/tests/Prime/Response/Presenter/HtmlPresenterTest.php @@ -0,0 +1,113 @@ + + */ + +namespace Flasher\Tests\Prime\Response\Presenter; + +use Flasher\Prime\Notification\Envelope; +use Flasher\Prime\Notification\Notification; +use Flasher\Prime\Response\Presenter\HtmlPresenter; +use Flasher\Prime\Response\Response; +use Flasher\Tests\Prime\TestCase; + +class HtmlPresenterTest extends TestCase +{ + /** + * @return void + */ + public function testArrayPresenter() + { + $envelopes = array(); + + $notification = new Notification(); + $notification->setMessage('success message'); + $notification->setTitle('PHPFlasher'); + $notification->setType('success'); + $envelopes[] = new Envelope($notification); + + $notification = new Notification(); + $notification->setMessage('warning message'); + $notification->setTitle('yoeunes/toastr'); + $notification->setType('warning'); + $envelopes[] = new Envelope($notification); + + $response = << +(function() { + var rootScript = ''; + var FLASHER_FLASH_BAG_PLACE_HOLDER = {}; + var options = mergeOptions({"envelopes":[{"notification":{"type":"success","message":"success message","title":"PHPFlasher","options":[]}},{"notification":{"type":"warning","message":"warning message","title":"yoeunes\/toastr","options":[]}}]}, FLASHER_FLASH_BAG_PLACE_HOLDER); + + function mergeOptions(first, second) { + return { + context: merge(first.context || {}, second.context || {}), + envelopes: merge(first.envelopes || [], second.envelopes || []), + options: merge(first.options || {}, second.options || {}), + scripts: merge(first.scripts || [], second.scripts || []), + styles: merge(first.styles || [], second.styles || []), + }; + } + + function merge(first, second) { + if (Array.isArray(first) && Array.isArray(second)) { + return first.concat(second).filter(function(item, index, array) { + return array.indexOf(item) === index; + }); + } + + return Object.assign({}, first, second); + } + + function renderOptions(options) { + if(!window.hasOwnProperty('flasher')) { + console.error('Flasher is not loaded'); + return; + } + + requestAnimationFrame(function () { + window.flasher.render(options); + }); + } + + function render(options) { + if ('loading' !== document.readyState) { + renderOptions(options); + + return; + } + + document.addEventListener('DOMContentLoaded', function() { + renderOptions(options); + }); + } + + if (1 === document.querySelectorAll('script.flasher-js').length) { + document.addEventListener('flasher:render', function (event) { + render(event.detail); + }); + } + + if (window.hasOwnProperty('flasher') || !rootScript || document.querySelector('script[src="' + rootScript + '"]')) { + render(options); + } else { + var tag = document.createElement('script'); + tag.setAttribute('src', rootScript); + tag.setAttribute('type', 'text/javascript'); + tag.onload = function () { + render(options); + }; + + document.head.appendChild(tag); + } +})(); + +JAVASCRIPT; + + $presenter = new HtmlPresenter(); + + $this->assertEquals($response, $presenter->render(new Response($envelopes, array()))); + } +}