diff --git a/.travis.yml b/.travis.yml index 311b6ad..f7f7541 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,7 +16,6 @@ before_script: - travis_retry composer install --prefer-source --no-interaction script: - - composer validate - mkdir -p build/logs - vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover diff --git a/_docs/1-Installation-and-Setup.md b/_docs/1-Installation-and-Setup.md index f71c68d..787e0f0 100644 --- a/_docs/1-Installation-and-Setup.md +++ b/_docs/1-Installation-and-Setup.md @@ -16,15 +16,15 @@ The Notify package has a few system requirements: ## Version Compatibility -| Notify | Laravel | -|:-------------------------------|:--------------------------------------------------------------------------------------------------------------------| -| ![Notify v1.x][notify_1_x] | ![Laravel v4.2][laravel_4_2] | -| ![Notify v3.2.x][notify_3_2_x] | ![Laravel v5.0][laravel_5_0] ![Laravel v5.1][laravel_5_1] ![Laravel v5.2][laravel_5_2] ![Laravel v5.3][laravel_5_3] | -| ![Notify v3.3.x][notify_3_3_x] | ![Laravel v5.4][laravel_5_4] | -| ![Notify v3.4.x][notify_3_4_x] | ![Laravel v5.5][laravel_5_5] | -| ![Notify v3.5.x][notify_3_5_x] | ![Laravel v5.6][laravel_5_6] | -| ![Notify v3.6.x][notify_3_6_x] | ![Laravel v5.7][laravel_5_7] | -| ![Notify v3.7.x][notify_3_7_x] | ![Laravel v5.8][laravel_5_8] | +| Notify | Laravel | +|:--------------------------------------------------------------|:--------------------------------------------------------------------------------------------------------------------| +| ![Notify v1.x][notify_1_x] | ![Laravel v4.2][laravel_4_2] | +| ![Notify v3.2.x][notify_3_2_x] | ![Laravel v5.0][laravel_5_0] ![Laravel v5.1][laravel_5_1] ![Laravel v5.2][laravel_5_2] ![Laravel v5.3][laravel_5_3] | +| ![Notify v3.3.x][notify_3_3_x] | ![Laravel v5.4][laravel_5_4] | +| ![Notify v3.4.x][notify_3_4_x] | ![Laravel v5.5][laravel_5_5] | +| ![Notify v3.5.x][notify_3_5_x] | ![Laravel v5.6][laravel_5_6] | +| ![Notify v3.6.x][notify_3_6_x] | ![Laravel v5.7][laravel_5_7] | +| ![Notify v3.7.x][notify_3_7_x] ![Notify v4.0.x][notify_4_0_x] | ![Laravel v5.8][laravel_5_8] | [laravel_4_2]: https://img.shields.io/badge/v4.2-supported-brightgreen.svg?style=flat-square "Laravel v4.2" [laravel_5_0]: https://img.shields.io/badge/v5.0-supported-brightgreen.svg?style=flat-square "Laravel v5.0" @@ -44,6 +44,7 @@ The Notify package has a few system requirements: [notify_3_5_x]: https://img.shields.io/badge/version-3.5.*-blue.svg?style=flat-square "Notify v3.5.*" [notify_3_6_x]: https://img.shields.io/badge/version-3.6.*-blue.svg?style=flat-square "Notify v3.6.*" [notify_3_7_x]: https://img.shields.io/badge/version-3.7.*-blue.svg?style=flat-square "Notify v3.7.*" +[notify_4_0_x]: https://img.shields.io/badge/version-4.0.*-blue.svg?style=flat-square "Notify v4.0.*" ## Composer diff --git a/_docs/2-Configuration.md b/_docs/2-Configuration.md index 3d71673..fb2d1ee 100644 --- a/_docs/2-Configuration.md +++ b/_docs/2-Configuration.md @@ -9,16 +9,34 @@ After you've published the config file `config/notify.php`, you can customize the settings : ```php + [ - 'prefix' => 'notifier' + + 'default' => 'session', + + /* ----------------------------------------------------------------- + | Supported Stores + | ----------------------------------------------------------------- + */ + + 'stores' => [ + + 'session' => [ + 'driver' => Arcanedev\Notify\Stores\SessionStore::class, + 'options' => [ + 'key' => 'notifications' + ], + ], + ], + ]; ``` -The `prefix` value is the session prefix name for all your flash notifications. +You can create you own store like `database` or `redis` store for your notifications. diff --git a/_docs/3-Usage.md b/_docs/3-Usage.md index f4fd506..a9d753d 100644 --- a/_docs/3-Usage.md +++ b/_docs/3-Usage.md @@ -6,6 +6,8 @@ 2. [Configuration](2-Configuration.md) 3. [Usage](3-Usage.md) +> **NOTE:** This documentation is for `v4.0` (master branch), switch the branch if you're using an older version. + #### Basic Usage From your application, call the flash method with a message and type. @@ -29,58 +31,37 @@ notify()->flash('Welcome back!', 'success'); Within a view, you can now check if a flash message exists and output it. ```blade -@if (notify()->ready()) -
- {{ notify()->message() }} +@if (notify()->isNotEmpty()) + @foreach(notify()->notifications() as $notification) +
+ {{ $notification['message'] }}
+ @endforeach @endif ``` > Notify is front-end framework agnostic, so you're free to easily implement the output however you choose (Like Twitter bootstrap, Zurb Foundation, Semantic UI ...). -#### Options +#### Extra Options -You can pass additional `options` to the `flash` method, which are then easily accessible within your view. +You can pass additional `extra` options to the `flash` method, which are then easily accessible within your view. ```php notify()->flash("Great to see you again, Bruh!", 'success', [ 'title' => 'Welcome back!', - 'timer' => 5000, + 'icon' => 'success-icon', ]); ``` Then, in your view. ```blade -@if (notify()->ready()) - -@endif -``` - -You can also check if an `option` exists or not like this: - -```blade -@if (notify()->ready()) - +@if (notify()->isNotEmpty()) + @foreach(notify()->notifications() as $notification) +
+
{{ $notification['extra']['title'] ?? 'Default Title' }}
+

