Merge pull request #136 from php-flasher/test/response

test: add more responses tests
This commit is contained in:
Younes KHOUBZA
2023-02-05 18:13:45 +01:00
committed by GitHub
6 changed files with 371 additions and 2 deletions
+2 -2
View File
@@ -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;
}
@@ -145,6 +145,11 @@ final class ResourceManager implements ResourceManagerInterface
if ('flasher' === $theme) {
/** @var array<string, mixed> $options */
$options = $this->config->get('options', array());
if (isset($this->options[$handler])) {
$options = array_merge($this->options[$handler], $options);
}
$this->addOptions('theme.flasher', $options);
}
@@ -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))));
}
}
@@ -0,0 +1,77 @@
<?php
/*
* This file is part of the PHPFlasher package.
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
*/
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());
}
}
@@ -0,0 +1,135 @@
<?php
/*
* This file is part of the PHPFlasher package.
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
*/
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 = <<<JAVASCRIPT
<script type="text/javascript" class="flasher-js">
(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);
}
})();
</script>
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');
}
}
+126
View File
@@ -0,0 +1,126 @@
<?php
/*
* This file is part of the PHPFlasher package.
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
*/
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());
}
}