mirror of
https://github.com/php-flasher/php-flasher.git
synced 2026-03-31 15:07:47 +01:00
chore: v2 full rewrite
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Laravel\Command;
|
||||
|
||||
use Flasher\Tests\Laravel\TestCase;
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
|
||||
final class InstallCommandTest extends TestCase
|
||||
{
|
||||
public function testExecute(): void
|
||||
{
|
||||
Artisan::call('flasher:install');
|
||||
|
||||
$output = Artisan::output();
|
||||
|
||||
$this->assertStringContainsString('PHPFlasher resources have been successfully installed.', $output);
|
||||
}
|
||||
|
||||
public function testExecuteWithConfigOption(): void
|
||||
{
|
||||
Artisan::call('flasher:install', ['--config' => true]);
|
||||
|
||||
$output = Artisan::output();
|
||||
|
||||
$this->assertStringContainsString('Configuration files have been published.', $output);
|
||||
}
|
||||
|
||||
public function testExecuteWithSymlinkOption(): void
|
||||
{
|
||||
Artisan::call('flasher:install', ['--symlink' => true]);
|
||||
|
||||
$output = Artisan::output();
|
||||
|
||||
$this->assertStringContainsString('Assets were symlinked.', $output);
|
||||
}
|
||||
|
||||
public function testExecuteWithAllOptions(): void
|
||||
{
|
||||
Artisan::call('flasher:install', ['--config' => true, '--symlink' => true]);
|
||||
|
||||
$output = Artisan::output();
|
||||
|
||||
$this->assertStringContainsString('PHPFlasher resources have been successfully installed.', $output);
|
||||
$this->assertStringContainsString('Configuration files have been published.', $output);
|
||||
$this->assertStringContainsString('Assets were symlinked.', $output);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Laravel\Component;
|
||||
|
||||
use Flasher\Laravel\Component\FlasherComponent;
|
||||
use Flasher\Prime\FlasherInterface;
|
||||
use Illuminate\Container\Container;
|
||||
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* FlasherComponentTest tests the render method in the FlasherComponent class.
|
||||
*/
|
||||
final class FlasherComponentTest extends TestCase
|
||||
{
|
||||
use MockeryPHPUnitIntegration;
|
||||
|
||||
private FlasherComponent $flasherComponent;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$flasherServiceMock = \Mockery::mock(FlasherInterface::class);
|
||||
$flasherServiceMock->allows('render')
|
||||
->andReturns('Your expected result');
|
||||
|
||||
Container::getInstance()->instance('flasher', $flasherServiceMock);
|
||||
|
||||
$this->flasherComponent = new FlasherComponent('{"key":"value"}', '{"key":"value"}');
|
||||
}
|
||||
|
||||
public function testRender(): void
|
||||
{
|
||||
$expectedResult = 'Your expected result';
|
||||
$actualResult = $this->flasherComponent->render();
|
||||
$this->assertSame($expectedResult, $actualResult);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Laravel\Http;
|
||||
|
||||
use Flasher\Laravel\Http\Request;
|
||||
use Illuminate\Http\Request as LaravelRequest;
|
||||
use Illuminate\Session\Store as SessionStore;
|
||||
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
|
||||
use Mockery\MockInterface;
|
||||
use Orchestra\Testbench\TestCase;
|
||||
|
||||
final class RequestTest extends TestCase
|
||||
{
|
||||
use MockeryPHPUnitIntegration;
|
||||
|
||||
private MockInterface&LaravelRequest $laravelRequestMock;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->laravelRequestMock = \Mockery::mock(LaravelRequest::class);
|
||||
}
|
||||
|
||||
public function testIsXmlHttpRequest(): void
|
||||
{
|
||||
$this->laravelRequestMock->expects('ajax')->andReturns(true);
|
||||
|
||||
$request = new Request($this->laravelRequestMock);
|
||||
|
||||
$this->assertTrue($request->isXmlHttpRequest());
|
||||
}
|
||||
|
||||
public function testIsHtmlRequestFormat(): void
|
||||
{
|
||||
$this->laravelRequestMock->expects('acceptsHtml')->andReturns(true);
|
||||
|
||||
$request = new Request($this->laravelRequestMock);
|
||||
|
||||
$this->assertTrue($request->isHtmlRequestFormat());
|
||||
}
|
||||
|
||||
public function testHasSession(): void
|
||||
{
|
||||
$this->laravelRequestMock->expects('hasSession')->andReturns(true);
|
||||
|
||||
$request = new Request($this->laravelRequestMock);
|
||||
|
||||
$this->assertTrue($request->hasSession());
|
||||
}
|
||||
|
||||
public function testIsSessionStarted(): void
|
||||
{
|
||||
$sessionMock = \Mockery::mock(SessionStore::class);
|
||||
$sessionMock->expects('isStarted')->andReturns(true);
|
||||
|
||||
$this->laravelRequestMock->expects('session')->andReturns($sessionMock);
|
||||
|
||||
$request = new Request($this->laravelRequestMock);
|
||||
|
||||
$this->assertTrue($request->isSessionStarted());
|
||||
}
|
||||
|
||||
public function testHasType(): void
|
||||
{
|
||||
$sessionMock = \Mockery::mock(SessionStore::class);
|
||||
$sessionMock->expects('has')->with('type')->andReturns(true);
|
||||
$sessionMock->expects('isStarted')->andReturns(true);
|
||||
|
||||
$this->laravelRequestMock->expects('session')->twice()->andReturns($sessionMock);
|
||||
$this->laravelRequestMock->expects('hasSession')->andReturn(true);
|
||||
|
||||
$request = new Request($this->laravelRequestMock);
|
||||
|
||||
$this->assertTrue($request->hasType('type'));
|
||||
}
|
||||
|
||||
public function testGetType(): void
|
||||
{
|
||||
$expectedValue = 'value';
|
||||
$sessionMock = \Mockery::mock(SessionStore::class);
|
||||
$sessionMock->expects('get')->with('type')->andReturns($expectedValue);
|
||||
|
||||
$this->laravelRequestMock->expects('session')->andReturns($sessionMock);
|
||||
|
||||
$request = new Request($this->laravelRequestMock);
|
||||
|
||||
$this->assertSame($expectedValue, $request->getType('type'));
|
||||
}
|
||||
|
||||
public function testForgetType(): void
|
||||
{
|
||||
$sessionMock = \Mockery::mock(SessionStore::class);
|
||||
$sessionMock->expects('forget')->with('type');
|
||||
|
||||
$this->laravelRequestMock->expects('session')->andReturns($sessionMock);
|
||||
|
||||
$request = new Request($this->laravelRequestMock);
|
||||
|
||||
$request->forgetType('type');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,168 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Laravel\Http;
|
||||
|
||||
use Flasher\Laravel\Http\Response;
|
||||
use Illuminate\Http\JsonResponse as LaravelJsonResponse;
|
||||
use Illuminate\Http\Response as LaravelResponse;
|
||||
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
|
||||
use Mockery\MockInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
|
||||
|
||||
final class ResponseTest extends TestCase
|
||||
{
|
||||
use MockeryPHPUnitIntegration;
|
||||
|
||||
private MockInterface&ResponseHeaderBag $responseHeaderBagMock;
|
||||
private MockInterface&LaravelResponse $responseMock;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->responseHeaderBagMock = \Mockery::mock(ResponseHeaderBag::class);
|
||||
|
||||
$this->responseMock = \Mockery::mock(LaravelResponse::class);
|
||||
$this->responseMock->headers = $this->responseHeaderBagMock;
|
||||
}
|
||||
|
||||
public function testIsRedirection(): void
|
||||
{
|
||||
$this->responseMock->expects()->isRedirection()->andReturns(true);
|
||||
|
||||
$response = new Response($this->responseMock);
|
||||
|
||||
$this->assertTrue($response->isRedirection());
|
||||
}
|
||||
|
||||
public function testIsJson(): void
|
||||
{
|
||||
$jsonResponseMock = \Mockery::mock(LaravelJsonResponse::class);
|
||||
|
||||
$response = new Response($jsonResponseMock);
|
||||
|
||||
$this->assertTrue($response->isJson());
|
||||
}
|
||||
|
||||
public function testIsHtml(): void
|
||||
{
|
||||
$this->responseHeaderBagMock->expects()->get('Content-Type')->andReturns('text/html; charset=UTF-8');
|
||||
|
||||
$response = new Response($this->responseMock);
|
||||
|
||||
$this->assertTrue($response->isHtml());
|
||||
}
|
||||
|
||||
public function testIsAttachment(): void
|
||||
{
|
||||
$this->responseHeaderBagMock->expects()->get('Content-Disposition', '')->andReturns('attachment; filename="filename.jpg"');
|
||||
|
||||
$response = new Response($this->responseMock);
|
||||
|
||||
$this->assertTrue($response->isAttachment());
|
||||
}
|
||||
|
||||
public function testIsSuccessful(): void
|
||||
{
|
||||
$this->responseMock->expects()->isSuccessful()->andReturns(true);
|
||||
|
||||
$response = new Response($this->responseMock);
|
||||
|
||||
$this->assertTrue($response->isSuccessful());
|
||||
}
|
||||
|
||||
public function testGetContent(): void
|
||||
{
|
||||
$expectedContent = 'response content';
|
||||
$this->responseMock->expects()->getContent()->andReturns($expectedContent);
|
||||
|
||||
$response = new Response($this->responseMock);
|
||||
|
||||
$this->assertSame($expectedContent, $response->getContent());
|
||||
}
|
||||
|
||||
public function testSetContentOnLaravelResponseWithOriginalContent(): void
|
||||
{
|
||||
$originalContent = '<h1>Original</h1>';
|
||||
$newContent = 'New content';
|
||||
|
||||
$laravelResponse = \Mockery::mock(LaravelResponse::class);
|
||||
$laravelResponse->allows('getOriginalContent')->andReturns($originalContent);
|
||||
$laravelResponse->expects('setContent')->with($newContent);
|
||||
$laravelResponse->original = null; // Simulate initial state
|
||||
|
||||
$response = new Response($laravelResponse);
|
||||
$response->setContent($newContent);
|
||||
|
||||
$this->assertSame($originalContent, $laravelResponse->original);
|
||||
}
|
||||
|
||||
public function testSetContentOnLaravelResponseWithoutOriginalContent(): void
|
||||
{
|
||||
$newContent = 'New content';
|
||||
|
||||
$laravelResponse = \Mockery::mock(LaravelResponse::class);
|
||||
$laravelResponse->allows('getOriginalContent')->andReturnNull();
|
||||
$laravelResponse->expects('setContent')->with($newContent);
|
||||
$laravelResponse->original = null; // Simulate initial state
|
||||
|
||||
$response = new Response($laravelResponse);
|
||||
$response->setContent($newContent);
|
||||
|
||||
$this->assertNull($laravelResponse->original);
|
||||
}
|
||||
|
||||
public function testSetContentOnJsonResponse(): void
|
||||
{
|
||||
$newContent = '{"message":"New content"}';
|
||||
|
||||
$laravelJsonResponse = \Mockery::mock(LaravelJsonResponse::class);
|
||||
$laravelJsonResponse->expects('setContent')->with($newContent);
|
||||
// JsonResponses don't usually handle `original` content, but let's ensure no side effects
|
||||
$laravelJsonResponse->original = null;
|
||||
|
||||
$response = new Response($laravelJsonResponse);
|
||||
$response->setContent($newContent);
|
||||
}
|
||||
|
||||
public function testHasHeader(): void
|
||||
{
|
||||
$headerKey = 'X-Custom-Header';
|
||||
$this->responseHeaderBagMock->expects()->has($headerKey)->andReturns(true);
|
||||
|
||||
$response = new Response($this->responseMock);
|
||||
|
||||
$this->assertTrue($response->hasHeader($headerKey));
|
||||
}
|
||||
|
||||
public function testGetHeader(): void
|
||||
{
|
||||
$headerKey = 'X-Custom-Header';
|
||||
$headerValue = 'Value';
|
||||
$this->responseHeaderBagMock->expects()->get($headerKey)->andReturns($headerValue);
|
||||
|
||||
$response = new Response($this->responseMock);
|
||||
|
||||
$this->assertSame($headerValue, $response->getHeader($headerKey));
|
||||
}
|
||||
|
||||
public function testSetHeader(): void
|
||||
{
|
||||
$headerKey = 'X-Custom-Header';
|
||||
$headerValue = 'Value';
|
||||
$this->responseHeaderBagMock->expects()->set($headerKey, $headerValue);
|
||||
|
||||
$response = new Response($this->responseMock);
|
||||
$response->setHeader($headerKey, $headerValue);
|
||||
}
|
||||
|
||||
public function testRemoveHeader(): void
|
||||
{
|
||||
$headerKey = 'X-Custom-Header';
|
||||
$this->responseHeaderBagMock->expects()->remove($headerKey);
|
||||
|
||||
$response = new Response($this->responseMock);
|
||||
$response->removeHeader($headerKey);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Laravel\Middleware;
|
||||
|
||||
use Flasher\Laravel\Http\Request;
|
||||
use Flasher\Laravel\Http\Response;
|
||||
use Flasher\Laravel\Middleware\FlasherMiddleware;
|
||||
use Flasher\Prime\Http\ResponseExtensionInterface;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request as LaravelRequest;
|
||||
use Illuminate\Http\Response as LaravelResponse;
|
||||
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
|
||||
use Mockery\MockInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class FlasherMiddlewareTest extends TestCase
|
||||
{
|
||||
use MockeryPHPUnitIntegration;
|
||||
|
||||
private FlasherMiddleware $middleware;
|
||||
private MockInterface&ResponseExtensionInterface $responseExtensionMock;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->responseExtensionMock = \Mockery::mock(ResponseExtensionInterface::class);
|
||||
$this->middleware = new FlasherMiddleware($this->responseExtensionMock);
|
||||
}
|
||||
|
||||
public function testHandle(): void
|
||||
{
|
||||
$laravelRequest = \Mockery::mock(LaravelRequest::class);
|
||||
|
||||
$this->responseExtensionMock
|
||||
->expects('render')
|
||||
->with(\Mockery::type(Request::class), \Mockery::type(Response::class))
|
||||
->andReturnUsing(function (Request $request, Response $response): Response {
|
||||
$response->setContent('Modified content');
|
||||
|
||||
return $response;
|
||||
});
|
||||
|
||||
/** @var LaravelResponse $response */
|
||||
$response = $this->middleware->handle($laravelRequest, fn ($r) => new LaravelResponse());
|
||||
$this->assertSame('Modified content', $response->getContent());
|
||||
}
|
||||
|
||||
public function testHandleJsonResponse(): void
|
||||
{
|
||||
$laravelRequest = \Mockery::mock(LaravelRequest::class);
|
||||
|
||||
$this->responseExtensionMock
|
||||
->expects('render')
|
||||
->with(\Mockery::type(Request::class), \Mockery::type(Response::class))
|
||||
->andReturnUsing(function (Request $request, Response $response): Response {
|
||||
$response->setContent(json_encode(['foo' => 'modified bar'], \JSON_THROW_ON_ERROR));
|
||||
|
||||
return $response;
|
||||
});
|
||||
|
||||
/** @var LaravelResponse $response */
|
||||
$response = $this->middleware->handle(
|
||||
$laravelRequest,
|
||||
fn ($r) => new JsonResponse(['foo' => 'bar'])
|
||||
);
|
||||
|
||||
$this->assertSame('{"foo":"modified bar"}', $response->getContent());
|
||||
}
|
||||
|
||||
public function testHandleWithNonLaravelResponse(): void
|
||||
{
|
||||
$laravelRequest = \Mockery::mock(LaravelRequest::class);
|
||||
|
||||
$this->responseExtensionMock->allows('render')->never();
|
||||
|
||||
$response = $this->middleware->handle($laravelRequest, fn ($r) => 'Not a Laravel Response');
|
||||
$this->assertSame('Not a Laravel Response', $response);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Laravel\Middleware;
|
||||
|
||||
use Flasher\Laravel\Http\Request;
|
||||
use Flasher\Laravel\Http\Response;
|
||||
use Flasher\Laravel\Middleware\SessionMiddleware;
|
||||
use Flasher\Prime\Http\RequestExtensionInterface;
|
||||
use Illuminate\Http\JsonResponse as LaravelJsonResponse;
|
||||
use Illuminate\Http\Request as LaravelRequest;
|
||||
use Illuminate\Http\Response as LaravelResponse;
|
||||
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
|
||||
use Mockery\MockInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* This TestCase is designed to test handle method of SessionMiddleware
|
||||
* which wraps the given request into a Flasher Request and Flasher Response,
|
||||
* and attaches them into Extension.
|
||||
*/
|
||||
final class SessionMiddlewareTest extends TestCase
|
||||
{
|
||||
use MockeryPHPUnitIntegration;
|
||||
|
||||
private MockInterface&RequestExtensionInterface $requestExtensionMock;
|
||||
|
||||
private SessionMiddleware $sessionMiddleware;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->requestExtensionMock = \Mockery::mock(RequestExtensionInterface::class);
|
||||
$this->sessionMiddleware = new SessionMiddleware($this->requestExtensionMock);
|
||||
}
|
||||
|
||||
public function testHandleWithLaravelResponse(): void
|
||||
{
|
||||
$requestMock = \Mockery::mock(LaravelRequest::class);
|
||||
$responseMock = \Mockery::mock(LaravelResponse::class);
|
||||
|
||||
$this->requestExtensionMock->expects('flash')
|
||||
->withArgs(function ($flasherRequest, $flasherResponse) {
|
||||
return $flasherRequest instanceof Request && $flasherResponse instanceof Response;
|
||||
});
|
||||
|
||||
$handle = $this->sessionMiddleware->handle($requestMock, fn () => $responseMock);
|
||||
|
||||
$this->assertSame($responseMock, $handle);
|
||||
}
|
||||
|
||||
public function testHandleWithLaravelJsonResponse(): void
|
||||
{
|
||||
$requestMock = \Mockery::mock(LaravelRequest::class);
|
||||
$responseMock = \Mockery::mock(LaravelJsonResponse::class);
|
||||
|
||||
$this->requestExtensionMock->expects('flash')
|
||||
->withArgs(function ($flasherRequest, $flasherResponse) {
|
||||
return $flasherRequest instanceof Request && $flasherResponse instanceof Response;
|
||||
});
|
||||
|
||||
$handle = $this->sessionMiddleware->handle($requestMock, fn () => $responseMock);
|
||||
|
||||
$this->assertSame($responseMock, $handle);
|
||||
}
|
||||
|
||||
public function testHandleWithOtherResponses(): void
|
||||
{
|
||||
$requestMock = \Mockery::mock(LaravelRequest::class);
|
||||
$responseMock = \Mockery::mock(\Symfony\Component\HttpFoundation\Response::class);
|
||||
|
||||
$this->requestExtensionMock->allows('flash')->never();
|
||||
|
||||
$handle = $this->sessionMiddleware->handle($requestMock, fn () => $responseMock);
|
||||
|
||||
$this->assertSame($responseMock, $handle);
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,6 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the PHPFlasher package.
|
||||
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Laravel;
|
||||
|
||||
@@ -11,47 +8,36 @@ use Flasher\Prime\FlasherInterface;
|
||||
|
||||
final class ServiceProviderTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testContainerContainServices()
|
||||
public function testContainerContainServices(): void
|
||||
{
|
||||
$this->assertTrue($this->app->bound('flasher'));
|
||||
$this->assertTrue($this->app->bound('flasher.noty'));
|
||||
$this->assertTrue($this->app->bound('flasher.notyf'));
|
||||
$this->assertTrue($this->app->bound('flasher.pnotify'));
|
||||
$this->assertTrue($this->app->bound('flasher.sweetalert'));
|
||||
$this->assertTrue($this->app->bound('flasher.toastr'));
|
||||
|
||||
$this->assertInstanceOf('Flasher\Prime\Flasher', $this->app->make('flasher'));
|
||||
$this->assertInstanceOf('Flasher\Noty\Prime\NotyFactory', $this->app->make('flasher.noty'));
|
||||
$this->assertInstanceOf('Flasher\Notyf\Prime\NotyfFactory', $this->app->make('flasher.notyf'));
|
||||
$this->assertInstanceOf('Flasher\Pnotify\Prime\PnotifyFactory', $this->app->make('flasher.pnotify'));
|
||||
$this->assertInstanceOf('Flasher\SweetAlert\Prime\SweetAlertFactory', $this->app->make('flasher.sweetalert'));
|
||||
$this->assertInstanceOf('Flasher\Toastr\Prime\ToastrFactory', $this->app->make('flasher.toastr'));
|
||||
$this->assertInstanceOf(\Flasher\Prime\Flasher::class, $this->app->make('flasher'));
|
||||
$this->assertInstanceOf(\Flasher\Noty\Prime\Noty::class, $this->app->make('flasher.noty'));
|
||||
$this->assertInstanceOf(\Flasher\Notyf\Prime\Notyf::class, $this->app->make('flasher.notyf'));
|
||||
$this->assertInstanceOf(\Flasher\SweetAlert\Prime\SweetAlert::class, $this->app->make('flasher.sweetalert'));
|
||||
$this->assertInstanceOf(\Flasher\Toastr\Prime\Toastr::class, $this->app->make('flasher.toastr'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testFlasherCanCreateServicesFromAlias()
|
||||
public function testFlasherCanCreateServicesFromAlias(): void
|
||||
{
|
||||
/** @var FlasherInterface $flasher */
|
||||
$flasher = $this->app->make('flasher');
|
||||
|
||||
$adapter = $flasher->create('noty');
|
||||
$this->assertInstanceOf('Flasher\Noty\Prime\NotyFactory', $adapter);
|
||||
$adapter = $flasher->use('noty');
|
||||
$this->assertInstanceOf(\Flasher\Noty\Prime\Noty::class, $adapter);
|
||||
|
||||
$adapter = $flasher->create('notyf');
|
||||
$this->assertInstanceOf('Flasher\Notyf\Prime\NotyfFactory', $adapter);
|
||||
$adapter = $flasher->use('notyf');
|
||||
$this->assertInstanceOf(\Flasher\Notyf\Prime\Notyf::class, $adapter);
|
||||
|
||||
$adapter = $flasher->create('pnotify');
|
||||
$this->assertInstanceOf('Flasher\Pnotify\Prime\PnotifyFactory', $adapter);
|
||||
$adapter = $flasher->use('sweetalert');
|
||||
$this->assertInstanceOf(\Flasher\SweetAlert\Prime\SweetAlert::class, $adapter);
|
||||
|
||||
$adapter = $flasher->create('sweetalert');
|
||||
$this->assertInstanceOf('Flasher\SweetAlert\Prime\SweetAlertFactory', $adapter);
|
||||
|
||||
$adapter = $flasher->create('toastr');
|
||||
$this->assertInstanceOf('Flasher\Toastr\Prime\ToastrFactory', $adapter);
|
||||
$adapter = $flasher->use('toastr');
|
||||
$this->assertInstanceOf(\Flasher\Toastr\Prime\Toastr::class, $adapter);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Laravel\Storage;
|
||||
|
||||
use Flasher\Laravel\Storage\SessionBag;
|
||||
use Flasher\Prime\Notification\Envelope;
|
||||
use Flasher\Prime\Notification\Notification;
|
||||
use Flasher\Prime\Stamp\IdStamp;
|
||||
use Flasher\Tests\Laravel\TestCase;
|
||||
use Illuminate\Session\SessionManager;
|
||||
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
|
||||
use Mockery\MockInterface;
|
||||
|
||||
final class SessionBagTest extends TestCase
|
||||
{
|
||||
use MockeryPHPUnitIntegration;
|
||||
|
||||
private MockInterface&SessionManager $sessionManagerMock;
|
||||
|
||||
private SessionBag $sessionBag;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->sessionManagerMock = \Mockery::mock(SessionManager::class);
|
||||
$this->sessionBag = new SessionBag($this->sessionManagerMock);
|
||||
}
|
||||
|
||||
public function testGet(): void
|
||||
{
|
||||
$envelopes = [
|
||||
new Envelope(new Notification(), new IdStamp('1111')),
|
||||
new Envelope(new Notification(), new IdStamp('2222')),
|
||||
];
|
||||
|
||||
$this->sessionManagerMock->expects()
|
||||
->get(SessionBag::ENVELOPES_NAMESPACE, [])
|
||||
->andReturns($envelopes);
|
||||
|
||||
$this->assertEquals($envelopes, $this->sessionBag->get());
|
||||
}
|
||||
|
||||
public function testSet(): void
|
||||
{
|
||||
$envelopes = [
|
||||
new Envelope(new Notification(), new IdStamp('1111')),
|
||||
new Envelope(new Notification(), new IdStamp('2222')),
|
||||
];
|
||||
|
||||
$this->sessionManagerMock->allows()
|
||||
->get(SessionBag::ENVELOPES_NAMESPACE, [])
|
||||
->andReturns($envelopes);
|
||||
|
||||
$this->sessionManagerMock->expects()
|
||||
->put(SessionBag::ENVELOPES_NAMESPACE, $envelopes);
|
||||
|
||||
$this->sessionBag->set($envelopes);
|
||||
|
||||
$this->assertSame($envelopes, $this->sessionBag->get());
|
||||
}
|
||||
}
|
||||
@@ -1,159 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the PHPFlasher package.
|
||||
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
|
||||
*/
|
||||
|
||||
namespace Flasher\Tests\Laravel;
|
||||
|
||||
use Flasher\Laravel\Storage\SessionBag;
|
||||
use Flasher\Prime\Notification\Envelope;
|
||||
use Flasher\Prime\Notification\Notification;
|
||||
use Flasher\Prime\Stamp\PriorityStamp;
|
||||
use Flasher\Prime\Stamp\UuidStamp;
|
||||
use Flasher\Prime\Storage\StorageBag;
|
||||
|
||||
final class StorageTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testInitialState()
|
||||
{
|
||||
$storage = $this->getStorage();
|
||||
$this->assertEquals(array(), $storage->all());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testAddEnvelope()
|
||||
{
|
||||
$storage = $this->getStorage();
|
||||
$envelope = new Envelope(new Notification());
|
||||
$storage->add($envelope);
|
||||
|
||||
$this->assertEquals(array($envelope), $storage->all());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testAddMultipleEnvelopes()
|
||||
{
|
||||
$storage = $this->getStorage();
|
||||
$envelopes = array(
|
||||
new Envelope(new Notification()),
|
||||
new Envelope(new Notification()),
|
||||
);
|
||||
|
||||
$storage->add($envelopes);
|
||||
$this->assertEquals($envelopes, $storage->all());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testUpdateEnvelopes()
|
||||
{
|
||||
$storage = $this->getStorage();
|
||||
$envelopes = array(
|
||||
new Envelope(new Notification(), array(
|
||||
new UuidStamp(),
|
||||
)),
|
||||
new Envelope(new Notification(), array(
|
||||
new UuidStamp(),
|
||||
)),
|
||||
);
|
||||
|
||||
$storage->add($envelopes);
|
||||
$this->assertEquals($envelopes, $storage->all());
|
||||
|
||||
$envelopes[1]->withStamp(new PriorityStamp(1));
|
||||
$storage->update($envelopes[1]);
|
||||
|
||||
$this->assertEquals($envelopes, $storage->all());
|
||||
$this->assertInstanceOf(
|
||||
'Flasher\Prime\Stamp\PriorityStamp',
|
||||
$envelopes[1]->get('Flasher\Prime\Stamp\PriorityStamp')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testRemoveEnvelopes()
|
||||
{
|
||||
$storage = $this->getStorage();
|
||||
$envelopes = array(
|
||||
new Envelope(new Notification(), array(
|
||||
new UuidStamp(),
|
||||
)),
|
||||
new Envelope(new Notification(), array(
|
||||
new UuidStamp(),
|
||||
)),
|
||||
);
|
||||
|
||||
$storage->add($envelopes);
|
||||
$this->assertEquals($envelopes, $storage->all());
|
||||
|
||||
$storage->remove($envelopes[1]);
|
||||
$this->assertEquals(array($envelopes[0]), $storage->all());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testRemoveMultipleEnvelopes()
|
||||
{
|
||||
$storage = $this->getStorage();
|
||||
$envelopes = array(
|
||||
new Envelope(new Notification(), array(
|
||||
new UuidStamp(),
|
||||
)),
|
||||
new Envelope(new Notification(), array(
|
||||
new UuidStamp(),
|
||||
)),
|
||||
);
|
||||
|
||||
$storage->add($envelopes);
|
||||
$this->assertEquals($envelopes, $storage->all());
|
||||
|
||||
$storage->remove($envelopes);
|
||||
$this->assertEquals(array(), $storage->all());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testClearAllEnvelopes()
|
||||
{
|
||||
$storage = $this->getStorage();
|
||||
$envelopes = array(
|
||||
new Envelope(new Notification(), array(
|
||||
new UuidStamp(),
|
||||
)),
|
||||
new Envelope(new Notification(), array(
|
||||
new UuidStamp(),
|
||||
)),
|
||||
);
|
||||
|
||||
$storage->add($envelopes);
|
||||
$this->assertEquals($envelopes, $storage->all());
|
||||
|
||||
$storage->clear();
|
||||
$this->assertEquals(array(), $storage->all());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return StorageBag
|
||||
*/
|
||||
private function getStorage()
|
||||
{
|
||||
/** @var \Illuminate\Session\Store $session */
|
||||
$session = $this->app->make('session');
|
||||
|
||||
return new StorageBag(new SessionBag($session));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Laravel\Support;
|
||||
|
||||
use Flasher\Laravel\EventListener\LivewireListener;
|
||||
use Flasher\Prime\FlasherInterface;
|
||||
use Flasher\Prime\Http\Csp\ContentSecurityPolicyHandlerInterface;
|
||||
use Flasher\Prime\Notification\Envelope;
|
||||
use Flasher\Prime\Notification\Notification;
|
||||
use Flasher\Prime\Stamp\IdStamp;
|
||||
use Illuminate\Http\Request as LaravelRequest;
|
||||
use Livewire\Component;
|
||||
use Livewire\LivewireManager;
|
||||
use Livewire\Mechanisms\HandleComponents\ComponentContext;
|
||||
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class LivewireListenerTest extends TestCase
|
||||
{
|
||||
use MockeryPHPUnitIntegration;
|
||||
|
||||
public function testInvocationWithConditionsToSkip(): void
|
||||
{
|
||||
$flasherMock = \Mockery::mock(FlasherInterface::class);
|
||||
$livewireManagerMock = \Mockery::mock(LivewireManager::class);
|
||||
$cspHandlerMock = \Mockery::mock(ContentSecurityPolicyHandlerInterface::class);
|
||||
$requestMock = \Mockery::mock(LaravelRequest::class);
|
||||
|
||||
$livewireListener = new LivewireListener($livewireManagerMock, $flasherMock, $cspHandlerMock, fn () => $requestMock);
|
||||
|
||||
$componentMock = \Mockery::mock(Component::class);
|
||||
$contextMock = \Mockery::mock(ComponentContext::class);
|
||||
|
||||
$livewireManagerMock->expects('isLivewireRequest')->andReturns(false);
|
||||
$flasherMock->expects('render')->never();
|
||||
|
||||
$livewireListener->__invoke($componentMock, $contextMock);
|
||||
}
|
||||
|
||||
public function testInvokeMethodDispatchNotifications(): void
|
||||
{
|
||||
$flasherMock = \Mockery::mock(FlasherInterface::class);
|
||||
$livewireManagerMock = \Mockery::mock(LivewireManager::class);
|
||||
$cspHandlerMock = \Mockery::mock(ContentSecurityPolicyHandlerInterface::class);
|
||||
$requestMock = \Mockery::mock(LaravelRequest::class);
|
||||
|
||||
$livewireListener = new LivewireListener($livewireManagerMock, $flasherMock, $cspHandlerMock, fn () => $requestMock);
|
||||
|
||||
$componentMock = \Mockery::mock(Component::class);
|
||||
$contextMock = \Mockery::mock(ComponentContext::class);
|
||||
|
||||
$livewireManagerMock->expects('isLivewireRequest')->andReturns(true);
|
||||
$cspHandlerMock->expects('getNonces')->andReturns(['csp_script_nonce' => null, 'csp_style_nonce' => null]);
|
||||
|
||||
$componentMock->expects('getId')->andReturns('1');
|
||||
$componentMock->expects('getName')->andReturns('MyComponent');
|
||||
$contextMock->expects('addEffect');
|
||||
|
||||
$flasherMock
|
||||
->expects('render')
|
||||
->andReturns(['envelopes' => [new Envelope(new Notification(), new IdStamp('1111'))]]);
|
||||
|
||||
$livewireListener->__invoke($componentMock, $contextMock);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Laravel\Template;
|
||||
|
||||
use Flasher\Laravel\Template\BladeTemplateEngine;
|
||||
use Illuminate\View\Factory;
|
||||
use Illuminate\View\View;
|
||||
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class BladeTemplateEngineTest extends TestCase
|
||||
{
|
||||
use MockeryPHPUnitIntegration;
|
||||
|
||||
public function testRender(): void
|
||||
{
|
||||
$name = 'test';
|
||||
$context = ['key' => 'value'];
|
||||
|
||||
$view = \Mockery::mock(View::class);
|
||||
$view->allows('render')->andReturns('rendered data');
|
||||
|
||||
$blade = \Mockery::mock(Factory::class);
|
||||
$blade->allows('make')->with($name, $context)->andReturns($view);
|
||||
|
||||
$bladeTemplateEngine = new BladeTemplateEngine($blade);
|
||||
|
||||
$result = $bladeTemplateEngine->render($name, $context);
|
||||
|
||||
$this->assertIsString($result);
|
||||
$this->assertSame('rendered data', $result);
|
||||
}
|
||||
}
|
||||
+29
-73
@@ -1,93 +1,49 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the PHPFlasher package.
|
||||
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Laravel;
|
||||
|
||||
use Flasher\Laravel\Support\Laravel;
|
||||
use Illuminate\Config\Repository as Config;
|
||||
use Illuminate\Foundation\AliasLoader;
|
||||
use Illuminate\Foundation\Application;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Config\Repository;
|
||||
use Illuminate\Support\Facades\Facade;
|
||||
use Orchestra\Testbench\TestCase as Orchestra;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class TestCase extends Orchestra
|
||||
class TestCase extends \Orchestra\Testbench\TestCase
|
||||
{
|
||||
public function createApplication()
|
||||
/**
|
||||
* @return array<class-string<ServiceProvider>>
|
||||
*/
|
||||
protected function getPackageProviders($app): array
|
||||
{
|
||||
if (0 !== strpos(Application::VERSION, '4.0')) {
|
||||
return parent::createApplication();
|
||||
}
|
||||
|
||||
$app = new Application();
|
||||
|
||||
$app->detectEnvironment(array(
|
||||
'local' => array('your-machine-name'),
|
||||
));
|
||||
|
||||
$app->bindInstallPaths($this->getApplicationPaths());
|
||||
|
||||
$app['env'] = 'testing';
|
||||
|
||||
$app->instance('app', $app);
|
||||
|
||||
Facade::clearResolvedInstances();
|
||||
Facade::setFacadeApplication($app);
|
||||
|
||||
$config = new Config($app->getConfigLoader(), $app['env']);
|
||||
$app->instance('config', $config);
|
||||
$app->startExceptionHandling();
|
||||
|
||||
if ($app->runningInConsole()) {
|
||||
$app->setRequestForConsoleEnvironment();
|
||||
}
|
||||
|
||||
date_default_timezone_set($this->getApplicationTimezone());
|
||||
|
||||
$aliases = array_merge($this->getApplicationAliases(), $this->getPackageAliases());
|
||||
AliasLoader::getInstance($aliases)->register();
|
||||
|
||||
Request::enableHttpMethodParameterOverride();
|
||||
|
||||
$providers = array_merge($this->getApplicationProviders(), $this->getPackageProviders());
|
||||
$app->getProviderRepository()->load($app, $providers);
|
||||
|
||||
$this->getEnvironmentSetUp($app);
|
||||
|
||||
$app->boot();
|
||||
|
||||
return $app;
|
||||
return [
|
||||
\Flasher\Laravel\FlasherServiceProvider::class,
|
||||
\Flasher\Noty\Laravel\FlasherNotyServiceProvider::class,
|
||||
\Flasher\Notyf\Laravel\FlasherNotyfServiceProvider::class,
|
||||
\Flasher\SweetAlert\Laravel\FlasherSweetAlertServiceProvider::class,
|
||||
\Flasher\Toastr\Laravel\FlasherToastrServiceProvider::class,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Application|null $app
|
||||
* Override application aliases.
|
||||
*
|
||||
* @return string[]
|
||||
* @return array<string, class-string<Facade>>
|
||||
*/
|
||||
protected function getPackageProviders($app = null)
|
||||
protected function getPackageAliases($app): array
|
||||
{
|
||||
return array(
|
||||
'Flasher\Laravel\FlasherServiceProvider',
|
||||
'Flasher\Noty\Laravel\FlasherNotyServiceProvider',
|
||||
'Flasher\Notyf\Laravel\FlasherNotyfServiceProvider',
|
||||
'Flasher\Pnotify\Laravel\FlasherPnotifyServiceProvider',
|
||||
'Flasher\SweetAlert\Laravel\FlasherSweetAlertServiceProvider',
|
||||
'Flasher\Toastr\Laravel\FlasherToastrServiceProvider',
|
||||
);
|
||||
return [
|
||||
'Flasher' => \Flasher\Laravel\Facade\Flasher::class,
|
||||
'Noty' => \Flasher\Noty\Laravel\Facade\Noty::class,
|
||||
'Notyf' => \Flasher\Notyf\Laravel\Facade\Notyf::class,
|
||||
'SweetAlert' => \Flasher\SweetAlert\Laravel\Facade\SweetAlert::class,
|
||||
'Toastr' => \Flasher\Toastr\Laravel\Facade\Toastr::class,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Application $app
|
||||
*/
|
||||
protected function getEnvironmentSetUp($app)
|
||||
protected function defineEnvironment($app): void
|
||||
{
|
||||
$separator = Laravel::isVersion('4') ? '::config' : '';
|
||||
|
||||
$app->make('config')->set('session.driver', 'array');
|
||||
$app->make('config')->set('session'.$separator.'.driver', 'array');
|
||||
tap($app['config'], function (Repository $config) {
|
||||
$config->set('session.driver', 'array');
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Laravel\Translation;
|
||||
|
||||
use Flasher\Laravel\Translation\Translator;
|
||||
use Illuminate\Translation\Translator as LaravelTranslator;
|
||||
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
|
||||
use Mockery\MockInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class TranslatorTest extends TestCase
|
||||
{
|
||||
use MockeryPHPUnitIntegration;
|
||||
|
||||
private MockInterface&LaravelTranslator $laravelTranslatorMock;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->laravelTranslatorMock = \Mockery::mock(LaravelTranslator::class);
|
||||
}
|
||||
|
||||
public function testTranslateWithExistingTranslation(): void
|
||||
{
|
||||
$this->laravelTranslatorMock->expects()
|
||||
->has('flasher::messages.key', null)
|
||||
->andReturnTrue();
|
||||
|
||||
$this->laravelTranslatorMock->expects()
|
||||
->get('flasher::messages.key', ['some_param' => 1], null)
|
||||
->andReturns('translated message');
|
||||
|
||||
$translator = new Translator($this->laravelTranslatorMock);
|
||||
$this->assertSame('translated message', $translator->translate('key', ['some_param' => 1]));
|
||||
}
|
||||
|
||||
public function testTranslateWithFallbackTranslation(): void
|
||||
{
|
||||
$this->laravelTranslatorMock->expects()
|
||||
->has('flasher::messages.key', null)
|
||||
->andReturnFalse();
|
||||
|
||||
$this->laravelTranslatorMock->expects()
|
||||
->has('messages.key', null)
|
||||
->andReturnTrue();
|
||||
|
||||
$this->laravelTranslatorMock->expects()
|
||||
->get('messages.key', ['some_param' => 1], null)
|
||||
->andReturns('fallback translated message');
|
||||
|
||||
$translator = new Translator($this->laravelTranslatorMock);
|
||||
$this->assertSame('fallback translated message', $translator->translate('key', ['some_param' => 1]));
|
||||
}
|
||||
|
||||
public function testTranslateWithNoTranslationFound(): void
|
||||
{
|
||||
$this->laravelTranslatorMock->allows('has')
|
||||
->andReturnFalse();
|
||||
|
||||
$this->laravelTranslatorMock->allows('get')
|
||||
->andReturnUsing(function ($id, $parameters, $locale) {
|
||||
return $id; // Simulate Laravel's behavior of returning the key when no translation is found
|
||||
});
|
||||
|
||||
$translator = new Translator($this->laravelTranslatorMock);
|
||||
$this->assertSame('key', $translator->translate('key', ['some_param' => 1]));
|
||||
}
|
||||
|
||||
public function testGetLocale(): void
|
||||
{
|
||||
$this->laravelTranslatorMock->expects()
|
||||
->getLocale()
|
||||
->andReturns('en');
|
||||
|
||||
$translator = new Translator($this->laravelTranslatorMock);
|
||||
$this->assertSame('en', $translator->getLocale());
|
||||
}
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the PHPFlasher package.
|
||||
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
|
||||
*/
|
||||
|
||||
namespace Flasher\Tests\Laravel;
|
||||
|
||||
use Flasher\Laravel\Translation\Translator;
|
||||
|
||||
final class TranslatorTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testTranslateMessage()
|
||||
{
|
||||
$translator = $this->getTranslator();
|
||||
|
||||
$this->assertEquals('Success', $translator->translate('success', array(), 'en'));
|
||||
$this->assertEquals('Succès', $translator->translate('success', array(), 'fr'));
|
||||
$this->assertEquals('نجاح', $translator->translate('success', array(), 'ar'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testTranslateMessageWithParameters()
|
||||
{
|
||||
$translator = $this->getTranslator();
|
||||
|
||||
$this->assertEquals('The :resource was created', $translator->translate('The resource was created', array(), 'en'));
|
||||
$this->assertEquals('The user was created', $translator->translate('The resource was created', array('resource' => 'user'), 'en'));
|
||||
|
||||
$this->assertEquals('La ressource :resource a été ajoutée', $translator->translate('The resource was created', array(), 'fr'));
|
||||
$this->assertEquals('La ressource utilisateur a été ajoutée', $translator->translate('The resource was created', array('resource' => 'utilisateur'), 'fr'));
|
||||
|
||||
$this->assertEquals('تم إنشاء :resource', $translator->translate('The resource was created', array(), 'ar'));
|
||||
$this->assertEquals('تم إنشاء الملف', $translator->translate('The resource was created', array('resource' => 'الملف'), 'ar'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Translator
|
||||
*/
|
||||
private function getTranslator()
|
||||
{
|
||||
/** @var \Illuminate\Translation\Translator $laravelTranslator */
|
||||
$laravelTranslator = $this->app->make('translator');
|
||||
|
||||
return new Translator($laravelTranslator);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Noty\Laravel;
|
||||
|
||||
use Flasher\Noty\Laravel\FlasherNotyServiceProvider;
|
||||
use Flasher\Noty\Prime\NotyPlugin;
|
||||
use Illuminate\Contracts\Config\Repository;
|
||||
use Illuminate\Contracts\Foundation\Application;
|
||||
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
|
||||
use Mockery\MockInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class FlasherNotyServiceProviderTest extends TestCase
|
||||
{
|
||||
use MockeryPHPUnitIntegration;
|
||||
|
||||
private MockInterface&Application $app;
|
||||
private FlasherNotyServiceProvider $serviceProvider;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->app = \Mockery::mock(Application::class);
|
||||
$this->serviceProvider = new FlasherNotyServiceProvider($this->app);
|
||||
}
|
||||
|
||||
public function testCreatePlugin(): void
|
||||
{
|
||||
$this->assertInstanceOf(NotyPlugin::class, $this->serviceProvider->createPlugin());
|
||||
}
|
||||
|
||||
public function testRegister(): void
|
||||
{
|
||||
$this->app->expects()->make('config')->andReturns($configMock = \Mockery::mock(Repository::class));
|
||||
$configMock->expects('get')->andReturns([]);
|
||||
$configMock->expects('set');
|
||||
|
||||
$this->app->expects('configurationIsCached')->never();
|
||||
|
||||
$this->serviceProvider->register();
|
||||
$this->addToAssertionCount(1);
|
||||
}
|
||||
|
||||
public function testBoot(): void
|
||||
{
|
||||
$this->app->expects()->make('config')->andReturns($configMock = \Mockery::mock(Repository::class));
|
||||
$configMock->expects('get')->andReturns([]);
|
||||
$configMock->expects('set');
|
||||
|
||||
$this->app->expects('singleton');
|
||||
$this->app->expects('alias');
|
||||
$this->app->expects('extend');
|
||||
|
||||
$this->serviceProvider->register();
|
||||
$this->serviceProvider->boot();
|
||||
$this->addToAssertionCount(1);
|
||||
}
|
||||
|
||||
public function testGetConfigurationFile(): void
|
||||
{
|
||||
$expectedPath = $this->getResourcesPathFromServiceProvider();
|
||||
$this->assertStringEndsWith('/Resources/config.php', $this->serviceProvider->getConfigurationFile());
|
||||
$this->assertStringContainsString($expectedPath, $this->serviceProvider->getConfigurationFile());
|
||||
}
|
||||
|
||||
private function getResourcesPathFromServiceProvider(): string
|
||||
{
|
||||
$reflection = new \ReflectionClass(FlasherNotyServiceProvider::class);
|
||||
$method = $reflection->getMethod('getResourcesDir');
|
||||
$method->setAccessible(true);
|
||||
|
||||
/** @var string $string */
|
||||
$string = $method->invoke($this->serviceProvider);
|
||||
|
||||
return rtrim($string, '/').'/';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,202 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Noty\Prime;
|
||||
|
||||
use Flasher\Noty\Prime\NotyBuilder;
|
||||
use Flasher\Prime\Storage\StorageManagerInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class NotyBuilderTest extends TestCase
|
||||
{
|
||||
private NotyBuilder $notyBuilder;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$storageManagerMock = \Mockery::mock(StorageManagerInterface::class);
|
||||
$this->notyBuilder = new NotyBuilder('noty', $storageManagerMock);
|
||||
}
|
||||
|
||||
public function testText(): void
|
||||
{
|
||||
$this->notyBuilder->text('Test message');
|
||||
|
||||
$envelope = $this->notyBuilder->getEnvelope();
|
||||
$notification = $envelope->getNotification();
|
||||
$actualMessage = $notification->getMessage();
|
||||
|
||||
$this->assertSame('Test message', $actualMessage);
|
||||
}
|
||||
|
||||
public function testAlert(): void
|
||||
{
|
||||
$this->notyBuilder->alert('Test alert');
|
||||
|
||||
$envelope = $this->notyBuilder->getEnvelope();
|
||||
$notification = $envelope->getNotification();
|
||||
|
||||
$this->assertSame('alert', $notification->getType());
|
||||
}
|
||||
|
||||
public function testLayout(): void
|
||||
{
|
||||
$this->notyBuilder->layout('top');
|
||||
|
||||
$envelope = $this->notyBuilder->getEnvelope();
|
||||
$options = $envelope->getNotification()->getOptions();
|
||||
|
||||
$this->assertSame(['layout' => 'top'], $options);
|
||||
}
|
||||
|
||||
public function testTheme(): void
|
||||
{
|
||||
$this->notyBuilder->theme('mint');
|
||||
|
||||
$envelope = $this->notyBuilder->getEnvelope();
|
||||
$options = $envelope->getNotification()->getOptions();
|
||||
|
||||
$this->assertSame(['theme' => 'mint'], $options);
|
||||
}
|
||||
|
||||
public function testTimeout(): void
|
||||
{
|
||||
$this->notyBuilder->timeout(5000);
|
||||
|
||||
$envelope = $this->notyBuilder->getEnvelope();
|
||||
$options = $envelope->getNotification()->getOptions();
|
||||
|
||||
$this->assertSame(['timeout' => 5000], $options);
|
||||
}
|
||||
|
||||
public function testProgressBar(): void
|
||||
{
|
||||
$this->notyBuilder->progressBar(true);
|
||||
|
||||
$envelope = $this->notyBuilder->getEnvelope();
|
||||
$options = $envelope->getNotification()->getOptions();
|
||||
|
||||
$this->assertSame(['progressBar' => true], $options);
|
||||
}
|
||||
|
||||
public function testCloseWith(): void
|
||||
{
|
||||
$closeWith = 'click';
|
||||
$this->notyBuilder->closeWith($closeWith);
|
||||
|
||||
$envelope = $this->notyBuilder->getEnvelope();
|
||||
$options = $envelope->getNotification()->getOptions();
|
||||
|
||||
$this->assertSame(['closeWith' => ['click']], $options);
|
||||
}
|
||||
|
||||
public function testAnimation(): void
|
||||
{
|
||||
$this->notyBuilder->animation('open', 'fadeIn');
|
||||
|
||||
$envelope = $this->notyBuilder->getEnvelope();
|
||||
$options = $envelope->getNotification()->getOptions();
|
||||
|
||||
$this->assertSame(['animation.open' => 'fadeIn'], $options);
|
||||
}
|
||||
|
||||
public function testSounds(): void
|
||||
{
|
||||
$this->notyBuilder->sounds('open', 'sound.mp3');
|
||||
|
||||
$envelope = $this->notyBuilder->getEnvelope();
|
||||
$options = $envelope->getNotification()->getOptions();
|
||||
|
||||
$this->assertSame(['sounds.open' => 'sound.mp3'], $options);
|
||||
}
|
||||
|
||||
public function testDocTitle(): void
|
||||
{
|
||||
$this->notyBuilder->docTitle('Success', 'Title Changed');
|
||||
|
||||
$envelope = $this->notyBuilder->getEnvelope();
|
||||
$options = $envelope->getNotification()->getOptions();
|
||||
|
||||
$this->assertSame(['docTitleSuccess' => 'Title Changed'], $options);
|
||||
}
|
||||
|
||||
public function testModal(): void
|
||||
{
|
||||
$this->notyBuilder->modal(true);
|
||||
|
||||
$envelope = $this->notyBuilder->getEnvelope();
|
||||
$options = $envelope->getNotification()->getOptions();
|
||||
|
||||
$this->assertSame(['modal' => true], $options);
|
||||
}
|
||||
|
||||
public function testId(): void
|
||||
{
|
||||
$this->notyBuilder->id('custom_id');
|
||||
|
||||
$envelope = $this->notyBuilder->getEnvelope();
|
||||
$options = $envelope->getNotification()->getOptions();
|
||||
|
||||
$this->assertSame(['id' => 'custom_id'], $options);
|
||||
}
|
||||
|
||||
public function testForce(): void
|
||||
{
|
||||
$this->notyBuilder->force(true);
|
||||
|
||||
$envelope = $this->notyBuilder->getEnvelope();
|
||||
$options = $envelope->getNotification()->getOptions();
|
||||
|
||||
$this->assertSame(['force' => true], $options);
|
||||
}
|
||||
|
||||
public function testQueue(): void
|
||||
{
|
||||
$this->notyBuilder->queue('customQueue');
|
||||
|
||||
$envelope = $this->notyBuilder->getEnvelope();
|
||||
$options = $envelope->getNotification()->getOptions();
|
||||
|
||||
$this->assertSame(['queue' => 'customQueue'], $options);
|
||||
}
|
||||
|
||||
public function testKiller(): void
|
||||
{
|
||||
$this->notyBuilder->killer(true);
|
||||
|
||||
$envelope = $this->notyBuilder->getEnvelope();
|
||||
$options = $envelope->getNotification()->getOptions();
|
||||
|
||||
$this->assertSame(['killer' => true], $options);
|
||||
}
|
||||
|
||||
public function testContainer(): void
|
||||
{
|
||||
$this->notyBuilder->container('.custom-container');
|
||||
|
||||
$envelope = $this->notyBuilder->getEnvelope();
|
||||
$options = $envelope->getNotification()->getOptions();
|
||||
|
||||
$this->assertSame(['container' => '.custom-container'], $options);
|
||||
}
|
||||
|
||||
public function testButtons(): void
|
||||
{
|
||||
$this->notyBuilder->buttons(['Yes', 'No']);
|
||||
|
||||
$envelope = $this->notyBuilder->getEnvelope();
|
||||
$options = $envelope->getNotification()->getOptions();
|
||||
|
||||
$this->assertSame(['buttons' => ['Yes', 'No']], $options);
|
||||
}
|
||||
|
||||
public function testVisibilityControl(): void
|
||||
{
|
||||
$this->notyBuilder->visibilityControl(true);
|
||||
|
||||
$envelope = $this->notyBuilder->getEnvelope();
|
||||
$options = $envelope->getNotification()->getOptions();
|
||||
|
||||
$this->assertSame(['visibilityControl' => true], $options);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Noty\Prime;
|
||||
|
||||
use Flasher\Noty\Prime\Noty;
|
||||
use Flasher\Noty\Prime\NotyInterface;
|
||||
use Flasher\Noty\Prime\NotyPlugin;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class NotyPluginTest extends TestCase
|
||||
{
|
||||
private NotyPlugin $notyPlugin;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->notyPlugin = new NotyPlugin();
|
||||
}
|
||||
|
||||
public function testGetAlias(): void
|
||||
{
|
||||
$this->assertSame('noty', $this->notyPlugin->getAlias());
|
||||
}
|
||||
|
||||
public function testGetFactory(): void
|
||||
{
|
||||
$this->assertSame(Noty::class, $this->notyPlugin->getFactory());
|
||||
}
|
||||
|
||||
public function testGetServiceAliases(): void
|
||||
{
|
||||
$this->assertSame(NotyInterface::class, $this->notyPlugin->getServiceAliases());
|
||||
}
|
||||
|
||||
public function testGetScripts(): void
|
||||
{
|
||||
$this->assertSame([
|
||||
'/vendor/flasher/noty.min.js',
|
||||
'/vendor/flasher/flasher-noty.min.js',
|
||||
], $this->notyPlugin->getScripts());
|
||||
}
|
||||
|
||||
public function testGetStyles(): void
|
||||
{
|
||||
$this->assertSame([
|
||||
'/vendor/flasher/noty.css',
|
||||
'/vendor/flasher/mint.css',
|
||||
], $this->notyPlugin->getStyles());
|
||||
}
|
||||
|
||||
public function testGetName(): void
|
||||
{
|
||||
$this->assertSame('flasher_noty', $this->notyPlugin->getName());
|
||||
}
|
||||
|
||||
public function testGetServiceId(): void
|
||||
{
|
||||
$this->assertSame('flasher.noty', $this->notyPlugin->getServiceId());
|
||||
}
|
||||
|
||||
public function testNormalizeConfig(): void
|
||||
{
|
||||
$expected = [
|
||||
'scripts' => [
|
||||
'/vendor/flasher/noty.min.js',
|
||||
'/vendor/flasher/flasher-noty.min.js',
|
||||
],
|
||||
'styles' => [
|
||||
'/vendor/flasher/noty.css',
|
||||
'/vendor/flasher/mint.css',
|
||||
],
|
||||
'options' => [],
|
||||
];
|
||||
|
||||
$this->assertSame($expected, $this->notyPlugin->normalizeConfig([]));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Noty\Prime;
|
||||
|
||||
use Flasher\Noty\Prime\Noty;
|
||||
use Flasher\Noty\Prime\NotyBuilder;
|
||||
use Flasher\Prime\Storage\StorageManagerInterface;
|
||||
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class NotyTest extends TestCase
|
||||
{
|
||||
use MockeryPHPUnitIntegration;
|
||||
|
||||
public function testCanCreateNotificationBuilder(): void
|
||||
{
|
||||
$storageManagerMock = \Mockery::mock(StorageManagerInterface::class);
|
||||
|
||||
$noty = new Noty($storageManagerMock);
|
||||
$result = $noty->createNotificationBuilder();
|
||||
|
||||
$this->assertInstanceOf(NotyBuilder::class, $result);
|
||||
}
|
||||
|
||||
public function testNotyBuilderTextMethod(): void
|
||||
{
|
||||
$storageManager = \Mockery::mock(StorageManagerInterface::class);
|
||||
|
||||
$noty = new Noty($storageManager);
|
||||
|
||||
$builder = $noty->createNotificationBuilder();
|
||||
$response = $noty->text('Hello World');
|
||||
|
||||
$this->assertInstanceOf(NotyBuilder::class, $response);
|
||||
$this->assertInstanceOf(NotyBuilder::class, $builder);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Noty\Symfony;
|
||||
|
||||
use Flasher\Noty\Prime\NotyPlugin;
|
||||
use Flasher\Noty\Symfony\FlasherNotyBundle;
|
||||
use Flasher\Symfony\Support\PluginBundle;
|
||||
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class FlasherNotyBundleTest extends TestCase
|
||||
{
|
||||
use MockeryPHPUnitIntegration;
|
||||
|
||||
private FlasherNotyBundle $flasherNotyBundle;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->flasherNotyBundle = new FlasherNotyBundle();
|
||||
}
|
||||
|
||||
public function testInstance(): void
|
||||
{
|
||||
$this->assertInstanceOf(PluginBundle::class, $this->flasherNotyBundle);
|
||||
}
|
||||
|
||||
public function testCreatePlugin(): void
|
||||
{
|
||||
$this->assertInstanceOf(NotyPlugin::class, $this->flasherNotyBundle->createPlugin());
|
||||
}
|
||||
|
||||
public function testGetConfigurationFileReturnsExpectedPath(): void
|
||||
{
|
||||
$expectedPath = $this->flasherNotyBundle->getPath().'/Resources/config/config.yaml';
|
||||
|
||||
$this->assertSame($expectedPath, $this->flasherNotyBundle->getConfigurationFile());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Notyf\Laravel;
|
||||
|
||||
use Flasher\Notyf\Laravel\FlasherNotyfServiceProvider;
|
||||
use Flasher\Notyf\Prime\NotyfPlugin;
|
||||
use Illuminate\Contracts\Config\Repository;
|
||||
use Illuminate\Contracts\Foundation\Application;
|
||||
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
|
||||
use Mockery\MockInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class FlasherNotyfServiceProviderTest extends TestCase
|
||||
{
|
||||
use MockeryPHPUnitIntegration;
|
||||
|
||||
private MockInterface&Application $app;
|
||||
private FlasherNotyfServiceProvider $serviceProvider;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->app = \Mockery::mock(Application::class);
|
||||
$this->serviceProvider = new FlasherNotyfServiceProvider($this->app);
|
||||
}
|
||||
|
||||
public function testCreatePlugin(): void
|
||||
{
|
||||
$this->assertInstanceOf(NotyfPlugin::class, $this->serviceProvider->createPlugin());
|
||||
}
|
||||
|
||||
public function testRegister(): void
|
||||
{
|
||||
$this->app->expects()->make('config')->andReturns($configMock = \Mockery::mock(Repository::class));
|
||||
$configMock->expects('get')->andReturns([]);
|
||||
$configMock->expects('set');
|
||||
|
||||
$this->app->expects('configurationIsCached')->never();
|
||||
|
||||
$this->serviceProvider->register();
|
||||
$this->addToAssertionCount(1);
|
||||
}
|
||||
|
||||
public function testBoot(): void
|
||||
{
|
||||
$this->app->expects()->make('config')->andReturns($configMock = \Mockery::mock(Repository::class));
|
||||
$configMock->expects('get')->andReturns([]);
|
||||
$configMock->expects('set');
|
||||
|
||||
$this->app->expects('singleton');
|
||||
$this->app->expects('alias');
|
||||
$this->app->expects('extend');
|
||||
|
||||
$this->serviceProvider->register();
|
||||
$this->serviceProvider->boot();
|
||||
$this->addToAssertionCount(1);
|
||||
}
|
||||
|
||||
public function testGetConfigurationFile(): void
|
||||
{
|
||||
$expectedPath = $this->getResourcesPathFromServiceProvider();
|
||||
$this->assertStringEndsWith('/Resources/config.php', $this->serviceProvider->getConfigurationFile());
|
||||
$this->assertStringContainsString($expectedPath, $this->serviceProvider->getConfigurationFile());
|
||||
}
|
||||
|
||||
private function getResourcesPathFromServiceProvider(): string
|
||||
{
|
||||
$reflection = new \ReflectionClass(FlasherNotyfServiceProvider::class);
|
||||
$method = $reflection->getMethod('getResourcesDir');
|
||||
$method->setAccessible(true);
|
||||
|
||||
/** @var string $string */
|
||||
$string = $method->invoke($this->serviceProvider);
|
||||
|
||||
return rtrim($string, '/').'/';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Notyf\Prime;
|
||||
|
||||
use Flasher\Notyf\Prime\NotyfBuilder;
|
||||
use Flasher\Prime\Storage\StorageManagerInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class NotyfBuilderTest extends TestCase
|
||||
{
|
||||
private NotyfBuilder $notyfBuilder;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$storageManagerMock = \Mockery::mock(StorageManagerInterface::class);
|
||||
$this->notyfBuilder = new NotyfBuilder('notyf', $storageManagerMock);
|
||||
}
|
||||
|
||||
public function testDuration(): void
|
||||
{
|
||||
$this->notyfBuilder->duration(6000);
|
||||
|
||||
$envelope = $this->notyfBuilder->getEnvelope();
|
||||
$options = $envelope->getNotification()->getOptions();
|
||||
|
||||
$this->assertSame(['duration' => 6000], $options);
|
||||
}
|
||||
|
||||
public function testRipple(): void
|
||||
{
|
||||
$this->notyfBuilder->ripple();
|
||||
|
||||
$envelope = $this->notyfBuilder->getEnvelope();
|
||||
$options = $envelope->getNotification()->getOptions();
|
||||
|
||||
$this->assertSame(['ripple' => true], $options);
|
||||
}
|
||||
|
||||
public function testPosition(): void
|
||||
{
|
||||
$this->notyfBuilder->position('x', 'center');
|
||||
$this->notyfBuilder->position('y', 'top');
|
||||
|
||||
$envelope = $this->notyfBuilder->getEnvelope();
|
||||
$options = $envelope->getNotification()->getOptions();
|
||||
|
||||
$this->assertSame(['position' => ['x' => 'center', 'y' => 'top']], $options);
|
||||
}
|
||||
|
||||
public function testDismissible(): void
|
||||
{
|
||||
$this->notyfBuilder->dismissible(true);
|
||||
|
||||
$envelope = $this->notyfBuilder->getEnvelope();
|
||||
$options = $envelope->getNotification()->getOptions();
|
||||
|
||||
$this->assertSame(['dismissible' => true], $options);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Notyf\Prime;
|
||||
|
||||
use Flasher\Notyf\Prime\Notyf;
|
||||
use Flasher\Notyf\Prime\NotyfInterface;
|
||||
use Flasher\Notyf\Prime\NotyfPlugin;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class NotyfPluginTest extends TestCase
|
||||
{
|
||||
private NotyfPlugin $notyfPlugin;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->notyfPlugin = new NotyfPlugin();
|
||||
}
|
||||
|
||||
public function testGetAlias(): void
|
||||
{
|
||||
$this->assertSame('notyf', $this->notyfPlugin->getAlias());
|
||||
}
|
||||
|
||||
public function testGetFactory(): void
|
||||
{
|
||||
$this->assertSame(Notyf::class, $this->notyfPlugin->getFactory());
|
||||
}
|
||||
|
||||
public function testGetServiceAliases(): void
|
||||
{
|
||||
$this->assertSame(NotyfInterface::class, $this->notyfPlugin->getServiceAliases());
|
||||
}
|
||||
|
||||
public function testGetScripts(): void
|
||||
{
|
||||
$this->assertSame(['/vendor/flasher/flasher-notyf.min.js'], $this->notyfPlugin->getScripts());
|
||||
}
|
||||
|
||||
public function testGetStyles(): void
|
||||
{
|
||||
$this->assertSame(['/vendor/flasher/flasher-notyf.min.css'], $this->notyfPlugin->getStyles());
|
||||
}
|
||||
|
||||
public function testGetName(): void
|
||||
{
|
||||
$this->assertSame('flasher_notyf', $this->notyfPlugin->getName());
|
||||
}
|
||||
|
||||
public function testGetServiceId(): void
|
||||
{
|
||||
$this->assertSame('flasher.notyf', $this->notyfPlugin->getServiceId());
|
||||
}
|
||||
|
||||
public function testNormalizeConfig(): void
|
||||
{
|
||||
$expected = [
|
||||
'scripts' => ['/vendor/flasher/flasher-notyf.min.js'],
|
||||
'styles' => ['/vendor/flasher/flasher-notyf.min.css'],
|
||||
'options' => [],
|
||||
];
|
||||
|
||||
$this->assertSame($expected, $this->notyfPlugin->normalizeConfig([]));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Notyf\Prime;
|
||||
|
||||
use Flasher\Notyf\Prime\Notyf;
|
||||
use Flasher\Notyf\Prime\NotyfBuilder;
|
||||
use Flasher\Prime\Storage\StorageManagerInterface;
|
||||
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class NotyfTest extends TestCase
|
||||
{
|
||||
use MockeryPHPUnitIntegration;
|
||||
|
||||
public function testCanCreateNotificationBuilder(): void
|
||||
{
|
||||
$storageManagerMock = \Mockery::mock(StorageManagerInterface::class);
|
||||
|
||||
$notyf = new Notyf($storageManagerMock);
|
||||
$result = $notyf->createNotificationBuilder();
|
||||
|
||||
$this->assertInstanceOf(NotyfBuilder::class, $result);
|
||||
}
|
||||
|
||||
public function testNotyfBuilderTextMethod(): void
|
||||
{
|
||||
$storageManager = \Mockery::mock(StorageManagerInterface::class);
|
||||
|
||||
$notyf = new Notyf($storageManager);
|
||||
|
||||
$builder = $notyf->createNotificationBuilder();
|
||||
$response = $notyf->duration(6000);
|
||||
|
||||
$this->assertInstanceOf(NotyfBuilder::class, $response);
|
||||
$this->assertInstanceOf(NotyfBuilder::class, $builder);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Notyf\Symfony;
|
||||
|
||||
use Flasher\Notyf\Prime\NotyfPlugin;
|
||||
use Flasher\Notyf\Symfony\FlasherNotyfBundle;
|
||||
use Flasher\Symfony\Support\PluginBundle;
|
||||
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class FlasherNotyfBundleTest extends TestCase
|
||||
{
|
||||
use MockeryPHPUnitIntegration;
|
||||
|
||||
private FlasherNotyfBundle $flasherNotyfBundle;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->flasherNotyfBundle = new FlasherNotyfBundle();
|
||||
}
|
||||
|
||||
public function testInstance(): void
|
||||
{
|
||||
$this->assertInstanceOf(PluginBundle::class, $this->flasherNotyfBundle);
|
||||
}
|
||||
|
||||
public function testCreatePlugin(): void
|
||||
{
|
||||
$this->assertInstanceOf(NotyfPlugin::class, $this->flasherNotyfBundle->createPlugin());
|
||||
}
|
||||
|
||||
public function testGetConfigurationFileReturnsExpectedPath(): void
|
||||
{
|
||||
$expectedPath = $this->flasherNotyfBundle->getPath().'/Resources/config/config.yaml';
|
||||
|
||||
$this->assertSame($expectedPath, $this->flasherNotyfBundle->getConfigurationFile());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\Asset;
|
||||
|
||||
use Flasher\Prime\Asset\AssetManager;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class AssetManagerTest extends TestCase
|
||||
{
|
||||
private string $publicDir;
|
||||
private string $manifestPath;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->publicDir = __DIR__.'/../Fixtures/Asset';
|
||||
$this->manifestPath = __DIR__.'/../Fixtures/Asset/manifest.json';
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
if (file_exists($this->manifestPath)) {
|
||||
unlink($this->manifestPath);
|
||||
}
|
||||
}
|
||||
|
||||
public function testConstruct(): void
|
||||
{
|
||||
$assetManager = new AssetManager($this->publicDir, $this->manifestPath);
|
||||
$this->assertInstanceOf(AssetManager::class, $assetManager);
|
||||
}
|
||||
|
||||
public function testGetPath(): void
|
||||
{
|
||||
$assetManager = new AssetManager($this->publicDir, $this->manifestPath);
|
||||
|
||||
$filePath = __DIR__.'/../Fixtures/Asset/test.css';
|
||||
|
||||
$assetManager->createManifest([$filePath]);
|
||||
|
||||
$expectedPath = '/test.css?id=2cb85c44817ffbc50452dab7fc3e4823';
|
||||
|
||||
$this->assertSame($expectedPath, $assetManager->getPath('/test.css'));
|
||||
}
|
||||
|
||||
public function testGetPaths(): void
|
||||
{
|
||||
$assetManager = new AssetManager($this->publicDir, $this->manifestPath);
|
||||
|
||||
$assetManager->createManifest([
|
||||
__DIR__.'/../Fixtures/Asset/test1.css',
|
||||
__DIR__.'/../Fixtures/Asset/test2.css',
|
||||
__DIR__.'/../Fixtures/Asset/test3.css',
|
||||
]);
|
||||
|
||||
$files = ['/test1.css', '/test2.css', '/test3.css'];
|
||||
$expectedPaths = [
|
||||
'/test1.css?id=38eeac10df68fe4b49c30f8c6af0b1cc',
|
||||
'/test2.css?id=3cb80f170ff572502dca33a5ddb3ead3',
|
||||
'/test3.css?id=e7172b646b854195291ebc5b12c88022',
|
||||
];
|
||||
|
||||
$this->assertSame($expectedPaths, $assetManager->getPaths($files));
|
||||
}
|
||||
|
||||
public function testCreateManifest(): void
|
||||
{
|
||||
$assetManager = new AssetManager($this->publicDir, $this->manifestPath);
|
||||
$files = [
|
||||
__DIR__.'/../Fixtures/Asset/test1.css',
|
||||
__DIR__.'/../Fixtures/Asset/test2.css',
|
||||
__DIR__.'/../Fixtures/Asset/test3.css',
|
||||
];
|
||||
$assetManager->createManifest($files);
|
||||
|
||||
$expectedEntries = [
|
||||
'/test1.css' => '/test1.css?id=38eeac10df68fe4b49c30f8c6af0b1cc',
|
||||
'/test2.css' => '/test2.css?id=3cb80f170ff572502dca33a5ddb3ead3',
|
||||
'/test3.css' => '/test3.css?id=e7172b646b854195291ebc5b12c88022',
|
||||
];
|
||||
|
||||
// Using reflection to make getEntriesData() accessible
|
||||
$reflection = new \ReflectionClass(AssetManager::class);
|
||||
$method = $reflection->getMethod('getEntriesData');
|
||||
$method->setAccessible(true);
|
||||
|
||||
$entriesData = $method->invoke($assetManager);
|
||||
|
||||
$this->assertSame($expectedEntries, $entriesData);
|
||||
}
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the PHPFlasher package.
|
||||
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
|
||||
*/
|
||||
|
||||
namespace Flasher\Tests\Prime\Config;
|
||||
|
||||
use Flasher\Prime\Config\Config;
|
||||
use Flasher\Tests\Prime\TestCase;
|
||||
|
||||
final class ConfigTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testGet()
|
||||
{
|
||||
/** @phpstan-ignore-next-line */
|
||||
$config = new Config(array(
|
||||
'default' => 'flasher',
|
||||
'root_script' => 'flasher.min.js',
|
||||
'themes' => array(
|
||||
'flasher' => array(
|
||||
'scripts' => array('script.js'),
|
||||
'styles' => array('styles.css'),
|
||||
'options' => array(),
|
||||
),
|
||||
),
|
||||
'auto_translate' => true,
|
||||
'flash_bag' => array(
|
||||
'enabled' => true,
|
||||
'mapping' => array(
|
||||
'success' => array('success'),
|
||||
'error' => array('error'),
|
||||
),
|
||||
),
|
||||
'presets' => array(
|
||||
'success' => array(
|
||||
'type' => 'success',
|
||||
'title' => 'Success',
|
||||
'message' => 'Success message',
|
||||
'options' => array(),
|
||||
),
|
||||
'error' => array(
|
||||
'type' => 'error',
|
||||
'title' => 'Error',
|
||||
'message' => 'Error message',
|
||||
'options' => array(),
|
||||
),
|
||||
),
|
||||
));
|
||||
|
||||
$this->assertEquals('flasher', $config->get('default'));
|
||||
$this->assertEquals(array(
|
||||
'scripts' => array('script.js'),
|
||||
'styles' => array('styles.css'),
|
||||
'options' => array(),
|
||||
), $config->get('themes.flasher'));
|
||||
$this->assertEquals(array('styles.css'), $config->get('themes.flasher.styles'));
|
||||
$this->assertEquals(array('script.js'), $config->get('themes.flasher.scripts'));
|
||||
$this->assertEquals(array(), $config->get('themes.flasher.options'));
|
||||
$this->assertNull($config->get('drivers.not_exists.options'));
|
||||
$this->assertEquals('now_it_exists', $config->get('drivers.not_exists.options', 'now_it_exists'));
|
||||
}
|
||||
}
|
||||
@@ -1,59 +1,71 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the PHPFlasher package.
|
||||
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\Container;
|
||||
|
||||
use Flasher\Prime\Container\FlasherContainer;
|
||||
use Flasher\Tests\Prime\TestCase;
|
||||
use Flasher\Prime\Factory\NotificationFactoryInterface;
|
||||
use Flasher\Prime\FlasherInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
class FlasherContainerTest extends TestCase
|
||||
final class FlasherContainerTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testInit()
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->setProperty('Flasher\Prime\Container\FlasherContainer', 'instance', null);
|
||||
$container = $this->getMockBuilder('Flasher\Prime\Container\ContainerInterface')->getMock();
|
||||
|
||||
FlasherContainer::init($container);
|
||||
|
||||
$property = $this->getProperty('Flasher\Prime\Container\FlasherContainer', 'container');
|
||||
|
||||
$this->assertEquals($container, $property);
|
||||
// Reset the FlasherContainer instance to ensure isolation between tests
|
||||
FlasherContainer::reset();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testCreate()
|
||||
public function testCreateReturnsCorrectType(): void
|
||||
{
|
||||
$this->setProperty('Flasher\Prime\Container\FlasherContainer', 'instance', null);
|
||||
$flasher = $this->createMock(FlasherInterface::class);
|
||||
|
||||
$container = $this->getMockBuilder('Flasher\Prime\Container\ContainerInterface')->getMock();
|
||||
$container
|
||||
->method('get')
|
||||
->willreturn($this->getMockBuilder('Flasher\Prime\FlasherInterface')->getMock());
|
||||
$container = $this->createMock(ContainerInterface::class);
|
||||
$container->method('has')->willReturn(true);
|
||||
$container->method('get')->willReturn($flasher);
|
||||
|
||||
FlasherContainer::init($container);
|
||||
FlasherContainer::from($container);
|
||||
|
||||
$service = FlasherContainer::create('flasher');
|
||||
|
||||
$this->assertInstanceOf('Flasher\Prime\FlasherInterface', $service);
|
||||
$this->assertInstanceOf(FlasherInterface::class, FlasherContainer::create('flasher'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testThrowsExceptionIfNotInitialized()
|
||||
public function testCreateThrowsExceptionForNotFoundService(): void
|
||||
{
|
||||
$this->setExpectedException('\LogicException', 'Container is not initialized yet. Container::init() must be called with a real container.');
|
||||
$invalidService = new \stdClass();
|
||||
$container = $this->createMock(ContainerInterface::class);
|
||||
$container->method('has')->willReturn(false);
|
||||
$container->method('get')->willReturn($invalidService);
|
||||
|
||||
$this->setProperty('Flasher\Prime\Container\FlasherContainer', 'instance', null);
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
$this->expectExceptionMessage('The container does not have the requested service "invalid_service".');
|
||||
|
||||
FlasherContainer::from($container);
|
||||
FlasherContainer::create('invalid_service');
|
||||
}
|
||||
|
||||
public function testCreateThrowsExceptionForInvalidServiceType(): void
|
||||
{
|
||||
$invalidService = new \stdClass();
|
||||
$container = $this->createMock(ContainerInterface::class);
|
||||
$container->method('has')->willReturn(true);
|
||||
$container->method('get')->willReturn($invalidService);
|
||||
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
$this->expectExceptionMessage(sprintf('Expected an instance of "%s" or "%s", got "%s".', FlasherInterface::class, NotificationFactoryInterface::class, get_debug_type($invalidService)));
|
||||
|
||||
FlasherContainer::from($container);
|
||||
FlasherContainer::create('invalid_service');
|
||||
}
|
||||
|
||||
public function testCreateThrowsExceptionIfNotInitialized(): void
|
||||
{
|
||||
$this->expectException(\LogicException::class);
|
||||
$this->expectExceptionMessage('FlasherContainer has not been initialized. Please initialize it by calling FlasherContainer::from(ContainerInterface $container).');
|
||||
|
||||
// Ensure that FlasherContainer is not initialized
|
||||
FlasherContainer::reset();
|
||||
|
||||
FlasherContainer::create('flasher');
|
||||
}
|
||||
|
||||
@@ -1,40 +1,34 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the PHPFlasher package.
|
||||
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\EventDispatcher\Event;
|
||||
|
||||
use Flasher\Prime\EventDispatcher\Event\FilterEvent;
|
||||
use Flasher\Prime\Filter\Filter;
|
||||
use Flasher\Prime\Notification\Envelope;
|
||||
use Flasher\Prime\Notification\Notification;
|
||||
use Flasher\Tests\Prime\TestCase;
|
||||
use Flasher\Prime\Storage\Filter\Filter;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class FilterEventTest extends TestCase
|
||||
final class FilterEventTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testFilterEvent()
|
||||
public function testFilterEvent(): void
|
||||
{
|
||||
$envelopes = array(
|
||||
$filter = new Filter();
|
||||
|
||||
$envelopes = [
|
||||
new Envelope(new Notification()),
|
||||
new Envelope(new Notification()),
|
||||
new Envelope(new Notification()),
|
||||
new Envelope(new Notification()),
|
||||
);
|
||||
];
|
||||
|
||||
$event = new FilterEvent($envelopes, array('limit' => 2));
|
||||
$criteria = ['limit' => 2];
|
||||
|
||||
$this->assertInstanceOf('Flasher\Prime\Filter\Filter', $event->getFilter());
|
||||
$this->assertEquals(array($envelopes[0], $envelopes[1]), $event->getEnvelopes());
|
||||
$event = new FilterEvent($filter, $envelopes, $criteria);
|
||||
|
||||
$filter = new Filter($envelopes, array());
|
||||
$event->setFilter($filter);
|
||||
|
||||
$this->assertEquals($envelopes, $event->getEnvelopes());
|
||||
$this->assertSame($filter, $event->getFilter());
|
||||
$this->assertSame($envelopes, $event->getEnvelopes());
|
||||
$this->assertSame($criteria, $event->getCriteria());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,38 +1,32 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the PHPFlasher package.
|
||||
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\EventDispatcher\Event;
|
||||
|
||||
use Flasher\Prime\EventDispatcher\Event\PersistEvent;
|
||||
use Flasher\Prime\Notification\Envelope;
|
||||
use Flasher\Prime\Notification\Notification;
|
||||
use Flasher\Tests\Prime\TestCase;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class PersistEventTest extends TestCase
|
||||
final class PersistEventTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testPersistEvent()
|
||||
public function testPersistEvent(): void
|
||||
{
|
||||
$envelopes = array(
|
||||
$envelopes = [
|
||||
new Envelope(new Notification()),
|
||||
new Envelope(new Notification()),
|
||||
new Envelope(new Notification()),
|
||||
new Envelope(new Notification()),
|
||||
);
|
||||
];
|
||||
|
||||
$event = new PersistEvent($envelopes);
|
||||
|
||||
$this->assertEquals($envelopes, $event->getEnvelopes());
|
||||
|
||||
$envelopes = array(
|
||||
$envelopes = [
|
||||
new Envelope(new Notification()),
|
||||
);
|
||||
];
|
||||
$event->setEnvelopes($envelopes);
|
||||
|
||||
$this->assertEquals($envelopes, $event->getEnvelopes());
|
||||
|
||||
@@ -1,30 +1,24 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the PHPFlasher package.
|
||||
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\EventDispatcher\Event;
|
||||
|
||||
use Flasher\Prime\EventDispatcher\Event\PostPersistEvent;
|
||||
use Flasher\Prime\Notification\Envelope;
|
||||
use Flasher\Prime\Notification\Notification;
|
||||
use Flasher\Tests\Prime\TestCase;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class PostPersistEventTest extends TestCase
|
||||
final class PostPersistEventTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testPostPersistEvent()
|
||||
public function testPostPersistEvent(): void
|
||||
{
|
||||
$envelopes = array(
|
||||
$envelopes = [
|
||||
new Envelope(new Notification()),
|
||||
new Envelope(new Notification()),
|
||||
new Envelope(new Notification()),
|
||||
new Envelope(new Notification()),
|
||||
);
|
||||
];
|
||||
|
||||
$event = new PostPersistEvent($envelopes);
|
||||
|
||||
|
||||
@@ -1,33 +1,27 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the PHPFlasher package.
|
||||
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\EventDispatcher\Event;
|
||||
|
||||
use Flasher\Prime\EventDispatcher\Event\PostRemoveEvent;
|
||||
use Flasher\Prime\Notification\Envelope;
|
||||
use Flasher\Prime\Notification\Notification;
|
||||
use Flasher\Tests\Prime\TestCase;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class PostRemoveEventTest extends TestCase
|
||||
final class PostRemoveEventTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testPostRemoveEvent()
|
||||
public function testPostRemoveEvent(): void
|
||||
{
|
||||
$envelopesToRemove = array(
|
||||
$envelopesToRemove = [
|
||||
new Envelope(new Notification()),
|
||||
new Envelope(new Notification()),
|
||||
);
|
||||
];
|
||||
|
||||
$envelopesToKeep = array(
|
||||
$envelopesToKeep = [
|
||||
new Envelope(new Notification()),
|
||||
new Envelope(new Notification()),
|
||||
);
|
||||
];
|
||||
|
||||
$event = new PostRemoveEvent($envelopesToRemove, $envelopesToKeep);
|
||||
|
||||
|
||||
@@ -1,30 +1,24 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the PHPFlasher package.
|
||||
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\EventDispatcher\Event;
|
||||
|
||||
use Flasher\Prime\EventDispatcher\Event\PostUpdateEvent;
|
||||
use Flasher\Prime\Notification\Envelope;
|
||||
use Flasher\Prime\Notification\Notification;
|
||||
use Flasher\Tests\Prime\TestCase;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class PostUpdateEventTest extends TestCase
|
||||
final class PostUpdateEventTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testPostUpdateEvent()
|
||||
public function testPostUpdateEvent(): void
|
||||
{
|
||||
$envelopes = array(
|
||||
$envelopes = [
|
||||
new Envelope(new Notification()),
|
||||
new Envelope(new Notification()),
|
||||
new Envelope(new Notification()),
|
||||
new Envelope(new Notification()),
|
||||
);
|
||||
];
|
||||
|
||||
$event = new PostUpdateEvent($envelopes);
|
||||
|
||||
|
||||
@@ -1,34 +1,28 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the PHPFlasher package.
|
||||
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\EventDispatcher\Event;
|
||||
|
||||
use Flasher\Prime\EventDispatcher\Event\PresentationEvent;
|
||||
use Flasher\Prime\Notification\Envelope;
|
||||
use Flasher\Prime\Notification\Notification;
|
||||
use Flasher\Tests\Prime\TestCase;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class PresentationEventTest extends TestCase
|
||||
final class PresentationEventTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testPresentationEvent()
|
||||
public function testPresentationEvent(): void
|
||||
{
|
||||
$envelopes = array(
|
||||
$envelopes = [
|
||||
new Envelope(new Notification()),
|
||||
new Envelope(new Notification()),
|
||||
new Envelope(new Notification()),
|
||||
new Envelope(new Notification()),
|
||||
);
|
||||
];
|
||||
|
||||
$context = array(
|
||||
$context = [
|
||||
'livewire' => true,
|
||||
);
|
||||
];
|
||||
|
||||
$event = new PresentationEvent($envelopes, $context);
|
||||
|
||||
|
||||
@@ -1,40 +1,34 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the PHPFlasher package.
|
||||
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\EventDispatcher\Event;
|
||||
|
||||
use Flasher\Prime\EventDispatcher\Event\RemoveEvent;
|
||||
use Flasher\Prime\Notification\Envelope;
|
||||
use Flasher\Prime\Notification\Notification;
|
||||
use Flasher\Tests\Prime\TestCase;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class RemoveEventTest extends TestCase
|
||||
final class RemoveEventTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testRemoveEvent()
|
||||
public function testRemoveEvent(): void
|
||||
{
|
||||
$envelopes = array(
|
||||
$envelopes = [
|
||||
new Envelope(new Notification()),
|
||||
new Envelope(new Notification()),
|
||||
new Envelope(new Notification()),
|
||||
new Envelope(new Notification()),
|
||||
);
|
||||
];
|
||||
|
||||
$event = new RemoveEvent($envelopes);
|
||||
|
||||
$this->assertEquals($envelopes, $event->getEnvelopesToRemove());
|
||||
$this->assertEquals(array(), $event->getEnvelopesToKeep());
|
||||
$this->assertSame([], $event->getEnvelopesToKeep());
|
||||
|
||||
$event->setEnvelopesToKeep(array($envelopes[0], $envelopes[1]));
|
||||
$event->setEnvelopesToRemove(array($envelopes[2], $envelopes[3]));
|
||||
$event->setEnvelopesToKeep([$envelopes[0], $envelopes[1]]);
|
||||
$event->setEnvelopesToRemove([$envelopes[2], $envelopes[3]]);
|
||||
|
||||
$this->assertEquals(array($envelopes[2], $envelopes[3]), $event->getEnvelopesToRemove());
|
||||
$this->assertEquals(array($envelopes[0], $envelopes[1]), $event->getEnvelopesToKeep());
|
||||
$this->assertEquals([$envelopes[2], $envelopes[3]], $event->getEnvelopesToRemove());
|
||||
$this->assertEquals([$envelopes[0], $envelopes[1]], $event->getEnvelopesToKeep());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,29 +1,23 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the PHPFlasher package.
|
||||
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\EventDispatcher\Event;
|
||||
|
||||
use Flasher\Prime\EventDispatcher\Event\ResponseEvent;
|
||||
use Flasher\Tests\Prime\TestCase;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class ResponseEventTest extends TestCase
|
||||
final class ResponseEventTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testResponseEvent()
|
||||
public function testResponseEvent(): void
|
||||
{
|
||||
$event = new ResponseEvent('{"foo": "bar"}', 'json');
|
||||
|
||||
$this->assertEquals('{"foo": "bar"}', $event->getResponse());
|
||||
$this->assertEquals('json', $event->getPresenter());
|
||||
$this->assertSame('{"foo": "bar"}', $event->getResponse());
|
||||
$this->assertSame('json', $event->getPresenter());
|
||||
|
||||
$event->setResponse('{"foo": "baz"}');
|
||||
|
||||
$this->assertEquals('{"foo": "baz"}', $event->getResponse());
|
||||
$this->assertSame('{"foo": "baz"}', $event->getResponse());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,38 +1,32 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the PHPFlasher package.
|
||||
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\EventDispatcher\Event;
|
||||
|
||||
use Flasher\Prime\EventDispatcher\Event\UpdateEvent;
|
||||
use Flasher\Prime\Notification\Envelope;
|
||||
use Flasher\Prime\Notification\Notification;
|
||||
use Flasher\Tests\Prime\TestCase;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class UpdateEventTest extends TestCase
|
||||
final class UpdateEventTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testUpdateEvent()
|
||||
public function testUpdateEvent(): void
|
||||
{
|
||||
$envelopes = array(
|
||||
$envelopes = [
|
||||
new Envelope(new Notification()),
|
||||
new Envelope(new Notification()),
|
||||
new Envelope(new Notification()),
|
||||
new Envelope(new Notification()),
|
||||
);
|
||||
];
|
||||
|
||||
$event = new UpdateEvent($envelopes);
|
||||
|
||||
$this->assertEquals($envelopes, $event->getEnvelopes());
|
||||
|
||||
$envelopes = array(
|
||||
$envelopes = [
|
||||
new Envelope(new Notification()),
|
||||
);
|
||||
];
|
||||
$event->setEnvelopes($envelopes);
|
||||
|
||||
$this->assertEquals($envelopes, $event->getEnvelopes());
|
||||
|
||||
@@ -1,192 +1,113 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the PHPFlasher package.
|
||||
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\EventDispatcher;
|
||||
|
||||
use Flasher\Prime\EventDispatcher\Event\StoppableEventInterface;
|
||||
use Flasher\Prime\EventDispatcher\EventDispatcher;
|
||||
use Flasher\Prime\EventDispatcher\EventListener\EventSubscriberInterface;
|
||||
use Flasher\Tests\Prime\TestCase;
|
||||
use Flasher\Prime\EventDispatcher\EventListener\EventListenerInterface;
|
||||
use Flasher\Tests\Prime\Fixtures\EventDispatcher\Event\InvokeableEvent;
|
||||
use Flasher\Tests\Prime\Fixtures\EventDispatcher\Event\StoppableEvent;
|
||||
use Flasher\Tests\Prime\Fixtures\EventDispatcher\EventListener\InvokeableEventListener;
|
||||
use Flasher\Tests\Prime\Fixtures\EventDispatcher\EventListener\NonCallableListener;
|
||||
use Flasher\Tests\Prime\Fixtures\EventDispatcher\EventListener\StoppableEventListener;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class EventDispatcherTest extends TestCase
|
||||
final class EventDispatcherTest extends TestCase
|
||||
{
|
||||
// Some pseudo events
|
||||
const preFoo = 'pre.foo';
|
||||
private EventDispatcher $dispatcher;
|
||||
|
||||
const postFoo = 'post.foo';
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->dispatcher = new EventDispatcher();
|
||||
}
|
||||
|
||||
const preBar = 'pre.bar';
|
||||
|
||||
const postBar = 'post.bar';
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testInitialState()
|
||||
public function testInitialState(): void
|
||||
{
|
||||
$dispatcher = new EventDispatcher();
|
||||
$this->assertEquals(array(), $dispatcher->getListeners('fake_event'));
|
||||
$this->assertSame([], $dispatcher->getListeners('fake_event'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testAddListener()
|
||||
public function testAddAndRetrieveListeners(): void
|
||||
{
|
||||
$dispatcher = new EventDispatcher();
|
||||
$listener = new TestEventListener();
|
||||
$listener = $this->createMock(EventListenerInterface::class);
|
||||
$listener->method('getSubscribedEvents')
|
||||
->willReturn(['some_event']);
|
||||
|
||||
$dispatcher->addListener('pre.foo', array($listener, 'preFoo'));
|
||||
$dispatcher->addListener('post.foo', array($listener, 'postFoo'));
|
||||
$this->assertCount(1, $dispatcher->getListeners(self::preFoo));
|
||||
$this->assertCount(1, $dispatcher->getListeners(self::postFoo));
|
||||
$this->dispatcher->addListener($listener);
|
||||
|
||||
$listeners = $this->dispatcher->getListeners('some_event');
|
||||
$this->assertCount(1, $listeners);
|
||||
$this->assertSame($listener, $listeners[0]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testDispatch()
|
||||
public function testDispatchCallsListeners(): void
|
||||
{
|
||||
$dispatcher = new EventDispatcher();
|
||||
$listener = new TestEventListener();
|
||||
$event = new InvokeableEvent();
|
||||
$listener = new InvokeableEventListener();
|
||||
|
||||
$event = new Event();
|
||||
$dispatcher->addListener('Flasher\Tests\Prime\EventDispatcher\Event', array($listener, 'preFoo'));
|
||||
$dispatcher->addListener('NotFoundEvent', array($listener, 'postFoo'));
|
||||
$this->dispatcher->addListener($listener);
|
||||
$this->dispatcher->dispatch($event);
|
||||
|
||||
$return = $dispatcher->dispatch($event);
|
||||
|
||||
$this->assertTrue($listener->preFooInvoked);
|
||||
$this->assertFalse($listener->postFooInvoked);
|
||||
|
||||
$this->assertInstanceOf('Flasher\Tests\Prime\EventDispatcher\Event', $return);
|
||||
$this->assertEquals($event, $return);
|
||||
$this->assertTrue($event->isInvoked());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testDispatchForClosure()
|
||||
public function testDispatchWithStoppableEvent(): void
|
||||
{
|
||||
$dispatcher = new EventDispatcher();
|
||||
$event = new StoppableEvent();
|
||||
$listener = new StoppableEventListener();
|
||||
|
||||
$invoked = 0;
|
||||
$listener = function () use (&$invoked) {
|
||||
++$invoked;
|
||||
};
|
||||
$this->dispatcher->addListener($listener);
|
||||
$this->dispatcher->dispatch($event);
|
||||
|
||||
$event = new Event();
|
||||
$dispatcher->addListener('Flasher\Tests\Prime\EventDispatcher\Event', $listener);
|
||||
$dispatcher->addListener('AnotherEvent', $listener);
|
||||
$dispatcher->dispatch($event);
|
||||
$this->assertEquals(1, $invoked);
|
||||
$this->assertTrue($event->isPropagationStopped());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testStopEventPropagation()
|
||||
public function testDispatchWithNonCallableListener(): void
|
||||
{
|
||||
$dispatcher = new EventDispatcher();
|
||||
$listener = new TestEventListener();
|
||||
$event = new class() {};
|
||||
$eventName = $event::class;
|
||||
|
||||
$otherListener = new TestEventListener();
|
||||
$listener = new NonCallableListener($eventName);
|
||||
|
||||
$event = new Event();
|
||||
// postFoo() stops the propagation, so only one listener should
|
||||
// be executed
|
||||
// Manually set priority to enforce $listener to be called first
|
||||
$dispatcher->addListener('Flasher\Tests\Prime\EventDispatcher\Event', array($listener, 'postFoo'), 10);
|
||||
$dispatcher->addListener('Flasher\Tests\Prime\EventDispatcher\Event', array($otherListener, 'preFoo'));
|
||||
$dispatcher->dispatch($event);
|
||||
$this->assertTrue($listener->postFooInvoked);
|
||||
$this->assertFalse($otherListener->postFooInvoked);
|
||||
}
|
||||
}
|
||||
|
||||
class Event implements StoppableEventInterface
|
||||
{
|
||||
private $propagationStopped = false;
|
||||
|
||||
private $data;
|
||||
|
||||
public function __construct($data = null)
|
||||
{
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
public function isPropagationStopped()
|
||||
{
|
||||
return $this->propagationStopped;
|
||||
}
|
||||
|
||||
public function stopPropagation()
|
||||
{
|
||||
$this->propagationStopped = true;
|
||||
}
|
||||
}
|
||||
|
||||
class CallableClass
|
||||
{
|
||||
public function __invoke()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
class TestEventListener
|
||||
{
|
||||
public $preFooInvoked = false;
|
||||
|
||||
public $postFooInvoked = false;
|
||||
|
||||
public function preFoo(Event $e)
|
||||
{
|
||||
$this->preFooInvoked = true;
|
||||
}
|
||||
|
||||
public function postFoo(Event $e)
|
||||
{
|
||||
$this->postFooInvoked = true;
|
||||
|
||||
$e->stopPropagation();
|
||||
}
|
||||
}
|
||||
|
||||
class TestEventSubscriber implements EventSubscriberInterface
|
||||
{
|
||||
public static function getSubscribedEvents()
|
||||
{
|
||||
return array(
|
||||
'pre.foo' => 'preFoo',
|
||||
'post.foo' => 'postFoo',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class TestEventSubscriberWithPriorities implements EventSubscriberInterface
|
||||
{
|
||||
public static function getSubscribedEvents()
|
||||
{
|
||||
return array(
|
||||
'pre.foo' => array('preFoo', 10),
|
||||
'post.foo' => array('postFoo'),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class TestEventSubscriberWithMultipleListeners implements EventSubscriberInterface
|
||||
{
|
||||
public static function getSubscribedEvents()
|
||||
{
|
||||
return array(
|
||||
'pre.foo' => array(
|
||||
array('preFoo1'),
|
||||
array('preFoo2', 10),
|
||||
),
|
||||
);
|
||||
$this->dispatcher->addListener($listener);
|
||||
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
$this->dispatcher->dispatch($event);
|
||||
}
|
||||
|
||||
public function testMultipleListenersForSingleEvent(): void
|
||||
{
|
||||
$event = new InvokeableEvent();
|
||||
$listener1 = new InvokeableEventListener();
|
||||
$listener2 = new InvokeableEventListener();
|
||||
|
||||
$this->dispatcher->addListener($listener1);
|
||||
$this->dispatcher->addListener($listener2);
|
||||
$this->dispatcher->dispatch($event);
|
||||
|
||||
$this->assertSame(2, $event->getInvokeCount());
|
||||
}
|
||||
|
||||
public function testMultipleListenersForStoppableEvent(): void
|
||||
{
|
||||
$event = new InvokeableEvent();
|
||||
$listener1 = new StoppableEventListener();
|
||||
$listener2 = new InvokeableEventListener();
|
||||
|
||||
$this->dispatcher->addListener($listener1);
|
||||
$this->dispatcher->addListener($listener2);
|
||||
$this->dispatcher->dispatch($event);
|
||||
|
||||
$this->assertTrue($event->isInvoked());
|
||||
$this->assertSame(1, $event->getInvokeCount());
|
||||
}
|
||||
|
||||
public function testDispatchEventWithNoListeners(): void
|
||||
{
|
||||
$event = new InvokeableEvent();
|
||||
$this->dispatcher->dispatch($event);
|
||||
|
||||
$this->assertFalse($event->isInvoked());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the PHPFlasher package.
|
||||
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\EventDispatcher\EventListener;
|
||||
|
||||
@@ -14,31 +11,30 @@ use Flasher\Prime\Notification\Envelope;
|
||||
use Flasher\Prime\Notification\Notification;
|
||||
use Flasher\Prime\Stamp\UnlessStamp;
|
||||
use Flasher\Prime\Stamp\WhenStamp;
|
||||
use Flasher\Tests\Prime\TestCase;
|
||||
use Flasher\Tests\Prime\Helper\ObjectInvader;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class AddToStorageListenerTest extends TestCase
|
||||
final class AddToStorageListenerTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testAddToStorageListener()
|
||||
public function testAddToStorageListener(): void
|
||||
{
|
||||
$eventDispatcher = new EventDispatcher();
|
||||
$this->setProperty($eventDispatcher, 'listeners', array());
|
||||
|
||||
ObjectInvader::from($eventDispatcher)->set('listeners', []);
|
||||
|
||||
$listener = new AddToStorageListener();
|
||||
$eventDispatcher->addSubscriber($listener);
|
||||
$eventDispatcher->addListener($listener);
|
||||
|
||||
$envelopes = array(
|
||||
$envelopes = [
|
||||
new Envelope(new Notification(), new WhenStamp(false)),
|
||||
new Envelope(new Notification()),
|
||||
new Envelope(new Notification(), new UnlessStamp(true)),
|
||||
new Envelope(new Notification()),
|
||||
);
|
||||
$event = new PersistEvent($envelopes);
|
||||
];
|
||||
|
||||
$event = new PersistEvent($envelopes);
|
||||
$eventDispatcher->dispatch($event);
|
||||
|
||||
$this->assertEquals(array($envelopes[1], $envelopes[3]), $event->getEnvelopes());
|
||||
$this->assertCount(2, $event->getEnvelopes());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,44 +1,40 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the PHPFlasher package.
|
||||
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\EventDispatcher\EventListener;
|
||||
|
||||
use Flasher\Prime\EventDispatcher\Event\PersistEvent;
|
||||
use Flasher\Prime\EventDispatcher\EventDispatcher;
|
||||
use Flasher\Prime\EventDispatcher\EventListener\PresetListener;
|
||||
use Flasher\Prime\EventDispatcher\EventListener\ApplyPresetListener;
|
||||
use Flasher\Prime\Exception\PresetNotFoundException;
|
||||
use Flasher\Prime\Notification\Envelope;
|
||||
use Flasher\Prime\Notification\Notification;
|
||||
use Flasher\Prime\Stamp\PresetStamp;
|
||||
use Flasher\Tests\Prime\TestCase;
|
||||
use Flasher\Tests\Prime\Helper\ObjectInvader;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class PresetListenerTest extends TestCase
|
||||
final class PresetListenerTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testPresetListener()
|
||||
public function testPresetListener(): void
|
||||
{
|
||||
$eventDispatcher = new EventDispatcher();
|
||||
$this->setProperty($eventDispatcher, 'listeners', array());
|
||||
ObjectInvader::from($eventDispatcher)->set('listeners', []);
|
||||
|
||||
$listener = new PresetListener(array(
|
||||
'entity_saved' => array(
|
||||
$listener = new ApplyPresetListener([
|
||||
'entity_saved' => [
|
||||
'type' => 'success',
|
||||
'title' => 'PHPFlasher',
|
||||
'message' => 'success message',
|
||||
'options' => array('timeout' => 2500),
|
||||
),
|
||||
));
|
||||
$eventDispatcher->addSubscriber($listener);
|
||||
'options' => ['timeout' => 2500],
|
||||
],
|
||||
]);
|
||||
$eventDispatcher->addListener($listener);
|
||||
|
||||
$envelopes = array(
|
||||
$envelopes = [
|
||||
new Envelope(new Notification(), new PresetStamp('entity_saved')),
|
||||
new Envelope(new Notification()),
|
||||
);
|
||||
];
|
||||
$event = new PersistEvent($envelopes);
|
||||
|
||||
$eventDispatcher->dispatch($event);
|
||||
@@ -46,39 +42,38 @@ class PresetListenerTest extends TestCase
|
||||
$envelopes = $event->getEnvelopes();
|
||||
|
||||
$this->assertCount(2, $envelopes);
|
||||
$this->assertEquals('success', $envelopes[0]->getType());
|
||||
$this->assertEquals('PHPFlasher', $envelopes[0]->getTitle());
|
||||
$this->assertEquals('success message', $envelopes[0]->getMessage());
|
||||
$this->assertEquals(array('timeout' => 2500), $envelopes[0]->getOptions());
|
||||
$this->assertSame('success', $envelopes[0]->getType());
|
||||
$this->assertSame('PHPFlasher', $envelopes[0]->getTitle());
|
||||
$this->assertSame('success message', $envelopes[0]->getMessage());
|
||||
$this->assertSame(['timeout' => 2500], $envelopes[0]->getOptions());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testThrowExceptionIfPresetNotFound()
|
||||
public function testThrowExceptionIfPresetNotFound(): void
|
||||
{
|
||||
$this->setExpectedException(
|
||||
'Flasher\Prime\Exception\PresetNotFoundException',
|
||||
'Preset "entity_deleted" not found, did you forget to register it? Available presets: entity_saved'
|
||||
$this->expectException(
|
||||
PresetNotFoundException::class
|
||||
);
|
||||
$this->expectExceptionMessage(
|
||||
'Preset "entity_deleted" not found, did you forget to register it? Available presets: "entity_saved"'
|
||||
);
|
||||
|
||||
$eventDispatcher = new EventDispatcher();
|
||||
$this->setProperty($eventDispatcher, 'listeners', array());
|
||||
ObjectInvader::from($eventDispatcher)->set('listeners', []);
|
||||
|
||||
$listener = new PresetListener(array(
|
||||
'entity_saved' => array(
|
||||
$listener = new ApplyPresetListener([
|
||||
'entity_saved' => [
|
||||
'type' => 'success',
|
||||
'title' => 'PHPFlasher',
|
||||
'message' => 'success message',
|
||||
'options' => array('timeout' => 2500),
|
||||
),
|
||||
));
|
||||
$eventDispatcher->addSubscriber($listener);
|
||||
'options' => ['timeout' => 2500],
|
||||
],
|
||||
]);
|
||||
$eventDispatcher->addListener($listener);
|
||||
|
||||
$envelopes = array(
|
||||
$envelopes = [
|
||||
new Envelope(new Notification(), new PresetStamp('entity_deleted')),
|
||||
new Envelope(new Notification()),
|
||||
);
|
||||
];
|
||||
$event = new PersistEvent($envelopes);
|
||||
|
||||
$eventDispatcher->dispatch($event);
|
||||
|
||||
@@ -1,44 +1,39 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the PHPFlasher package.
|
||||
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\EventDispatcher\EventListener;
|
||||
|
||||
use Flasher\Prime\EventDispatcher\Event\RemoveEvent;
|
||||
use Flasher\Prime\EventDispatcher\EventDispatcher;
|
||||
use Flasher\Prime\EventDispatcher\EventListener\RemoveListener;
|
||||
use Flasher\Prime\EventDispatcher\EventListener\EnvelopeRemovalListener;
|
||||
use Flasher\Prime\Notification\Envelope;
|
||||
use Flasher\Prime\Notification\Notification;
|
||||
use Flasher\Prime\Stamp\HopsStamp;
|
||||
use Flasher\Tests\Prime\TestCase;
|
||||
use Flasher\Tests\Prime\Helper\ObjectInvader;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class RemoveListenerTest extends TestCase
|
||||
final class RemoveListenerTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testRemoveListener()
|
||||
public function testRemoveListener(): void
|
||||
{
|
||||
$eventDispatcher = new EventDispatcher();
|
||||
$this->setProperty($eventDispatcher, 'listeners', array());
|
||||
ObjectInvader::from($eventDispatcher)->set('listeners', []);
|
||||
|
||||
$listener = new RemoveListener();
|
||||
$eventDispatcher->addSubscriber($listener);
|
||||
$listener = new EnvelopeRemovalListener();
|
||||
$eventDispatcher->addListener($listener);
|
||||
|
||||
$envelopes = array(
|
||||
new Envelope(new Notification()),
|
||||
new Envelope(new Notification(), new HopsStamp(2)),
|
||||
new Envelope(new Notification(), new HopsStamp(1)),
|
||||
new Envelope(new Notification(), new HopsStamp(3)),
|
||||
);
|
||||
$envelopes = [
|
||||
new Envelope(new Notification()),
|
||||
new Envelope(new Notification(), new HopsStamp(2)),
|
||||
new Envelope(new Notification(), new HopsStamp(1)),
|
||||
new Envelope(new Notification(), new HopsStamp(3)),
|
||||
];
|
||||
$event = new RemoveEvent($envelopes);
|
||||
|
||||
$eventDispatcher->dispatch($event);
|
||||
|
||||
$this->assertEquals(array($envelopes[0], $envelopes[2]), $event->getEnvelopesToRemove());
|
||||
$this->assertEquals(array($envelopes[1], $envelopes[3]), $event->getEnvelopesToKeep());
|
||||
$this->assertEquals([$envelopes[0], $envelopes[2]], $event->getEnvelopesToRemove());
|
||||
$this->assertEquals([$envelopes[1], $envelopes[3]], $event->getEnvelopesToKeep());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,45 +1,45 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the PHPFlasher package.
|
||||
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\EventDispatcher\EventListener;
|
||||
|
||||
use Flasher\Prime\EventDispatcher\Event\PersistEvent;
|
||||
use Flasher\Prime\EventDispatcher\EventDispatcher;
|
||||
use Flasher\Prime\EventDispatcher\EventListener\StampsListener;
|
||||
use Flasher\Prime\EventDispatcher\EventListener\AttachDefaultStampsListener;
|
||||
use Flasher\Prime\Notification\Envelope;
|
||||
use Flasher\Prime\Notification\Notification;
|
||||
use Flasher\Tests\Prime\TestCase;
|
||||
use Flasher\Prime\Stamp\CreatedAtStamp;
|
||||
use Flasher\Prime\Stamp\DelayStamp;
|
||||
use Flasher\Prime\Stamp\HopsStamp;
|
||||
use Flasher\Prime\Stamp\IdStamp;
|
||||
use Flasher\Prime\Stamp\PriorityStamp;
|
||||
use Flasher\Tests\Prime\Helper\ObjectInvader;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class StampsListenerTest extends TestCase
|
||||
final class StampsListenerTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testStampsListener()
|
||||
public function testStampsListener(): void
|
||||
{
|
||||
$eventDispatcher = new EventDispatcher();
|
||||
$this->setProperty($eventDispatcher, 'listeners', array());
|
||||
ObjectInvader::from($eventDispatcher)->set('listeners', []);
|
||||
|
||||
$listener = new StampsListener();
|
||||
$eventDispatcher->addSubscriber($listener);
|
||||
$listener = new AttachDefaultStampsListener();
|
||||
$eventDispatcher->addListener($listener);
|
||||
|
||||
$envelopes = array(
|
||||
$envelopes = [
|
||||
new Envelope(new Notification()),
|
||||
);
|
||||
];
|
||||
$event = new PersistEvent($envelopes);
|
||||
|
||||
$eventDispatcher->dispatch($event);
|
||||
|
||||
$envelopes = $event->getEnvelopes();
|
||||
|
||||
$this->assertInstanceOf('Flasher\Prime\Stamp\CreatedAtStamp', $envelopes[0]->get('Flasher\Prime\Stamp\CreatedAtStamp'));
|
||||
$this->assertInstanceOf('Flasher\Prime\Stamp\UuidStamp', $envelopes[0]->get('Flasher\Prime\Stamp\UuidStamp'));
|
||||
$this->assertInstanceOf('Flasher\Prime\Stamp\DelayStamp', $envelopes[0]->get('Flasher\Prime\Stamp\DelayStamp'));
|
||||
$this->assertInstanceOf('Flasher\Prime\Stamp\HopsStamp', $envelopes[0]->get('Flasher\Prime\Stamp\HopsStamp'));
|
||||
$this->assertInstanceOf('Flasher\Prime\Stamp\PriorityStamp', $envelopes[0]->get('Flasher\Prime\Stamp\PriorityStamp'));
|
||||
$this->assertInstanceOf(CreatedAtStamp::class, $envelopes[0]->get(CreatedAtStamp::class));
|
||||
$this->assertInstanceOf(IdStamp::class, $envelopes[0]->get(IdStamp::class));
|
||||
$this->assertInstanceOf(DelayStamp::class, $envelopes[0]->get(DelayStamp::class));
|
||||
$this->assertInstanceOf(HopsStamp::class, $envelopes[0]->get(HopsStamp::class));
|
||||
$this->assertInstanceOf(PriorityStamp::class, $envelopes[0]->get(PriorityStamp::class));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the PHPFlasher package.
|
||||
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\EventDispatcher\EventListener;
|
||||
|
||||
@@ -15,71 +12,66 @@ use Flasher\Prime\Notification\Notification;
|
||||
use Flasher\Prime\Stamp\PresetStamp;
|
||||
use Flasher\Prime\Stamp\TranslationStamp;
|
||||
use Flasher\Prime\Translation\EchoTranslator;
|
||||
use Flasher\Tests\Prime\TestCase;
|
||||
use Flasher\Tests\Prime\Helper\ObjectInvader;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class TranslationListenerTest extends TestCase
|
||||
final class TranslationListenerTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testTranslationListenerWithAutoTranslateEnabled()
|
||||
public function testTranslationListenerWithAutoTranslateEnabled(): void
|
||||
{
|
||||
$eventDispatcher = new EventDispatcher();
|
||||
$this->setProperty($eventDispatcher, 'listeners', array());
|
||||
ObjectInvader::from($eventDispatcher)->set('listeners', []);
|
||||
|
||||
$listener = new TranslationListener(new EchoTranslator(), true);
|
||||
$eventDispatcher->addSubscriber($listener);
|
||||
$listener = new TranslationListener(new EchoTranslator());
|
||||
$eventDispatcher->addListener($listener);
|
||||
|
||||
$notification = new Notification();
|
||||
$notification->setTitle('PHPFlasher');
|
||||
$notification->setMessage('success message');
|
||||
|
||||
$envelopes = array(
|
||||
$envelopes = [
|
||||
new Envelope($notification),
|
||||
new Envelope(new Notification()),
|
||||
new Envelope(new Notification()),
|
||||
);
|
||||
];
|
||||
|
||||
$envelopes[0]->withStamp(new TranslationStamp(array('resource' => 'resource'), 'ar'));
|
||||
$envelopes[0]->withStamp(new PresetStamp('entity_saved', array('resource' => 'resource')));
|
||||
$envelopes[0]->withStamp(new TranslationStamp(['resource' => 'resource'], 'ar'));
|
||||
$envelopes[0]->withStamp(new PresetStamp('entity_saved', ['resource' => 'resource']));
|
||||
|
||||
$envelopes[1]->withStamp(new TranslationStamp(array('resource' => 'resource'), 'ar'));
|
||||
$envelopes[1]->withStamp(new PresetStamp('entity_saved', array('resource' => 'resource')));
|
||||
$envelopes[1]->withStamp(new TranslationStamp(['resource' => 'resource'], 'ar'));
|
||||
$envelopes[1]->withStamp(new PresetStamp('entity_saved', ['resource' => 'resource']));
|
||||
|
||||
$event = new PresentationEvent($envelopes, array());
|
||||
$event = new PresentationEvent($envelopes, []);
|
||||
$eventDispatcher->dispatch($event);
|
||||
|
||||
$this->assertEquals($envelopes, $event->getEnvelopes());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testTranslationListenerWithAutoTranslateDisabled()
|
||||
public function testTranslationListenerWithAutoTranslateDisabled(): void
|
||||
{
|
||||
$eventDispatcher = new EventDispatcher();
|
||||
$this->setProperty($eventDispatcher, 'listeners', array());
|
||||
ObjectInvader::from($eventDispatcher)->set('listeners', []);
|
||||
|
||||
$listener = new TranslationListener(new EchoTranslator(), false);
|
||||
$eventDispatcher->addSubscriber($listener);
|
||||
$listener = new TranslationListener(new EchoTranslator());
|
||||
$eventDispatcher->addListener($listener);
|
||||
|
||||
$notification = new Notification();
|
||||
$notification->setTitle('PHPFlasher');
|
||||
$notification->setMessage('success message');
|
||||
|
||||
$envelopes = array(
|
||||
$envelopes = [
|
||||
new Envelope($notification),
|
||||
new Envelope(new Notification()),
|
||||
new Envelope(new Notification()),
|
||||
);
|
||||
];
|
||||
|
||||
$envelopes[0]->withStamp(new TranslationStamp(array('resource' => 'resource'), 'ar'));
|
||||
$envelopes[0]->withStamp(new PresetStamp('entity_saved', array('resource' => 'resource')));
|
||||
$envelopes[0]->withStamp(new TranslationStamp(['resource' => 'resource'], 'ar'));
|
||||
$envelopes[0]->withStamp(new PresetStamp('entity_saved', ['resource' => 'resource']));
|
||||
|
||||
$envelopes[1]->withStamp(new TranslationStamp(array('resource' => 'resource'), 'ar'));
|
||||
$envelopes[1]->withStamp(new PresetStamp('entity_saved', array('resource' => 'resource')));
|
||||
$envelopes[1]->withStamp(new TranslationStamp(['resource' => 'resource'], 'ar'));
|
||||
$envelopes[1]->withStamp(new PresetStamp('entity_saved', ['resource' => 'resource']));
|
||||
|
||||
$event = new PresentationEvent($envelopes, array());
|
||||
$event = new PresentationEvent($envelopes, []);
|
||||
$eventDispatcher->dispatch($event);
|
||||
|
||||
$this->assertEquals($envelopes, $event->getEnvelopes());
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\Factory;
|
||||
|
||||
use Flasher\Prime\Exception\FactoryNotFoundException;
|
||||
use Flasher\Prime\Factory\NotificationFactoryInterface;
|
||||
use Flasher\Prime\Factory\NotificationFactoryLocator;
|
||||
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class NotificationFactoryLocatorTest extends TestCase
|
||||
{
|
||||
use MockeryPHPUnitIntegration;
|
||||
|
||||
public function testGetWithRegisteredFactory(): void
|
||||
{
|
||||
$factoryMock = \Mockery::mock(NotificationFactoryInterface::class);
|
||||
$notificationFactoryLocator = new NotificationFactoryLocator();
|
||||
$notificationFactoryLocator->addFactory('alias', $factoryMock);
|
||||
|
||||
$retrievedFactory = $notificationFactoryLocator->get('alias');
|
||||
|
||||
$this->assertSame($factoryMock, $retrievedFactory);
|
||||
}
|
||||
|
||||
public function testGetWithUnregisteredFactory(): void
|
||||
{
|
||||
$notificationFactoryLocator = new NotificationFactoryLocator();
|
||||
|
||||
$this->expectException(FactoryNotFoundException::class);
|
||||
$notificationFactoryLocator->get('alias');
|
||||
}
|
||||
|
||||
public function testHas(): void
|
||||
{
|
||||
$factoryMock = \Mockery::mock(NotificationFactoryInterface::class);
|
||||
$notificationFactoryLocator = new NotificationFactoryLocator();
|
||||
|
||||
$this->assertFalse($notificationFactoryLocator->has('alias'));
|
||||
|
||||
$notificationFactoryLocator->addFactory('alias', $factoryMock);
|
||||
|
||||
$this->assertTrue($notificationFactoryLocator->has('alias'));
|
||||
}
|
||||
|
||||
public function testAddFactory(): void
|
||||
{
|
||||
$factoryMock = \Mockery::mock(NotificationFactoryInterface::class);
|
||||
$notificationFactoryLocator = new NotificationFactoryLocator();
|
||||
$notificationFactoryLocator->addFactory('alias', $factoryMock);
|
||||
|
||||
$this->assertTrue($notificationFactoryLocator->has('alias'));
|
||||
}
|
||||
}
|
||||
@@ -1,48 +1,48 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the PHPFlasher package.
|
||||
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\Factory;
|
||||
|
||||
use Flasher\Prime\Factory\NotificationFactory;
|
||||
use Flasher\Tests\Prime\TestCase;
|
||||
use Flasher\Prime\Notification\NotificationBuilderInterface;
|
||||
use Flasher\Prime\Storage\StorageManagerInterface;
|
||||
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class NotificationFactoryTest extends TestCase
|
||||
final class NotificationFactoryTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testCreateNotificationBuilder()
|
||||
use MockeryPHPUnitIntegration;
|
||||
|
||||
public function testCreateNotificationBuilder(): void
|
||||
{
|
||||
$factory = new NotificationFactory();
|
||||
$storageManager = \Mockery::mock(StorageManagerInterface::class);
|
||||
$factory = new NotificationFactory($storageManager);
|
||||
|
||||
$builder = $factory->createNotificationBuilder();
|
||||
|
||||
$this->assertInstanceOf('Flasher\Prime\Notification\NotificationBuilderInterface', $builder);
|
||||
$this->assertInstanceOf(NotificationBuilderInterface::class, $builder);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testGetStorageManager()
|
||||
public function testStorageManagerForwardsAnyMethodCall(): void
|
||||
{
|
||||
$factory = new NotificationFactory();
|
||||
$manager = $factory->getStorageManager();
|
||||
$method = 'test_method';
|
||||
$arguments = ['test_argument'];
|
||||
|
||||
$this->assertInstanceOf('Flasher\Prime\Storage\StorageManagerInterface', $manager);
|
||||
}
|
||||
$mockedInterface = \Mockery::mock(NotificationBuilderInterface::class);
|
||||
$mockedInterface->allows($method)
|
||||
->withArgs($arguments)
|
||||
->andReturnTrue();
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testDynamicCallToNotificationBuilder()
|
||||
{
|
||||
$storageManager = $this->getMockBuilder('Flasher\Prime\Storage\StorageManagerInterface')->getMock();
|
||||
$storageManager->expects($this->once())->method('add');
|
||||
$storageManager = \Mockery::mock(StorageManagerInterface::class);
|
||||
$factory = \Mockery::mock(NotificationFactory::class, [$storageManager]) // @phpstan-ignore-line
|
||||
->makePartial()
|
||||
->allows('createNotificationBuilder')
|
||||
->andReturns($mockedInterface)
|
||||
->getMock();
|
||||
|
||||
$factory = new NotificationFactory($storageManager);
|
||||
$factory->addCreated();
|
||||
$result = $factory->__call($method, $arguments); // @phpstan-ignore-line
|
||||
|
||||
$this->assertTrue($result);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,177 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the PHPFlasher package.
|
||||
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
|
||||
*/
|
||||
|
||||
namespace Flasher\Tests\Prime\Filter;
|
||||
|
||||
use Flasher\Prime\Filter\CriteriaBuilder;
|
||||
use Flasher\Prime\Filter\Filter;
|
||||
use Flasher\Prime\Notification\Envelope;
|
||||
use Flasher\Prime\Notification\Notification;
|
||||
use Flasher\Prime\Stamp\CreatedAtStamp;
|
||||
use Flasher\Prime\Stamp\PresetStamp;
|
||||
use Flasher\Prime\Stamp\PriorityStamp;
|
||||
use Flasher\Prime\Stamp\UuidStamp;
|
||||
use Flasher\Tests\Prime\TestCase;
|
||||
|
||||
class CriteriaBuilderTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testItAddsPrioritySpecification()
|
||||
{
|
||||
$filter = $this->getFilter();
|
||||
$criteria = array('priority' => 2);
|
||||
|
||||
$criteriaBuilder = new CriteriaBuilder($filter, $criteria);
|
||||
$criteriaBuilder->buildPriority();
|
||||
|
||||
$specification = $this->getProperty($filter, 'specification');
|
||||
|
||||
$this->assertInstanceOf('Flasher\Prime\Filter\Specification\PrioritySpecification', $specification);
|
||||
$this->assertEquals(2, $this->getProperty($specification, 'minPriority'));
|
||||
$this->assertNull($this->getProperty($specification, 'maxPriority'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testItAddsHopsSpecification()
|
||||
{
|
||||
$filter = $this->getFilter();
|
||||
$criteria = array('hops' => 2);
|
||||
|
||||
$criteriaBuilder = new CriteriaBuilder($filter, $criteria);
|
||||
$criteriaBuilder->buildHops();
|
||||
|
||||
$specification = $this->getProperty($filter, 'specification');
|
||||
|
||||
$this->assertInstanceOf('Flasher\Prime\Filter\Specification\HopsSpecification', $specification);
|
||||
$this->assertEquals(2, $this->getProperty($specification, 'minAmount'));
|
||||
$this->assertNull($this->getProperty($specification, 'maxAmount'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testItAddsDelaySpecification()
|
||||
{
|
||||
$filter = $this->getFilter();
|
||||
$criteria = array('delay' => 2);
|
||||
|
||||
$criteriaBuilder = new CriteriaBuilder($filter, $criteria);
|
||||
$criteriaBuilder->buildDelay();
|
||||
|
||||
$specification = $this->getProperty($filter, 'specification');
|
||||
|
||||
$this->assertInstanceOf('Flasher\Prime\Filter\Specification\DelaySpecification', $specification);
|
||||
$this->assertEquals(2, $this->getProperty($specification, 'minDelay'));
|
||||
$this->assertNull($this->getProperty($specification, 'maxDelay'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testItAddsLifeSpecification()
|
||||
{
|
||||
$filter = $this->getFilter();
|
||||
$criteria = array('life' => 2);
|
||||
|
||||
$criteriaBuilder = new CriteriaBuilder($filter, $criteria);
|
||||
$criteriaBuilder->buildLife();
|
||||
|
||||
$specification = $this->getProperty($filter, 'specification');
|
||||
|
||||
$this->assertInstanceOf('Flasher\Prime\Filter\Specification\HopsSpecification', $specification);
|
||||
$this->assertEquals(2, $this->getProperty($specification, 'minAmount'));
|
||||
$this->assertNull($this->getProperty($specification, 'maxAmount'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testItAddsOrdering()
|
||||
{
|
||||
$filter = $this->getFilter();
|
||||
$criteria = array('order_by' => 'priority');
|
||||
|
||||
$criteriaBuilder = new CriteriaBuilder($filter, $criteria);
|
||||
$criteriaBuilder->buildOrder();
|
||||
|
||||
$orderings = $this->getProperty($filter, 'orderings');
|
||||
|
||||
$this->assertEquals(array("Flasher\Prime\Stamp\PriorityStamp" => 'ASC'), $orderings);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testItFilterEnvelopesByStamps()
|
||||
{
|
||||
$filter = $this->getFilter();
|
||||
$criteria = array('stamps' => 'preset');
|
||||
|
||||
$criteriaBuilder = new CriteriaBuilder($filter, $criteria);
|
||||
$criteriaBuilder->buildStamps();
|
||||
|
||||
$specification = $this->getProperty($filter, 'specification');
|
||||
|
||||
$this->assertInstanceOf('Flasher\Prime\Filter\Specification\StampsSpecification', $specification);
|
||||
$this->assertEquals(array('Flasher\Prime\Stamp\PresetStamp'), $this->getProperty($specification, 'stamps'));
|
||||
$this->assertEquals('or', $this->getProperty($specification, 'strategy'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testItFilterEnvelopesByCallbacks()
|
||||
{
|
||||
$callback = function () {};
|
||||
$filter = $this->getFilter();
|
||||
$criteria = array('filter' => $callback);
|
||||
|
||||
$criteriaBuilder = new CriteriaBuilder($filter, $criteria);
|
||||
$criteriaBuilder->buildCallback();
|
||||
|
||||
$specification = $this->getProperty($filter, 'specification');
|
||||
|
||||
$this->assertInstanceOf('Flasher\Prime\Filter\Specification\CallbackSpecification', $specification);
|
||||
$this->assertEquals($filter, $this->getProperty($specification, 'filter'));
|
||||
$this->assertEquals($callback, $this->getProperty($specification, 'callback'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Filter
|
||||
*/
|
||||
private function getFilter()
|
||||
{
|
||||
$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'),
|
||||
new PriorityStamp(1),
|
||||
new PresetStamp('entity_saved'),
|
||||
));
|
||||
|
||||
$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'),
|
||||
new PriorityStamp(3),
|
||||
));
|
||||
|
||||
return new Filter($envelopes, array());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
body {
|
||||
background-color: #fff;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
body {
|
||||
background-color: #111;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
body {
|
||||
background-color: #222;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
body {
|
||||
background-color: #333;
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\Fixtures\EventDispatcher\Event;
|
||||
|
||||
use Flasher\Prime\EventDispatcher\Event\StoppableEventInterface;
|
||||
|
||||
final class InvokeableEvent implements StoppableEventInterface
|
||||
{
|
||||
private bool $invoked = false;
|
||||
private int $invokeCount = 0;
|
||||
private bool $propagationStopped = false;
|
||||
|
||||
public function invoke(): void
|
||||
{
|
||||
$this->invoked = true;
|
||||
++$this->invokeCount;
|
||||
}
|
||||
|
||||
public function isInvoked(): bool
|
||||
{
|
||||
return $this->invoked;
|
||||
}
|
||||
|
||||
public function getInvokeCount(): int
|
||||
{
|
||||
return $this->invokeCount;
|
||||
}
|
||||
|
||||
public function stopPropagation(): void
|
||||
{
|
||||
$this->propagationStopped = true;
|
||||
}
|
||||
|
||||
public function isPropagationStopped(): bool
|
||||
{
|
||||
return $this->propagationStopped;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\Fixtures\EventDispatcher\Event;
|
||||
|
||||
use Flasher\Prime\EventDispatcher\Event\StoppableEventInterface;
|
||||
|
||||
final class StoppableEvent implements StoppableEventInterface
|
||||
{
|
||||
private bool $propagationStopped = false;
|
||||
|
||||
public function stopPropagation(): void
|
||||
{
|
||||
$this->propagationStopped = true;
|
||||
}
|
||||
|
||||
public function isPropagationStopped(): bool
|
||||
{
|
||||
return $this->propagationStopped;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\Fixtures\EventDispatcher\EventListener;
|
||||
|
||||
use Flasher\Prime\EventDispatcher\EventListener\EventListenerInterface;
|
||||
use Flasher\Tests\Prime\Fixtures\EventDispatcher\Event\InvokeableEvent;
|
||||
|
||||
final class InvokeableEventListener implements EventListenerInterface
|
||||
{
|
||||
public function __invoke(InvokeableEvent $event): void
|
||||
{
|
||||
$event->invoke();
|
||||
}
|
||||
|
||||
public function getSubscribedEvents(): string
|
||||
{
|
||||
return InvokeableEvent::class;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\Fixtures\EventDispatcher\EventListener;
|
||||
|
||||
use Flasher\Prime\EventDispatcher\EventListener\EventListenerInterface;
|
||||
|
||||
final class NonCallableListener implements EventListenerInterface
|
||||
{
|
||||
private string $eventName;
|
||||
|
||||
public function __construct(string $eventName)
|
||||
{
|
||||
$this->eventName = $eventName;
|
||||
}
|
||||
|
||||
public function getSubscribedEvents(): string
|
||||
{
|
||||
return $this->eventName;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\Fixtures\EventDispatcher\EventListener;
|
||||
|
||||
use Flasher\Prime\EventDispatcher\EventListener\EventListenerInterface;
|
||||
use Flasher\Tests\Prime\Fixtures\EventDispatcher\Event\StoppableEvent;
|
||||
|
||||
final class StoppableEventListener implements EventListenerInterface
|
||||
{
|
||||
public function __invoke(StoppableEvent $event): void
|
||||
{
|
||||
$event->stopPropagation();
|
||||
}
|
||||
|
||||
public function getSubscribedEvents(): string
|
||||
{
|
||||
return StoppableEvent::class;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime;
|
||||
|
||||
use Flasher\Prime\Factory\NotificationFactoryInterface;
|
||||
use Flasher\Prime\Factory\NotificationFactoryLocatorInterface;
|
||||
use Flasher\Prime\Flasher;
|
||||
use Flasher\Prime\Response\ResponseManagerInterface;
|
||||
use Flasher\Prime\Storage\StorageManagerInterface;
|
||||
use Mockery\Adapter\Phpunit\MockeryTestCase;
|
||||
use Mockery\MockInterface;
|
||||
|
||||
/**
|
||||
* Tests for the Flasher class and its methods.
|
||||
* This test ensures correct behavior of factory selection and rendering,
|
||||
* as well as dynamic method calls.
|
||||
*/
|
||||
final class FlasherTest extends MockeryTestCase
|
||||
{
|
||||
private MockInterface&NotificationFactoryLocatorInterface $factoryLocatorMock;
|
||||
private MockInterface&ResponseManagerInterface $responseManagerMock;
|
||||
private MockInterface&StorageManagerInterface $storageManagerMock;
|
||||
private Flasher $flasher;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->factoryLocatorMock = \Mockery::mock(NotificationFactoryLocatorInterface::class);
|
||||
$this->responseManagerMock = \Mockery::mock(ResponseManagerInterface::class);
|
||||
$this->storageManagerMock = \Mockery::mock(StorageManagerInterface::class);
|
||||
|
||||
$this->flasher = new Flasher('default', $this->factoryLocatorMock, $this->responseManagerMock, $this->storageManagerMock);
|
||||
}
|
||||
|
||||
public function testUseWithEmptyFactory(): void
|
||||
{
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
$this->expectExceptionMessage('Unable to resolve empty factory');
|
||||
|
||||
$this->flasher->use(' ');
|
||||
}
|
||||
|
||||
public function testUseReturnsFactoryLocatorFactoryWhenAliasFound(): void
|
||||
{
|
||||
$this->factoryLocatorMock->expects()
|
||||
->has('alias')
|
||||
->andReturns(true);
|
||||
|
||||
$this->factoryLocatorMock->expects()
|
||||
->get('alias')
|
||||
->andReturns(\Mockery::mock(NotificationFactoryInterface::class));
|
||||
|
||||
$result = $this->flasher->use('alias');
|
||||
|
||||
$this->assertInstanceOf(NotificationFactoryInterface::class, $result);
|
||||
}
|
||||
|
||||
public function testUseReturnsNewFactoryWhenAliasNotFound(): void
|
||||
{
|
||||
$this->factoryLocatorMock->expects()
|
||||
->has('alias')
|
||||
->andReturns(false);
|
||||
|
||||
$this->factoryLocatorMock->expects()
|
||||
->get('')
|
||||
->never();
|
||||
|
||||
$result = $this->flasher->use('alias');
|
||||
|
||||
$this->assertInstanceOf(NotificationFactoryInterface::class, $result);
|
||||
}
|
||||
|
||||
public function testCreateRunsUse(): void
|
||||
{
|
||||
$this->factoryLocatorMock->expects()
|
||||
->has('alias')
|
||||
->andReturns(true);
|
||||
|
||||
$this->factoryLocatorMock->expects()
|
||||
->get('alias')
|
||||
->andReturns(\Mockery::mock(NotificationFactoryInterface::class));
|
||||
|
||||
$result = $this->flasher->create('alias');
|
||||
|
||||
$this->assertInstanceOf(NotificationFactoryInterface::class, $result);
|
||||
}
|
||||
|
||||
public function testRenderRunsRenderManager(): void
|
||||
{
|
||||
$this->responseManagerMock->expects()
|
||||
->render('html', [], [])
|
||||
->andReturns('Mocked Render Result');
|
||||
|
||||
$result = $this->flasher->render();
|
||||
|
||||
$this->assertSame('Mocked Render Result', $result);
|
||||
}
|
||||
|
||||
public function testCallForwardsToUseMethod(): void
|
||||
{
|
||||
$this->factoryLocatorMock->expects()
|
||||
->has('default')
|
||||
->andReturns(true);
|
||||
|
||||
$mockedFactory = \Mockery::mock(NotificationFactoryInterface::class);
|
||||
$mockedFactory->expects('randomMethod')
|
||||
->with('param')
|
||||
->andReturns('Mocked method call');
|
||||
|
||||
$this->factoryLocatorMock->expects('get')
|
||||
->with('default')
|
||||
->andReturns($mockedFactory);
|
||||
|
||||
$result = $this->flasher->randomMethod('param'); // @phpstan-ignore-line
|
||||
|
||||
$this->assertSame('Mocked method call', $result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\Helper;
|
||||
|
||||
/**
|
||||
* @template T of object
|
||||
*
|
||||
* @phpstan-type PropertyType array{name: string, type: string|null, accessible: bool}
|
||||
*
|
||||
* @mixin T
|
||||
*/
|
||||
final class ObjectInvader
|
||||
{
|
||||
/**
|
||||
* @phpstan-var T
|
||||
*/
|
||||
public object $obj;
|
||||
|
||||
/**
|
||||
* @phpstan-var \ReflectionClass<T>
|
||||
*/
|
||||
public \ReflectionClass $reflected;
|
||||
|
||||
/**
|
||||
* @phpstan-param T $obj
|
||||
*
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
public function __construct(object $obj)
|
||||
{
|
||||
$this->obj = $obj;
|
||||
$this->reflected = new \ReflectionClass($obj);
|
||||
}
|
||||
|
||||
/**
|
||||
* @phpstan-param T $obj
|
||||
*
|
||||
* @phpstan-return self<T>
|
||||
*
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
public static function from(object $obj): self
|
||||
{
|
||||
return new self($obj);
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows dynamic property access.
|
||||
*
|
||||
* @param string $name name of the property
|
||||
*
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
public function get(string $name): mixed
|
||||
{
|
||||
$property = $this->reflected->getProperty($name);
|
||||
|
||||
$property->setAccessible(true);
|
||||
|
||||
return $property->getValue($this->obj);
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows dynamic setting of properties.
|
||||
*
|
||||
* @param string $name name of the property
|
||||
* @param mixed $value new value for the property
|
||||
*
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
public function set(string $name, mixed $value): void
|
||||
{
|
||||
$property = $this->reflected->getProperty($name);
|
||||
|
||||
$property->setAccessible(true);
|
||||
|
||||
$property->setValue($this->obj, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows dynamic calling of methods.
|
||||
*
|
||||
* @param string $name name of the method
|
||||
* @param array $params parameters to pass to the method
|
||||
*
|
||||
* @throws \ReflectionException
|
||||
*
|
||||
* @phpstan-ignore-next-line
|
||||
*/
|
||||
public function call(string $name, array $params = []): mixed
|
||||
{
|
||||
$method = $this->reflected->getMethod($name);
|
||||
|
||||
$method->setAccessible(true);
|
||||
|
||||
return $method->invoke($this->obj, ...$params);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,245 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\Http\Csp;
|
||||
|
||||
use Flasher\Prime\Http\Csp\ContentSecurityPolicyHandler;
|
||||
use Flasher\Prime\Http\Csp\NonceGeneratorInterface;
|
||||
use Flasher\Prime\Http\RequestInterface;
|
||||
use Flasher\Prime\Http\ResponseInterface;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class ContentSecurityPolicyHandlerTest extends TestCase
|
||||
{
|
||||
private ContentSecurityPolicyHandler $cspHandler;
|
||||
|
||||
/** @var MockObject&NonceGeneratorInterface */
|
||||
private MockObject $nonceGeneratorMock;
|
||||
|
||||
/** @var MockObject&RequestInterface */
|
||||
private MockObject $requestMock;
|
||||
|
||||
/** @var MockObject&ResponseInterface */
|
||||
private MockObject $responseMock;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->nonceGeneratorMock = $this->createMock(NonceGeneratorInterface::class);
|
||||
$this->requestMock = $this->createMock(RequestInterface::class);
|
||||
$this->responseMock = $this->createMock(ResponseInterface::class);
|
||||
|
||||
$this->cspHandler = new ContentSecurityPolicyHandler($this->nonceGeneratorMock);
|
||||
}
|
||||
|
||||
public function testGetNoncesFromRequestHeaders(): void
|
||||
{
|
||||
$this->requestMock->method('hasHeader')->willReturnCallback(function ($headerName) {
|
||||
return \in_array($headerName, ['X-PHPFlasher-Script-Nonce', 'X-PHPFlasher-Style-Nonce']);
|
||||
});
|
||||
$this->requestMock->method('getHeader')->willReturnCallback(function ($headerName) {
|
||||
return 'X-PHPFlasher-Script-Nonce' === $headerName ? 'test-script-nonce' : 'test-style-nonce';
|
||||
});
|
||||
|
||||
$nonces = $this->cspHandler->getNonces($this->requestMock);
|
||||
|
||||
$this->assertSame([
|
||||
'csp_script_nonce' => 'test-script-nonce',
|
||||
'csp_style_nonce' => 'test-style-nonce',
|
||||
], $nonces);
|
||||
}
|
||||
|
||||
public function testGetNoncesFromResponseHeaders(): void
|
||||
{
|
||||
$this->requestMock->method('hasHeader')->willReturnCallback(function ($headerName) {
|
||||
return \in_array($headerName, ['X-PHPFlasher-Script-Nonce', 'X-PHPFlasher-Style-Nonce']);
|
||||
});
|
||||
$this->requestMock->method('getHeader')->willReturnCallback(function ($headerName) {
|
||||
return 'X-PHPFlasher-Script-Nonce' === $headerName ? 'test-script-nonce' : 'test-style-nonce';
|
||||
});
|
||||
|
||||
$nonces = $this->cspHandler->getNonces($this->requestMock, $this->responseMock);
|
||||
|
||||
$this->assertSame([
|
||||
'csp_script_nonce' => 'test-script-nonce',
|
||||
'csp_style_nonce' => 'test-style-nonce',
|
||||
], $nonces);
|
||||
}
|
||||
|
||||
public function testGetGeneratedNonces(): void
|
||||
{
|
||||
$this->nonceGeneratorMock->method('generate')
|
||||
->willReturn('generated-nonce');
|
||||
|
||||
$this->responseMock->expects($this->exactly(2))
|
||||
->method('setHeader')
|
||||
->willReturnCallback(function ($headerName, $value) {
|
||||
static $calls = 0;
|
||||
switch (++$calls) {
|
||||
case 1:
|
||||
$this->assertSame('X-PHPFlasher-Script-Nonce', $headerName);
|
||||
$this->assertSame('generated-nonce', $value);
|
||||
break;
|
||||
case 2:
|
||||
$this->assertSame('X-PHPFlasher-Style-Nonce', $headerName);
|
||||
$this->assertSame('generated-nonce', $value);
|
||||
break;
|
||||
default:
|
||||
$this->fail('setHeader called more than twice.');
|
||||
}
|
||||
});
|
||||
|
||||
$nonces = $this->cspHandler->getNonces($this->requestMock, $this->responseMock);
|
||||
|
||||
$this->assertSame([
|
||||
'csp_script_nonce' => 'generated-nonce',
|
||||
'csp_style_nonce' => 'generated-nonce',
|
||||
], $nonces);
|
||||
}
|
||||
|
||||
public function testDisableCsp(): void
|
||||
{
|
||||
$request = $this->createMock(RequestInterface::class);
|
||||
$response = $this->createMock(ResponseInterface::class);
|
||||
|
||||
// Simulate the internal tracking of CSP headers.
|
||||
$cspHeaders = [
|
||||
'Content-Security-Policy' => true,
|
||||
'X-Content-Security-Policy' => true,
|
||||
];
|
||||
|
||||
// Simulate response behavior based on CSP header tracking.
|
||||
$response->method('hasHeader')->willReturnCallback(function ($headerName) use (&$cspHeaders) {
|
||||
return !empty($cspHeaders[$headerName]);
|
||||
});
|
||||
|
||||
// Mock the removal of headers to update our simulated tracking.
|
||||
$response->method('removeHeader')->willReturnCallback(function ($headerName) use (&$cspHeaders) {
|
||||
unset($cspHeaders[$headerName]);
|
||||
});
|
||||
|
||||
// Assuming CSP is initially enabled and headers are present.
|
||||
// This call should set CSP headers.
|
||||
$this->cspHandler->updateResponseHeaders($request, $response);
|
||||
|
||||
// Check if CSP headers are initially present.
|
||||
$this->assertTrue($response->hasHeader('Content-Security-Policy'));
|
||||
$this->assertTrue($response->hasHeader('X-Content-Security-Policy'));
|
||||
|
||||
// Disabling CSP.
|
||||
$this->cspHandler->disableCsp();
|
||||
|
||||
// Now CSP headers should be removed.
|
||||
// This call should remove CSP headers.
|
||||
$this->cspHandler->updateResponseHeaders($request, $response);
|
||||
|
||||
// Check if CSP headers are removed.
|
||||
$this->assertFalse($response->hasHeader('Content-Security-Policy'));
|
||||
$this->assertFalse($response->hasHeader('X-Content-Security-Policy'));
|
||||
}
|
||||
|
||||
public function testUpdateResponseHeadersWhenCspIsDisabled(): void
|
||||
{
|
||||
$removedHeaders = [];
|
||||
$this->responseMock->method('removeHeader')->willReturnCallback(function ($headerName) use (&$removedHeaders) {
|
||||
$removedHeaders[] = $headerName;
|
||||
});
|
||||
|
||||
$this->cspHandler->disableCsp();
|
||||
$nonces = $this->cspHandler->updateResponseHeaders($this->requestMock, $this->responseMock);
|
||||
|
||||
$expectedRemovedHeaders = [
|
||||
'Content-Security-Policy',
|
||||
'X-Content-Security-Policy',
|
||||
'Content-Security-Policy-Report-Only',
|
||||
];
|
||||
$this->assertSame([], $nonces);
|
||||
foreach ($expectedRemovedHeaders as $header) {
|
||||
$this->assertContains($header, $removedHeaders, "$header was not removed.");
|
||||
}
|
||||
}
|
||||
|
||||
public function testUpdateResponseHeadersWhenCspIsEnabled(): void
|
||||
{
|
||||
$setHeaders = [];
|
||||
$this->responseMock->method('setHeader')->willReturnCallback(function ($headerName, $value) use (&$setHeaders) {
|
||||
$setHeaders[$headerName] = $value;
|
||||
});
|
||||
|
||||
$this->nonceGeneratorMock->method('generate')->willReturnOnConsecutiveCalls('nonce1', 'nonce2', 'nonce3', 'nonce4');
|
||||
|
||||
$nonces = $this->cspHandler->updateResponseHeaders($this->requestMock, $this->responseMock);
|
||||
|
||||
// Verify that nonces were generated and set as expected
|
||||
$this->assertCount(2, $setHeaders, 'Expected two headers to be set.');
|
||||
$this->assertSame('nonce1', $setHeaders['X-PHPFlasher-Script-Nonce']);
|
||||
$this->assertSame('nonce2', $setHeaders['X-PHPFlasher-Style-Nonce']);
|
||||
|
||||
$this->assertSame([
|
||||
'csp_script_nonce' => 'nonce1',
|
||||
'csp_style_nonce' => 'nonce2',
|
||||
], $nonces);
|
||||
}
|
||||
|
||||
public function testGetNoncesFromHeaders(): void
|
||||
{
|
||||
$nonces = ['csp_script_nonce' => 'random1', 'csp_style_nonce' => 'random2'];
|
||||
|
||||
$this->requestMock->method('hasHeader')->willReturn(true);
|
||||
|
||||
$this->requestMock->expects($this->exactly(2))->method('getHeader')
|
||||
->willReturnCallback(function ($header) use ($nonces) {
|
||||
return 'X-PHPFlasher-Script-Nonce' === $header ? $nonces['csp_script_nonce'] : $nonces['csp_style_nonce'];
|
||||
});
|
||||
|
||||
$result = $this->cspHandler->getNonces($this->requestMock);
|
||||
$this->assertSame($nonces, $result);
|
||||
}
|
||||
|
||||
public function testGetNoncesFromResponseHeadersWhenNoHeadersInRequest(): void
|
||||
{
|
||||
$nonces = ['csp_script_nonce' => 'random3', 'csp_style_nonce' => 'random4'];
|
||||
|
||||
$this->requestMock->method('hasHeader')->willReturn(false);
|
||||
$this->responseMock->method('hasHeader')->willReturn(true);
|
||||
|
||||
$this->responseMock->expects($this->exactly(2))->method('getHeader')
|
||||
->willReturnCallback(function ($header) use ($nonces) {
|
||||
return 'X-PHPFlasher-Script-Nonce' === $header ? $nonces['csp_script_nonce'] : $nonces['csp_style_nonce'];
|
||||
});
|
||||
|
||||
$result = $this->cspHandler->getNonces($this->requestMock, $this->responseMock);
|
||||
$this->assertSame($nonces, $result);
|
||||
}
|
||||
|
||||
public function testGetNoncesWithRandomGeneratedNoncesWhenHeadersEmpty(): void
|
||||
{
|
||||
$nonces = ['csp_script_nonce' => 'random5', 'csp_style_nonce' => 'random6'];
|
||||
|
||||
$this->nonceGeneratorMock->expects($this->exactly(2))->method('generate')
|
||||
->willReturnCallback(function () use ($nonces) {
|
||||
static $i = 0;
|
||||
|
||||
return $i++ ? $nonces['csp_style_nonce'] : $nonces['csp_script_nonce'];
|
||||
});
|
||||
|
||||
$this->requestMock->expects($this->exactly(1))->method('hasHeader')->willReturn(false);
|
||||
$this->responseMock->expects($this->exactly(1))->method('hasHeader')->willReturn(false);
|
||||
|
||||
$this->responseMock->expects($this->exactly(2))->method('setHeader')
|
||||
->willReturnCallback(function ($headerName, $headerValue) {
|
||||
static $i = 0;
|
||||
if (0 === $i++) {
|
||||
$this->assertSame('X-PHPFlasher-Script-Nonce', $headerName);
|
||||
$this->assertSame('random5', $headerValue);
|
||||
} else {
|
||||
$this->assertSame('X-PHPFlasher-Style-Nonce', $headerName);
|
||||
$this->assertSame('random6', $headerValue);
|
||||
}
|
||||
});
|
||||
|
||||
$result = $this->cspHandler->getNonces($this->requestMock, $this->responseMock);
|
||||
$this->assertSame($nonces, $result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\Http\Csp;
|
||||
|
||||
use Flasher\Prime\Http\Csp\NonceGenerator;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* The generate() method of the NonceGenerator class provides randomly generated
|
||||
* hexadecimal string each time it is called. This test suite validates that each
|
||||
* nonce is truly unique, meets the length requirements and is a valid hexadecimal string.
|
||||
*/
|
||||
final class NonceGeneratorTest extends TestCase
|
||||
{
|
||||
private NonceGenerator $nonceGenerator;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->nonceGenerator = new NonceGenerator();
|
||||
}
|
||||
|
||||
/**
|
||||
* testGenerateMethodUnique check that the nonce generated is unique.
|
||||
*/
|
||||
public function testGenerateMethodUnique(): void
|
||||
{
|
||||
$nonces = [];
|
||||
|
||||
// Generate a list of nonces
|
||||
for ($i = 0; $i < 10; ++$i) {
|
||||
$nonces[] = $this->nonceGenerator->generate();
|
||||
}
|
||||
|
||||
// Check that there are no duplicate values
|
||||
$this->assertCount(10, array_unique($nonces));
|
||||
}
|
||||
|
||||
/**
|
||||
* testGenerateMethodHexadecimal check that the nonce generated is a valid hexadecimal string.
|
||||
*/
|
||||
public function testGenerateMethodHexadecimal(): void
|
||||
{
|
||||
// Generate a nonce
|
||||
$nonce = $this->nonceGenerator->generate();
|
||||
|
||||
// Check that the nonce is a valid hexadecimal string
|
||||
$this->assertTrue(ctype_xdigit($nonce));
|
||||
}
|
||||
|
||||
/**
|
||||
* testGenerateMethodLength check that the nonce generated is indeed 32 characters long.
|
||||
*/
|
||||
public function testGenerateMethodLength(): void
|
||||
{
|
||||
// Generate a nonce
|
||||
$nonce = $this->nonceGenerator->generate();
|
||||
|
||||
// Check that the nonce is the correct length
|
||||
$this->assertSame(32, \strlen($nonce));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\Http;
|
||||
|
||||
use Flasher\Prime\FlasherInterface;
|
||||
use Flasher\Prime\Http\RequestExtension;
|
||||
use Flasher\Prime\Http\RequestInterface;
|
||||
use Flasher\Prime\Http\ResponseInterface;
|
||||
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
|
||||
use Mockery\MockInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class RequestExtensionTest extends TestCase
|
||||
{
|
||||
use MockeryPHPUnitIntegration;
|
||||
|
||||
private MockInterface&FlasherInterface $flasher;
|
||||
|
||||
private MockInterface&RequestInterface $request;
|
||||
|
||||
private MockInterface&ResponseInterface $response;
|
||||
|
||||
/** @var array<string, string[]> */
|
||||
private array $mapping = [
|
||||
'success' => ['happy', 'joy'],
|
||||
'error' => ['sad', 'oops'],
|
||||
];
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->flasher = \Mockery::mock(FlasherInterface::class);
|
||||
$this->request = \Mockery::mock(RequestInterface::class);
|
||||
$this->response = \Mockery::mock(ResponseInterface::class);
|
||||
}
|
||||
|
||||
public function testFlashWithoutSession(): void
|
||||
{
|
||||
$this->request->expects()->hasSession()->andReturns(false);
|
||||
|
||||
$extension = new RequestExtension($this->flasher, $this->mapping);
|
||||
$response = $extension->flash($this->request, $this->response);
|
||||
|
||||
$this->assertSame($this->response, $response, 'Response should be returned unchanged if request has no session.');
|
||||
}
|
||||
|
||||
public function testFlashWithSessionAndMessages(): void
|
||||
{
|
||||
$this->request->expects()->hasSession()->andReturns(true);
|
||||
$this->request->allows('hasType')->andReturnUsing(fn ($type) => 'happy' === $type);
|
||||
$this->request->allows('getType')->andReturnUsing(fn ($type) => 'happy' === $type ? ['Good job!'] : []);
|
||||
|
||||
$this->flasher->expects()->flash('success', 'Good job!');
|
||||
$this->request->expects()->forgetType('happy')->once();
|
||||
|
||||
$extension = new RequestExtension($this->flasher, $this->mapping);
|
||||
$extension->flash($this->request, $this->response);
|
||||
}
|
||||
|
||||
public function testProcessRequestIgnoresUnmappedTypes(): void
|
||||
{
|
||||
$this->request->expects()->hasSession()->andReturns(true);
|
||||
$this->request->allows('hasType')->andReturnUsing(fn ($type) => 'unmappedType' === $type);
|
||||
|
||||
$this->flasher->expects()->flash()->never();
|
||||
$this->request->expects()->forgetType('unmappedType')->never();
|
||||
|
||||
$extension = new RequestExtension($this->flasher, $this->mapping);
|
||||
$extension->flash($this->request, $this->response);
|
||||
}
|
||||
|
||||
public function testConstructorFlattensMappingCorrectly(): void
|
||||
{
|
||||
$extension = new RequestExtension($this->flasher, $this->mapping);
|
||||
|
||||
$reflectedClass = new \ReflectionClass($extension);
|
||||
$flatMappingProperty = $reflectedClass->getProperty('mapping');
|
||||
|
||||
$expectedFlatMapping = [
|
||||
'happy' => 'success',
|
||||
'joy' => 'success',
|
||||
'sad' => 'error',
|
||||
'oops' => 'error',
|
||||
];
|
||||
|
||||
$this->assertSame($expectedFlatMapping, $flatMappingProperty->getValue($extension), 'Mapping should be flattened correctly.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,205 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\Http;
|
||||
|
||||
use Flasher\Prime\FlasherInterface;
|
||||
use Flasher\Prime\Http\Csp\ContentSecurityPolicyHandlerInterface;
|
||||
use Flasher\Prime\Http\RequestInterface;
|
||||
use Flasher\Prime\Http\ResponseExtension;
|
||||
use Flasher\Prime\Http\ResponseInterface;
|
||||
use Flasher\Prime\Response\Presenter\HtmlPresenter;
|
||||
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class ResponseExtensionTest extends TestCase
|
||||
{
|
||||
use MockeryPHPUnitIntegration;
|
||||
|
||||
public function testRenderWithHtmlInjection(): void
|
||||
{
|
||||
$flasher = \Mockery::mock(FlasherInterface::class);
|
||||
$cspHandler = \Mockery::mock(ContentSecurityPolicyHandlerInterface::class);
|
||||
$request = \Mockery::mock(RequestInterface::class);
|
||||
$response = \Mockery::mock(ResponseInterface::class);
|
||||
$htmlResponse = '<div>Flasher HTML</div>';
|
||||
|
||||
$contentBefore = 'before content '.HtmlPresenter::BODY_END_PLACE_HOLDER.' after content';
|
||||
|
||||
$cspHandler->expects()->updateResponseHeaders($request, $response)->once()->andReturn([
|
||||
'csp_script_nonce' => 'script-nonce',
|
||||
'csp_style_nonce' => 'style-nonce',
|
||||
]);
|
||||
|
||||
$flasher->expects()->render('html', [], [
|
||||
'envelopes_only' => false,
|
||||
'csp_script_nonce' => 'script-nonce',
|
||||
'csp_style_nonce' => 'style-nonce',
|
||||
])->once()->andReturn($htmlResponse);
|
||||
|
||||
$request->allows([
|
||||
'isXmlHttpRequest' => false,
|
||||
'isHtmlRequestFormat' => true,
|
||||
]);
|
||||
|
||||
$response->allows([
|
||||
'isSuccessful' => true,
|
||||
'isHtml' => true,
|
||||
'isRedirection' => false,
|
||||
'isAttachment' => false,
|
||||
'isJson' => false,
|
||||
'getContent' => $contentBefore,
|
||||
'setContent' => \Mockery::on(function ($content) use ($htmlResponse) {
|
||||
$expectedContent = 'before content '.$htmlResponse."\n".' after content';
|
||||
$this->assertSame($expectedContent, $content);
|
||||
|
||||
return true;
|
||||
}),
|
||||
]);
|
||||
|
||||
$responseExtension = new ResponseExtension($flasher, $cspHandler);
|
||||
$modifiedResponse = $responseExtension->render($request, $response);
|
||||
|
||||
$this->assertInstanceOf(ResponseInterface::class, $modifiedResponse);
|
||||
}
|
||||
|
||||
public function testRenderNotRenderableDueToAjaxRequest(): void
|
||||
{
|
||||
$flasher = \Mockery::mock(FlasherInterface::class);
|
||||
$cspHandler = \Mockery::mock(ContentSecurityPolicyHandlerInterface::class);
|
||||
$request = \Mockery::mock(RequestInterface::class);
|
||||
$response = \Mockery::mock(ResponseInterface::class);
|
||||
|
||||
$request->allows([
|
||||
'isXmlHttpRequest' => true,
|
||||
'isHtmlRequestFormat' => false,
|
||||
]);
|
||||
|
||||
$response->allows('getContent')->never();
|
||||
$flasher->allows('render')->never();
|
||||
|
||||
$responseExtension = new ResponseExtension($flasher, $cspHandler);
|
||||
$unmodifiedResponse = $responseExtension->render($request, $response);
|
||||
|
||||
$this->assertSame($response, $unmodifiedResponse);
|
||||
}
|
||||
|
||||
public function testRenderWithoutPlaceholder(): void
|
||||
{
|
||||
$flasher = \Mockery::mock(FlasherInterface::class);
|
||||
$cspHandler = \Mockery::mock(ContentSecurityPolicyHandlerInterface::class);
|
||||
$request = \Mockery::mock(RequestInterface::class);
|
||||
$response = \Mockery::mock(ResponseInterface::class);
|
||||
|
||||
$contentBefore = 'content without placeholder';
|
||||
|
||||
$request->allows([
|
||||
'isXmlHttpRequest' => false,
|
||||
'isHtmlRequestFormat' => true,
|
||||
]);
|
||||
|
||||
$response->allows([
|
||||
'isSuccessful' => true,
|
||||
'isHtml' => true,
|
||||
'isRedirection' => false,
|
||||
'isAttachment' => false,
|
||||
'isJson' => false,
|
||||
'getContent' => $contentBefore,
|
||||
]);
|
||||
|
||||
$flasher->allows('render')->never();
|
||||
|
||||
$responseExtension = new ResponseExtension($flasher, $cspHandler);
|
||||
$unmodifiedResponse = $responseExtension->render($request, $response);
|
||||
|
||||
$this->assertSame($response, $unmodifiedResponse);
|
||||
}
|
||||
|
||||
#[DataProvider('placeholderProvider')]
|
||||
public function testRenderWithDifferentPlaceholders(string $placeholder): void
|
||||
{
|
||||
$flasher = \Mockery::mock(FlasherInterface::class);
|
||||
$cspHandler = \Mockery::mock(ContentSecurityPolicyHandlerInterface::class);
|
||||
$request = \Mockery::mock(RequestInterface::class);
|
||||
$response = \Mockery::mock(ResponseInterface::class);
|
||||
$htmlResponse = '<div>Flasher HTML</div>';
|
||||
|
||||
$contentBefore = "before content {$placeholder} after content";
|
||||
|
||||
$cspHandler->allows()->updateResponseHeaders($request, $response)->andReturn([]);
|
||||
$flasher->allows()->render('html', [], \Mockery::any())->andReturn($htmlResponse);
|
||||
|
||||
$request->allows([
|
||||
'isXmlHttpRequest' => false,
|
||||
'isHtmlRequestFormat' => true,
|
||||
]);
|
||||
|
||||
$response->allows([
|
||||
'isSuccessful' => true,
|
||||
'isHtml' => true,
|
||||
'isRedirection' => false,
|
||||
'isAttachment' => false,
|
||||
'isJson' => false,
|
||||
'getContent' => $contentBefore,
|
||||
'setContent' => \Mockery::any(),
|
||||
]);
|
||||
|
||||
$responseExtension = new ResponseExtension($flasher, $cspHandler);
|
||||
$responseExtension->render($request, $response);
|
||||
|
||||
$htmlInjection = HtmlPresenter::FLASHER_REPLACE_ME === $placeholder ? sprintf('options.push(%s);', $htmlResponse) : $htmlResponse;
|
||||
$expectedContent = str_replace($placeholder, "{$htmlInjection}\n{$placeholder}", $contentBefore);
|
||||
|
||||
$response->shouldHaveReceived('setContent')->with($expectedContent)->once();
|
||||
}
|
||||
|
||||
public function testRenderWithCspNonces(): void
|
||||
{
|
||||
$flasher = \Mockery::mock(FlasherInterface::class);
|
||||
$cspHandler = \Mockery::mock(ContentSecurityPolicyHandlerInterface::class);
|
||||
$request = \Mockery::mock(RequestInterface::class);
|
||||
$response = \Mockery::mock(ResponseInterface::class);
|
||||
|
||||
$cspNonceScript = 'nonce-script';
|
||||
$cspNonceStyle = 'nonce-style';
|
||||
$contentBefore = 'content '.HtmlPresenter::BODY_END_PLACE_HOLDER;
|
||||
|
||||
$cspHandler->expects()->updateResponseHeaders($request, $response)->once()->andReturn([
|
||||
'csp_script_nonce' => $cspNonceScript,
|
||||
'csp_style_nonce' => $cspNonceStyle,
|
||||
]);
|
||||
|
||||
$flasher->expects()->render('html', [], [
|
||||
'envelopes_only' => false,
|
||||
'csp_script_nonce' => $cspNonceScript,
|
||||
'csp_style_nonce' => $cspNonceStyle,
|
||||
])->once()->andReturn('<div>Flasher HTML with CSP</div>');
|
||||
|
||||
$request->allows([
|
||||
'isXmlHttpRequest' => false,
|
||||
'isHtmlRequestFormat' => true,
|
||||
]);
|
||||
|
||||
$response->allows([
|
||||
'isSuccessful' => true,
|
||||
'isHtml' => true,
|
||||
'isRedirection' => false,
|
||||
'isAttachment' => false,
|
||||
'isJson' => false,
|
||||
'getContent' => $contentBefore,
|
||||
'setContent' => \Mockery::any(),
|
||||
]);
|
||||
|
||||
$responseExtension = new ResponseExtension($flasher, $cspHandler);
|
||||
$responseExtension->render($request, $response);
|
||||
}
|
||||
|
||||
public static function placeholderProvider(): \Iterator
|
||||
{
|
||||
yield [HtmlPresenter::FLASHER_REPLACE_ME];
|
||||
yield [HtmlPresenter::HEAD_END_PLACE_HOLDER];
|
||||
yield [HtmlPresenter::BODY_END_PLACE_HOLDER];
|
||||
}
|
||||
}
|
||||
@@ -1,72 +1,59 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the PHPFlasher package.
|
||||
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\Notification;
|
||||
|
||||
use Flasher\Prime\Notification\Envelope;
|
||||
use Flasher\Prime\Notification\Notification;
|
||||
use Flasher\Prime\Stamp\HandlerStamp;
|
||||
use Flasher\Prime\Notification\NotificationInterface;
|
||||
use Flasher\Prime\Stamp\HopsStamp;
|
||||
use Flasher\Prime\Stamp\PluginStamp;
|
||||
use Flasher\Prime\Stamp\PresetStamp;
|
||||
use Flasher\Tests\Prime\TestCase;
|
||||
use Flasher\Prime\Stamp\StampInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class EnvelopeTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testConstruct()
|
||||
public function testConstruct(): void
|
||||
{
|
||||
$notification = $this->getMockBuilder('Flasher\Prime\Notification\NotificationInterface')->getMock();
|
||||
$stamp = $this->getMockBuilder('Flasher\Prime\Stamp\StampInterface')->getMock();
|
||||
$notification = $this->getMockBuilder(NotificationInterface::class)->getMock();
|
||||
$stamp = $this->getMockBuilder(StampInterface::class)->getMock();
|
||||
|
||||
$envelope = new Envelope($notification, array($stamp));
|
||||
$envelope = new Envelope($notification, [$stamp]);
|
||||
|
||||
$this->assertEquals($notification, $envelope->getNotification());
|
||||
$this->assertEquals(array(\get_class($stamp) => $stamp), $envelope->all());
|
||||
$this->assertEquals([$stamp::class => $stamp], $envelope->all());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testWrap()
|
||||
public function testWrap(): void
|
||||
{
|
||||
$notification = $this->getMockBuilder('Flasher\Prime\Notification\NotificationInterface')->getMock();
|
||||
$stamp = $this->getMockBuilder('Flasher\Prime\Stamp\StampInterface')->getMock();
|
||||
$notification = $this->getMockBuilder(NotificationInterface::class)->getMock();
|
||||
$stamp = $this->getMockBuilder(StampInterface::class)->getMock();
|
||||
|
||||
$envelope = Envelope::wrap($notification, array($stamp));
|
||||
$envelope = Envelope::wrap($notification, [$stamp]);
|
||||
|
||||
$this->assertEquals($notification, $envelope->getNotification());
|
||||
$this->assertEquals(array(\get_class($stamp) => $stamp), $envelope->all());
|
||||
$this->assertEquals([$stamp::class => $stamp], $envelope->all());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testWith()
|
||||
public function testWith(): void
|
||||
{
|
||||
$notification = $this->getMockBuilder('Flasher\Prime\Notification\NotificationInterface')->getMock();
|
||||
$stamp1 = $this->getMockBuilder('Flasher\Prime\Stamp\StampInterface')->getMock();
|
||||
$stamp2 = $this->getMockBuilder('Flasher\Prime\Stamp\StampInterface')->getMock();
|
||||
$notification = $this->getMockBuilder(NotificationInterface::class)->getMock();
|
||||
$stamp1 = $this->getMockBuilder(StampInterface::class)->getMock();
|
||||
$stamp2 = $this->getMockBuilder(StampInterface::class)->getMock();
|
||||
|
||||
$envelope = new Envelope($notification);
|
||||
$envelope->with($stamp1, $stamp2);
|
||||
|
||||
$this->assertEquals($notification, $envelope->getNotification());
|
||||
$this->assertEquals(array(\get_class($stamp1) => $stamp1, \get_class($stamp2) => $stamp2), $envelope->all());
|
||||
$this->assertEquals([$stamp1::class => $stamp1, $stamp2::class => $stamp2], $envelope->all());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testWithStamp()
|
||||
public function testWithStamp(): void
|
||||
{
|
||||
$notification = $this->getMockBuilder('Flasher\Prime\Notification\NotificationInterface')->getMock();
|
||||
$stamp = $this->getMockBuilder('Flasher\Prime\Stamp\StampInterface')->getMock();
|
||||
$notification = $this->getMockBuilder(NotificationInterface::class)->getMock();
|
||||
$stamp = $this->getMockBuilder(StampInterface::class)->getMock();
|
||||
|
||||
$envelope = new Envelope($notification);
|
||||
$envelope->withStamp($stamp);
|
||||
@@ -74,94 +61,79 @@ final class EnvelopeTest extends TestCase
|
||||
$this->assertContains($stamp, $envelope->all());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testWithout()
|
||||
public function testWithout(): void
|
||||
{
|
||||
$notification = $this->getMockBuilder('Flasher\Prime\Notification\NotificationInterface')->getMock();
|
||||
$notification = $this->getMockBuilder(NotificationInterface::class)->getMock();
|
||||
$stamp1 = new HopsStamp(2);
|
||||
$stamp2 = new HandlerStamp('flasher');
|
||||
$stamp2 = new PluginStamp('flasher');
|
||||
$stamp3 = new PresetStamp('entity_saved');
|
||||
|
||||
$envelope = new Envelope($notification);
|
||||
$envelope->with($stamp1, $stamp2, $stamp3);
|
||||
|
||||
$this->assertEquals($notification, $envelope->getNotification());
|
||||
$this->assertEquals(array(\get_class($stamp1) => $stamp1, \get_class($stamp2) => $stamp2, \get_class($stamp3) => $stamp3), $envelope->all());
|
||||
$this->assertEquals([$stamp1::class => $stamp1, $stamp2::class => $stamp2, $stamp3::class => $stamp3], $envelope->all());
|
||||
|
||||
$envelope->without($stamp1, $stamp3);
|
||||
|
||||
$this->assertEquals(array(\get_class($stamp2) => $stamp2), $envelope->all());
|
||||
$this->assertEquals([$stamp2::class => $stamp2], $envelope->all());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testWithoutStamp()
|
||||
public function testWithoutStamp(): void
|
||||
{
|
||||
$notification = $this->getMockBuilder('Flasher\Prime\Notification\NotificationInterface')->getMock();
|
||||
$notification = $this->getMockBuilder(NotificationInterface::class)->getMock();
|
||||
$stamp1 = new HopsStamp(2);
|
||||
$stamp2 = new HandlerStamp('flasher');
|
||||
$stamp2 = new PluginStamp('flasher');
|
||||
$stamp3 = new PresetStamp('entity_saved');
|
||||
|
||||
$envelope = new Envelope($notification);
|
||||
$envelope->with($stamp1, $stamp2, $stamp3);
|
||||
|
||||
$this->assertEquals($notification, $envelope->getNotification());
|
||||
$this->assertEquals(array(\get_class($stamp1) => $stamp1, \get_class($stamp2) => $stamp2, \get_class($stamp3) => $stamp3), $envelope->all());
|
||||
$this->assertEquals([$stamp1::class => $stamp1, $stamp2::class => $stamp2, $stamp3::class => $stamp3], $envelope->all());
|
||||
|
||||
$envelope->withoutStamp($stamp1);
|
||||
|
||||
$this->assertEquals(array(\get_class($stamp2) => $stamp2, \get_class($stamp3) => $stamp3), $envelope->all());
|
||||
$this->assertEquals([$stamp2::class => $stamp2, $stamp3::class => $stamp3], $envelope->all());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testGet()
|
||||
public function testGet(): void
|
||||
{
|
||||
$notification = $this->getMockBuilder('\Flasher\Prime\Notification\NotificationInterface')->getMock();
|
||||
$stamps = array(
|
||||
$notification = $this->createMock(NotificationInterface::class);
|
||||
$stamps = [
|
||||
new HopsStamp(2),
|
||||
new HandlerStamp('flasher'),
|
||||
new PluginStamp('flasher'),
|
||||
new PresetStamp('entity_saved'),
|
||||
);
|
||||
];
|
||||
|
||||
$envelope = new Envelope($notification, $stamps);
|
||||
|
||||
$this->assertEquals($notification, $envelope->getNotification());
|
||||
|
||||
$last = $envelope->get(\get_class($stamps[0]));
|
||||
$last = $envelope->get($stamps[0]::class);
|
||||
|
||||
$this->assertEquals($stamps[0], $last);
|
||||
$this->assertEquals($last, $envelope->get(\get_class($stamps[0])));
|
||||
$this->assertEquals($last, $envelope->get($stamps[0]::class));
|
||||
|
||||
$this->assertNull($envelope->get('NotFoundStamp')); // @phpstan-ignore-line
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testAll()
|
||||
public function testAll(): void
|
||||
{
|
||||
$notification = $this->getMockBuilder('Flasher\Prime\Notification\NotificationInterface')->getMock();
|
||||
$stamps = array(
|
||||
$notification = $this->getMockBuilder(NotificationInterface::class)->getMock();
|
||||
$stamps = [
|
||||
new HopsStamp(2),
|
||||
new HandlerStamp('flasher'),
|
||||
new PluginStamp('flasher'),
|
||||
new PresetStamp('entity_saved'),
|
||||
);
|
||||
];
|
||||
|
||||
$envelope = new Envelope($notification, $stamps);
|
||||
|
||||
$this->assertEquals($notification, $envelope->getNotification());
|
||||
$this->assertEquals(array(\get_class($stamps[0]) => $stamps[0], \get_class($stamps[1]) => $stamps[1], \get_class($stamps[2]) => $stamps[2]), $envelope->all());
|
||||
$this->assertEquals([$stamps[0]::class => $stamps[0], $stamps[1]::class => $stamps[1], $stamps[2]::class => $stamps[2]], $envelope->all());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testGetNotification()
|
||||
public function testGetNotification(): void
|
||||
{
|
||||
$notification = new Notification();
|
||||
|
||||
@@ -170,178 +142,130 @@ final class EnvelopeTest extends TestCase
|
||||
$this->assertEquals($notification, $envelope->getNotification());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testGetType()
|
||||
public function testGetType(): void
|
||||
{
|
||||
$notification = $this->getMockBuilder('Flasher\Prime\Notification\NotificationInterface')->getMock();
|
||||
$notification = $this->getMockBuilder(NotificationInterface::class)->getMock();
|
||||
$notification->expects($this->once())->method('getType')->willReturn('success');
|
||||
|
||||
$envelope = new Envelope($notification);
|
||||
|
||||
$this->assertEquals('success', $envelope->getType());
|
||||
$this->assertSame('success', $envelope->getType());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testSetType()
|
||||
public function testSetType(): void
|
||||
{
|
||||
$notification = $this->getMockBuilder('Flasher\Prime\Notification\NotificationInterface')->getMock();
|
||||
$notification = $this->getMockBuilder(NotificationInterface::class)->getMock();
|
||||
$notification->expects($this->once())->method('setType');
|
||||
|
||||
$envelope = new Envelope($notification);
|
||||
$envelope->setType('success');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testGetMessage()
|
||||
public function testGetMessage(): void
|
||||
{
|
||||
$notification = $this->getMockBuilder('Flasher\Prime\Notification\NotificationInterface')->getMock();
|
||||
$notification = $this->getMockBuilder(NotificationInterface::class)->getMock();
|
||||
$notification->expects($this->once())->method('getMessage')->willReturn('success message');
|
||||
|
||||
$envelope = new Envelope($notification);
|
||||
|
||||
$this->assertEquals('success message', $envelope->getMessage());
|
||||
$this->assertSame('success message', $envelope->getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testSetMessage()
|
||||
public function testSetMessage(): void
|
||||
{
|
||||
$notification = $this->getMockBuilder('Flasher\Prime\Notification\NotificationInterface')->getMock();
|
||||
$notification = $this->getMockBuilder(NotificationInterface::class)->getMock();
|
||||
$notification->expects($this->once())->method('setMessage');
|
||||
|
||||
$envelope = new Envelope($notification);
|
||||
$envelope->setMessage('success message');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testGetTitle()
|
||||
public function testGetTitle(): void
|
||||
{
|
||||
$notification = $this->getMockBuilder('Flasher\Prime\Notification\NotificationInterface')->getMock();
|
||||
$notification = $this->getMockBuilder(NotificationInterface::class)->getMock();
|
||||
$notification->expects($this->once())->method('getTitle')->willReturn('success title');
|
||||
|
||||
$envelope = new Envelope($notification);
|
||||
|
||||
$this->assertEquals('success title', $envelope->getTitle());
|
||||
$this->assertSame('success title', $envelope->getTitle());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testSetTitle()
|
||||
public function testSetTitle(): void
|
||||
{
|
||||
$notification = $this->getMockBuilder('Flasher\Prime\Notification\NotificationInterface')->getMock();
|
||||
$notification = $this->getMockBuilder(NotificationInterface::class)->getMock();
|
||||
$notification->expects($this->once())->method('setTitle');
|
||||
|
||||
$envelope = new Envelope($notification);
|
||||
$envelope->setTitle('success title');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testGetOptions()
|
||||
public function testGetOptions(): void
|
||||
{
|
||||
$notification = $this->getMockBuilder('Flasher\Prime\Notification\NotificationInterface')->getMock();
|
||||
$notification->expects($this->once())->method('getOptions')->willReturn(array('timeout' => 2500));
|
||||
$notification = $this->getMockBuilder(NotificationInterface::class)->getMock();
|
||||
$notification->expects($this->once())->method('getOptions')->willReturn(['timeout' => 2500]);
|
||||
|
||||
$envelope = new Envelope($notification);
|
||||
|
||||
$this->assertEquals(array('timeout' => 2500), $envelope->getOptions());
|
||||
$this->assertSame(['timeout' => 2500], $envelope->getOptions());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testSetOptions()
|
||||
public function testSetOptions(): void
|
||||
{
|
||||
$notification = $this->getMockBuilder('Flasher\Prime\Notification\NotificationInterface')->getMock();
|
||||
$notification = $this->getMockBuilder(NotificationInterface::class)->getMock();
|
||||
$notification->expects($this->once())->method('setOptions');
|
||||
|
||||
$envelope = new Envelope($notification);
|
||||
$envelope->setOptions(array('timeout' => 2500));
|
||||
$envelope->setOptions(['timeout' => 2500]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testGetOption()
|
||||
public function testGetOption(): void
|
||||
{
|
||||
$notification = $this->getMockBuilder('Flasher\Prime\Notification\NotificationInterface')->getMock();
|
||||
$notification = $this->getMockBuilder(NotificationInterface::class)->getMock();
|
||||
$notification->expects($this->once())->method('getOption')->willReturn(2500);
|
||||
|
||||
$envelope = new Envelope($notification);
|
||||
|
||||
$this->assertEquals(2500, $envelope->getOption('timeout'));
|
||||
$this->assertSame(2500, $envelope->getOption('timeout'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testSetOption()
|
||||
public function testSetOption(): void
|
||||
{
|
||||
$notification = $this->getMockBuilder('Flasher\Prime\Notification\NotificationInterface')->getMock();
|
||||
$notification = $this->getMockBuilder(NotificationInterface::class)->getMock();
|
||||
$notification->expects($this->once())->method('setOption');
|
||||
|
||||
$envelope = new Envelope($notification);
|
||||
$envelope->setOption('timeout', 2500);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testUnsetOption()
|
||||
public function testUnsetOption(): void
|
||||
{
|
||||
$notification = $this->getMockBuilder('Flasher\Prime\Notification\NotificationInterface')->getMock();
|
||||
$notification = $this->getMockBuilder(NotificationInterface::class)->getMock();
|
||||
$notification->expects($this->once())->method('unsetOption');
|
||||
|
||||
$envelope = new Envelope($notification);
|
||||
$envelope->unsetOption('timeout');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testToArray()
|
||||
public function testToArray(): void
|
||||
{
|
||||
$array = array(
|
||||
$array = [
|
||||
'title' => 'PHPFlasher',
|
||||
'message' => 'success message',
|
||||
'options' => array('timeout' => 2500),
|
||||
);
|
||||
'options' => ['timeout' => 2500],
|
||||
];
|
||||
|
||||
$notification = $this->getMockBuilder('Flasher\Prime\Notification\NotificationInterface')->getMock();
|
||||
$notification = $this->getMockBuilder(NotificationInterface::class)->getMock();
|
||||
$notification->expects($this->once())->method('toArray')->willReturn($array);
|
||||
|
||||
$envelope = new Envelope($notification, new HandlerStamp('flasher'));
|
||||
$envelope = new Envelope($notification, new PluginStamp('flasher'));
|
||||
|
||||
$this->assertEquals(array('notification' => $array, 'handler' => 'flasher'), $envelope->toArray());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testCallDynamicCallToNotification()
|
||||
{
|
||||
$notification = new DynamicNotification();
|
||||
$envelope = new Envelope($notification);
|
||||
|
||||
$this->assertEquals('foobar', $envelope->foo());
|
||||
}
|
||||
}
|
||||
|
||||
class DynamicNotification extends Notification
|
||||
{
|
||||
public function foo()
|
||||
{
|
||||
return 'foobar';
|
||||
$this->assertSame([
|
||||
'title' => 'PHPFlasher',
|
||||
'message' => 'success message',
|
||||
'options' => ['timeout' => 2500],
|
||||
'metadata' => [
|
||||
'plugin' => 'flasher',
|
||||
],
|
||||
], $envelope->toArray());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,595 +1,479 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the PHPFlasher package.
|
||||
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\Notification;
|
||||
|
||||
use Flasher\Prime\Notification\Notification;
|
||||
use Flasher\Prime\Notification\NotificationBuilder;
|
||||
use Flasher\Prime\Notification\NotificationBuilderInterface;
|
||||
use Flasher\Prime\Notification\NotificationInterface;
|
||||
use Flasher\Prime\Notification\Type;
|
||||
use Flasher\Prime\Stamp\ContextStamp;
|
||||
use Flasher\Prime\Stamp\DelayStamp;
|
||||
use Flasher\Prime\Stamp\HopsStamp;
|
||||
use Flasher\Prime\Stamp\PluginStamp;
|
||||
use Flasher\Prime\Stamp\PresetStamp;
|
||||
use Flasher\Prime\Stamp\PriorityStamp;
|
||||
use Flasher\Prime\Stamp\StampInterface;
|
||||
use Flasher\Prime\Stamp\TranslationStamp;
|
||||
use Flasher\Prime\Stamp\UnlessStamp;
|
||||
use Flasher\Prime\Stamp\WhenStamp;
|
||||
use Flasher\Prime\Storage\StorageManagerInterface;
|
||||
use Flasher\Tests\Prime\TestCase;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class NotificationBuilderTest extends TestCase
|
||||
final class NotificationBuilderTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testAddSuccessMessage()
|
||||
public function testAddSuccessMessage(): void
|
||||
{
|
||||
$storageManager = $this->getMockBuilder('Flasher\Prime\Storage\StorageManagerInterface')->getMock();
|
||||
$storageManager = $this->getMockBuilder(StorageManagerInterface::class)->getMock();
|
||||
$storageManager->expects($this->once())->method('add');
|
||||
|
||||
$builder = $this->getNotificationBuilder($storageManager);
|
||||
$builder->addSuccess('success message');
|
||||
$builder->success('success message');
|
||||
|
||||
$envelope = $builder->getEnvelope();
|
||||
|
||||
$this->assertEquals(NotificationInterface::SUCCESS, $envelope->getType());
|
||||
$this->assertEquals('success message', $envelope->getMessage());
|
||||
$this->assertSame(Type::SUCCESS, $envelope->getType());
|
||||
$this->assertSame('success message', $envelope->getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testAddErrorMessage()
|
||||
public function testAddErrorMessage(): void
|
||||
{
|
||||
$storageManager = $this->getMockBuilder('Flasher\Prime\Storage\StorageManagerInterface')->getMock();
|
||||
$storageManager = $this->getMockBuilder(StorageManagerInterface::class)->getMock();
|
||||
$storageManager->expects($this->once())->method('add');
|
||||
|
||||
$builder = $this->getNotificationBuilder($storageManager);
|
||||
$builder->addError('error message');
|
||||
$builder->error('error message');
|
||||
|
||||
$envelope = $builder->getEnvelope();
|
||||
|
||||
$this->assertEquals(NotificationInterface::ERROR, $envelope->getType());
|
||||
$this->assertEquals('error message', $envelope->getMessage());
|
||||
$this->assertSame(Type::ERROR, $envelope->getType());
|
||||
$this->assertSame('error message', $envelope->getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testAddWarningMessage()
|
||||
public function testAddWarningMessage(): void
|
||||
{
|
||||
$storageManager = $this->getMockBuilder('Flasher\Prime\Storage\StorageManagerInterface')->getMock();
|
||||
$storageManager = $this->getMockBuilder(StorageManagerInterface::class)->getMock();
|
||||
$storageManager->expects($this->once())->method('add');
|
||||
|
||||
$builder = $this->getNotificationBuilder($storageManager);
|
||||
$builder->addWarning('warning message');
|
||||
$builder->warning('warning message');
|
||||
|
||||
$envelope = $builder->getEnvelope();
|
||||
|
||||
$this->assertEquals(NotificationInterface::WARNING, $envelope->getType());
|
||||
$this->assertEquals('warning message', $envelope->getMessage());
|
||||
$this->assertSame(Type::WARNING, $envelope->getType());
|
||||
$this->assertSame('warning message', $envelope->getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testAddInfoMessage()
|
||||
public function testAddInfoMessage(): void
|
||||
{
|
||||
$storageManager = $this->getMockBuilder('Flasher\Prime\Storage\StorageManagerInterface')->getMock();
|
||||
$storageManager = $this->getMockBuilder(StorageManagerInterface::class)->getMock();
|
||||
$storageManager->expects($this->once())->method('add');
|
||||
|
||||
$builder = $this->getNotificationBuilder($storageManager);
|
||||
$builder->addInfo('info message');
|
||||
$builder->info('info message');
|
||||
|
||||
$envelope = $builder->getEnvelope();
|
||||
|
||||
$this->assertEquals(NotificationInterface::INFO, $envelope->getType());
|
||||
$this->assertEquals('info message', $envelope->getMessage());
|
||||
$this->assertSame(Type::INFO, $envelope->getType());
|
||||
$this->assertSame('info message', $envelope->getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testAddFlashMessage()
|
||||
public function testAddFlashMessage(): void
|
||||
{
|
||||
$storageManager = $this->getMockBuilder('Flasher\Prime\Storage\StorageManagerInterface')->getMock();
|
||||
$storageManager = $this->getMockBuilder(StorageManagerInterface::class)->getMock();
|
||||
$storageManager->expects($this->once())->method('add');
|
||||
|
||||
$builder = $this->getNotificationBuilder($storageManager);
|
||||
$builder->addFlash('success', 'success message');
|
||||
$builder->flash('success', 'success message');
|
||||
|
||||
$envelope = $builder->getEnvelope();
|
||||
|
||||
$this->assertEquals(NotificationInterface::SUCCESS, $envelope->getType());
|
||||
$this->assertEquals('success message', $envelope->getMessage());
|
||||
$this->assertSame(Type::SUCCESS, $envelope->getType());
|
||||
$this->assertSame('success message', $envelope->getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testPushToStorage()
|
||||
public function testPushToStorage(): void
|
||||
{
|
||||
$storageManager = $this->getMockBuilder('Flasher\Prime\Storage\StorageManagerInterface')->getMock();
|
||||
$storageManager = $this->getMockBuilder(StorageManagerInterface::class)->getMock();
|
||||
$storageManager->expects($this->once())->method('add');
|
||||
|
||||
$builder = $this->getNotificationBuilder($storageManager);
|
||||
$builder->push();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testNotificationType()
|
||||
public function testNotificationType(): void
|
||||
{
|
||||
$builder = $this->getNotificationBuilder();
|
||||
$builder->type('success');
|
||||
|
||||
$envelope = $builder->getEnvelope();
|
||||
|
||||
$this->assertEquals(NotificationInterface::SUCCESS, $envelope->getType());
|
||||
$this->assertSame(Type::SUCCESS, $envelope->getType());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testNotificationMessage()
|
||||
public function testNotificationMessage(): void
|
||||
{
|
||||
$builder = $this->getNotificationBuilder();
|
||||
$builder->message('some message');
|
||||
|
||||
$envelope = $builder->getEnvelope();
|
||||
|
||||
$this->assertEquals('some message', $envelope->getMessage());
|
||||
$this->assertSame('some message', $envelope->getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testNotificationTitle()
|
||||
public function testNotificationTitle(): void
|
||||
{
|
||||
$builder = $this->getNotificationBuilder();
|
||||
$builder->title('some title');
|
||||
|
||||
$envelope = $builder->getEnvelope();
|
||||
|
||||
$this->assertEquals('some title', $envelope->getTitle());
|
||||
$this->assertSame('some title', $envelope->getTitle());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testNotificationOptions()
|
||||
public function testNotificationOptions(): void
|
||||
{
|
||||
$builder = $this->getNotificationBuilder();
|
||||
$builder->options(array('timeout' => 2000));
|
||||
$builder->options(['timeout' => 2000]);
|
||||
|
||||
$envelope = $builder->getEnvelope();
|
||||
|
||||
$this->assertEquals(array('timeout' => 2000), $envelope->getOptions());
|
||||
$this->assertSame(['timeout' => 2000], $envelope->getOptions());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testNotificationOption()
|
||||
public function testNotificationOption(): void
|
||||
{
|
||||
$builder = $this->getNotificationBuilder();
|
||||
$builder->option('timeout', 2000);
|
||||
|
||||
$envelope = $builder->getEnvelope();
|
||||
|
||||
$this->assertEquals(2000, $envelope->getOption('timeout'));
|
||||
$this->assertSame(2000, $envelope->getOption('timeout'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testSuccessType()
|
||||
public function testSuccessType(): void
|
||||
{
|
||||
$builder = $this->getNotificationBuilder();
|
||||
$builder->success();
|
||||
$builder->type('success');
|
||||
|
||||
$envelope = $builder->getEnvelope();
|
||||
|
||||
$this->assertEquals(NotificationInterface::SUCCESS, $envelope->getType());
|
||||
$this->assertSame(Type::SUCCESS, $envelope->getType());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testErrorType()
|
||||
public function testErrorType(): void
|
||||
{
|
||||
$builder = $this->getNotificationBuilder();
|
||||
$builder->error();
|
||||
$builder->type('error');
|
||||
|
||||
$envelope = $builder->getEnvelope();
|
||||
|
||||
$this->assertEquals(NotificationInterface::ERROR, $envelope->getType());
|
||||
$this->assertSame(Type::ERROR, $envelope->getType());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testInfoType()
|
||||
public function testInfoType(): void
|
||||
{
|
||||
$builder = $this->getNotificationBuilder();
|
||||
$builder->info();
|
||||
$builder->type('info');
|
||||
|
||||
$envelope = $builder->getEnvelope();
|
||||
|
||||
$this->assertEquals(NotificationInterface::INFO, $envelope->getType());
|
||||
$this->assertSame(Type::INFO, $envelope->getType());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testWarningType()
|
||||
public function testWarningType(): void
|
||||
{
|
||||
$builder = $this->getNotificationBuilder();
|
||||
$builder->warning();
|
||||
$builder->type('warning');
|
||||
|
||||
$envelope = $builder->getEnvelope();
|
||||
|
||||
$this->assertEquals(NotificationInterface::WARNING, $envelope->getType());
|
||||
$this->assertSame(Type::WARNING, $envelope->getType());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testWhenCondition()
|
||||
public function testWhenCondition(): void
|
||||
{
|
||||
$builder = $this->getNotificationBuilder();
|
||||
$builder->when(true);
|
||||
|
||||
$envelope = $builder->getEnvelope();
|
||||
$stamp = $envelope->get('Flasher\Prime\Stamp\WhenStamp');
|
||||
$stamp = $envelope->get(WhenStamp::class);
|
||||
|
||||
$this->assertInstanceOf('Flasher\Prime\Stamp\WhenStamp', $stamp);
|
||||
$this->assertEquals(true, $stamp->getCondition());
|
||||
$this->assertInstanceOf(WhenStamp::class, $stamp);
|
||||
$this->assertTrue($stamp->getCondition());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testUnlessCondition()
|
||||
public function testUnlessCondition(): void
|
||||
{
|
||||
$builder = $this->getNotificationBuilder();
|
||||
$builder->unless(true);
|
||||
|
||||
$envelope = $builder->getEnvelope();
|
||||
$stamp = $envelope->get('Flasher\Prime\Stamp\UnlessStamp');
|
||||
$stamp = $envelope->get(UnlessStamp::class);
|
||||
|
||||
$this->assertInstanceOf('Flasher\Prime\Stamp\UnlessStamp', $stamp);
|
||||
$this->assertEquals(true, $stamp->getCondition());
|
||||
$this->assertInstanceOf(UnlessStamp::class, $stamp);
|
||||
$this->assertTrue($stamp->getCondition());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testPriority()
|
||||
public function testPriority(): void
|
||||
{
|
||||
$builder = $this->getNotificationBuilder();
|
||||
$builder->priority(2);
|
||||
|
||||
$envelope = $builder->getEnvelope();
|
||||
$stamp = $envelope->get('Flasher\Prime\Stamp\PriorityStamp');
|
||||
$stamp = $envelope->get(PriorityStamp::class);
|
||||
|
||||
$this->assertInstanceOf('Flasher\Prime\Stamp\PriorityStamp', $stamp);
|
||||
$this->assertEquals(2, $stamp->getPriority());
|
||||
$this->assertInstanceOf(PriorityStamp::class, $stamp);
|
||||
$this->assertSame(2, $stamp->getPriority());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testHops()
|
||||
public function testHops(): void
|
||||
{
|
||||
$builder = $this->getNotificationBuilder();
|
||||
$builder->hops(3);
|
||||
|
||||
$envelope = $builder->getEnvelope();
|
||||
$stamp = $envelope->get('Flasher\Prime\Stamp\HopsStamp');
|
||||
$stamp = $envelope->get(HopsStamp::class);
|
||||
|
||||
$this->assertInstanceOf('Flasher\Prime\Stamp\HopsStamp', $stamp);
|
||||
$this->assertEquals(3, $stamp->getAmount());
|
||||
$this->assertInstanceOf(HopsStamp::class, $stamp);
|
||||
$this->assertSame(3, $stamp->getAmount());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testKeep()
|
||||
public function testKeep(): void
|
||||
{
|
||||
$builder = $this->getNotificationBuilder();
|
||||
$builder->keep();
|
||||
|
||||
$envelope = $builder->getEnvelope();
|
||||
$stamp = $envelope->get('Flasher\Prime\Stamp\HopsStamp');
|
||||
$stamp = $envelope->get(HopsStamp::class);
|
||||
|
||||
$this->assertInstanceOf('Flasher\Prime\Stamp\HopsStamp', $stamp);
|
||||
$this->assertEquals(2, $stamp->getAmount());
|
||||
$this->assertInstanceOf(HopsStamp::class, $stamp);
|
||||
$this->assertSame(2, $stamp->getAmount());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testDelay()
|
||||
public function testDelay(): void
|
||||
{
|
||||
$builder = $this->getNotificationBuilder();
|
||||
$builder->delay(3);
|
||||
|
||||
$envelope = $builder->getEnvelope();
|
||||
$stamp = $envelope->get('Flasher\Prime\Stamp\DelayStamp');
|
||||
$stamp = $envelope->get(DelayStamp::class);
|
||||
|
||||
$this->assertInstanceOf('Flasher\Prime\Stamp\DelayStamp', $stamp);
|
||||
$this->assertEquals(3, $stamp->getDelay());
|
||||
$this->assertInstanceOf(DelayStamp::class, $stamp);
|
||||
$this->assertSame(3, $stamp->getDelay());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testNow()
|
||||
public function testTranslate(): void
|
||||
{
|
||||
$builder = $this->getNotificationBuilder();
|
||||
$builder->now();
|
||||
$builder->translate(['foo' => 'bar'], 'ar');
|
||||
|
||||
$envelope = $builder->getEnvelope();
|
||||
$stamp = $envelope->get('Flasher\Prime\Stamp\DelayStamp');
|
||||
$stamp = $envelope->get(TranslationStamp::class);
|
||||
|
||||
$this->assertInstanceOf('Flasher\Prime\Stamp\DelayStamp', $stamp);
|
||||
$this->assertEquals(0, $stamp->getDelay());
|
||||
$this->assertInstanceOf(TranslationStamp::class, $stamp);
|
||||
$this->assertSame('ar', $stamp->getLocale());
|
||||
$this->assertSame(['foo' => 'bar'], $stamp->getParameters());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testTranslate()
|
||||
public function testAddPreset(): void
|
||||
{
|
||||
$builder = $this->getNotificationBuilder();
|
||||
$builder->translate(array('foo' => 'bar'), 'ar');
|
||||
|
||||
$envelope = $builder->getEnvelope();
|
||||
$stamp = $envelope->get('Flasher\Prime\Stamp\TranslationStamp');
|
||||
|
||||
$this->assertInstanceOf('Flasher\Prime\Stamp\TranslationStamp', $stamp);
|
||||
$this->assertEquals('ar', $stamp->getLocale());
|
||||
$this->assertEquals(array('foo' => 'bar'), $stamp->getParameters());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testAddPreset()
|
||||
{
|
||||
$storageManager = $this->getMockBuilder('Flasher\Prime\Storage\StorageManagerInterface')->getMock();
|
||||
$storageManager = $this->getMockBuilder(StorageManagerInterface::class)->getMock();
|
||||
$storageManager->expects($this->once())->method('add');
|
||||
|
||||
$builder = $this->getNotificationBuilder($storageManager);
|
||||
$builder->addPreset('entity_saved');
|
||||
$builder->preset('entity_saved');
|
||||
|
||||
$envelope = $builder->getEnvelope();
|
||||
$stamp = $envelope->get('Flasher\Prime\Stamp\PresetStamp');
|
||||
$stamp = $envelope->get(PresetStamp::class);
|
||||
|
||||
$this->assertInstanceOf('Flasher\Prime\Stamp\PresetStamp', $stamp);
|
||||
$this->assertEquals('entity_saved', $stamp->getPreset());
|
||||
$this->assertEquals(array(), $stamp->getParameters());
|
||||
$this->assertInstanceOf(PresetStamp::class, $stamp);
|
||||
$this->assertSame('entity_saved', $stamp->getPreset());
|
||||
$this->assertSame([], $stamp->getParameters());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testAddOperation()
|
||||
public function testAddOperation(): void
|
||||
{
|
||||
$storageManager = $this->getMockBuilder('Flasher\Prime\Storage\StorageManagerInterface')->getMock();
|
||||
$storageManager = $this->getMockBuilder(StorageManagerInterface::class)->getMock();
|
||||
$storageManager->expects($this->once())->method('add');
|
||||
|
||||
$builder = $this->getNotificationBuilder($storageManager);
|
||||
$builder->addPreset('saved');
|
||||
$builder->preset('saved');
|
||||
|
||||
$envelope = $builder->getEnvelope();
|
||||
$stamp = $envelope->get('Flasher\Prime\Stamp\PresetStamp');
|
||||
$stamp = $envelope->get(PresetStamp::class);
|
||||
|
||||
$this->assertInstanceOf('Flasher\Prime\Stamp\PresetStamp', $stamp);
|
||||
$this->assertEquals('saved', $stamp->getPreset());
|
||||
$this->assertEquals(array(), $stamp->getParameters());
|
||||
$this->assertInstanceOf(PresetStamp::class, $stamp);
|
||||
$this->assertSame('saved', $stamp->getPreset());
|
||||
$this->assertSame([], $stamp->getParameters());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testAddCreated()
|
||||
public function testAddCreated(): void
|
||||
{
|
||||
$storageManager = $this->getMockBuilder('Flasher\Prime\Storage\StorageManagerInterface')->getMock();
|
||||
$storageManager = $this->getMockBuilder(StorageManagerInterface::class)->getMock();
|
||||
$storageManager->expects($this->once())->method('add');
|
||||
|
||||
$builder = $this->getNotificationBuilder($storageManager);
|
||||
$builder->addCreated();
|
||||
$builder->created();
|
||||
|
||||
$envelope = $builder->getEnvelope();
|
||||
$stamp = $envelope->get('Flasher\Prime\Stamp\PresetStamp');
|
||||
$stamp = $envelope->get(PresetStamp::class);
|
||||
|
||||
$this->assertInstanceOf('Flasher\Prime\Stamp\PresetStamp', $stamp);
|
||||
$this->assertEquals('created', $stamp->getPreset());
|
||||
$this->assertEquals(array('resource' => 'resource'), $stamp->getParameters());
|
||||
$this->assertInstanceOf(PresetStamp::class, $stamp);
|
||||
$this->assertSame('created', $stamp->getPreset());
|
||||
$this->assertSame(['resource' => 'resource'], $stamp->getParameters());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testAddUpdated()
|
||||
public function testAddUpdated(): void
|
||||
{
|
||||
$storageManager = $this->getMockBuilder('Flasher\Prime\Storage\StorageManagerInterface')->getMock();
|
||||
$storageManager = $this->getMockBuilder(StorageManagerInterface::class)->getMock();
|
||||
$storageManager->expects($this->once())->method('add');
|
||||
|
||||
$builder = $this->getNotificationBuilder($storageManager);
|
||||
$builder->addUpdated();
|
||||
$builder->updated();
|
||||
|
||||
$envelope = $builder->getEnvelope();
|
||||
$stamp = $envelope->get('Flasher\Prime\Stamp\PresetStamp');
|
||||
$stamp = $envelope->get(PresetStamp::class);
|
||||
|
||||
$this->assertInstanceOf('Flasher\Prime\Stamp\PresetStamp', $stamp);
|
||||
$this->assertEquals('updated', $stamp->getPreset());
|
||||
$this->assertEquals(array('resource' => 'resource'), $stamp->getParameters());
|
||||
$this->assertInstanceOf(PresetStamp::class, $stamp);
|
||||
$this->assertSame('updated', $stamp->getPreset());
|
||||
$this->assertSame(['resource' => 'resource'], $stamp->getParameters());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testAddSaved()
|
||||
public function testAddSaved(): void
|
||||
{
|
||||
$storageManager = $this->getMockBuilder('Flasher\Prime\Storage\StorageManagerInterface')->getMock();
|
||||
$storageManager = $this->getMockBuilder(StorageManagerInterface::class)->getMock();
|
||||
$storageManager->expects($this->once())->method('add');
|
||||
|
||||
$builder = $this->getNotificationBuilder($storageManager);
|
||||
$builder->addSaved();
|
||||
$builder->saved();
|
||||
|
||||
$envelope = $builder->getEnvelope();
|
||||
$stamp = $envelope->get('Flasher\Prime\Stamp\PresetStamp');
|
||||
$stamp = $envelope->get(PresetStamp::class);
|
||||
|
||||
$this->assertInstanceOf('Flasher\Prime\Stamp\PresetStamp', $stamp);
|
||||
$this->assertEquals('saved', $stamp->getPreset());
|
||||
$this->assertEquals(array('resource' => 'resource'), $stamp->getParameters());
|
||||
$this->assertInstanceOf(PresetStamp::class, $stamp);
|
||||
$this->assertSame('saved', $stamp->getPreset());
|
||||
$this->assertSame(['resource' => 'resource'], $stamp->getParameters());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testAddDeleted()
|
||||
public function testAddDeleted(): void
|
||||
{
|
||||
$storageManager = $this->getMockBuilder('Flasher\Prime\Storage\StorageManagerInterface')->getMock();
|
||||
$storageManager = $this->getMockBuilder(StorageManagerInterface::class)->getMock();
|
||||
$storageManager->expects($this->once())->method('add');
|
||||
|
||||
$builder = $this->getNotificationBuilder($storageManager);
|
||||
$builder->addDeleted();
|
||||
$builder->deleted();
|
||||
|
||||
$envelope = $builder->getEnvelope();
|
||||
$stamp = $envelope->get('Flasher\Prime\Stamp\PresetStamp');
|
||||
$stamp = $envelope->get(PresetStamp::class);
|
||||
|
||||
$this->assertInstanceOf('Flasher\Prime\Stamp\PresetStamp', $stamp);
|
||||
$this->assertEquals('deleted', $stamp->getPreset());
|
||||
$this->assertEquals(array('resource' => 'resource'), $stamp->getParameters());
|
||||
$this->assertInstanceOf(PresetStamp::class, $stamp);
|
||||
$this->assertSame('deleted', $stamp->getPreset());
|
||||
$this->assertSame(['resource' => 'resource'], $stamp->getParameters());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testPreset()
|
||||
public function testPreset(): void
|
||||
{
|
||||
$builder = $this->getNotificationBuilder();
|
||||
$builder->preset('entity_saved');
|
||||
|
||||
$envelope = $builder->getEnvelope();
|
||||
$stamp = $envelope->get('Flasher\Prime\Stamp\PresetStamp');
|
||||
$stamp = $envelope->get(PresetStamp::class);
|
||||
|
||||
$this->assertInstanceOf('Flasher\Prime\Stamp\PresetStamp', $stamp);
|
||||
$this->assertEquals('entity_saved', $stamp->getPreset());
|
||||
$this->assertEquals(array(), $stamp->getParameters());
|
||||
$this->assertInstanceOf(PresetStamp::class, $stamp);
|
||||
$this->assertSame('entity_saved', $stamp->getPreset());
|
||||
$this->assertSame([], $stamp->getParameters());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testOperation()
|
||||
public function testOperation(): void
|
||||
{
|
||||
$builder = $this->getNotificationBuilder();
|
||||
$builder->operation('someOperation');
|
||||
|
||||
$envelope = $builder->getEnvelope();
|
||||
$stamp = $envelope->get('Flasher\Prime\Stamp\PresetStamp');
|
||||
$stamp = $envelope->get(PresetStamp::class);
|
||||
|
||||
$this->assertInstanceOf('Flasher\Prime\Stamp\PresetStamp', $stamp);
|
||||
$this->assertEquals('someOperation', $stamp->getPreset());
|
||||
$this->assertEquals(array('resource' => 'resource'), $stamp->getParameters());
|
||||
$this->assertInstanceOf(PresetStamp::class, $stamp);
|
||||
$this->assertSame('someOperation', $stamp->getPreset());
|
||||
$this->assertSame(['resource' => 'resource'], $stamp->getParameters());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testCreated()
|
||||
public function testCreated(): void
|
||||
{
|
||||
$builder = $this->getNotificationBuilder();
|
||||
$builder->created();
|
||||
|
||||
$envelope = $builder->getEnvelope();
|
||||
$stamp = $envelope->get('Flasher\Prime\Stamp\PresetStamp');
|
||||
$stamp = $envelope->get(PresetStamp::class);
|
||||
|
||||
$this->assertInstanceOf('Flasher\Prime\Stamp\PresetStamp', $stamp);
|
||||
$this->assertEquals('created', $stamp->getPreset());
|
||||
$this->assertEquals(array('resource' => 'resource'), $stamp->getParameters());
|
||||
$this->assertInstanceOf(PresetStamp::class, $stamp);
|
||||
$this->assertSame('created', $stamp->getPreset());
|
||||
$this->assertSame(['resource' => 'resource'], $stamp->getParameters());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testUpdated()
|
||||
public function testUpdated(): void
|
||||
{
|
||||
$builder = $this->getNotificationBuilder();
|
||||
$builder->updated();
|
||||
|
||||
$envelope = $builder->getEnvelope();
|
||||
$stamp = $envelope->get('Flasher\Prime\Stamp\PresetStamp');
|
||||
$stamp = $envelope->get(PresetStamp::class);
|
||||
|
||||
$this->assertInstanceOf('Flasher\Prime\Stamp\PresetStamp', $stamp);
|
||||
$this->assertEquals('updated', $stamp->getPreset());
|
||||
$this->assertEquals(array('resource' => 'resource'), $stamp->getParameters());
|
||||
$this->assertInstanceOf(PresetStamp::class, $stamp);
|
||||
$this->assertSame('updated', $stamp->getPreset());
|
||||
$this->assertSame(['resource' => 'resource'], $stamp->getParameters());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testSaved()
|
||||
public function testSaved(): void
|
||||
{
|
||||
$builder = $this->getNotificationBuilder();
|
||||
$builder->saved();
|
||||
|
||||
$stamp = $builder->getEnvelope()->get('Flasher\Prime\Stamp\PresetStamp');
|
||||
$stamp = $builder->getEnvelope()->get(PresetStamp::class);
|
||||
|
||||
$this->assertInstanceOf('Flasher\Prime\Stamp\PresetStamp', $stamp);
|
||||
$this->assertEquals('saved', $stamp->getPreset());
|
||||
$this->assertEquals(array('resource' => 'resource'), $stamp->getParameters());
|
||||
$this->assertInstanceOf(PresetStamp::class, $stamp);
|
||||
$this->assertSame('saved', $stamp->getPreset());
|
||||
$this->assertSame(['resource' => 'resource'], $stamp->getParameters());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testDeleted()
|
||||
public function testDeleted(): void
|
||||
{
|
||||
$builder = $this->getNotificationBuilder();
|
||||
$builder->deleted();
|
||||
|
||||
$stamp = $builder->getEnvelope()->get('Flasher\Prime\Stamp\PresetStamp');
|
||||
$stamp = $builder->getEnvelope()->get(PresetStamp::class);
|
||||
|
||||
$this->assertInstanceOf('Flasher\Prime\Stamp\PresetStamp', $stamp);
|
||||
$this->assertEquals('deleted', $stamp->getPreset());
|
||||
$this->assertEquals(array('resource' => 'resource'), $stamp->getParameters());
|
||||
$this->assertInstanceOf(PresetStamp::class, $stamp);
|
||||
$this->assertSame('deleted', $stamp->getPreset());
|
||||
$this->assertSame(['resource' => 'resource'], $stamp->getParameters());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testWithStamps()
|
||||
public function testWithStamps(): void
|
||||
{
|
||||
$builder = $this->getNotificationBuilder();
|
||||
|
||||
$stamps = array(
|
||||
$stamps = [
|
||||
new PriorityStamp(1),
|
||||
new HopsStamp(0),
|
||||
);
|
||||
];
|
||||
$builder->with($stamps);
|
||||
|
||||
$envelope = $builder->getEnvelope();
|
||||
$all = $envelope->all();
|
||||
|
||||
array_unshift($stamps, new PluginStamp('flasher'));
|
||||
|
||||
$this->assertEquals($stamps, array_values($all));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testWithStamp()
|
||||
public function testWithStamp(): void
|
||||
{
|
||||
$builder = $this->getNotificationBuilder();
|
||||
|
||||
$stamp = $this->getMockBuilder('Flasher\Prime\Stamp\StampInterface')->getMock();
|
||||
$builder->withStamp($stamp);
|
||||
$stamp = $this->createMock(StampInterface::class);
|
||||
$builder->with($stamp);
|
||||
|
||||
$envelope = $builder->getEnvelope();
|
||||
$stamps = $envelope->all();
|
||||
@@ -597,66 +481,51 @@ class NotificationBuilderTest extends TestCase
|
||||
$this->assertContains($stamp, $stamps);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testHandler()
|
||||
public function testHandler(): void
|
||||
{
|
||||
$builder = $this->getNotificationBuilder();
|
||||
$builder->handler('flasher');
|
||||
|
||||
$envelope = $builder->getEnvelope();
|
||||
$stamp = $envelope->get('Flasher\Prime\Stamp\HandlerStamp');
|
||||
$stamp = $envelope->get(PluginStamp::class);
|
||||
|
||||
$this->assertInstanceOf('Flasher\Prime\Stamp\HandlerStamp', $stamp);
|
||||
$this->assertEquals('flasher', $stamp->getHandler());
|
||||
$this->assertInstanceOf(PluginStamp::class, $stamp);
|
||||
$this->assertSame('flasher', $stamp->getPlugin());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testContext()
|
||||
public function testContext(): void
|
||||
{
|
||||
$builder = $this->getNotificationBuilder();
|
||||
$builder->context(array('foo' => 'bar'));
|
||||
$builder->context(['foo' => 'bar']);
|
||||
|
||||
$envelope = $builder->getEnvelope();
|
||||
$stamp = $envelope->get('Flasher\Prime\Stamp\ContextStamp');
|
||||
$stamp = $envelope->get(ContextStamp::class);
|
||||
|
||||
$this->assertInstanceOf('Flasher\Prime\Stamp\ContextStamp', $stamp);
|
||||
$this->assertEquals(array('foo' => 'bar'), $stamp->getContext());
|
||||
$this->assertInstanceOf(ContextStamp::class, $stamp);
|
||||
$this->assertSame(['foo' => 'bar'], $stamp->getContext());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testMacro()
|
||||
public function testMacro(): void
|
||||
{
|
||||
if (version_compare(PHP_VERSION, '5.4', '<')) {
|
||||
$this->markTestSkipped('Not working for PHP 5.3');
|
||||
}
|
||||
|
||||
NotificationBuilder::macro('foo', function () {
|
||||
return 'Called from a macro';
|
||||
});
|
||||
NotificationBuilder::macro('foo', fn (): string => 'Called from a macro');
|
||||
|
||||
$builder = $this->getNotificationBuilder();
|
||||
$response = $builder->foo();
|
||||
$response = $builder->foo(); // @phpstan-ignore-line
|
||||
|
||||
$this->assertTrue(NotificationBuilder::hasMacro('foo'));
|
||||
$this->assertEquals('Called from a macro', $response);
|
||||
$this->assertSame('Called from a macro', $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* @phpstan-param MockObject|StorageManagerInterface $storageManager
|
||||
*
|
||||
* @return NotificationBuilderInterface
|
||||
* @phpstan-ignore-next-line
|
||||
*/
|
||||
private function getNotificationBuilder(StorageManagerInterface $storageManager = null)
|
||||
private function getNotificationBuilder(?StorageManagerInterface $storageManager = null): NotificationBuilderInterface
|
||||
{
|
||||
/** @var StorageManagerInterface $storageManager */
|
||||
$storageManager = $storageManager ?: $this->getMockBuilder('Flasher\Prime\Storage\StorageManagerInterface')->getMock();
|
||||
$storageManager = $storageManager ?: $this->createMock(StorageManagerInterface::class);
|
||||
|
||||
return new NotificationBuilder($storageManager, new Notification());
|
||||
return new NotificationBuilder(new Notification(), $storageManager);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,77 +1,59 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the PHPFlasher package.
|
||||
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\Notification;
|
||||
|
||||
use Flasher\Prime\Notification\Notification;
|
||||
use Flasher\Tests\Prime\TestCase;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class NotificationTest extends TestCase
|
||||
final class NotificationTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testType()
|
||||
public function testType(): void
|
||||
{
|
||||
$notification = new Notification();
|
||||
|
||||
$this->assertNull($notification->getType());
|
||||
$this->assertSame('', $notification->getType());
|
||||
|
||||
$notification->setType('success');
|
||||
|
||||
$this->assertEquals('success', $notification->getType());
|
||||
$this->assertSame('success', $notification->getType());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testMessage()
|
||||
public function testMessage(): void
|
||||
{
|
||||
$notification = new Notification();
|
||||
|
||||
$this->assertNull($notification->getMessage());
|
||||
$this->assertSame('', $notification->getMessage());
|
||||
|
||||
$notification->setMessage('success message');
|
||||
|
||||
$this->assertEquals('success message', $notification->getMessage());
|
||||
$this->assertSame('success message', $notification->getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testTitle()
|
||||
public function testTitle(): void
|
||||
{
|
||||
$notification = new Notification();
|
||||
|
||||
$this->assertNull($notification->getTitle());
|
||||
$this->assertSame('', $notification->getTitle());
|
||||
|
||||
$notification->setTitle('success title');
|
||||
|
||||
$this->assertEquals('success title', $notification->getTitle());
|
||||
$this->assertSame('success title', $notification->getTitle());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testOptions()
|
||||
public function testOptions(): void
|
||||
{
|
||||
$notification = new Notification();
|
||||
|
||||
$this->assertEquals(array(), $notification->getOptions());
|
||||
$this->assertSame([], $notification->getOptions());
|
||||
|
||||
$notification->setOptions(array('timeout' => 2500));
|
||||
$notification->setOptions(['timeout' => 2500]);
|
||||
|
||||
$this->assertEquals(array('timeout' => 2500), $notification->getOptions());
|
||||
$this->assertSame(['timeout' => 2500], $notification->getOptions());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testOption()
|
||||
public function testOption(): void
|
||||
{
|
||||
$notification = new Notification();
|
||||
|
||||
@@ -79,40 +61,34 @@ class NotificationTest extends TestCase
|
||||
|
||||
$notification->setOption('timeout', 2500);
|
||||
|
||||
$this->assertEquals(2500, $notification->getOption('timeout'));
|
||||
$this->assertSame(2500, $notification->getOption('timeout'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testUnsetOption()
|
||||
public function testUnsetOption(): void
|
||||
{
|
||||
$notification = new Notification();
|
||||
$notification->setOptions(array('timeout' => 2500, 'position' => 'center'));
|
||||
$notification->setOptions(['timeout' => 2500, 'position' => 'center']);
|
||||
|
||||
$this->assertEquals(array('timeout' => 2500, 'position' => 'center'), $notification->getOptions());
|
||||
$this->assertSame(['timeout' => 2500, 'position' => 'center'], $notification->getOptions());
|
||||
|
||||
$notification->unsetOption('timeout');
|
||||
|
||||
$this->assertEquals(array('position' => 'center'), $notification->getOptions());
|
||||
$this->assertSame(['position' => 'center'], $notification->getOptions());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testToArray()
|
||||
public function testToArray(): void
|
||||
{
|
||||
$notification = new Notification();
|
||||
$notification->setType('success');
|
||||
$notification->setTitle('PHPFlasher');
|
||||
$notification->setMessage('success message');
|
||||
$notification->setOptions(array('timeout' => 2500, 'position' => 'center'));
|
||||
$notification->setOptions(['timeout' => 2500, 'position' => 'center']);
|
||||
|
||||
$this->assertEquals(array(
|
||||
'type' => 'success',
|
||||
$this->assertSame([
|
||||
'title' => 'PHPFlasher',
|
||||
'message' => 'success message',
|
||||
'options' => array('timeout' => 2500, 'position' => 'center'),
|
||||
), $notification->toArray());
|
||||
'type' => 'success',
|
||||
'options' => ['timeout' => 2500, 'position' => 'center'],
|
||||
], $notification->toArray());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,209 +1,96 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the PHPFlasher package.
|
||||
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\Plugin;
|
||||
|
||||
use Flasher\Prime\Plugin\FlasherPlugin;
|
||||
use Flasher\Tests\Prime\TestCase;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class FlasherPluginTest extends TestCase
|
||||
final class FlasherPluginTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testGetName()
|
||||
public function testGetName(): void
|
||||
{
|
||||
$plugin = new FlasherPlugin();
|
||||
$this->assertEquals('flasher', $plugin->getName());
|
||||
$this->assertSame('flasher', $plugin->getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testGetServiceID()
|
||||
public function testGetServiceID(): void
|
||||
{
|
||||
$plugin = new FlasherPlugin();
|
||||
$this->assertEquals('flasher', $plugin->getServiceID());
|
||||
$this->assertSame('flasher', $plugin->getServiceId());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testGetDefault()
|
||||
public function testGetDefault(): void
|
||||
{
|
||||
$plugin = new FlasherPlugin();
|
||||
$this->assertEquals('flasher', $plugin->getDefault());
|
||||
$this->assertSame('flasher', $plugin->getDefault());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testGetRootScript()
|
||||
public function testGetRootScript(): void
|
||||
{
|
||||
$plugin = new FlasherPlugin();
|
||||
$rootScript = array(
|
||||
'cdn' => 'https://cdn.jsdelivr.net/npm/@flasher/flasher@1.3.2/dist/flasher.min.js',
|
||||
'local' => '/vendor/flasher/flasher.min.js',
|
||||
);
|
||||
$rootScript = '/vendor/flasher/flasher.min.js';
|
||||
|
||||
$this->assertEquals($rootScript, $plugin->getRootScript());
|
||||
$this->assertSame($rootScript, $plugin->getRootScript());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testGetScripts()
|
||||
public function testGetScripts(): void
|
||||
{
|
||||
$plugin = new FlasherPlugin();
|
||||
$scripts = array(
|
||||
'cdn' => array('https://cdn.jsdelivr.net/npm/@flasher/flasher@1.3.2/dist/flasher.min.js'),
|
||||
'local' => array('/vendor/flasher/flasher.min.js'),
|
||||
);
|
||||
|
||||
$this->assertEquals($scripts, $plugin->getScripts());
|
||||
$this->assertSame([], $plugin->getScripts());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testGetResourcesDir()
|
||||
public function testProcessConfiguration(): void
|
||||
{
|
||||
$plugin = new FlasherPlugin();
|
||||
$resourceDir = realpath(__DIR__.'/../../../src/Prime/Resources');
|
||||
|
||||
$this->assertEquals($resourceDir, $plugin->getResourcesDir());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testGetFlashBagMapping()
|
||||
{
|
||||
$plugin = new FlasherPlugin();
|
||||
$mapping = array(
|
||||
'success' => array('success'),
|
||||
'error' => array('error', 'danger'),
|
||||
'warning' => array('warning', 'alarm'),
|
||||
'info' => array('info', 'notice', 'alert'),
|
||||
);
|
||||
|
||||
$this->assertEquals($mapping, $plugin->getFlashBagMapping());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testProcessConfiguration()
|
||||
{
|
||||
$plugin = new FlasherPlugin();
|
||||
$config = array(
|
||||
$config = [
|
||||
'default' => 'flasher',
|
||||
'root_script' => array(
|
||||
'cdn' => 'https://cdn.jsdelivr.net/npm/@flasher/flasher@1.3.2/dist/flasher.min.js',
|
||||
'local' => '/vendor/flasher/flasher.min.js',
|
||||
),
|
||||
'scripts' => array(),
|
||||
'styles' => array(
|
||||
'cdn' => array('https://cdn.jsdelivr.net/npm/@flasher/flasher@1.3.2/dist/flasher.min.css'),
|
||||
'local' => array('/vendor/flasher/flasher.min.css'),
|
||||
),
|
||||
'options' => array(),
|
||||
'use_cdn' => true,
|
||||
'auto_translate' => true,
|
||||
'auto_render' => true,
|
||||
'flash_bag' => array(
|
||||
'enabled' => true,
|
||||
'mapping' => array(
|
||||
'success' => array('success'),
|
||||
'error' => array('error', 'danger'),
|
||||
'warning' => array('warning', 'alarm'),
|
||||
'info' => array('info', 'notice', 'alert'),
|
||||
),
|
||||
),
|
||||
'filter_criteria' => array(),
|
||||
'presets' => array(
|
||||
'created' => array(
|
||||
'main_script' => '/vendor/flasher/flasher.min.js',
|
||||
'scripts' => [],
|
||||
'styles' => ['/vendor/flasher/flasher.min.css'],
|
||||
'options' => [],
|
||||
'translate' => true,
|
||||
'inject_assets' => true,
|
||||
'flash_bag' => [
|
||||
'success' => ['success'],
|
||||
'error' => ['error', 'danger'],
|
||||
'warning' => ['warning', 'alarm'],
|
||||
'info' => ['info', 'notice', 'alert'],
|
||||
],
|
||||
'presets' => [
|
||||
'created' => [
|
||||
'type' => 'success',
|
||||
'message' => 'The resource was created',
|
||||
),
|
||||
'updated' => array(
|
||||
'options' => [],
|
||||
],
|
||||
'updated' => [
|
||||
'type' => 'success',
|
||||
'message' => 'The resource was updated',
|
||||
),
|
||||
'saved' => array(
|
||||
'options' => [],
|
||||
],
|
||||
'saved' => [
|
||||
'type' => 'success',
|
||||
'message' => 'The resource was saved',
|
||||
),
|
||||
'deleted' => array(
|
||||
'options' => [],
|
||||
],
|
||||
'deleted' => [
|
||||
'type' => 'success',
|
||||
'message' => 'The resource was deleted',
|
||||
),
|
||||
),
|
||||
);
|
||||
'options' => [],
|
||||
],
|
||||
],
|
||||
'plugins' => [
|
||||
'flasher' => [
|
||||
'scripts' => [],
|
||||
'styles' => ['/vendor/flasher/flasher.min.css'],
|
||||
'options' => [],
|
||||
],
|
||||
],
|
||||
'filter' => [],
|
||||
];
|
||||
|
||||
$this->assertEquals($config, $plugin->processConfiguration());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testNormalizeConfig()
|
||||
{
|
||||
$plugin = new FlasherPlugin();
|
||||
|
||||
$inputConfig = array(
|
||||
'template_factory' => array(
|
||||
'default' => 'flasher',
|
||||
'templates' => array(
|
||||
'flasher' => array(
|
||||
'options' => array(),
|
||||
'styles' => array(),
|
||||
),
|
||||
),
|
||||
),
|
||||
'auto_create_from_session' => true,
|
||||
'types_mapping' => array(),
|
||||
'observer_events' => array(),
|
||||
'translate_by_default' => true,
|
||||
'flash_bag' => array(),
|
||||
);
|
||||
|
||||
$outputConfig = array(
|
||||
'options' => array(),
|
||||
'themes' => array(
|
||||
'flasher' => array(
|
||||
'styles' => array(),
|
||||
),
|
||||
),
|
||||
'flash_bag' => array(
|
||||
'enabled' => true,
|
||||
'mapping' => array(),
|
||||
),
|
||||
'auto_translate' => true,
|
||||
'presets' => array(
|
||||
'created' => array(
|
||||
'type' => 'success',
|
||||
'message' => 'The resource was created',
|
||||
),
|
||||
'updated' => array(
|
||||
'type' => 'success',
|
||||
'message' => 'The resource was updated',
|
||||
),
|
||||
'saved' => array(
|
||||
'type' => 'success',
|
||||
'message' => 'The resource was saved',
|
||||
),
|
||||
'deleted' => array(
|
||||
'type' => 'success',
|
||||
'message' => 'The resource was deleted',
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
$this->assertEquals($outputConfig, $plugin->normalizeConfig($inputConfig));
|
||||
$this->assertEquals($config, $plugin->normalizeConfig());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the PHPFlasher package.
|
||||
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\Response\Presenter;
|
||||
|
||||
@@ -11,16 +8,13 @@ use Flasher\Prime\Notification\Envelope;
|
||||
use Flasher\Prime\Notification\Notification;
|
||||
use Flasher\Prime\Response\Presenter\ArrayPresenter;
|
||||
use Flasher\Prime\Response\Response;
|
||||
use Flasher\Tests\Prime\TestCase;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class ArrayPresenterTest extends TestCase
|
||||
final class ArrayPresenterTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testArrayPresenter()
|
||||
public function testArrayPresenter(): void
|
||||
{
|
||||
$envelopes = array();
|
||||
$envelopes = [];
|
||||
|
||||
$notification = new Notification();
|
||||
$notification->setMessage('success message');
|
||||
@@ -34,32 +28,31 @@ class ArrayPresenterTest extends TestCase
|
||||
$notification->setType('warning');
|
||||
$envelopes[] = new Envelope($notification);
|
||||
|
||||
$response = 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(),
|
||||
'styles' => array(),
|
||||
'options' => array(),
|
||||
);
|
||||
$response = [
|
||||
'envelopes' => [
|
||||
[
|
||||
'title' => 'PHPFlasher',
|
||||
'message' => 'success message',
|
||||
'type' => 'success',
|
||||
'options' => [],
|
||||
'metadata' => [],
|
||||
],
|
||||
[
|
||||
'title' => 'yoeunes/toastr',
|
||||
'message' => 'warning message',
|
||||
'type' => 'warning',
|
||||
'options' => [],
|
||||
'metadata' => [],
|
||||
],
|
||||
],
|
||||
'scripts' => [],
|
||||
'styles' => [],
|
||||
'options' => [],
|
||||
'context' => [],
|
||||
];
|
||||
|
||||
$presenter = new ArrayPresenter();
|
||||
|
||||
$this->assertEquals($response, $presenter->render(new Response($envelopes, array())));
|
||||
$this->assertSame($response, $presenter->render(new Response($envelopes, [])));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the PHPFlasher package.
|
||||
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\Response\Presenter;
|
||||
|
||||
@@ -11,16 +8,14 @@ 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;
|
||||
use Livewire\LivewireManager;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class HtmlPresenterTest extends TestCase
|
||||
final class HtmlPresenterTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testArrayPresenter()
|
||||
public function testArrayPresenter(): void
|
||||
{
|
||||
$envelopes = array();
|
||||
$envelopes = [];
|
||||
|
||||
$notification = new Notification();
|
||||
$notification->setMessage('success message');
|
||||
@@ -34,93 +29,100 @@ class HtmlPresenterTest extends TestCase
|
||||
$notification->setType('warning');
|
||||
$envelopes[] = new Envelope($notification);
|
||||
|
||||
$scriptTagWithNonce = '';
|
||||
$livewireListener = $this->getLivewireListenerScript();
|
||||
|
||||
$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":[]}},{"notification":{"type":"warning","message":"warning message","title":"yoeunes\/toastr","options":[]}}]}, FLASHER_FLASH_BAG_PLACE_HOLDER);
|
||||
<script type="text/javascript" class="flasher-js">
|
||||
(function(window, document) {
|
||||
const merge = (first, second) => {
|
||||
if (Array.isArray(first) && Array.isArray(second)) {
|
||||
return [...first, ...second.filter(item => !first.includes(item))];
|
||||
}
|
||||
|
||||
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 || []),
|
||||
};
|
||||
}
|
||||
if (typeof first === 'object' && typeof second === 'object') {
|
||||
for (const [key, value] of Object.entries(second)) {
|
||||
first[key] = key in first ? { ...first[key], ...value } : value;
|
||||
}
|
||||
return first;
|
||||
}
|
||||
|
||||
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 undefined;
|
||||
};
|
||||
|
||||
return Object.assign({}, first, second);
|
||||
}
|
||||
const mergeOptions = (...options) => {
|
||||
const result = {};
|
||||
|
||||
function renderOptions(options) {
|
||||
if(!window.hasOwnProperty('flasher')) {
|
||||
console.error('Flasher is not loaded');
|
||||
return;
|
||||
}
|
||||
options.forEach(option => {
|
||||
Object.entries(option).forEach(([key, value]) => {
|
||||
result[key] = key in result ? merge(result[key], value) : value;
|
||||
});
|
||||
});
|
||||
|
||||
requestAnimationFrame(function () {
|
||||
window.flasher.render(options);
|
||||
});
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
function render(options) {
|
||||
if ('loading' !== document.readyState) {
|
||||
renderOptions(options);
|
||||
const renderCallback = (options) => {
|
||||
if(!window.flasher) {
|
||||
throw new Error('Flasher is not loaded');
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
window.flasher.render(options);
|
||||
};
|
||||
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
renderOptions(options);
|
||||
});
|
||||
}
|
||||
const render = (options) => {
|
||||
if (options instanceof Event) {
|
||||
options = options.detail;
|
||||
}
|
||||
|
||||
if (1 === document.querySelectorAll('script.flasher-js').length) {
|
||||
document.addEventListener('flasher:render', function (event) {
|
||||
render(event.detail);
|
||||
});
|
||||
if (['interactive', 'complete'].includes(document.readyState)) {
|
||||
renderCallback(options);
|
||||
} else {
|
||||
document.addEventListener('DOMContentLoaded', () => renderCallback(options));
|
||||
}
|
||||
};
|
||||
|
||||
{$livewireListener}
|
||||
}
|
||||
const addScriptAndRender = (options) => {
|
||||
const mainScript = '';
|
||||
|
||||
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);
|
||||
};
|
||||
if (window.flasher || !mainScript || document.querySelector('script[src="' + mainScript + '"]')) {
|
||||
render(options);
|
||||
} else {
|
||||
const tag = document.createElement('script');
|
||||
tag.src = mainScript;
|
||||
tag.type = 'text/javascript';
|
||||
{$scriptTagWithNonce}
|
||||
tag.onload = () => render(options);
|
||||
|
||||
document.head.appendChild(tag);
|
||||
}
|
||||
})();
|
||||
</script>
|
||||
JAVASCRIPT;
|
||||
document.head.appendChild(tag);
|
||||
}
|
||||
};
|
||||
|
||||
const addRenderListener = () => {
|
||||
if (1 === document.querySelectorAll('script.flasher-js').length) {
|
||||
document.addEventListener('flasher:render', render);
|
||||
}
|
||||
|
||||
{$livewireListener}
|
||||
};
|
||||
|
||||
const options = [];
|
||||
options.push({"envelopes":[{"title":"PHPFlasher","message":"success message","type":"success","options":[],"metadata":[]},{"title":"yoeunes\/toastr","message":"warning message","type":"warning","options":[],"metadata":[]}],"scripts":[],"styles":[],"options":[],"context":[]});
|
||||
/** {--FLASHER_REPLACE_ME--} **/
|
||||
addScriptAndRender(mergeOptions(...options));
|
||||
addRenderListener();
|
||||
})(window, document);
|
||||
</script>
|
||||
JAVASCRIPT;
|
||||
|
||||
$presenter = new HtmlPresenter();
|
||||
|
||||
$this->assertEquals($response, $presenter->render(new Response($envelopes, array())));
|
||||
$this->assertSame($response, $presenter->render(new Response($envelopes, [])));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testItRenderOnlyEnvelopesAsJsonObject()
|
||||
public function testItRenderOnlyEnvelopesAsJsonObject(): void
|
||||
{
|
||||
$envelopes = array();
|
||||
$envelopes = [];
|
||||
|
||||
$notification = new Notification();
|
||||
$notification->setMessage('success message');
|
||||
@@ -134,31 +136,26 @@ JAVASCRIPT;
|
||||
$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":[]}}]}';
|
||||
$response = '{"envelopes":[{"title":"PHPFlasher","message":"success message","type":"success","options":[],"metadata":[]},{"title":"yoeunes\/toastr","message":"warning message","type":"warning","options":[],"metadata":[]}],"scripts":[],"styles":[],"options":[],"context":{"envelopes_only":true}}';
|
||||
|
||||
$presenter = new HtmlPresenter();
|
||||
|
||||
$this->assertEquals($response, $presenter->render(new Response($envelopes, array('envelopes_only' => true))));
|
||||
$this->assertSame($response, $presenter->render(new Response($envelopes, ['envelopes_only' => true])));
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate the script for Livewire event handling.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function getLivewireListenerScript()
|
||||
private function getLivewireListenerScript(): string
|
||||
{
|
||||
if (!class_exists('Livewire\LivewireManager')) {
|
||||
if (!class_exists(LivewireManager::class)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return <<<JAVASCRIPT
|
||||
document.addEventListener('livewire:navigating', function () {
|
||||
var elements = document.querySelectorAll('.fl-no-cache');
|
||||
for (var i = 0; i < elements.length; i++) {
|
||||
elements[i].remove();
|
||||
}
|
||||
});
|
||||
JAVASCRIPT;
|
||||
document.addEventListener('livewire:navigating', () => {
|
||||
document.querySelectorAll('.fl-no-cache').forEach(el => el.remove());
|
||||
});
|
||||
JAVASCRIPT;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,77 +1,76 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the PHPFlasher package.
|
||||
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\Response\Resource;
|
||||
|
||||
use Flasher\Prime\Config\Config;
|
||||
use Flasher\Prime\Asset\AssetManagerInterface;
|
||||
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;
|
||||
use Flasher\Prime\Stamp\IdStamp;
|
||||
use Flasher\Prime\Stamp\PluginStamp;
|
||||
use Flasher\Prime\Template\TemplateEngineInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class ResourceManagerTest extends TestCase
|
||||
final class ResourceManagerTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testItPopulateResponseFromResources()
|
||||
public function testItPopulateResponseFromResources(): void
|
||||
{
|
||||
$config = new Config(array(
|
||||
'default' => 'flasher',
|
||||
'root_script' => 'root_script.min.js',
|
||||
));
|
||||
$resourceManager = new ResourceManager($config);
|
||||
$templateEngine = $this->createMock(TemplateEngineInterface::class);
|
||||
|
||||
$resourceManager->addScripts('flasher', array('flasher.min.js'));
|
||||
$resourceManager->addStyles('flasher', array('flasher.min.css'));
|
||||
$resourceManager->addOptions('flasher', array('timeout' => 2500, 'position' => 'center'));
|
||||
$assetManager = $this->createMock(AssetManagerInterface::class);
|
||||
$assetManager->method('getPath')->willReturnArgument(0);
|
||||
$assetManager->method('getPaths')->willReturnArgument(0);
|
||||
|
||||
$resourceManager->addScripts('toastr', array('toastr.min.js', 'jquery.min.js'));
|
||||
$resourceManager->addStyles('toastr', array('toastr.min.css'));
|
||||
$resourceManager->addOptions('toastr', array('sounds' => false));
|
||||
$resourceManager = new ResourceManager($templateEngine, $assetManager, 'main_script.min.js', [
|
||||
'flasher' => [
|
||||
'scripts' => ['flasher.min.js'],
|
||||
'styles' => ['flasher.min.css'],
|
||||
'options' => ['timeout' => 2500, 'position' => 'center'],
|
||||
],
|
||||
'toastr' => [
|
||||
'scripts' => ['toastr.min.js', 'jquery.min.js'],
|
||||
'styles' => ['toastr.min.css'],
|
||||
'options' => ['sounds' => false],
|
||||
],
|
||||
]);
|
||||
|
||||
$envelopes = array();
|
||||
$envelopes = [];
|
||||
|
||||
$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'),
|
||||
));
|
||||
$envelopes[] = new Envelope($notification, [
|
||||
new PluginStamp('flasher'),
|
||||
new CreatedAtStamp(new \DateTimeImmutable('2023-02-05 16:22:50')),
|
||||
new IdStamp('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'),
|
||||
));
|
||||
$envelopes[] = new Envelope($notification, [
|
||||
new PluginStamp('toastr'),
|
||||
new CreatedAtStamp(new \DateTimeImmutable('2023-02-05 16:22:50')),
|
||||
new IdStamp('2222'),
|
||||
]);
|
||||
|
||||
$response = new Response($envelopes, array());
|
||||
$response = new Response($envelopes, []);
|
||||
|
||||
$response = $resourceManager->buildResponse($response);
|
||||
$response = $resourceManager->populateResponse($response);
|
||||
|
||||
$this->assertEquals($envelopes, $response->getEnvelopes());
|
||||
$this->assertEquals('root_script.min.js', $response->getRootScript());
|
||||
$this->assertEquals(array('flasher.min.js', 'toastr.min.js', 'jquery.min.js'), $response->getScripts());
|
||||
$this->assertEquals(array('flasher.min.css', '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());
|
||||
$this->assertSame('main_script.min.js', $response->getMainScript());
|
||||
$this->assertSame(['flasher.min.js', 'toastr.min.js', 'jquery.min.js'], $response->getScripts());
|
||||
$this->assertSame(['flasher.min.css', 'toastr.min.css'], $response->getStyles());
|
||||
$this->assertEquals([
|
||||
'toastr' => ['sounds' => false],
|
||||
'flasher' => ['timeout' => 2500, 'position' => 'center'],
|
||||
], $response->getOptions());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,159 +1,169 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the PHPFlasher package.
|
||||
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\Response;
|
||||
|
||||
use Flasher\Prime\EventDispatcher\EventDispatcherInterface;
|
||||
use Flasher\Prime\Exception\PresenterNotFoundException;
|
||||
use Flasher\Prime\Notification\Envelope;
|
||||
use Flasher\Prime\Notification\Notification;
|
||||
use Flasher\Prime\Response\Resource\ResourceManagerInterface;
|
||||
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;
|
||||
use Flasher\Prime\Stamp\IdStamp;
|
||||
use Flasher\Prime\Storage\StorageManagerInterface;
|
||||
use Livewire\LivewireManager;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class ResponseManagerTest extends TestCase
|
||||
final class ResponseManagerTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testRenderSavedNotifications()
|
||||
public function testRenderSavedNotifications(): void
|
||||
{
|
||||
$envelopes = array();
|
||||
$envelopes = [];
|
||||
|
||||
$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'),
|
||||
));
|
||||
$envelopes[] = new Envelope($notification, [
|
||||
new CreatedAtStamp(new \DateTimeImmutable('2023-02-05 16:22:50')),
|
||||
new IdStamp('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'),
|
||||
));
|
||||
$envelopes[] = new Envelope($notification, [
|
||||
new CreatedAtStamp(new \DateTimeImmutable('2023-02-06 16:22:50')),
|
||||
new IdStamp('2222'),
|
||||
]);
|
||||
|
||||
$storageManager = new StorageManager();
|
||||
$storageManager->add($envelopes);
|
||||
$resourceManager = $this->createMock(ResourceManagerInterface::class);
|
||||
$resourceManager->method('populateResponse')->willReturnArgument(0);
|
||||
|
||||
$responseManager = new ResponseManager(null, $storageManager);
|
||||
$storageManager = $this->createMock(StorageManagerInterface::class);
|
||||
$eventDispatcher = $this->createMock(EventDispatcherInterface::class);
|
||||
|
||||
$responseManager = new ResponseManager($resourceManager, $storageManager, $eventDispatcher);
|
||||
$scriptTagWithNonce = '';
|
||||
$livewireListener = $this->getLivewireListenerScript();
|
||||
|
||||
$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);
|
||||
<script type="text/javascript" class="flasher-js">
|
||||
(function(window, document) {
|
||||
const merge = (first, second) => {
|
||||
if (Array.isArray(first) && Array.isArray(second)) {
|
||||
return [...first, ...second.filter(item => !first.includes(item))];
|
||||
}
|
||||
|
||||
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 || []),
|
||||
};
|
||||
if (typeof first === 'object' && typeof second === 'object') {
|
||||
for (const [key, value] of Object.entries(second)) {
|
||||
first[key] = key in first ? { ...first[key], ...value } : value;
|
||||
}
|
||||
return first;
|
||||
}
|
||||
|
||||
return undefined;
|
||||
};
|
||||
|
||||
const mergeOptions = (...options) => {
|
||||
const result = {};
|
||||
|
||||
options.forEach(option => {
|
||||
Object.entries(option).forEach(([key, value]) => {
|
||||
result[key] = key in result ? merge(result[key], value) : value;
|
||||
});
|
||||
});
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
const renderCallback = (options) => {
|
||||
if(!window.flasher) {
|
||||
throw new Error('Flasher is not loaded');
|
||||
}
|
||||
|
||||
window.flasher.render(options);
|
||||
};
|
||||
|
||||
const render = (options) => {
|
||||
if (options instanceof Event) {
|
||||
options = options.detail;
|
||||
}
|
||||
|
||||
if (['interactive', 'complete'].includes(document.readyState)) {
|
||||
renderCallback(options);
|
||||
} else {
|
||||
document.addEventListener('DOMContentLoaded', () => renderCallback(options));
|
||||
}
|
||||
};
|
||||
|
||||
const addScriptAndRender = (options) => {
|
||||
const mainScript = '';
|
||||
|
||||
if (window.flasher || !mainScript || document.querySelector('script[src="' + mainScript + '"]')) {
|
||||
render(options);
|
||||
} else {
|
||||
const tag = document.createElement('script');
|
||||
tag.src = mainScript;
|
||||
tag.type = 'text/javascript';
|
||||
{$scriptTagWithNonce}
|
||||
tag.onload = () => render(options);
|
||||
|
||||
document.head.appendChild(tag);
|
||||
}
|
||||
};
|
||||
|
||||
const addRenderListener = () => {
|
||||
if (1 === document.querySelectorAll('script.flasher-js').length) {
|
||||
document.addEventListener('flasher:render', render);
|
||||
}
|
||||
|
||||
{$livewireListener}
|
||||
};
|
||||
|
||||
const options = [];
|
||||
options.push({"envelopes":[],"scripts":[],"styles":[],"options":[],"context":[]});
|
||||
/** {--FLASHER_REPLACE_ME--} **/
|
||||
addScriptAndRender(mergeOptions(...options));
|
||||
addRenderListener();
|
||||
})(window, document);
|
||||
</script>
|
||||
JAVASCRIPT;
|
||||
|
||||
$this->assertSame($response, $responseManager->render('html'));
|
||||
}
|
||||
|
||||
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);
|
||||
});
|
||||
|
||||
{$livewireListener}
|
||||
}
|
||||
|
||||
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()
|
||||
public function testItThrowsExceptionIfPresenterNotFound(): void
|
||||
{
|
||||
$this->setExpectedException('\InvalidArgumentException', 'Presenter [xml] not supported.');
|
||||
$this->expectException(PresenterNotFoundException::class);
|
||||
$this->expectExceptionMessage('Presenter "xml" not found, did you forget to register it? Available presenters: [html, json, array]');
|
||||
|
||||
$responseManager = new ResponseManager();
|
||||
$responseManager->render(array(), 'xml');
|
||||
$resourceManager = $this->createMock(ResourceManagerInterface::class);
|
||||
$resourceManager->method('populateResponse')->willReturnArgument(0);
|
||||
|
||||
$storageManager = $this->createMock(StorageManagerInterface::class);
|
||||
$eventDispatcher = $this->createMock(EventDispatcherInterface::class);
|
||||
|
||||
$responseManager = new ResponseManager($resourceManager, $storageManager, $eventDispatcher);
|
||||
$responseManager->render('xml');
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate the script for Livewire event handling.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function getLivewireListenerScript()
|
||||
private function getLivewireListenerScript(): string
|
||||
{
|
||||
if (!class_exists('Livewire\LivewireManager')) {
|
||||
if (!class_exists(LivewireManager::class)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return <<<JAVASCRIPT
|
||||
document.addEventListener('livewire:navigating', function () {
|
||||
var elements = document.querySelectorAll('.fl-no-cache');
|
||||
for (var i = 0; i < elements.length; i++) {
|
||||
elements[i].remove();
|
||||
}
|
||||
});
|
||||
JAVASCRIPT;
|
||||
document.addEventListener('livewire:navigating', () => {
|
||||
document.querySelectorAll('.fl-no-cache').forEach(el => el.remove());
|
||||
});
|
||||
JAVASCRIPT;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,79 +1,61 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the PHPFlasher package.
|
||||
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
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;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class ResponseTest extends TestCase
|
||||
final class ResponseTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testAddRootScriptToResponse()
|
||||
public function testAddRootScriptToResponse(): void
|
||||
{
|
||||
$response = new Response(array(), array());
|
||||
$response = new Response([], []);
|
||||
|
||||
$response->setRootScript('flasher.min.js');
|
||||
$response->setMainScript('flasher.min.js');
|
||||
|
||||
$this->assertEquals('flasher.min.js', $response->getRootScript());
|
||||
$this->assertSame('flasher.min.js', $response->getMainScript());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testItAddsScriptToResponse()
|
||||
public function testItAddsScriptToResponse(): void
|
||||
{
|
||||
$response = new Response(array(), array());
|
||||
$response = new Response([], []);
|
||||
|
||||
$response->addScripts(array('flasher.min.js', 'toastr.min.js'));
|
||||
$response->addScripts(array('flasher.min.js', 'noty.min.js'));
|
||||
$response->addScripts(['flasher.min.js', 'toastr.min.js']);
|
||||
$response->addScripts(['flasher.min.js', 'noty.min.js']);
|
||||
|
||||
$this->assertEquals(array('flasher.min.js', 'toastr.min.js', 'noty.min.js'), $response->getScripts());
|
||||
$this->assertSame(['flasher.min.js', 'toastr.min.js', 'noty.min.js'], $response->getScripts());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testItAddsStylesToResponse()
|
||||
public function testItAddsStylesToResponse(): void
|
||||
{
|
||||
$response = new Response(array(), array());
|
||||
$response = new Response([], []);
|
||||
|
||||
$response->addStyles(array('flasher.min.css', 'toastr.min.css'));
|
||||
$response->addStyles(array('flasher.min.css', 'noty.min.css'));
|
||||
$response->addStyles(['flasher.min.css', 'toastr.min.css']);
|
||||
$response->addStyles(['flasher.min.css', 'noty.min.css']);
|
||||
|
||||
$this->assertEquals(array('flasher.min.css', 'toastr.min.css', 'noty.min.css'), $response->getStyles());
|
||||
$this->assertSame(['flasher.min.css', 'toastr.min.css', 'noty.min.css'], $response->getStyles());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testItAddsAdaptersOptionsToResponse()
|
||||
public function testItAddsAdaptersOptionsToResponse(): void
|
||||
{
|
||||
$response = new Response(array(), array());
|
||||
$response = new Response([], []);
|
||||
|
||||
$response->addOptions('flasher', array('position' => 'center', 'timeout' => 2500));
|
||||
$response->addOptions('toastr', array('sounds' => false));
|
||||
$response->addOptions('flasher', ['position' => 'center', 'timeout' => 2500]);
|
||||
$response->addOptions('toastr', ['sounds' => false]);
|
||||
|
||||
$this->assertEquals(array(
|
||||
'flasher' => array('position' => 'center', 'timeout' => 2500),
|
||||
'toastr' => array('sounds' => false),
|
||||
), $response->getOptions());
|
||||
$this->assertEquals([
|
||||
'flasher' => ['position' => 'center', 'timeout' => 2500],
|
||||
'toastr' => ['sounds' => false],
|
||||
], $response->getOptions());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testItTurnsTheResponseIntoAnArray()
|
||||
public function testItTurnsTheResponseIntoAnArray(): void
|
||||
{
|
||||
$envelopes = array();
|
||||
$envelopes = [];
|
||||
|
||||
$notification = new Notification();
|
||||
$notification->setMessage('success message');
|
||||
@@ -87,39 +69,38 @@ class ResponseTest extends TestCase
|
||||
$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));
|
||||
$response = new Response($envelopes, []);
|
||||
$response->setMainScript('flasher.min.js');
|
||||
$response->addScripts(['noty.min.js', 'toastr.min.js']);
|
||||
$response->addStyles(['noty.min.css', 'toastr.min.css']);
|
||||
$response->addOptions('flasher', ['position' => 'center', 'timeout' => 2500]);
|
||||
$response->addOptions('toastr', ['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),
|
||||
),
|
||||
);
|
||||
$expected = [
|
||||
'envelopes' => [
|
||||
[
|
||||
'type' => 'success',
|
||||
'title' => 'PHPFlasher',
|
||||
'message' => 'success message',
|
||||
'options' => [],
|
||||
'metadata' => [],
|
||||
],
|
||||
[
|
||||
'type' => 'warning',
|
||||
'title' => 'yoeunes/toastr',
|
||||
'message' => 'warning message',
|
||||
'options' => [],
|
||||
'metadata' => [],
|
||||
],
|
||||
],
|
||||
'scripts' => ['noty.min.js', 'toastr.min.js'],
|
||||
'styles' => ['noty.min.css', 'toastr.min.css'],
|
||||
'options' => [
|
||||
'flasher' => ['position' => 'center', 'timeout' => 2500],
|
||||
'toastr' => ['sounds' => false],
|
||||
],
|
||||
'context' => [],
|
||||
];
|
||||
|
||||
$this->assertEquals($expected, $response->toArray());
|
||||
}
|
||||
|
||||
@@ -1,27 +1,63 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the PHPFlasher package.
|
||||
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\Stamp;
|
||||
|
||||
use Flasher\Prime\Stamp\ContextStamp;
|
||||
use Flasher\Tests\Prime\TestCase;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class ContextStampTest extends TestCase
|
||||
final class ContextStampTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testContextStamp()
|
||||
public function testGetContextReturnsTheCorrectArray(): void
|
||||
{
|
||||
$stamp = new ContextStamp(array('component' => 'livewire'));
|
||||
$contextArray = ['key1' => 'value1', 'key2' => 'value2'];
|
||||
|
||||
$this->assertInstanceOf('Flasher\Prime\Stamp\StampInterface', $stamp);
|
||||
$this->assertInstanceOf('Flasher\Prime\Stamp\PresentableStampInterface', $stamp);
|
||||
$this->assertEquals(array('component' => 'livewire'), $stamp->getContext());
|
||||
$this->assertEquals(array('context' => array('component' => 'livewire')), $stamp->toArray());
|
||||
$contextStamp = new ContextStamp($contextArray);
|
||||
|
||||
$this->assertSame(
|
||||
$contextArray,
|
||||
$contextStamp->getContext(),
|
||||
'The getContext method did not return the expected array.'
|
||||
);
|
||||
}
|
||||
|
||||
public function testGetContextWithEmptyArray(): void
|
||||
{
|
||||
$contextArray = [];
|
||||
|
||||
$contextStamp = new ContextStamp($contextArray);
|
||||
|
||||
$this->assertSame(
|
||||
$contextArray,
|
||||
$contextStamp->getContext(),
|
||||
'The getContext method did not return an empty array with empty context.'
|
||||
);
|
||||
}
|
||||
|
||||
public function testToArrayReturnsContextInArray(): void
|
||||
{
|
||||
$contextArray = ['key1' => 'value1', 'key2' => 'value2'];
|
||||
|
||||
$contextStamp = new ContextStamp($contextArray);
|
||||
|
||||
$this->assertSame(
|
||||
['context' => $contextArray],
|
||||
$contextStamp->toArray(),
|
||||
'The toArray method did not return the expected array.'
|
||||
);
|
||||
}
|
||||
|
||||
public function testToArrayWithEmptyArray(): void
|
||||
{
|
||||
$contextArray = [];
|
||||
|
||||
$contextStamp = new ContextStamp($contextArray);
|
||||
|
||||
$this->assertSame(
|
||||
['context' => $contextArray],
|
||||
$contextStamp->toArray(),
|
||||
'The toArray method did not return an array with empty context as expected.'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,43 +1,74 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the PHPFlasher package.
|
||||
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\Stamp;
|
||||
|
||||
use Flasher\Prime\Stamp\CreatedAtStamp;
|
||||
use Flasher\Prime\Stamp\HopsStamp;
|
||||
use Flasher\Tests\Prime\TestCase;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class CreatedAtStampTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testCreatedAtStamp()
|
||||
{
|
||||
$createdAt = new \DateTime('2023-01-30 23:33:51');
|
||||
$stamp = new CreatedAtStamp($createdAt, 'Y-m-d H:i:s');
|
||||
private \DateTimeImmutable $time;
|
||||
|
||||
$this->assertInstanceOf('Flasher\Prime\Stamp\StampInterface', $stamp);
|
||||
$this->assertInstanceOf('Flasher\Prime\Stamp\PresentableStampInterface', $stamp);
|
||||
$this->assertInstanceOf('Flasher\Prime\Stamp\OrderableStampInterface', $stamp);
|
||||
$this->assertInstanceOf('DateTime', $stamp->getCreatedAt());
|
||||
$this->assertEquals('2023-01-30 23:33:51', $stamp->getCreatedAt()->format('Y-m-d H:i:s'));
|
||||
$this->assertEquals(array('created_at' => '2023-01-30 23:33:51'), $stamp->toArray());
|
||||
private CreatedAtStamp $createdAtStamp;
|
||||
|
||||
private string $format;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->time = new \DateTimeImmutable('2023-01-01 12:00:00');
|
||||
$this->format = 'Y-m-d H:i:s';
|
||||
$this->createdAtStamp = new CreatedAtStamp($this->time, $this->format);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* Test getCreatedAt method to ensure it returns correct DateTimeImmutable object.
|
||||
*/
|
||||
public function testCompare()
|
||||
public function testGetCreatedAt(): void
|
||||
{
|
||||
$createdAt1 = new CreatedAtStamp(new \DateTime('2023-01-30 23:35:49'));
|
||||
$createdAt2 = new CreatedAtStamp(new \DateTime('2023-01-30 23:36:06'));
|
||||
$createdAt = $this->createdAtStamp->getCreatedAt();
|
||||
$this->assertInstanceOf(\DateTimeImmutable::class, $createdAt);
|
||||
}
|
||||
|
||||
$this->assertEquals(-17, $createdAt1->compare($createdAt2));
|
||||
$this->assertEquals(1, $createdAt1->compare(new HopsStamp(1)));
|
||||
/**
|
||||
* Test if the format of the datetime object returned by getCreatedAt matches the expected format.
|
||||
*/
|
||||
public function testGetCreatedAtFormat(): void
|
||||
{
|
||||
$createdAt = $this->createdAtStamp->getCreatedAt();
|
||||
$formattedDate = $createdAt->format($this->format);
|
||||
$expectedDate = $this->time->format($this->format);
|
||||
$this->assertSame($expectedDate, $formattedDate);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test compare method to compare timestamps correctly.
|
||||
*/
|
||||
public function testCompare(): void
|
||||
{
|
||||
// Testing with a time exactly 1 second later
|
||||
$laterTime = $this->time->modify('+1 second');
|
||||
$laterStamp = new CreatedAtStamp($laterTime, $this->format);
|
||||
|
||||
// Testing with the same time
|
||||
$sameStamp = new CreatedAtStamp($this->time, $this->format);
|
||||
|
||||
// laterStamp should be "greater" than createdAtStamp
|
||||
$this->assertSame(-1, $this->createdAtStamp->compare($laterStamp));
|
||||
$this->assertSame(1, $laterStamp->compare($this->createdAtStamp));
|
||||
|
||||
// Comparing with the same time should result in 0
|
||||
$this->assertSame(0, $this->createdAtStamp->compare($sameStamp));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test toArray method to return correct array format.
|
||||
*/
|
||||
public function testToArray(): void
|
||||
{
|
||||
$arrayForm = $this->createdAtStamp->toArray();
|
||||
$this->assertArrayHasKey('created_at', $arrayForm);
|
||||
$this->assertSame($this->time->format($this->format), $arrayForm['created_at']);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,25 +1,27 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the PHPFlasher package.
|
||||
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\Stamp;
|
||||
|
||||
use Flasher\Prime\Stamp\DelayStamp;
|
||||
use Flasher\Tests\Prime\TestCase;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class DelayStampTest extends TestCase
|
||||
final class DelayStampTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testDelayStamp()
|
||||
{
|
||||
$stamp = new DelayStamp(2);
|
||||
private int $testDelay;
|
||||
private DelayStamp $instance;
|
||||
|
||||
$this->assertInstanceOf('Flasher\Prime\Stamp\StampInterface', $stamp);
|
||||
$this->assertEquals(2, $stamp->getDelay());
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->testDelay = 100;
|
||||
$this->instance = new DelayStamp($this->testDelay);
|
||||
}
|
||||
|
||||
public function testGetDelay(): void
|
||||
{
|
||||
$delay = $this->instance->getDelay();
|
||||
|
||||
$this->assertSame($this->testDelay, $delay);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,27 +1,23 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the PHPFlasher package.
|
||||
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\Stamp;
|
||||
|
||||
use Flasher\Prime\Stamp\HandlerStamp;
|
||||
use Flasher\Tests\Prime\TestCase;
|
||||
use Flasher\Prime\Stamp\PluginStamp;
|
||||
use Flasher\Prime\Stamp\PresentableStampInterface;
|
||||
use Flasher\Prime\Stamp\StampInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class HandlerStampTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testHandlerStamp()
|
||||
public function testHandlerStamp(): void
|
||||
{
|
||||
$stamp = new HandlerStamp('toastr');
|
||||
$stamp = new PluginStamp('toastr');
|
||||
|
||||
$this->assertInstanceOf('Flasher\Prime\Stamp\StampInterface', $stamp);
|
||||
$this->assertInstanceOf('Flasher\Prime\Stamp\PresentableStampInterface', $stamp);
|
||||
$this->assertEquals('toastr', $stamp->getHandler());
|
||||
$this->assertEquals(array('handler' => 'toastr'), $stamp->toArray());
|
||||
$this->assertInstanceOf(StampInterface::class, $stamp);
|
||||
$this->assertInstanceOf(PresentableStampInterface::class, $stamp);
|
||||
$this->assertSame('toastr', $stamp->getPlugin());
|
||||
$this->assertSame(['plugin' => 'toastr'], $stamp->toArray());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,25 +1,23 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the PHPFlasher package.
|
||||
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\Stamp;
|
||||
|
||||
use Flasher\Prime\Stamp\HopsStamp;
|
||||
use Flasher\Tests\Prime\TestCase;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class HopsStampTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
/*
|
||||
* Test to verify that calling getAmount on a HopsStamp instance
|
||||
* with a certain initial amount correctly returns that amount.
|
||||
*/
|
||||
public function testHopsStamp()
|
||||
public function testGetAmount(): void
|
||||
{
|
||||
$stamp = new HopsStamp(5);
|
||||
$initialAmount = 5;
|
||||
$hopsStamp = new HopsStamp($initialAmount);
|
||||
|
||||
$this->assertInstanceOf('Flasher\Prime\Stamp\StampInterface', $stamp);
|
||||
$this->assertEquals(5, $stamp->getAmount());
|
||||
$this->assertSame($initialAmount, $hopsStamp->getAmount());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\Stamp;
|
||||
|
||||
use Flasher\Prime\Stamp\HtmlStamp;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class HtmlStampTest extends TestCase
|
||||
{
|
||||
private string $htmlString;
|
||||
private HtmlStamp $htmlStamp;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->htmlString = '<div>Hello World</div>';
|
||||
$this->htmlStamp = new HtmlStamp($this->htmlString);
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing the getHtml method of the HtmlStamp class.
|
||||
*/
|
||||
public function testGetHtml(): void
|
||||
{
|
||||
$this->assertSame($this->htmlString, $this->htmlStamp->getHtml());
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing the toArray method of the HtmlStamp class.
|
||||
*/
|
||||
public function testToArray(): void
|
||||
{
|
||||
$expected = ['html' => $this->htmlString];
|
||||
$this->assertSame($expected, $this->htmlStamp->toArray());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\Stamp;
|
||||
|
||||
use Flasher\Prime\Notification\Envelope;
|
||||
use Flasher\Prime\Notification\Notification;
|
||||
use Flasher\Prime\Stamp\IdStamp;
|
||||
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class IdStampTest extends TestCase
|
||||
{
|
||||
use MockeryPHPUnitIntegration;
|
||||
|
||||
/**
|
||||
* TestCase for constructor and `getId`.
|
||||
*/
|
||||
public function testConstructorAndGetId(): void
|
||||
{
|
||||
// Test with null ID
|
||||
$ifStamp = new IdStamp();
|
||||
$this->assertIsString($ifStamp->getId());
|
||||
|
||||
// Test with known ID
|
||||
$knownId = 'KnownID123';
|
||||
$StampWithKnownId = new IdStamp($knownId);
|
||||
|
||||
$this->assertSame($knownId, $StampWithKnownId->getId());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test `toArray` method.
|
||||
*/
|
||||
public function testToArray(): void
|
||||
{
|
||||
$ifStamp = new IdStamp();
|
||||
$arrayRepresentation = $ifStamp->toArray();
|
||||
$this->assertIsArray($arrayRepresentation);
|
||||
$this->assertArrayHasKey('id', $arrayRepresentation);
|
||||
$this->assertSame($arrayRepresentation['id'], $ifStamp->getId());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests `indexById` function.
|
||||
*/
|
||||
public function testIndexById(): void
|
||||
{
|
||||
$stamp1 = new IdStamp('1111');
|
||||
$stamp2 = new IdStamp('2222');
|
||||
|
||||
$envelope1 = new Envelope(new Notification(), $stamp1);
|
||||
$envelope2 = new Envelope(new Notification(), $stamp2);
|
||||
|
||||
$map = IdStamp::indexById([$envelope1, $envelope2]);
|
||||
|
||||
$this->assertCount(2, $map);
|
||||
$this->assertArrayHasKey($stamp1->getId(), $map);
|
||||
$this->assertSame($envelope1, $map[$stamp1->getId()]);
|
||||
$this->assertArrayHasKey($stamp2->getId(), $map);
|
||||
$this->assertSame($envelope2, $map[$stamp2->getId()]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\Stamp;
|
||||
|
||||
use Flasher\Prime\Stamp\PluginStamp;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class PluginStampTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* Test that the getPlugin method in the PluginStamp class returns the correct
|
||||
* string value that was passed to the class constructor during instantiation.
|
||||
*/
|
||||
public function testGetPluginMethod(): void
|
||||
{
|
||||
$plugin = 'myPlugin';
|
||||
$pluginStamp = new PluginStamp($plugin);
|
||||
|
||||
$result = $pluginStamp->getPlugin();
|
||||
|
||||
$this->assertSame($plugin, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that the toArray method in PluginStamp class returns the correct
|
||||
* array with 'plugin' key that was passed to the class constructor during instantiation.
|
||||
*/
|
||||
public function testToArrayMethod(): void
|
||||
{
|
||||
$plugin = 'myPlugin';
|
||||
$pluginStamp = new PluginStamp($plugin);
|
||||
|
||||
$result = $pluginStamp->toArray();
|
||||
|
||||
$this->assertIsArray($result);
|
||||
$this->assertCount(1, $result);
|
||||
$this->assertArrayHasKey('plugin', $result);
|
||||
$this->assertSame($plugin, $result['plugin']);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\Stamp;
|
||||
|
||||
use Flasher\Prime\Stamp\PresenterStamp;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class PresenterStampTest extends TestCase
|
||||
{
|
||||
// Test for the getPattern method of the PresenterStamp class
|
||||
public function testGetPattern(): void
|
||||
{
|
||||
$pattern = '/test-pattern/';
|
||||
$presenterStamp = new PresenterStamp($pattern);
|
||||
$this->assertSame($pattern, $presenterStamp->getPattern());
|
||||
}
|
||||
|
||||
// Test for invalid pattern in PresenterStamp class
|
||||
public function testInvalidPatternThrowsException(): void
|
||||
{
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
$invalidPattern = '[invalid-pattern';
|
||||
$presenterStamp = new PresenterStamp($invalidPattern);
|
||||
}
|
||||
}
|
||||
@@ -1,26 +1,41 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the PHPFlasher package.
|
||||
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\Stamp;
|
||||
|
||||
use Flasher\Prime\Stamp\PresetStamp;
|
||||
use Flasher\Tests\Prime\TestCase;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class PresetStampTest extends TestCase
|
||||
final class PresetStampTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
* getPreset method test.
|
||||
*
|
||||
* This test case focuses on the proper retrieval of the preset value from the PresetStamp class.
|
||||
*/
|
||||
public function testPresetStamp()
|
||||
public function testGetPreset(): void
|
||||
{
|
||||
$stamp = new PresetStamp('entity_saved', array('resource' => 'resource'));
|
||||
$preset = 'preset_value';
|
||||
$parameters = ['parameter1' => 'value1'];
|
||||
|
||||
$this->assertInstanceOf('Flasher\Prime\Stamp\StampInterface', $stamp);
|
||||
$this->assertEquals('entity_saved', $stamp->getPreset());
|
||||
$this->assertEquals(array('resource' => 'resource'), $stamp->getParameters());
|
||||
$presetStamp = new PresetStamp($preset, $parameters);
|
||||
|
||||
$this->assertSame($preset, $presetStamp->getPreset());
|
||||
}
|
||||
|
||||
/**
|
||||
* getParameters method test.
|
||||
*
|
||||
* This test case focuses on the proper retrieval of parameters from the PresetStamp class.
|
||||
*/
|
||||
public function testGetParameters(): void
|
||||
{
|
||||
$preset = 'preset_value';
|
||||
$parameters = ['parameter1' => 'value1'];
|
||||
|
||||
$presetStamp = new PresetStamp($preset, $parameters);
|
||||
|
||||
$this->assertSame($parameters, $presetStamp->getParameters());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,41 +1,59 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the PHPFlasher package.
|
||||
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\Stamp;
|
||||
|
||||
use Flasher\Prime\Stamp\HopsStamp;
|
||||
use Flasher\Prime\Stamp\PriorityStamp;
|
||||
use Flasher\Tests\Prime\TestCase;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class PriorityStampTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
* Test case for getPriority method of PriorityStamp class.
|
||||
*/
|
||||
public function testPriorityStamp()
|
||||
public function testGetPriority(): void
|
||||
{
|
||||
$stamp = new PriorityStamp(5);
|
||||
// Define test priority
|
||||
$priority = 10;
|
||||
|
||||
$this->assertInstanceOf('Flasher\Prime\Stamp\StampInterface', $stamp);
|
||||
$this->assertInstanceOf('Flasher\Prime\Stamp\OrderableStampInterface', $stamp);
|
||||
$this->assertInstanceOf('Flasher\Prime\Stamp\PresentableStampInterface', $stamp);
|
||||
$this->assertEquals(5, $stamp->getPriority());
|
||||
$this->assertEquals(array('priority' => 5), $stamp->toArray());
|
||||
// Instantiate PriorityStamp
|
||||
$stamp = new PriorityStamp($priority);
|
||||
|
||||
// Check if the result of getPriority is as expected
|
||||
$this->assertSame($priority, $stamp->getPriority());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* Test case for compare method of PriorityStamp class.
|
||||
*/
|
||||
public function testCompare()
|
||||
public function testCompare(): void
|
||||
{
|
||||
$stamp1 = new PriorityStamp(1);
|
||||
$stamp2 = new PriorityStamp(2);
|
||||
// Define test priorities
|
||||
$priority1 = 10;
|
||||
$priority2 = 20;
|
||||
|
||||
$this->assertEquals(-1, $stamp1->compare($stamp2));
|
||||
$this->assertEquals(1, $stamp1->compare(new HopsStamp(1)));
|
||||
// Instantiate PriorityStamps
|
||||
$stamp1 = new PriorityStamp($priority1);
|
||||
$stamp2 = new PriorityStamp($priority2);
|
||||
|
||||
// Check if the result of compare is as expected
|
||||
$this->assertSame($priority1 - $priority2, $stamp1->compare($stamp2));
|
||||
$this->assertSame($priority2 - $priority1, $stamp2->compare($stamp1));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test case for toArray method of PriorityStamp class.
|
||||
*/
|
||||
public function testToArray(): void
|
||||
{
|
||||
// Define test priority
|
||||
$priority = 10;
|
||||
|
||||
// Instantiate PriorityStamp
|
||||
$stamp = new PriorityStamp($priority);
|
||||
|
||||
// Check if the result of toArray is as expected
|
||||
$this->assertSame(['priority' => $priority], $stamp->toArray());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,36 +1,63 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the PHPFlasher package.
|
||||
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\Stamp;
|
||||
|
||||
use Flasher\Prime\Stamp\TranslationStamp;
|
||||
use Flasher\Tests\Prime\TestCase;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class TranslationStampTest extends TestCase
|
||||
final class TranslationStampTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
* Test if the getParameters method returns correct parameters.
|
||||
*/
|
||||
public function testTranslationStamp()
|
||||
public function testGetParameters(): void
|
||||
{
|
||||
$stamp = new TranslationStamp(array('foo' => 'bar'), 'ar');
|
||||
$parameters = ['param1' => 'value1', 'param2' => 'value2'];
|
||||
|
||||
$this->assertInstanceOf('Flasher\Prime\Stamp\StampInterface', $stamp);
|
||||
$this->assertEquals(array('foo' => 'bar'), $stamp->getParameters());
|
||||
$this->assertEquals('ar', $stamp->getLocale());
|
||||
// Create a TranslationStamp instance with parameters
|
||||
$translationStamp = new TranslationStamp($parameters);
|
||||
|
||||
// Assert that the getParameters method should return the same parameters as those given to the constructor
|
||||
$this->assertSame($parameters, $translationStamp->getParameters());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* Test if the getParameters method returns an empty array when no parameters are provided.
|
||||
*/
|
||||
public function testParametersOrder()
|
||||
public function testGetParametersEmpty(): void
|
||||
{
|
||||
$parameters = TranslationStamp::parametersOrder('ar');
|
||||
// Create a TranslationStamp instance without providing parameters
|
||||
$translationStamp = new TranslationStamp();
|
||||
|
||||
$this->assertEquals(array('locale' => 'ar', 'parameters' => array()), $parameters);
|
||||
// Assert that the getParameters method should return an empty array
|
||||
$this->assertSame([], $translationStamp->getParameters());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if the getLocale method returns the correct locale.
|
||||
*/
|
||||
public function testGetLocale(): void
|
||||
{
|
||||
$locale = 'en_US';
|
||||
|
||||
// Create a TranslationStamp instance with a locale
|
||||
$translationStamp = new TranslationStamp([], $locale);
|
||||
|
||||
// Assert that the getLocale method should return the same locale as that given to the constructor
|
||||
$this->assertSame($locale, $translationStamp->getLocale());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if the getLocale method returns null when no locale is provided.
|
||||
*/
|
||||
public function testGetLocaleNull(): void
|
||||
{
|
||||
// Create a TranslationStamp instance without providing a locale
|
||||
$translationStamp = new TranslationStamp();
|
||||
|
||||
// Assert that the getLocale method should return null
|
||||
$this->assertNull($translationStamp->getLocale());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,25 +1,27 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the PHPFlasher package.
|
||||
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\Stamp;
|
||||
|
||||
use Flasher\Prime\Stamp\UnlessStamp;
|
||||
use Flasher\Tests\Prime\TestCase;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class UnlessStampTest extends TestCase
|
||||
final class UnlessStampTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testUnlessStamp()
|
||||
// Test case for getCondition() method
|
||||
public function testGetCondition(): void
|
||||
{
|
||||
$stamp = new UnlessStamp(true);
|
||||
// Create a testable instance of UnlessStamp class
|
||||
$unlessStamp = new UnlessStamp(true);
|
||||
|
||||
$this->assertInstanceOf('Flasher\Prime\Stamp\StampInterface', $stamp);
|
||||
$this->assertTrue($stamp->getCondition());
|
||||
// Assert that getCondition correctly returns the value passed in the constructor
|
||||
$this->assertTrue($unlessStamp->getCondition());
|
||||
|
||||
// Create another testable instance of UnlessStamp class
|
||||
$unlessStamp = new UnlessStamp(false);
|
||||
|
||||
// Again assert that getCondition correctly returns the value passed in the constructor
|
||||
$this->assertFalse($unlessStamp->getCondition());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,29 +1,24 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the PHPFlasher package.
|
||||
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\Stamp;
|
||||
|
||||
use Flasher\Prime\Stamp\UuidStamp;
|
||||
use Flasher\Tests\Prime\TestCase;
|
||||
use Flasher\Prime\Stamp\IdStamp;
|
||||
use Flasher\Prime\Stamp\StampInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class UuidStampTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testUuidStamp()
|
||||
public function testUuidStamp(): void
|
||||
{
|
||||
$stamp = new UuidStamp();
|
||||
$stamp = new IdStamp();
|
||||
|
||||
$this->assertInstanceOf('Flasher\Prime\Stamp\StampInterface', $stamp);
|
||||
$this->assertNotEmpty($stamp->getUuid());
|
||||
$this->assertInstanceOf(StampInterface::class, $stamp);
|
||||
$this->assertNotEmpty($stamp->getId());
|
||||
|
||||
$stamp = new UuidStamp('aaaa-bbbb-cccc');
|
||||
$this->assertEquals('aaaa-bbbb-cccc', $stamp->getUuid());
|
||||
$this->assertEquals(array('uuid' => 'aaaa-bbbb-cccc'), $stamp->toArray());
|
||||
$stamp = new IdStamp('aaaa-bbbb-cccc');
|
||||
$this->assertSame('aaaa-bbbb-cccc', $stamp->getId());
|
||||
$this->assertSame(['id' => 'aaaa-bbbb-cccc'], $stamp->toArray());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the PHPFlasher package.
|
||||
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
|
||||
*/
|
||||
|
||||
namespace Flasher\Tests\Prime\Stamp;
|
||||
|
||||
use Flasher\Prime\Stamp\ViewStamp;
|
||||
use Flasher\Tests\Prime\TestCase;
|
||||
|
||||
class ViewStampTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testViewStamp()
|
||||
{
|
||||
$stamp = new ViewStamp('template.html.twig');
|
||||
|
||||
$this->assertInstanceOf('Flasher\Prime\Stamp\StampInterface', $stamp);
|
||||
$this->assertEquals('template.html.twig', $stamp->getView());
|
||||
$this->assertEquals(array('view' => 'template.html.twig'), $stamp->toArray());
|
||||
}
|
||||
}
|
||||
@@ -1,25 +1,20 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the PHPFlasher package.
|
||||
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\Stamp;
|
||||
|
||||
use Flasher\Prime\Stamp\WhenStamp;
|
||||
use Flasher\Tests\Prime\TestCase;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class WhenStampTest extends TestCase
|
||||
final class WhenStampTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testWhenStamp()
|
||||
public function testGetCondition(): void
|
||||
{
|
||||
$stamp = new WhenStamp(true);
|
||||
$whenStamp = new WhenStamp(true);
|
||||
$this->assertTrue($whenStamp->getCondition());
|
||||
|
||||
$this->assertInstanceOf('Flasher\Prime\Stamp\StampInterface', $stamp);
|
||||
$this->assertTrue($stamp->getCondition());
|
||||
$whenStamp = new WhenStamp(false);
|
||||
$this->assertFalse($whenStamp->getCondition());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,33 +1,47 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the PHPFlasher package.
|
||||
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\Storage\Bag;
|
||||
|
||||
use Flasher\Prime\Notification\Envelope;
|
||||
use Flasher\Prime\Notification\Notification;
|
||||
use Flasher\Prime\Storage\Bag\ArrayBag;
|
||||
use Flasher\Tests\Prime\TestCase;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class ArrayBagTest extends TestCase
|
||||
final class ArrayBagTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testArrayBag()
|
||||
private ArrayBag $bag;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$bag = new ArrayBag();
|
||||
$this->bag = new ArrayBag();
|
||||
}
|
||||
|
||||
$envelopes = array(
|
||||
new Envelope(new Notification()),
|
||||
new Envelope(new Notification()),
|
||||
);
|
||||
/**
|
||||
* Test the `get` method of the `ArrayBag` class.
|
||||
* It should return an array of envelopes that have been set.
|
||||
*/
|
||||
public function testGet(): void
|
||||
{
|
||||
$this->assertSame([], $this->bag->get());
|
||||
|
||||
$bag->set($envelopes);
|
||||
$envelope = new Envelope(new Notification());
|
||||
$this->bag->set([$envelope]);
|
||||
$this->assertSame([$envelope], $this->bag->get());
|
||||
}
|
||||
|
||||
$this->assertEquals($envelopes, $bag->get());
|
||||
/**
|
||||
* Test the `set` method of the `ArrayBag` class.
|
||||
* It should set the envelopes in the bag.
|
||||
*/
|
||||
public function testSet(): void
|
||||
{
|
||||
$envelope = new Envelope(new Notification());
|
||||
|
||||
$this->bag->set([$envelope]);
|
||||
|
||||
$envelopes = $this->bag->get();
|
||||
$this->assertSame([$envelope], $envelopes);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,33 +1,34 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the PHPFlasher package.
|
||||
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\Storage\Bag;
|
||||
|
||||
use Flasher\Prime\Notification\Envelope;
|
||||
use Flasher\Prime\Notification\Notification;
|
||||
use Flasher\Prime\Storage\Bag\StaticBag;
|
||||
use Flasher\Tests\Prime\TestCase;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class StaticBagTest extends TestCase
|
||||
final class StaticBagTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testStaticBag()
|
||||
private StaticBag $staticBag;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$bag = new StaticBag();
|
||||
$this->staticBag = new StaticBag();
|
||||
}
|
||||
|
||||
$envelopes = array(
|
||||
public function testGetAndSetMethods(): void
|
||||
{
|
||||
$this->assertSame([], $this->staticBag->get());
|
||||
|
||||
$envelopes = [
|
||||
new Envelope(new Notification()),
|
||||
new Envelope(new Notification()),
|
||||
);
|
||||
];
|
||||
|
||||
$bag->set($envelopes);
|
||||
$this->staticBag->set($envelopes);
|
||||
|
||||
$this->assertEquals($envelopes, $bag->get());
|
||||
$this->assertEquals($envelopes, $this->staticBag->get());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\Storage\Filter;
|
||||
|
||||
use Flasher\Prime\Exception\CriteriaNotRegisteredException;
|
||||
use Flasher\Prime\Storage\Filter\Criteria\CriteriaInterface;
|
||||
use Flasher\Prime\Storage\Filter\Criteria\LimitCriteria;
|
||||
use Flasher\Prime\Storage\Filter\Filter;
|
||||
use Flasher\Prime\Storage\Filter\FilterFactory;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class FilterFactoryTest extends TestCase
|
||||
{
|
||||
private FilterFactory $filterFactory;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->filterFactory = new FilterFactory();
|
||||
}
|
||||
|
||||
public function testConstructor(): void
|
||||
{
|
||||
$reflect = new \ReflectionClass(FilterFactory::class);
|
||||
$reflectValue = $reflect->getProperty('criteria');
|
||||
|
||||
/** @var array<string, CriteriaInterface> $value */
|
||||
$value = $reflectValue->getValue($this->filterFactory);
|
||||
|
||||
$this->assertArrayHasKey('priority', $value);
|
||||
$this->assertArrayHasKey('hops', $value);
|
||||
$this->assertArrayHasKey('delay', $value);
|
||||
$this->assertArrayHasKey('order_by', $value);
|
||||
$this->assertArrayHasKey('limit', $value);
|
||||
$this->assertArrayHasKey('stamps', $value);
|
||||
$this->assertArrayHasKey('filter', $value);
|
||||
$this->assertArrayHasKey('presenter', $value);
|
||||
}
|
||||
|
||||
public function testCreateFilter(): void
|
||||
{
|
||||
$config = [
|
||||
'limit' => 5,
|
||||
];
|
||||
|
||||
$filter = $this->filterFactory->createFilter($config);
|
||||
|
||||
$reflect = new \ReflectionClass(Filter::class);
|
||||
$reflectValue = $reflect->getProperty('criteriaChain');
|
||||
|
||||
/** @var CriteriaInterface[] $value */
|
||||
$value = $reflectValue->getValue($filter);
|
||||
|
||||
$this->assertInstanceOf(LimitCriteria::class, $value[0]);
|
||||
}
|
||||
|
||||
public function testCreateFilterWithInvalidCriteriaName(): void
|
||||
{
|
||||
$this->expectException(CriteriaNotRegisteredException::class);
|
||||
|
||||
$config = ['invalid_criteria_name' => 'invalid_criteria_value'];
|
||||
$this->filterFactory->createFilter($config);
|
||||
}
|
||||
|
||||
public function testAddCriteria(): void
|
||||
{
|
||||
$reflect = new \ReflectionClass(FilterFactory::class);
|
||||
$reflectValue = $reflect->getProperty('criteria');
|
||||
|
||||
$this->filterFactory->addCriteria('custom_criteria', fn () => new class() implements CriteriaInterface {
|
||||
public function apply(array $envelopes): array
|
||||
{
|
||||
return $envelopes;
|
||||
}
|
||||
});
|
||||
|
||||
/** @var array<string, \Closure> $value */
|
||||
$value = $reflectValue->getValue($this->filterFactory);
|
||||
|
||||
$this->assertArrayHasKey('custom_criteria', $value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\Storage\Filter;
|
||||
|
||||
use Flasher\Prime\Notification\Envelope;
|
||||
use Flasher\Prime\Notification\Notification;
|
||||
use Flasher\Prime\Stamp\IdStamp;
|
||||
use Flasher\Prime\Storage\Filter\Criteria\CriteriaInterface;
|
||||
use Flasher\Prime\Storage\Filter\Filter;
|
||||
use Mockery\Adapter\Phpunit\MockeryTestCase;
|
||||
|
||||
final class FilterTest extends MockeryTestCase
|
||||
{
|
||||
private Filter $filter;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->filter = new Filter();
|
||||
}
|
||||
|
||||
public function testApply(): void
|
||||
{
|
||||
$envelopes = [
|
||||
new Envelope(new Notification(), new IdStamp('1111')),
|
||||
new Envelope(new Notification(), new IdStamp('2222')),
|
||||
];
|
||||
|
||||
$expectedEnvelopes = [
|
||||
new Envelope(new Notification(), new IdStamp('1111')),
|
||||
new Envelope(new Notification(), new IdStamp('2222')),
|
||||
];
|
||||
|
||||
$criteria = \Mockery::mock(CriteriaInterface::class);
|
||||
$criteria->allows()
|
||||
->apply($envelopes)
|
||||
->andReturns($expectedEnvelopes);
|
||||
|
||||
$this->filter->addCriteria($criteria);
|
||||
|
||||
$this->assertSame($expectedEnvelopes, $this->filter->apply($envelopes));
|
||||
}
|
||||
|
||||
public function testAddCriteria(): void
|
||||
{
|
||||
$criteria = \Mockery::mock(CriteriaInterface::class);
|
||||
|
||||
$this->filter->addCriteria($criteria);
|
||||
|
||||
$reflection = new \ReflectionClass($this->filter);
|
||||
$property = $reflection->getProperty('criteriaChain');
|
||||
|
||||
/** @var CriteriaInterface[] $criteriaChain */
|
||||
$criteriaChain = $property->getValue($this->filter);
|
||||
|
||||
$this->assertContains($criteria, $criteriaChain);
|
||||
}
|
||||
}
|
||||
@@ -1,84 +1,71 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the PHPFlasher package.
|
||||
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\Storage;
|
||||
|
||||
use Flasher\Prime\Notification\Envelope;
|
||||
use Flasher\Prime\Notification\Notification;
|
||||
use Flasher\Prime\Stamp\UuidStamp;
|
||||
use Flasher\Prime\Storage\StorageBag;
|
||||
use Flasher\Tests\Prime\TestCase;
|
||||
use Flasher\Prime\Stamp\IdStamp;
|
||||
use Flasher\Prime\Storage\Bag\ArrayBag;
|
||||
use Flasher\Prime\Storage\Storage;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class StorageBagTest extends TestCase
|
||||
final class StorageBagTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testAddEnvelopes()
|
||||
public function testAddEnvelopes(): void
|
||||
{
|
||||
$envelopes = array(
|
||||
array(
|
||||
new Envelope(new Notification(), new UuidStamp('1111')),
|
||||
new Envelope(new Notification(), new UuidStamp('2222')),
|
||||
),
|
||||
array(
|
||||
new Envelope(new Notification(), new UuidStamp('3333')),
|
||||
new Envelope(new Notification(), new UuidStamp('4444')),
|
||||
),
|
||||
);
|
||||
$envelopes = [
|
||||
[
|
||||
new Envelope(new Notification(), new IdStamp('1111')),
|
||||
new Envelope(new Notification(), new IdStamp('2222')),
|
||||
],
|
||||
[
|
||||
new Envelope(new Notification(), new IdStamp('3333')),
|
||||
new Envelope(new Notification(), new IdStamp('4444')),
|
||||
],
|
||||
];
|
||||
|
||||
$storageBag = new StorageBag();
|
||||
$storageBag->add($envelopes[0]);
|
||||
$storageBag->add($envelopes[1]);
|
||||
$storageBag = new Storage(new ArrayBag());
|
||||
$storageBag->add(...$envelopes[0]);
|
||||
$storageBag->add(...$envelopes[1]);
|
||||
|
||||
$this->assertEquals(array_merge($envelopes[0], $envelopes[1]), $storageBag->all());
|
||||
$this->assertEquals([...$envelopes[0], ...$envelopes[1]], $storageBag->all());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testUpdateEnvelopes()
|
||||
public function testUpdateEnvelopes(): void
|
||||
{
|
||||
$envelopes = array(
|
||||
array(
|
||||
new Envelope(new Notification(), new UuidStamp('1111')),
|
||||
new Envelope(new Notification(), new UuidStamp('2222')),
|
||||
),
|
||||
array(
|
||||
new Envelope(new Notification(), new UuidStamp('3333')),
|
||||
new Envelope(new Notification(), new UuidStamp('4444')),
|
||||
),
|
||||
);
|
||||
$envelopes = [
|
||||
[
|
||||
new Envelope(new Notification(), new IdStamp('1111')),
|
||||
new Envelope(new Notification(), new IdStamp('2222')),
|
||||
],
|
||||
[
|
||||
new Envelope(new Notification(), new IdStamp('3333')),
|
||||
new Envelope(new Notification(), new IdStamp('4444')),
|
||||
],
|
||||
];
|
||||
|
||||
$storageBag = new StorageBag();
|
||||
$storageBag->update($envelopes[0]);
|
||||
$storageBag->update($envelopes[1]);
|
||||
$storageBag = new Storage(new ArrayBag());
|
||||
$storageBag->update(...$envelopes[0]);
|
||||
$storageBag->update(...$envelopes[1]);
|
||||
|
||||
$this->assertEquals(array_merge($envelopes[0], $envelopes[1]), $storageBag->all());
|
||||
$this->assertEquals([...$envelopes[0], ...$envelopes[1]], $storageBag->all());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testRemoveEnvelopes()
|
||||
public function testRemoveEnvelopes(): void
|
||||
{
|
||||
$envelopes = array(
|
||||
new Envelope(new Notification(), new UuidStamp('1111')),
|
||||
new Envelope(new Notification(), new UuidStamp('2222')),
|
||||
new Envelope(new Notification(), new UuidStamp('3333')),
|
||||
new Envelope(new Notification(), new UuidStamp('4444')),
|
||||
);
|
||||
$envelopes = [
|
||||
new Envelope(new Notification(), new IdStamp('1111')),
|
||||
new Envelope(new Notification(), new IdStamp('2222')),
|
||||
new Envelope(new Notification(), new IdStamp('3333')),
|
||||
new Envelope(new Notification(), new IdStamp('4444')),
|
||||
];
|
||||
|
||||
$storageBag = new StorageBag();
|
||||
$storageBag->add($envelopes);
|
||||
$storageBag = new Storage(new ArrayBag());
|
||||
$storageBag->add(...$envelopes);
|
||||
|
||||
$storageBag->remove(array(
|
||||
new Envelope(new Notification(), new UuidStamp('2222')),
|
||||
));
|
||||
$storageBag->remove(new Envelope(new Notification(), new IdStamp('2222')));
|
||||
|
||||
unset($envelopes[1]);
|
||||
|
||||
|
||||
@@ -1,138 +1,125 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the PHPFlasher package.
|
||||
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\Storage;
|
||||
|
||||
use Flasher\Prime\EventDispatcher\Event\FilterEvent;
|
||||
use Flasher\Prime\EventDispatcher\Event\PersistEvent;
|
||||
use Flasher\Prime\EventDispatcher\Event\PostPersistEvent;
|
||||
use Flasher\Prime\EventDispatcher\Event\PostRemoveEvent;
|
||||
use Flasher\Prime\EventDispatcher\Event\PostUpdateEvent;
|
||||
use Flasher\Prime\EventDispatcher\Event\RemoveEvent;
|
||||
use Flasher\Prime\EventDispatcher\Event\UpdateEvent;
|
||||
use Flasher\Prime\EventDispatcher\EventDispatcherInterface;
|
||||
use Flasher\Prime\Notification\Envelope;
|
||||
use Flasher\Prime\Notification\Notification;
|
||||
use Flasher\Prime\Stamp\DelayStamp;
|
||||
use Flasher\Prime\Stamp\HopsStamp;
|
||||
use Flasher\Prime\Stamp\UuidStamp;
|
||||
use Flasher\Prime\Stamp\IdStamp;
|
||||
use Flasher\Prime\Storage\Filter\Filter;
|
||||
use Flasher\Prime\Storage\Filter\FilterFactoryInterface;
|
||||
use Flasher\Prime\Storage\StorageInterface;
|
||||
use Flasher\Prime\Storage\StorageManager;
|
||||
use Flasher\Tests\Prime\TestCase;
|
||||
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
|
||||
use Mockery\MockInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class StorageManagerTest extends TestCase
|
||||
final class StorageManagerTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testGetAllStoredEnvelopes()
|
||||
use MockeryPHPUnitIntegration;
|
||||
|
||||
private MockInterface&StorageInterface $storageMock;
|
||||
private MockInterface&FilterFactoryInterface $filterFactoryMock;
|
||||
private MockInterface&EventDispatcherInterface $eventDispatcherMock;
|
||||
private StorageManager $storageManager;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$envelopes = array(
|
||||
new Envelope(new Notification(), new UuidStamp('1111')),
|
||||
new Envelope(new Notification(), new UuidStamp('2222')),
|
||||
new Envelope(new Notification(), new UuidStamp('3333')),
|
||||
new Envelope(new Notification(), new UuidStamp('4444')),
|
||||
);
|
||||
$this->storageMock = \Mockery::mock(StorageInterface::class);
|
||||
$this->filterFactoryMock = \Mockery::mock(FilterFactoryInterface::class);
|
||||
$this->eventDispatcherMock = \Mockery::mock(EventDispatcherInterface::class);
|
||||
|
||||
$storage = $this->getMockBuilder('Flasher\Prime\Storage\StorageInterface')->getMock();
|
||||
$storage->expects($this->once())->method('all')->willReturn($envelopes);
|
||||
|
||||
$storageManager = new StorageManager($storage);
|
||||
|
||||
$this->assertEquals($envelopes, $storageManager->all());
|
||||
$this->storageManager = new StorageManager($this->storageMock, $this->eventDispatcherMock, $this->filterFactoryMock);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testGetFilteredEnvelopes()
|
||||
public function testAll(): void
|
||||
{
|
||||
$envelopes = array(
|
||||
new Envelope(new Notification(), new UuidStamp('1111')),
|
||||
new Envelope(new Notification(), new UuidStamp('2222'), new HopsStamp(1), new DelayStamp(0)),
|
||||
new Envelope(new Notification(), new UuidStamp('3333')),
|
||||
new Envelope(new Notification(), new UuidStamp('4444')),
|
||||
);
|
||||
$envelopes = [
|
||||
new Envelope(new Notification(), new IdStamp('1111')),
|
||||
new Envelope(new Notification(), new IdStamp('2222')),
|
||||
new Envelope(new Notification(), new IdStamp('3333')),
|
||||
new Envelope(new Notification(), new IdStamp('4444')),
|
||||
];
|
||||
|
||||
$storage = $this->getMockBuilder('Flasher\Prime\Storage\StorageInterface')->getMock();
|
||||
$storage->expects($this->once())->method('all')->willReturn($envelopes);
|
||||
$this->storageMock->expects()->all()->once()->andReturns($envelopes);
|
||||
|
||||
$storageManager = new StorageManager($storage);
|
||||
|
||||
$this->assertEquals(array($envelopes[1]), $storageManager->filter());
|
||||
$this->assertSame($envelopes, $this->storageManager->all());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testAddEnvelopes()
|
||||
public function testGetFilteredEnvelopes(): void
|
||||
{
|
||||
$envelopes = array(
|
||||
new Envelope(new Notification()),
|
||||
new Envelope(new Notification()),
|
||||
new Envelope(new Notification()),
|
||||
new Envelope(new Notification()),
|
||||
);
|
||||
$envelopes = [
|
||||
new Envelope(new Notification(), new IdStamp('1111')),
|
||||
new Envelope(new Notification(), [new IdStamp('2222'), new HopsStamp(1), new DelayStamp(0)]),
|
||||
new Envelope(new Notification(), new IdStamp('3333')),
|
||||
new Envelope(new Notification(), new IdStamp('4444')),
|
||||
];
|
||||
|
||||
$storageManager = new StorageManager();
|
||||
$storageManager->add($envelopes);
|
||||
$this->storageMock->expects()->all()->once()->andReturns($envelopes);
|
||||
$this->eventDispatcherMock->expects()->dispatch(\Mockery::type(FilterEvent::class))->once();
|
||||
$this->filterFactoryMock->expects()->createFilter([])->andReturns(new Filter());
|
||||
|
||||
$this->assertEquals($envelopes, $storageManager->all());
|
||||
$this->assertSame($envelopes, $this->storageManager->filter());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testUpdateEnvelopes()
|
||||
public function testAdd(): void
|
||||
{
|
||||
$envelopes = array(
|
||||
new Envelope(new Notification()),
|
||||
new Envelope(new Notification()),
|
||||
new Envelope(new Notification()),
|
||||
new Envelope(new Notification()),
|
||||
);
|
||||
$envelopes = [
|
||||
new Envelope(new Notification(), new IdStamp('1111')),
|
||||
new Envelope(new Notification(), new IdStamp('2222')),
|
||||
];
|
||||
|
||||
$storageManager = new StorageManager();
|
||||
$storageManager->update($envelopes);
|
||||
$this->eventDispatcherMock->expects()->dispatch(\Mockery::type(PersistEvent::class))->once();
|
||||
$this->storageMock->expects()->add(...$envelopes)->once();
|
||||
$this->eventDispatcherMock->expects()->dispatch(\Mockery::type(PostPersistEvent::class))->once();
|
||||
|
||||
$this->assertEquals($envelopes, $storageManager->all());
|
||||
$this->storageManager->add(...$envelopes);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testRemoveEnvelopes()
|
||||
public function testUpdate(): void
|
||||
{
|
||||
$envelopes = array(
|
||||
new Envelope(new Notification(), new UuidStamp('1111')),
|
||||
new Envelope(new Notification(), new UuidStamp('2222')),
|
||||
new Envelope(new Notification(), new UuidStamp('3333')),
|
||||
new Envelope(new Notification(), new UuidStamp('4444')),
|
||||
);
|
||||
$envelopes = [
|
||||
new Envelope(new Notification(), new IdStamp('5555')),
|
||||
new Envelope(new Notification(), new IdStamp('6666')),
|
||||
];
|
||||
|
||||
$storageManager = new StorageManager();
|
||||
$storageManager->add($envelopes);
|
||||
$this->eventDispatcherMock->expects()->dispatch(\Mockery::type(UpdateEvent::class))->once();
|
||||
$this->storageMock->expects()->update(...$envelopes)->once();
|
||||
$this->eventDispatcherMock->expects()->dispatch(\Mockery::type(PostUpdateEvent::class))->once();
|
||||
|
||||
$storageManager->remove(array(
|
||||
new Envelope(new Notification(), new UuidStamp('2222')),
|
||||
new Envelope(new Notification(), new UuidStamp('3333')),
|
||||
));
|
||||
|
||||
$this->assertEquals(array($envelopes[0], $envelopes[3]), $storageManager->all());
|
||||
$this->storageManager->update(...$envelopes);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testClearEnvelopes()
|
||||
public function testRemove(): void
|
||||
{
|
||||
$envelopes = array(
|
||||
new Envelope(new Notification()),
|
||||
new Envelope(new Notification()),
|
||||
new Envelope(new Notification()),
|
||||
new Envelope(new Notification()),
|
||||
);
|
||||
$envelopesToRemove = [
|
||||
new Envelope(new Notification(), new IdStamp('7777')),
|
||||
new Envelope(new Notification(), new IdStamp('8888')),
|
||||
];
|
||||
|
||||
$storageManager = new StorageManager();
|
||||
$storageManager->add($envelopes);
|
||||
$this->eventDispatcherMock->expects()->dispatch(\Mockery::type(RemoveEvent::class))->once();
|
||||
$this->storageMock->expects()->remove(...$envelopesToRemove)->once();
|
||||
$this->storageMock->expects()->update()->once();
|
||||
$this->eventDispatcherMock->expects()->dispatch(\Mockery::type(PostRemoveEvent::class))->once();
|
||||
|
||||
$storageManager->clear();
|
||||
$this->storageManager->remove(...$envelopesToRemove);
|
||||
}
|
||||
|
||||
$this->assertEquals(array(), $storageManager->all());
|
||||
public function testClear(): void
|
||||
{
|
||||
$this->storageMock->expects()->clear()->once();
|
||||
|
||||
$this->storageManager->clear();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\Storage;
|
||||
|
||||
use Flasher\Prime\Notification\Envelope;
|
||||
use Flasher\Prime\Notification\Notification;
|
||||
use Flasher\Prime\Stamp\IdStamp;
|
||||
use Flasher\Prime\Storage\Bag\ArrayBag;
|
||||
use Flasher\Prime\Storage\Storage;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class StorageTest extends TestCase
|
||||
{
|
||||
private Storage $storage;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->storage = new Storage(new ArrayBag());
|
||||
}
|
||||
|
||||
public function testAllMethod(): void
|
||||
{
|
||||
$envelopes = [
|
||||
[
|
||||
new Envelope(new Notification(), new IdStamp('1111')),
|
||||
new Envelope(new Notification(), new IdStamp('2222')),
|
||||
],
|
||||
[
|
||||
new Envelope(new Notification(), new IdStamp('3333')),
|
||||
new Envelope(new Notification(), new IdStamp('4444')),
|
||||
],
|
||||
];
|
||||
|
||||
$this->storage->add(...$envelopes[0]);
|
||||
$this->storage->add(...$envelopes[1]);
|
||||
|
||||
$this->assertEquals([...$envelopes[0], ...$envelopes[1]], $this->storage->all());
|
||||
$this->assertNotEquals(array_reverse([...$envelopes[0], ...$envelopes[1]]), $this->storage->all());
|
||||
}
|
||||
|
||||
public function testAddMethod(): void
|
||||
{
|
||||
$envelope = new Envelope(new Notification(), new IdStamp('1111'));
|
||||
|
||||
$this->storage->add($envelope);
|
||||
|
||||
$this->assertContains($envelope, $this->storage->all());
|
||||
}
|
||||
|
||||
public function testUpdateMethod(): void
|
||||
{
|
||||
$envelopes = [
|
||||
new Envelope(new Notification(), new IdStamp('1111')),
|
||||
new Envelope(new Notification(), new IdStamp('2222')),
|
||||
];
|
||||
|
||||
$this->storage->add(...$envelopes);
|
||||
$this->assertContains($envelopes[1], $this->storage->all());
|
||||
|
||||
$envelopes[1] = new Envelope(new Notification(), new IdStamp('3333'));
|
||||
|
||||
$this->storage->update(...$envelopes);
|
||||
$this->assertContains($envelopes[1], $this->storage->all());
|
||||
$this->assertNotContains('Notification2', $this->storage->all());
|
||||
}
|
||||
|
||||
public function testRemoveMethod(): void
|
||||
{
|
||||
$envelopes = [
|
||||
new Envelope(new Notification(), new IdStamp('1111')),
|
||||
new Envelope(new Notification(), new IdStamp('2222')),
|
||||
];
|
||||
|
||||
$this->storage->add(...$envelopes);
|
||||
|
||||
$this->assertContains($envelopes[1], $this->storage->all());
|
||||
|
||||
$this->storage->remove($envelopes[1]);
|
||||
|
||||
$this->assertNotContains($envelopes[1], $this->storage->all());
|
||||
}
|
||||
|
||||
public function testClearMethod(): void
|
||||
{
|
||||
$envelopes = [
|
||||
new Envelope(new Notification(), new IdStamp('1111')),
|
||||
new Envelope(new Notification(), new IdStamp('2222')),
|
||||
];
|
||||
|
||||
$this->storage->add(...$envelopes);
|
||||
|
||||
$this->assertNotEmpty($this->storage->all());
|
||||
|
||||
$this->storage->clear();
|
||||
|
||||
$this->assertEmpty($this->storage->all());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\Support\Traits;
|
||||
|
||||
use Flasher\Prime\Support\Traits\ForwardsCalls;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class ForwardsCallsTest extends TestCase
|
||||
{
|
||||
public function testSuccessfulMethodForwarding(): void
|
||||
{
|
||||
$testable = new class() {
|
||||
use ForwardsCalls;
|
||||
};
|
||||
|
||||
$secondary = new class() {
|
||||
public function someMethod(): string
|
||||
{
|
||||
return 'method result';
|
||||
}
|
||||
};
|
||||
|
||||
$reflection = new \ReflectionClass($testable::class);
|
||||
$method = $reflection->getMethod('forwardCallTo');
|
||||
|
||||
$result = $method->invokeArgs($testable, [$secondary, 'someMethod', []]);
|
||||
$this->assertSame('method result', $result);
|
||||
}
|
||||
|
||||
public function testForwardingAndReturningThis(): void
|
||||
{
|
||||
$testable = new class() {
|
||||
use ForwardsCalls;
|
||||
};
|
||||
|
||||
$secondary = new class() {
|
||||
public function selfReturningMethod(): self
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
};
|
||||
|
||||
$reflection = new \ReflectionClass($testable::class);
|
||||
$method = $reflection->getMethod('forwardDecoratedCallTo');
|
||||
|
||||
$result = $method->invokeArgs($testable, [$secondary, 'selfReturningMethod', []]);
|
||||
$this->assertNotSame($secondary, $result);
|
||||
$this->assertInstanceOf($testable::class, $result);
|
||||
}
|
||||
|
||||
public function testUndefinedMethodCall(): void
|
||||
{
|
||||
$testable = new class() {
|
||||
use ForwardsCalls;
|
||||
};
|
||||
|
||||
$secondary = new class() {
|
||||
// This class intentionally left blank to simulate an undefined method call.
|
||||
};
|
||||
|
||||
// Use reflection to change visibility
|
||||
$reflection = new \ReflectionClass($testable::class);
|
||||
$method = $reflection->getMethod('forwardCallTo');
|
||||
|
||||
$this->expectException(\Error::class);
|
||||
$this->expectExceptionMessage('Call to undefined method class@anonymous::undefinedMethod()');
|
||||
$method->invokeArgs($testable, [$secondary, 'undefinedMethod', []]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\Support\Traits;
|
||||
|
||||
use Flasher\Prime\Support\Traits\Macroable;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class MacroableTest extends TestCase
|
||||
{
|
||||
public function testMacroRegistrationAndExecution(): void
|
||||
{
|
||||
$macroableClass = new class() {
|
||||
use Macroable;
|
||||
};
|
||||
|
||||
$macroableClass::macro('testMacro', static function () {
|
||||
return 'macro result';
|
||||
});
|
||||
|
||||
$this->assertTrue($macroableClass::hasMacro('testMacro'));
|
||||
$this->assertSame('macro result', $macroableClass::testMacro()); // @phpstan-ignore-line
|
||||
}
|
||||
|
||||
public function testMixin(): void
|
||||
{
|
||||
$macroableClass = new class() {
|
||||
use Macroable;
|
||||
};
|
||||
|
||||
$mixin = new class() {
|
||||
public function mixedInMethod(): callable
|
||||
{
|
||||
return static function () {
|
||||
return 'mixed in';
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
$macroableClass::mixin($mixin);
|
||||
|
||||
$this->assertTrue($macroableClass::hasMacro('mixedInMethod'));
|
||||
$this->assertSame('mixed in', $macroableClass::mixedInMethod()); // @phpstan-ignore-line
|
||||
}
|
||||
|
||||
public function testExceptionForNonExistingMacro(): void
|
||||
{
|
||||
$macroableClass = new class() {
|
||||
use Macroable;
|
||||
};
|
||||
|
||||
$this->expectException(\BadMethodCallException::class);
|
||||
$macroableClass::nonExistingMacro(); // @phpstan-ignore-line
|
||||
}
|
||||
|
||||
public function testExceptionForNonCallableMacro(): void
|
||||
{
|
||||
$macroableClass = new class() {
|
||||
use Macroable;
|
||||
};
|
||||
|
||||
$macroableClass::macro('nonCallable', new \stdClass());
|
||||
$this->expectException(\BadMethodCallException::class);
|
||||
$macroableClass::nonCallable(); // @phpstan-ignore-line
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\Template;
|
||||
|
||||
use Flasher\Prime\Template\PHPTemplateEngine;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class PHPTemplateEngineTest extends TestCase
|
||||
{
|
||||
private PHPTemplateEngine $templateEngine;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->templateEngine = new PHPTemplateEngine();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test case for testing the render method with a valid filename and context.
|
||||
*/
|
||||
public function testRenderWithValidFilenameContext(): void
|
||||
{
|
||||
$name = 'someTemplateFile.php';
|
||||
$context = ['key' => 'value'];
|
||||
|
||||
// Create a fake template file for the purpose of this test.
|
||||
file_put_contents($name, '<?php echo $key;');
|
||||
|
||||
$result = $this->templateEngine->render($name, $context);
|
||||
|
||||
// Cleanup the fake template file
|
||||
unlink($name);
|
||||
|
||||
$this->assertSame('value', trim($result), "Rendered template content doesn't match expected content.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test case for testing the render method with an invalid filename.
|
||||
*/
|
||||
public function testRenderWithInvalidFilename(): void
|
||||
{
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
|
||||
$this->templateEngine->render('invalidFileName.php');
|
||||
}
|
||||
}
|
||||
@@ -1,102 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the PHPFlasher package.
|
||||
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
|
||||
*/
|
||||
|
||||
namespace Flasher\Tests\Prime;
|
||||
|
||||
class TestCase extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @param class-string<\Throwable> $exceptionName
|
||||
* @param string $exceptionMessage
|
||||
* @param int $exceptionCode
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setExpectedException($exceptionName, $exceptionMessage = '', $exceptionCode = null)
|
||||
{
|
||||
if (method_exists($this, 'expectException')) {
|
||||
$this->expectException($exceptionName);
|
||||
$this->expectExceptionMessage($exceptionMessage);
|
||||
} else {
|
||||
parent::setExpectedException($exceptionName, $exceptionMessage, $exceptionCode); // @phpstan-ignore-line
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Call a protected or private method of a class using reflection.
|
||||
*
|
||||
* @param object|string $object instantiated object or FQCN that we will run method
|
||||
* @param string $methodName method name to call
|
||||
* @param array|mixed $parameters array of parameters to pass into method
|
||||
*
|
||||
* @return mixed method return
|
||||
*
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
protected function invokeMethod($object, $methodName, $parameters = array())
|
||||
{
|
||||
$class = is_string($object) ? $object : get_class($object);
|
||||
|
||||
$reflection = new \ReflectionClass($class);
|
||||
|
||||
$method = $reflection->getMethod($methodName);
|
||||
$method->setAccessible(true);
|
||||
|
||||
$object = is_string($object) ? null : $object;
|
||||
$parameters = \is_array($parameters) ? $parameters : \array_slice(\func_get_args(), 2);
|
||||
|
||||
return $method->invokeArgs($object, $parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of a protected or private property of a class using reflection.
|
||||
*
|
||||
* @param object|string $object instantiated object or FQCN that we will access property from
|
||||
* @param string $propertyName name of property to access
|
||||
*
|
||||
* @return mixed property value
|
||||
*
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
protected function getProperty($object, $propertyName)
|
||||
{
|
||||
$class = is_string($object) ? $object : get_class($object);
|
||||
|
||||
$reflection = new \ReflectionClass($class);
|
||||
|
||||
$property = $reflection->getProperty($propertyName);
|
||||
$property->setAccessible(true);
|
||||
|
||||
$object = is_string($object) ? null : $object;
|
||||
|
||||
return $property->getValue($object);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the value of a protected or private property of a class using reflection.
|
||||
*
|
||||
* @param object|string $object instantiated object or FQCN that we will run method
|
||||
* @param string $propertyName name of property to set
|
||||
* @param mixed $value value to set the property to
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
protected function setProperty($object, $propertyName, $value)
|
||||
{
|
||||
$class = is_string($object) ? $object : get_class($object);
|
||||
|
||||
$reflection = new \ReflectionClass($class);
|
||||
|
||||
$property = $reflection->getProperty($propertyName);
|
||||
$property->setAccessible(true);
|
||||
|
||||
$object = is_string($object) ? null : $object;
|
||||
$property->setValue($object, $value);
|
||||
}
|
||||
}
|
||||
@@ -1,25 +1,43 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the PHPFlasher package.
|
||||
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\Translation;
|
||||
|
||||
use Flasher\Prime\Translation\EchoTranslator;
|
||||
use Flasher\Tests\Prime\TestCase;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class EchoTranslatorTest extends TestCase
|
||||
final class EchoTranslatorTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testEchoTranslator()
|
||||
{
|
||||
$translator = new EchoTranslator();
|
||||
private EchoTranslator $translator;
|
||||
|
||||
$this->assertEquals('en', $translator->getLocale());
|
||||
$this->assertEquals('PHPFlasher', $translator->translate('PHPFlasher'));
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->translator = new EchoTranslator();
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is for testing if the Translate method in EchoTranslator class returns the same
|
||||
* id it received as input without any transformations.
|
||||
*/
|
||||
public function testTranslate(): void
|
||||
{
|
||||
$id = 'TestID';
|
||||
$parameters = [];
|
||||
$locale = null;
|
||||
|
||||
$result = $this->translator->translate($id, $parameters, $locale);
|
||||
|
||||
$this->assertSame($id, $result, 'The Translate method should return the same id it received as input');
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is for testing if the getLocale method in EchoTranslator class always returns 'en'.
|
||||
*/
|
||||
public function testGetLocale(): void
|
||||
{
|
||||
$locale = $this->translator->getLocale();
|
||||
|
||||
$this->assertSame('en', $locale, "The getLocale method should return 'en'");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\Translation\Language;
|
||||
|
||||
use Flasher\Prime\Translation\Language\Arabic;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class ArabicTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* Function to test the translations method of the Arabic class.
|
||||
*/
|
||||
public function testTranslations(): void
|
||||
{
|
||||
$expectedTranslations = [
|
||||
'success' => 'نجاح',
|
||||
'error' => 'خطأ',
|
||||
'warning' => 'تحذير',
|
||||
'info' => 'معلومة',
|
||||
|
||||
'The resource was created' => 'تم إنشاء :resource',
|
||||
'The resource was updated' => 'تم تعديل :resource',
|
||||
'The resource was saved' => 'تم حفظ :resource',
|
||||
'The resource was deleted' => 'تم حذف :resource',
|
||||
|
||||
'resource' => 'الملف',
|
||||
];
|
||||
|
||||
$this->assertSame($expectedTranslations, Arabic::translations());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\Translation\Language;
|
||||
|
||||
use Flasher\Prime\Translation\Language\Chinese;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class ChineseTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* Function to test the translations method of the Chinese class.
|
||||
*/
|
||||
public function testTranslations(): void
|
||||
{
|
||||
$expectedTranslations = [
|
||||
'success' => '成功',
|
||||
'error' => '错误',
|
||||
'warning' => '警告',
|
||||
'info' => '信息',
|
||||
|
||||
'The resource was created' => ':resource 已创建',
|
||||
'The resource was updated' => ':resource 已更新',
|
||||
'The resource was saved' => ':resource 已保存',
|
||||
'The resource was deleted' => ':resource 已删除',
|
||||
|
||||
'resource' => '资源',
|
||||
];
|
||||
|
||||
$this->assertSame($expectedTranslations, Chinese::translations());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\Translation\Language;
|
||||
|
||||
use Flasher\Prime\Translation\Language\English;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class EnglishTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* Function to test the translations method of the English class.
|
||||
*/
|
||||
public function testTranslations(): void
|
||||
{
|
||||
$expectedTranslations = [
|
||||
'success' => 'Success',
|
||||
'error' => 'Error',
|
||||
'warning' => 'Warning',
|
||||
'info' => 'Info',
|
||||
|
||||
'The resource was created' => 'The :resource was created',
|
||||
'The resource was updated' => 'The :resource was updated',
|
||||
'The resource was saved' => 'The :resource was saved',
|
||||
'The resource was deleted' => 'The :resource was deleted',
|
||||
|
||||
'resource' => 'resource',
|
||||
];
|
||||
|
||||
$this->assertSame($expectedTranslations, English::translations());
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user