{{ $notification['message'] }}

+
+ @endforeach @endif ``` - -> The above example uses [SweetAlert](http://t4t5.github.io/sweetalert/), but the flexibily of Notify means you can easily use it with any JavaScript alert solution (For example [toastr](https://github.com/CodeSeven/toastr)). diff --git a/composer.json b/composer.json index 57a822e..4abdf68 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "arcanedev/notify", "description": "Flexible flash notifications helper for Laravel.", - "keywords": ["arcanedev", "notify", "flash", "sweetalert", "sweet-alert", "laravel"], + "keywords": ["arcanedev", "laravel", "notify", "flash", "notifications", "alerts"], "homepage": "https://github.com/ARCANEDEV/Notify", "authors": [ { @@ -38,10 +38,7 @@ "laravel": { "providers": [ "Arcanedev\\Notify\\NotifyServiceProvider" - ], - "aliases": { - "Notify": "Arcanedev\\Notify\\Facades\\Notify" - } + ] } } } diff --git a/config/notify.php b/config/notify.php index 8fdfdda..f527978 100644 --- a/config/notify.php +++ b/config/notify.php @@ -3,12 +3,26 @@ return [ /* ----------------------------------------------------------------- - | Session + | Default Store | ----------------------------------------------------------------- */ - 'session' => [ - 'prefix' => 'notifier' + 'default' => 'session', + + /* ----------------------------------------------------------------- + | Supported Stores + | ----------------------------------------------------------------- + */ + + 'stores' => [ + + 'session' => [ + 'driver' => Arcanedev\Notify\Stores\SessionStore::class, + 'options' => [ + 'key' => 'notifications' + ], + ], + ], ]; diff --git a/helpers.php b/helpers.php index cbbddb7..cfaad33 100644 --- a/helpers.php +++ b/helpers.php @@ -8,17 +8,15 @@ * * @param string|null $message * @param string $type - * @param array $options + * @param array $extra * * @return \Arcanedev\Notify\Contracts\Notify */ - function notify($message = null, $type = 'info', array $options = []) + function notify($message = null, $type = 'info', array $extra = []) { /** @var Arcanedev\Notify\Contracts\Notify $notifier */ $notifier = app(Notify::class); - return is_null($message) - ? $notifier - : $notifier->flash($message, $type, $options); + return is_null($message) ? $notifier : $notifier->flash($message, $type, $extra); } } diff --git a/src/Contracts/Notify.php b/src/Contracts/Notify.php index d6cffd0..73de61b 100644 --- a/src/Contracts/Notify.php +++ b/src/Contracts/Notify.php @@ -1,5 +1,7 @@ - */ -interface SessionStore -{ - /* ----------------------------------------------------------------- - | Main Methods - | ----------------------------------------------------------------- - */ - - /** - * Flash a message to the session. - * - * @param string|array $key - * @param mixed $value - */ - public function flash($key, $value = true); - - /** - * Flash multiple key/value pairs. - * - * @param array $data - */ - public function flashMany($data); - - /** - * Get a value from session storage. - * - * @param string $key - * - * @return mixed - */ - public function get($key); -} diff --git a/src/Contracts/Store.php b/src/Contracts/Store.php new file mode 100644 index 0000000..4731a5a --- /dev/null +++ b/src/Contracts/Store.php @@ -0,0 +1,52 @@ + + */ +interface Store +{ + /* ----------------------------------------------------------------- + | Main Methods + | ----------------------------------------------------------------- + */ + + /** + * Get all the notifications. + * + * @return \Illuminate\Support\Collection + */ + public function all(): Collection; + + /** + * Push the new notification. + * + * @param array $notification + */ + public function push(array $notification); + + /** + * Forget the notifications. + * + * @return void + */ + public function forget(); + + /** + * Check if it has notifications. + * + * @return bool + */ + public function isEmpty(): bool; + + /** + * Check if there is no notifications. + * + * @return bool + */ + public function isNotEmpty(): bool; +} diff --git a/src/Notify.php b/src/Notify.php index 70b78c2..e24661d 100644 --- a/src/Notify.php +++ b/src/Notify.php @@ -1,8 +1,9 @@ session = $session; - $this->sessionPrefix = $prefix; + $this->setStore($store); } /* ----------------------------------------------------------------- @@ -54,125 +53,139 @@ public function __construct(SessionStore $session, $prefix) */ /** - * Get the notification message. + * Get the store. * - * @return string + * @return \Arcanedev\Notify\Contracts\Store */ - public function message() + public function store(): Store { - return $this->getSession('message'); + return $this->store; } /** - * Get the notification type. + * Set the store. + * + * @param \Arcanedev\Notify\Contracts\Store $store * - * @return string + * @return $this */ - public function type() + public function setStore(Store $store) { - return $this->getSession('type'); + $this->store = $store; + + return $this; } /** - * Get an additional stored options. + * Get all the notifications. * - * @param bool $assoc - * - * @return array + * @return \Illuminate\Support\Collection */ - public function options($assoc = false) + public function notifications(): Collection { - return json_decode($this->getSession('options'), $assoc); + return $this->store()->all(); } + /* ----------------------------------------------------------------- + | Main Methods + | ----------------------------------------------------------------- + */ + /** - * Get a notification option. + * Flash an information message. * - * @param string $key - * @param mixed|null $default + * @param string $message + * @param array $extra * - * @return mixed + * @return $this */ - public function option($key, $default = null) + public function info(string $message, array $extra = []) { - return Arr::get($this->options(true), $key, $default); + return $this->flash($message, 'info', $extra); } /** - * Check if the flash notification has an option. + * Flash a success message. * - * @param string $key + * @param string $message + * @param array $extra * - * @return bool + * @return $this */ - public function hasOption($key) + public function success(string $message, array $extra = []) { - return Arr::has($this->options(true), $key); + return $this->flash($message, 'success', $extra); } /** - * If the notification is ready to be shown. + * Flash an error message. * - * @return bool + * @param string $message + * @param array $extra + * + * @return $this */ - public function ready() + public function error(string $message, array $extra = []) { - return ! empty($this->message()); + return $this->flash($message, 'danger', $extra); } - /* ----------------------------------------------------------------- - | Main Methods - | ----------------------------------------------------------------- + /** + * Flash a warning message. + * + * @param string $message + * @param array $extra + * + * @return $this */ + public function warning(string $message, array $extra = []) + { + return $this->flash($message, 'warning', $extra); + } /** - * Flash a message. + * Flash a new notification. * * @param string $message * @param string|null $type - * @param array $options + * @param array $extra * * @return $this */ - public function flash($message, $type = null, array $options = []) + public function flash($message, $type = 'info', array $extra = []) { - $this->session->flash([ - $this->getPrefixedName('message') => $message, - $this->getPrefixedName('type') => $type ?: '', - $this->getPrefixedName('options') => json_encode($options), - ]); + $this->store()->push(compact('message', 'type', 'extra')); return $this; } - /* ----------------------------------------------------------------- - | Other Methods - | ----------------------------------------------------------------- + /** + * Forget the notification. + * + * @return void */ + public function forget() + { + $this->store->forget(); + } /** - * Prefix the name. - * - * @param string $name + * Check if it has notifications. * - * @return string + * @return bool */ - private function getPrefixedName($name) + public function isEmpty(): bool { - return "{$this->sessionPrefix}.$name"; + return $this->store()->isEmpty(); } /** - * Get session value. + * Check if there is no notifications. * - * @param string $name - * - * @return mixed + * @return bool */ - private function getSession($name) + public function isNotEmpty(): bool { - return $this->session->get( - $this->getPrefixedName($name) - ); + return $this->store()->isNotEmpty(); } } diff --git a/src/NotifyServiceProvider.php b/src/NotifyServiceProvider.php index 4d2843f..12d0d85 100644 --- a/src/NotifyServiceProvider.php +++ b/src/NotifyServiceProvider.php @@ -43,8 +43,17 @@ public function register() $this->registerConfig(); - $this->bindSession(); - $this->registerNotifyService(); + $this->singleton(StoreManager::class, function ($app) { + return tap(new StoreManager($app), function (StoreManager $manager) { + $manager->loadStores(); + }); + }); + + $this->bind(Contracts\Store::class, function ($app) { + return $app[StoreManager::class]->driver(); + }); + + $this->singleton(Contracts\Notify::class, Notify::class); } /** @@ -68,33 +77,4 @@ public function provides() Contracts\Notify::class, ]; } - - /* ----------------------------------------------------------------- - | Services - | ----------------------------------------------------------------- - */ - - /** - * Bind the Session Class. - */ - private function bindSession() - { - $this->bind(Contracts\SessionStore::class, Storage\Session::class); - } - - /** - * Register the Notify service. - */ - private function registerNotifyService() - { - $this->singleton(Contracts\Notify::class, function ($app) { - /** @var \Illuminate\Config\Repository $config */ - $config = $app['config']; - - return new Notify( - $app[Contracts\SessionStore::class], - $config->get('notify.session.prefix', 'notifier') - ); - }); - } } diff --git a/src/Storage/Session.php b/src/Storage/Session.php deleted file mode 100644 index c6b80ef..0000000 --- a/src/Storage/Session.php +++ /dev/null @@ -1,83 +0,0 @@ - - */ -class Session implements SessionStore -{ - /* ----------------------------------------------------------------- - | Properties - | ----------------------------------------------------------------- - */ - - /** - * The Illuminate Session instance. - * - * @var \Illuminate\Contracts\Session\Session - */ - private $session; - - /* ----------------------------------------------------------------- - | Constructor - | ----------------------------------------------------------------- - */ - - /** - * Make session store instance. - * - * @param \Illuminate\Contracts\Session\Session $session - */ - public function __construct(SessionContract $session) - { - $this->session = $session; - } - - /* ----------------------------------------------------------------- - | Main Methods - | ----------------------------------------------------------------- - */ - - /** - * Flash a message to the session. - * - * @param string|array $key - * @param mixed $value - */ - public function flash($key, $value = true) - { - if (is_array($key)) - $this->flashMany($key); - else - $this->session->flash($key, $value); - } - - /** - * Flash multiple key/value pairs. - * - * @param \iterable|array $data - */ - public function flashMany($data) - { - foreach ($data as $key => $value) { - $this->flash($key, $value); - } - } - - /** - * Get a value from session storage. - * - * @param string $key - * - * @return mixed - */ - public function get($key) - { - return $this->session->get($key); - } -} diff --git a/src/StoreManager.php b/src/StoreManager.php new file mode 100644 index 0000000..9667d81 --- /dev/null +++ b/src/StoreManager.php @@ -0,0 +1,62 @@ + + */ +class StoreManager extends Manager +{ + /* ----------------------------------------------------------------- + | Main Methods + | ----------------------------------------------------------------- + */ + + /** + * Get the default driver name. + * + * @return string + */ + public function getDefaultDriver() + { + return $this->app['config']->get('notify.default', 'session'); + } + + /** + * Load the stores. + * + * @return void + */ + public function loadStores(): void + { + $stores = $this->config()->get('notify.stores', []); + + foreach ($stores as $driver => $store) { + $this->extend($driver, function () use ($store) { + return $this->app->make($store['driver']); + }); + + $this->app->when($store['driver']) + ->needs('$options') + ->give($store['options'] ?? []); + } + } + + /* ----------------------------------------------------------------- + | Other Methods + | ----------------------------------------------------------------- + */ + + /** + * Get the config repository. + * + * @return \Illuminate\Contracts\Config\Repository + */ + protected function config() + { + return $this->app['config']; + } +} diff --git a/src/Stores/SessionStore.php b/src/Stores/SessionStore.php new file mode 100644 index 0000000..5ba0c84 --- /dev/null +++ b/src/Stores/SessionStore.php @@ -0,0 +1,125 @@ + + */ +class SessionStore implements Store +{ + /* ----------------------------------------------------------------- + | Properties + | ----------------------------------------------------------------- + */ + + /** + * The Illuminate Session instance. + * + * @var \Illuminate\Contracts\Session\Session + */ + private $session; + + /** + * The store's options. + * + * @var array + */ + protected $options; + + /* ----------------------------------------------------------------- + | Constructor + | ----------------------------------------------------------------- + */ + + /** + * Make session store instance. + * + * @param \Illuminate\Contracts\Session\Session $session + * @param array $options + */ + public function __construct(SessionContract $session, array $options) + { + $this->session = $session; + $this->options = $options; + } + + /* ----------------------------------------------------------------- + | Getters + | ----------------------------------------------------------------- + */ + + /** + * Get the session key. + * + * @return string + */ + protected function getSessionKey(): string + { + return $this->options['key']; + } + + /* ----------------------------------------------------------------- + | Main Methods + | ----------------------------------------------------------------- + */ + + /** + * Get all the notifications. + * + * @return \Illuminate\Support\Collection + */ + public function all(): Collection + { + return new Collection( + $this->session->get($this->getSessionKey(), []) + ); + } + + /** + * Push the new notification. + * + * @param array $notification + */ + public function push(array $notification) + { + $this->session->flash( + $this->getSessionKey(), + $this->all()->push($notification) + ); + } + + /** + * Forget the notifications. + * + * @return void + */ + public function forget() + { + $this->session->forget($this->getSessionKey()); + } + + /** + * Check if it has notifications. + * + * @return bool + */ + public function isEmpty(): bool + { + return $this->all()->isEmpty(); + } + + /** + * Check if there is no notifications. + * + * @return bool + */ + public function isNotEmpty(): bool + { + return ! $this->isEmpty(); + } +} diff --git a/tests/HelperTest.php b/tests/HelperTest.php index 4815d1d..0e33620 100644 --- a/tests/HelperTest.php +++ b/tests/HelperTest.php @@ -1,5 +1,7 @@ ready()); - } + $expectations = [ + \Arcanedev\Notify\Contracts\Notify::class, + \Arcanedev\Notify\Notify::class, + ]; - /* ----------------------------------------------------------------- - | Tests - | ----------------------------------------------------------------- - */ + foreach ($expectations as $expected) { + static::assertInstanceOf($expected, $notify); + } + + static::assertTrue($notify->isEmpty()); + static::assertFalse($notify->isNotEmpty()); + } /** @test */ public function it_can_flash_a_notification_with_only_message() { - notify($message = 'Welcome Aboard'); + notify('Welcome Aboard'); + + static::assertTrue(notify()->isNotEmpty()); + + $expected = [ + [ + 'message' => 'Welcome Aboard', + 'type' => 'info', + 'extra' => [], + ] + ]; - static::assertTrue(notify()->ready()); - static::assertSame($message, notify()->message()); - static::assertSame('info', notify()->type()); - static::assertEmpty(notify()->options()); + static::assertInstanceOf( + Collection::class, + $notifications = notify()->notifications() + ); + static::assertEquals($expected, $notifications->toArray()); + + notify()->flash('Welcome Aboard'); + + $expected[] = [ + 'message' => 'Welcome Aboard', + 'type' => 'info', + 'extra' => [], + ]; + + static::assertInstanceOf( + Collection::class, + $notifications = notify()->notifications() + ); + static::assertEquals($expected, $notifications->toArray()); } /** @test */ public function it_can_flash_a_notification_with_message_and_type() { - notify($message = "You've got an error!", $type = 'danger'); + notify("You've got an error!", 'danger'); + + static::assertTrue(notify()->isNotEmpty()); + + $expected = [ + [ + 'message' => "You've got an error!", + 'type' => 'danger', + 'extra' => [], + ] + ]; - static::assertTrue(notify()->ready()); - static::assertSame($message, notify()->message()); - static::assertSame($type, notify()->type()); - static::assertEmpty(notify()->options()); + static::assertInstanceOf( + Collection::class, + $notifications = notify()->notifications() + ); + static::assertEquals($expected, $notifications->toArray()); + + notify()->flash('Welcome Aboard', 'info'); + + $expected[] = [ + 'message' => 'Welcome Aboard', + 'type' => 'info', + 'extra' => [], + ]; + + static::assertInstanceOf( + Collection::class, + $notifications = notify()->notifications() + ); + static::assertEquals($expected, $notifications->toArray()); } /** @test */ - public function it_can_flash_a_notification_with_only_message_two() + public function it_can_flash_notification_with_extra_options() { - notify()->flash($message = 'Welcome Aboard'); + notify()->flash('Welcome Aboard', 'success', [ + 'content' => '

It is nice to see you again!

', + 'icon' => ':tada:', + ]); + + static::assertTrue(notify()->isNotEmpty()); + + $expected = [ + [ + 'message' => 'Welcome Aboard', + 'type' => 'success', + 'extra' => [ + 'content' => '

It is nice to see you again!

', + 'icon' => ':tada:', + ], + ] + ]; - static::assertTrue(notify()->ready()); - static::assertSame($message, notify()->message()); - static::assertEmpty(notify()->type()); - static::assertEmpty(notify()->options()); + static::assertInstanceOf( + Collection::class, + $notifications = notify()->notifications() + ); + static::assertEquals($expected, $notifications->toArray()); } /** @test */ - public function it_can_flash_notification_with_type() + public function it_can_forget_all_the_notifications() { - $message = 'Welcome Aboard'; - $type = 'info'; + notify()->flash('Welcome Aboard'); + + static::assertFalse(notify()->isEmpty()); + static::assertTrue(notify()->isNotEmpty()); - notify()->flash($message, $type); + notify()->forget(); - static::assertTrue(notify()->ready()); - static::assertSame($message, notify()->message()); - static::assertSame($type, notify()->type()); - static::assertEmpty(notify()->options()); + static::assertTrue(notify()->isEmpty()); + static::assertFalse(notify()->isNotEmpty()); + static::assertEmpty(notify()->notifications()); } /** @test */ - public function it_can_flash_notification_with_options() + public function it_can_flash_with_predefined_types() { - $message = 'Welcome Aboard'; - $type = 'success'; - $options = [ - 'color' => '#BADA55', - 'position' => 'absolute', + notify()->info('Info notification', ['icon' => 'info-icon']); + notify()->success('Success notification', ['icon' => 'success-icon']); + notify()->error('Error notification', ['icon' => 'error-icon']); + notify()->warning('Warning notification', ['icon' => 'warning-icon']); + + $expected = [ + [ + 'message' => 'Info notification', + 'type' => 'info', + 'extra' => [ + 'icon' => 'info-icon', + ], + ], + [ + 'message' => 'Success notification', + 'type' => 'success', + 'extra' => [ + 'icon' => 'success-icon', + ], + ], + [ + 'message' => 'Error notification', + 'type' => 'danger', + 'extra' => [ + 'icon' => 'error-icon', + ], + ], + [ + 'message' => 'Warning notification', + 'type' => 'warning', + 'extra' => [ + 'icon' => 'warning-icon', + ], + ], ]; - notify()->flash($message, $type, $options); - - static::assertTrue(notify()->ready()); - static::assertSame($message, notify()->message()); - static::assertSame($type, notify()->type()); - static::assertSame($options, notify()->options(true)); - static::assertSame($options['color'], notify()->option('color')); - static::assertSame($options['position'], notify()->option('position')); + static::assertInstanceOf( + Collection::class, + $notifications = notify()->notifications() + ); + static::assertEquals($expected, $notifications->toArray()); } } diff --git a/tests/NotifyTest.php b/tests/NotifyTest.php index 45421ae..f01b5a6 100644 --- a/tests/NotifyTest.php +++ b/tests/NotifyTest.php @@ -1,7 +1,8 @@ notify = new Notify(app(SessionStore::class), $this->sessionPrefix); - - static::assertFalse($this->notify->ready()); - } - - public function tearDown(): void - { - unset($this->notify); - - parent::tearDown(); + $this->notify = new Notify($this->app->make(Store::class)); } /* ----------------------------------------------------------------- @@ -58,55 +50,144 @@ public function it_can_be_instantiated_via_the_contract() foreach ($expectations as $expected) { static::assertInstanceOf($expected, $this->notify); } + + static::assertTrue($this->notify->isEmpty()); + static::assertFalse($this->notify->isNotEmpty()); } /** @test */ public function it_can_flash_a_notification_with_only_message() { - $message = 'Welcome Aboard'; + $this->notify->flash('Welcome Aboard'); - $this->notify->flash($message); + static::assertFalse($this->notify->isEmpty()); + static::assertTrue($this->notify->isNotEmpty()); - static::assertTrue($this->notify->ready()); - static::assertEquals($message, $this->notify->message()); - static::assertEmpty($this->notify->type()); - static::assertEmpty($this->notify->options()); + $expected = [ + [ + 'message' => 'Welcome Aboard', + 'type' => 'info', + 'extra' => [], + ] + ]; + + static::assertInstanceOf( + Collection::class, + $notifications = $this->notify->notifications() + ); + static::assertEquals($expected, $notifications->toArray()); } /** @test */ public function it_can_flash_notification_with_type() { - $message = 'Welcome Aboard'; - $type = 'info'; + $this->notify->flash('Welcome Aboard', 'success'); - $this->notify->flash($message, $type); + static::assertFalse($this->notify->isEmpty()); + static::assertTrue($this->notify->isNotEmpty()); - static::assertTrue($this->notify->ready()); - static::assertEquals($message, $this->notify->message()); - static::assertEquals($type, $this->notify->type()); - static::assertEmpty($this->notify->options()); + $expected = [ + [ + 'message' => 'Welcome Aboard', + 'type' => 'success', + 'extra' => [], + ] + ]; + + static::assertInstanceOf( + Collection::class, + $notifications = $this->notify->notifications() + ); + static::assertEquals($expected, $notifications->toArray()); } /** @test */ - public function it_can_flash_notification_with_options() + public function it_can_flash_notification_with_extra_options() { - $message = 'Welcome Aboard'; - $type = 'success'; - $options = [ - 'color' => '#BADA55', - 'position' => 'absolute', + $this->notify->flash('Welcome Aboard', 'success', [ + 'content' => '

It is nice to see you again!

', + 'icon' => ':tada:', + ]); + + static::assertFalse($this->notify->isEmpty()); + static::assertTrue($this->notify->isNotEmpty()); + + $expected = [ + [ + 'message' => 'Welcome Aboard', + 'type' => 'success', + 'extra' => [ + 'content' => '

It is nice to see you again!

', + 'icon' => ':tada:', + ], + ] ]; - $this->notify->flash($message, $type, $options); + static::assertInstanceOf( + Collection::class, + $notifications = $this->notify->notifications() + ); + static::assertEquals($expected, $notifications->toArray()); + } - static::assertTrue($this->notify->ready()); - static::assertTrue($this->notify->hasOption('color')); - static::assertTrue($this->notify->hasOption('position')); + /** @test */ + public function it_can_forget_all_the_notifications() + { + $this->notify->flash('Welcome Aboard'); + + static::assertFalse($this->notify->isEmpty()); + static::assertTrue($this->notify->isNotEmpty()); + + $this->notify->forget(); + + static::assertTrue($this->notify->isEmpty()); + static::assertFalse($this->notify->isNotEmpty()); + static::assertEmpty($this->notify->notifications()); + } + + /** @test */ + public function it_can_flash_with_predefined_types() + { + $this->notify->info('Info notification', ['icon' => 'info-icon']); + $this->notify->success('Success notification', ['icon' => 'success-icon']); + $this->notify->error('Error notification', ['icon' => 'error-icon']); + $this->notify->warning('Warning notification', ['icon' => 'warning-icon']); + + $expected = [ + [ + 'message' => 'Info notification', + 'type' => 'info', + 'extra' => [ + 'icon' => 'info-icon', + ], + ], + [ + 'message' => 'Success notification', + 'type' => 'success', + 'extra' => [ + 'icon' => 'success-icon', + ], + ], + [ + 'message' => 'Error notification', + 'type' => 'danger', + 'extra' => [ + 'icon' => 'error-icon', + ], + ], + [ + 'message' => 'Warning notification', + 'type' => 'warning', + 'extra' => [ + 'icon' => 'warning-icon', + ], + ], + ]; - static::assertEquals($message, $this->notify->message()); - static::assertEquals($type, $this->notify->type()); - static::assertEquals($options, $this->notify->options(true)); - static::assertEquals($options['color'], $this->notify->option('color')); - static::assertEquals($options['position'], $this->notify->option('position')); + static::assertInstanceOf( + Collection::class, + $notifications = $this->notify->notifications() + ); + static::assertEquals($expected, $notifications->toArray()); } } diff --git a/tests/TestCase.php b/tests/TestCase.php index dae0f10..07ad9cb 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -1,6 +1,6 @@