diff --git a/src/Prime/Filter/Filter.php b/src/Prime/Filter/Filter.php index 7fe01bfd..f8b15813 100644 --- a/src/Prime/Filter/Filter.php +++ b/src/Prime/Filter/Filter.php @@ -65,7 +65,7 @@ final class Filter $this->applyOrdering(); $this->applyLimit(); - return $this->envelopes; + return array_values($this->envelopes); } /** @@ -134,7 +134,7 @@ final class Filter */ private function applyOrdering() { - if (null === $this->orderings) { + if (array() === $this->orderings) { return; } diff --git a/src/Prime/Response/Resource/ResourceManager.php b/src/Prime/Response/Resource/ResourceManager.php index 99210993..81527628 100644 --- a/src/Prime/Response/Resource/ResourceManager.php +++ b/src/Prime/Response/Resource/ResourceManager.php @@ -145,6 +145,11 @@ final class ResourceManager implements ResourceManagerInterface if ('flasher' === $theme) { /** @var array $options */ $options = $this->config->get('options', array()); + + if (isset($this->options[$handler])) { + $options = array_merge($this->options[$handler], $options); + } + $this->addOptions('theme.flasher', $options); } diff --git a/tests/Prime/Response/Presenter/HtmlPresenterTest.php b/tests/Prime/Response/Presenter/HtmlPresenterTest.php index 247fa95b..422daab2 100644 --- a/tests/Prime/Response/Presenter/HtmlPresenterTest.php +++ b/tests/Prime/Response/Presenter/HtmlPresenterTest.php @@ -110,4 +110,30 @@ JAVASCRIPT; $this->assertEquals($response, $presenter->render(new Response($envelopes, array()))); } + + /** + * @return void + */ + public function testItRenderOnlyEnvelopesAsJsonObject() + { + $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 = '{"envelopes":[{"notification":{"type":"success","message":"success message","title":"PHPFlasher","options":[]}},{"notification":{"type":"warning","message":"warning message","title":"yoeunes\/toastr","options":[]}}]}'; + + $presenter = new HtmlPresenter(); + + $this->assertEquals($response, $presenter->render(new Response($envelopes, array('envelopes_only' => true)))); + } } diff --git a/tests/Prime/Response/Resource/ResourceManagerTest.php b/tests/Prime/Response/Resource/ResourceManagerTest.php new file mode 100644 index 00000000..e4041b26 --- /dev/null +++ b/tests/Prime/Response/Resource/ResourceManagerTest.php @@ -0,0 +1,77 @@ + + */ + +namespace Flasher\Tests\Prime\Response\Resource; + +use Flasher\Prime\Config\Config; +use Flasher\Prime\Notification\Envelope; +use Flasher\Prime\Notification\Notification; +use Flasher\Prime\Response\Resource\ResourceManager; +use Flasher\Prime\Response\Response; +use Flasher\Prime\Stamp\CreatedAtStamp; +use Flasher\Prime\Stamp\HandlerStamp; +use Flasher\Prime\Stamp\UuidStamp; +use Flasher\Tests\Prime\TestCase; + +class ResourceManagerTest extends TestCase +{ + /** + * @return void + */ + public function testItPopulateResponseFromResources() + { + $config = new Config(array( + 'default' => 'flasher', + 'root_script' => 'root_script.min.js', + )); + $resourceManager = new ResourceManager($config); + + $resourceManager->addScripts('flasher', array('flasher.min.js')); + $resourceManager->addStyles('flasher', array('flasher.min.css')); + $resourceManager->addOptions('flasher', array('timeout' => 2500, 'position' => 'center')); + + $resourceManager->addScripts('toastr', array('toastr.min.js', 'jquery.min.js')); + $resourceManager->addStyles('toastr', array('toastr.min.css')); + $resourceManager->addOptions('toastr', array('sounds' => false)); + + $envelopes = array(); + + $notification = new Notification(); + $notification->setMessage('success message'); + $notification->setTitle('PHPFlasher'); + $notification->setType('success'); + $envelopes[] = new Envelope($notification, array( + new HandlerStamp('flasher'), + new CreatedAtStamp(new \DateTime('2023-02-05 16:22:50')), + new UuidStamp('1111'), + )); + + $notification = new Notification(); + $notification->setMessage('warning message'); + $notification->setTitle('yoeunes/toastr'); + $notification->setType('warning'); + $envelopes[] = new Envelope($notification, array( + new HandlerStamp('toastr'), + new CreatedAtStamp(new \DateTime('2023-02-05 16:22:50')), + new UuidStamp('2222'), + )); + + $response = new Response($envelopes, array()); + + $response = $resourceManager->buildResponse($response); + + $this->assertEquals($envelopes, $response->getEnvelopes()); + $this->assertEquals('root_script.min.js', $response->getRootScript()); + $this->assertEquals(array('toastr.min.js', 'jquery.min.js'), $response->getScripts()); + $this->assertEquals(array('toastr.min.css'), $response->getStyles()); + $this->assertEquals(array( + 'theme.flasher' => array('timeout' => 2500, 'position' => 'center'), + 'toastr' => array('sounds' => false), + ), $response->getOptions()); + $this->assertEquals($config, $resourceManager->getConfig()); + } +} diff --git a/tests/Prime/Response/ResponseManagerTest.php b/tests/Prime/Response/ResponseManagerTest.php new file mode 100644 index 00000000..34df7ce4 --- /dev/null +++ b/tests/Prime/Response/ResponseManagerTest.php @@ -0,0 +1,135 @@ + + */ + +namespace Flasher\Tests\Prime\Response; + +use Flasher\Prime\Notification\Envelope; +use Flasher\Prime\Notification\Notification; +use Flasher\Prime\Response\ResponseManager; +use Flasher\Prime\Stamp\CreatedAtStamp; +use Flasher\Prime\Stamp\UuidStamp; +use Flasher\Prime\Storage\StorageManager; +use Flasher\Tests\Prime\TestCase; + +class ResponseManagerTest extends TestCase +{ + /** + * @return void + */ + public function testRenderSavedNotifications() + { + $envelopes = array(); + + $notification = new Notification(); + $notification->setMessage('success message'); + $notification->setTitle('PHPFlasher'); + $notification->setType('success'); + $envelopes[] = new Envelope($notification, array( + new CreatedAtStamp(new \DateTime('2023-02-05 16:22:50')), + new UuidStamp('1111'), + )); + + $notification = new Notification(); + $notification->setMessage('warning message'); + $notification->setTitle('yoeunes/toastr'); + $notification->setType('warning'); + $envelopes[] = new Envelope($notification, array( + new CreatedAtStamp(new \DateTime('2023-02-06 16:22:50')), + new UuidStamp('2222'), + )); + + $storageManager = new StorageManager(); + $storageManager->add($envelopes); + + $responseManager = new ResponseManager(null, $storageManager); + + $response = << +(function() { + var rootScript = ''; + var FLASHER_FLASH_BAG_PLACE_HOLDER = {}; + var options = mergeOptions({"envelopes":[{"notification":{"type":"success","message":"success message","title":"PHPFlasher","options":[]},"created_at":"2023-02-05 16:22:50","uuid":"1111","priority":0},{"notification":{"type":"warning","message":"warning message","title":"yoeunes\/toastr","options":[]},"created_at":"2023-02-06 16:22:50","uuid":"2222","priority":0}]}, 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; + + $this->assertEquals($response, $responseManager->render()); + } + + /** + * @return void + */ + public function testItThrowsExceptionIfPresenterNotFound() + { + $this->setExpectedException('\InvalidArgumentException', 'Presenter [xml] not supported.'); + + $responseManager = new ResponseManager(); + $responseManager->render(array(), 'xml'); + } +} diff --git a/tests/Prime/Response/ResponseTest.php b/tests/Prime/Response/ResponseTest.php new file mode 100644 index 00000000..2bcf17d0 --- /dev/null +++ b/tests/Prime/Response/ResponseTest.php @@ -0,0 +1,126 @@ + + */ + +namespace Flasher\Tests\Prime\Response; + +use Flasher\Prime\Notification\Envelope; +use Flasher\Prime\Notification\Notification; +use Flasher\Prime\Response\Response; +use Flasher\Tests\Prime\TestCase; + +class ResponseTest extends TestCase +{ + /** + * @return void + */ + public function testAddRootScriptToResponse() + { + $response = new Response(array(), array()); + + $response->setRootScript('flasher.min.js'); + + $this->assertEquals('flasher.min.js', $response->getRootScript()); + } + + /** + * @return void + */ + public function testItAddsScriptToResponse() + { + $response = new Response(array(), array()); + + $response->addScripts(array('flasher.min.js', 'toastr.min.js')); + $response->addScripts(array('flasher.min.js', 'noty.min.js')); + + $this->assertEquals(array('flasher.min.js', 'toastr.min.js', 'noty.min.js'), $response->getScripts()); + } + + /** + * @return void + */ + public function testItAddsStylesToResponse() + { + $response = new Response(array(), array()); + + $response->addStyles(array('flasher.min.css', 'toastr.min.css')); + $response->addStyles(array('flasher.min.css', 'noty.min.css')); + + $this->assertEquals(array('flasher.min.css', 'toastr.min.css', 'noty.min.css'), $response->getStyles()); + } + + /** + * @return void + */ + public function testItAddsAdaptersOptionsToResponse() + { + $response = new Response(array(), array()); + + $response->addOptions('flasher', array('position' => 'center', 'timeout' => 2500)); + $response->addOptions('toastr', array('sounds' => false)); + + $this->assertEquals(array( + 'flasher' => array('position' => 'center', 'timeout' => 2500), + 'toastr' => array('sounds' => false), + ), $response->getOptions()); + } + + /** + * @return void + */ + public function testItTurnsTheResponseIntoAnArray() + { + $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 = new Response($envelopes, array()); + $response->setRootScript('flasher.min.js'); + $response->addScripts(array('noty.min.js', 'toastr.min.js')); + $response->addStyles(array('noty.min.css', 'toastr.min.css')); + $response->addOptions('flasher', array('position' => 'center', 'timeout' => 2500)); + $response->addOptions('toastr', array('sounds' => false)); + + $expected = array( + 'envelopes' => array( + array( + 'notification' => array( + 'type' => 'success', + 'title' => 'PHPFlasher', + 'message' => 'success message', + 'options' => array(), + ), + ), + array( + 'notification' => array( + 'type' => 'warning', + 'title' => 'yoeunes/toastr', + 'message' => 'warning message', + 'options' => array(), + ), + ), + ), + 'scripts' => array('noty.min.js', 'toastr.min.js'), + 'styles' => array('noty.min.css', 'toastr.min.css'), + 'options' => array( + 'flasher' => array('position' => 'center', 'timeout' => 2500), + 'toastr' => array('sounds' => false), + ), + ); + + $this->assertEquals($expected, $response->toArray()); + } +}