├── .travis.yml
├── CHANGELOG.md
├── LICENSE.md
├── README.md
├── composer.json
├── phpunit.xml
├── src
├── Krucas
│ └── Notification
│ │ ├── Collection.php
│ │ ├── Facades
│ │ └── Notification.php
│ │ ├── Message.php
│ │ ├── Middleware
│ │ └── NotificationMiddleware.php
│ │ ├── Notification.php
│ │ ├── NotificationServiceProvider.php
│ │ ├── NotificationsBag.php
│ │ └── Subscriber.php
└── config
│ ├── .gitkeep
│ └── notification.php
└── tests
├── .gitkeep
├── CollectionTest.php
├── MessageTest.php
├── Middleware
└── NotificationMiddlewareTest.php
├── Mocks
└── NotificationsBagMock.php
├── NotificationBagTest.php
├── NotificationTest.php
└── SubscriberTest.php
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: php
2 |
3 | php:
4 | - 7.1
5 | - 7.2
6 | - 7.3
7 |
8 | before_script:
9 | - curl -s http://getcomposer.org/installer | php
10 | - php composer.phar install --dev
11 |
12 | script: phpunit
13 |
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | #5.1.1
2 | ---
3 |
4 | * Update version matrix information
5 |
6 | ---
7 |
8 | #5.1.0
9 | ---
10 |
11 | * Improved configuration options
12 | * Create new containers dynamically
13 | * Add messages via closures
14 | * Removed message aliasing
15 | * Improved message positioning
16 | * Render messages via blade extension
17 |
18 | ---
19 |
20 | #5.0.0
21 | ---
22 |
23 | * Fully supported Laravel 5
24 |
25 | ---
26 |
27 | #4.0.0
28 | ---
29 |
30 | * Support for Laravel 5
31 |
32 | ---
33 |
34 | # 2.0.1
35 | ---
36 |
37 | * Fix ```$this``` usage when in Closure in ServiceProvider
38 |
39 | ---
40 |
41 | # 2.0.0
42 | ---
43 |
44 | * Add message types dynamically
45 | * Changes in position and alias API
46 | * Added events
47 | * Updated ```config.php``` file
48 | * Messages now flashed using events
49 | * Refactored library
50 |
51 | ---
52 |
53 | # 1.2.3
54 | ---
55 |
56 | * Check if ```session.store``` is set before using it.
57 |
58 | ---
59 |
60 | # 1.2
61 |
62 | ---
63 |
64 | * Refactored how messages are stored in bag.
65 | * Added method ```getAtPosition($position)``` to a NotificationBag.
66 | * Added method ```getAliased($alias)``` to a NotificationBag and Collection classes.
67 | * Added method ```group()``` to NotificationBag to allow render grouping.
68 | * When working directly with ```Notification```, you will work just with default container.
69 | * Session prefix now is configurable.
70 | * Refactored ```Notification``` class, now uses ```__call()``` to call methods on a default container.
71 |
72 | ---
73 |
74 | # 1.1.1
75 |
76 | ---
77 |
78 | * Added test to test message flashing after adding alias and / or position.
79 | * Fix message flashing when using ```alias()``` and / or ```atPosition```.
80 |
81 | ---
82 |
83 | # 1.1
84 |
85 | ---
86 |
87 | * Added methods to clear notifications for a given type / all in a container.
88 | * Message aliasing, allows to use alias on message, so it can be overridden when needed.
89 | * Message positioning.
90 |
91 | ---
92 |
93 | # 1.0.1
94 |
95 | ---
96 |
97 | * Fixed $app scopes when registering component.
98 |
99 | ---
100 |
101 | # 1.0
102 |
103 | ---
104 |
105 | * Initial release.
--------------------------------------------------------------------------------
/LICENSE.md:
--------------------------------------------------------------------------------
1 | Copyright (C) 2013 Edvinas Kručas
2 |
3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4 |
5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6 |
7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | **Package is looking for maintainers Please contact me if interested.**
2 |
3 | # Notification package for Laravel4 / Laravel5
4 |
5 | [](https://travis-ci.org/edvinaskrucas/notification)
6 |
7 | ---
8 |
9 | A simple notification management package for Laravel4.
10 |
11 | ---
12 |
13 | * Notification containers
14 | * Notification collections
15 | * Notification messages
16 | * Formats for notifications
17 | * Flash / instant notifications
18 | * Method chaining
19 | * Message positioning
20 |
21 | ---
22 |
23 | ## Installation
24 |
25 | Just place require new package for your laravel installation via composer.json
26 |
27 | "edvinaskrucas/notification": "5.*"
28 |
29 | Then hit ```composer update```
30 |
31 | ### Version matrix
32 |
33 | | Laravel Version | Package version |
34 | | --------------------- | ------------------------ |
35 | | = 5.4 | 5.2.* |
36 | | >= 5.1 | 5.1.* |
37 | | >= 5.0, < 5.1 | 5.0.* |
38 | | >= 4, < 5 | >= 2, <= 3 |
39 |
40 | ### Registering to use it with laravel
41 |
42 | Add following lines to ```app/config/app.php```
43 |
44 | ServiceProvider array
45 |
46 | ```php
47 | \Krucas\Notification\NotificationServiceProvider::class,
48 | ```
49 |
50 | Kernel middleware array (```must be placed after 'Illuminate\Session\Middleware\StartSession' middleware```)
51 | ```php
52 | \Krucas\Notification\Middleware\NotificationMiddleware::class,
53 | ```
54 |
55 | Now you are able to use it with Laravel4.
56 |
57 | ### Publishing config file
58 |
59 | If you want to edit default config file, just publish it to your app folder.
60 |
61 | php artisan vendor:publish --provider="\Krucas\Notification\NotificationServiceProvider" --tag="config"
62 |
63 | ## Usage
64 |
65 | ### Default usage
66 |
67 | Adding message to default container.
68 | ```php
69 | \Krucas\Notification\Facades\Notification::success('Success message');
70 | \Krucas\Notification\Facades\Notification::error('Error message');
71 | \Krucas\Notification\Facades\Notification::info('Info message');
72 | \Krucas\Notification\Facades\Notification::warning('Warning message');
73 | ```
74 |
75 | ### Containers
76 |
77 | Containers allows you to set up different containers for different placeholders.
78 |
79 | You can pass closure to modify containers, simply use this syntax showed below
80 | ```php
81 | \Krucas\Notification\Facades\Notification::container('myContainer', function($container)
82 | {
83 | $container->info('Test info message');
84 | $container->error('Error');
85 | });
86 | ```
87 |
88 | Also you can access container like this
89 | ```php
90 | \Krucas\Notification\Facades\Notification::container('myContainer')->info('Info message');
91 | ```
92 |
93 | Method chaining
94 | ```php
95 | \Krucas\Notification\Facades\Notification::container('myContainer')->info('Info message')->error('Error message');
96 | ```
97 |
98 | If you want to use default container just use ```null``` as container name. Name will be taken from config file.
99 | ```php
100 | \Krucas\Notification\Facades\Notification::container()->info('Info message');
101 | ```
102 |
103 | ### Instant notifications (shown in same request)
104 |
105 | Library supports not only flash messages, if you want to show notifications in same request just use
106 | ```php
107 | \Krucas\Notification\Facades\Notification::successInstant('Instant success message');
108 | ```
109 |
110 | ### Custom single message format
111 |
112 | Want a custom format for single message? No problem
113 | ```php
114 | \Krucas\Notification\Facades\Notification::success('Success message', 'Custom format :message');
115 | ```
116 |
117 | Also you can still pass second param (format), to format messages, but you can format individual messages as shown above.
118 |
119 | ### Add message as object
120 |
121 | You can add messages as objects
122 | ```php
123 | \Krucas\Notification\Facades\Notification::success(
124 | \Krucas\Notification\Facades\Notification::message('Sample text')
125 | );
126 | ```
127 |
128 | When adding message as object you can add additional params to message
129 | ```php
130 | \Krucas\Notification\Facades\Notification::success(
131 | \Krucas\Notification\Facades\Notification::message('Sample text')->format(':message')
132 | );
133 | ```
134 |
135 | ### Add message as closure
136 |
137 | You can add messages by using a closure
138 | ```php
139 | \Krucas\Notification\Facades\Notification::success(function (Message $message) {
140 | $message->setMessage('Sample text')->setPosition(1);
141 | });
142 | ```
143 |
144 | ### Accessing first notification from container
145 |
146 | You can access and show just first notification in container
147 | ```php
148 | {!! \Krucas\Notification\Facades\Notification::container('myContainer')->get('success')->first() !!}
149 | ```
150 |
151 | Accessing first notification from all types
152 | ```php
153 | {!! \Krucas\Notification\Facades\Notification::container('myContainer')->all()->first() !!}
154 | ```
155 |
156 | ### Displaying notifications
157 |
158 | To display all notifications in a default container you need to add just one line to your view file
159 | ```php
160 | {!! \Krucas\Notification\Facades\Notification::showAll() !!}
161 | ```
162 |
163 | When using ```showAll()``` you may want to group your messages by type, it can be done like this
164 | ```php
165 | {!! \Krucas\Notification\Facades\Notification::group('info', 'success', 'error', 'warning')->showAll() !!}
166 | ```
167 | This will group all your messages in group and output it, also you can use just one, two or three groups.
168 |
169 | Manipulating group output on the fly
170 | ```php
171 | \Krucas\Notification\Facades\Notification::addToGrouping('success')->removeFromGrouping('error');
172 | ```
173 |
174 | Display notifications by type in default container, you can pass custom format
175 | ```php
176 | {!! \Krucas\Notification\Facades\Notification::showError() !!}
177 | {!! \Krucas\Notification\Facades\Notification::showInfo() !!}
178 | {!! \Krucas\Notification\Facades\Notification::showWarning() !!}
179 | {!! \Krucas\Notification\Facades\Notification::showSuccess(':message') !!}
180 | ```
181 |
182 | Displaying notifications in a specific container with custom format.
183 | ```php
184 | {!! \Krucas\Notification\Facades\Notification::container('myContainer')->showInfo(':message') !!}
185 | ```
186 |
187 | Or you can just use blade extension
188 | ```php
189 | @notification() // will render default container
190 |
191 | @notification('custom') // will render 'custom' container
192 | ```
193 |
194 | ### Message positioning
195 |
196 | There is ability to add message to certain position.
197 | ```php
198 | // This will add message at 5th position
199 | \Krucas\Notification\Facades\Notification::info(Notification::message('info')->position(5));
200 | \Krucas\Notification\Facades\Notification::info(Notification::message('info2')->position(1);
201 | ```
202 |
203 | ### Clearing messages
204 |
205 | You can clear all messages or by type.
206 | ```php
207 | \Krucas\Notification\Facades\Notification::clearError();
208 | \Krucas\Notification\Facades\Notification::clearWarning();
209 | \Krucas\Notification\Facades\Notification::clearSuccess();
210 | \Krucas\Notification\Facades\Notification::clearInfo();
211 | \Krucas\Notification\Facades\Notification::clearAll();
212 | ```
213 |
214 | ### Add message and display it instantly in a view file
215 |
216 | Want to add message in a view file and display it? Its very simple:
217 |
218 | ```php
219 | {!! \Krucas\Notification\Facades\Notification::container('myInstant')
220 | ->infoInstant('Instant message added in a view and displayed!') !!}
221 | ```
222 |
223 | You can also add multiple messages
224 |
225 | ```php
226 | {!! \Krucas\Notification\Facades\Notification::container('myInstant')
227 | ->infoInstant('Instant message added in a view and displayed!')
228 | ->errorInstant('Error...') !!}
229 | ```
230 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "edvinaskrucas/notification",
3 | "keywords": ["laravel", "notifications", "messages", "flash", "instant"],
4 | "description": "Package for Laravel for helping to manage flash / instant notifications / messages.",
5 | "authors": [
6 | {
7 | "name": "Edvinas Kručas",
8 | "email": "edv.krucas@gmail.com"
9 | }
10 | ],
11 | "license": "MIT",
12 | "require": {
13 | "php" : ">=7.2",
14 | "illuminate/support": "6.*",
15 | "illuminate/session": "6.*"
16 | },
17 | "require-dev": {
18 | "mockery/mockery": "^1.0",
19 | "phpunit/phpunit": "^8.0"
20 | },
21 | "autoload": {
22 | "psr-0": {
23 | "Krucas\\Notification": "src/"
24 | }
25 | },
26 | "extra": {
27 | "laravel": {
28 | "providers": [
29 | "Krucas\\Notification\\NotificationServiceProvider"
30 | ]
31 | }
32 | },
33 | "minimum-stability": "dev"
34 | }
35 |
--------------------------------------------------------------------------------
/phpunit.xml:
--------------------------------------------------------------------------------
1 |
2 |
12 |
13 |
14 | ./tests/
15 |
16 |
17 |
18 |
19 | ./src/Krucas/
20 |
21 |
22 |
--------------------------------------------------------------------------------
/src/Krucas/Notification/Collection.php:
--------------------------------------------------------------------------------
1 | queue = new \SplPriorityQueue();
23 |
24 | $items = is_array($items) ? $items : $this->getArrayableItems($items);
25 |
26 | foreach ($items as $item) {
27 | $this->add($item);
28 | }
29 | }
30 |
31 | /**
32 | * Add message to collection.
33 | *
34 | * @param Message $item
35 | * @return \Krucas\Notification\Collection
36 | */
37 | public function add($item)
38 | {
39 | if (is_array($item)) {
40 | $position = $item['position'];
41 | } else {
42 | $position = $item->getPosition();
43 | }
44 |
45 | $this->queue->insert($item, is_null($position) ? null : -$position);
46 |
47 | $this->copyQueue(clone $this->queue);
48 |
49 | return $this;
50 | }
51 |
52 | /**
53 | * Copy queue items.
54 | *
55 | * @param \SplPriorityQueue $queue
56 | * @return void
57 | */
58 | protected function copyQueue(\SplPriorityQueue $queue)
59 | {
60 | $this->items = [];
61 |
62 | foreach ($queue as $item) {
63 | $this->items[] = $item;
64 | }
65 | }
66 |
67 | /**
68 | * Get the evaluated contents of the object.
69 | *
70 | * @return string
71 | */
72 | public function render()
73 | {
74 | $output = '';
75 |
76 | foreach ($this->items as $message) {
77 | $output .= $message->render();
78 | }
79 |
80 | return $output;
81 | }
82 |
83 | /**
84 | * Convert the collection to its string representation.
85 | *
86 | * @return string
87 | */
88 | public function __toString()
89 | {
90 | return $this->render();
91 | }
92 | }
93 |
--------------------------------------------------------------------------------
/src/Krucas/Notification/Facades/Notification.php:
--------------------------------------------------------------------------------
1 | setType($type);
61 | $this->setMessage($message);
62 | $this->setFlash($flash);
63 | $this->setFormat($format);
64 | $this->setPosition($position);
65 | }
66 |
67 | /**
68 | * Returns message value.
69 | *
70 | * @return null|string
71 | */
72 | public function getMessage()
73 | {
74 | return $this->message;
75 | }
76 |
77 | /**
78 | * Sets message value, and returns message object.
79 | *
80 | * @param $message
81 | * @return \Krucas\Notification\Message
82 | */
83 | public function setMessage($message)
84 | {
85 | $this->message = $message;
86 |
87 | return $this;
88 | }
89 |
90 | /**
91 | * Returns if message is flashable.
92 | *
93 | * @return bool
94 | */
95 | public function isFlash()
96 | {
97 | return $this->flash;
98 | }
99 |
100 | /**
101 | * Sets flash value, and returns message object.
102 | *
103 | * @param $flash
104 | * @return \Krucas\Notification\Message
105 | */
106 | public function setFlash($flash)
107 | {
108 | $this->flash = $flash;
109 |
110 | return $this;
111 | }
112 |
113 | /**
114 | * Returns message format.
115 | *
116 | * @return null|string
117 | */
118 | public function getFormat()
119 | {
120 | return $this->format;
121 | }
122 |
123 | /**
124 | * Sets message format, and returns message object.
125 | *
126 | * @param $format
127 | * @return \Krucas\Notification\Message
128 | */
129 | public function setFormat($format)
130 | {
131 | $this->format = $format;
132 |
133 | return $this;
134 | }
135 |
136 | /**
137 | * Returns message type.
138 | *
139 | * @return null|string
140 | */
141 | public function getType()
142 | {
143 | return $this->type;
144 | }
145 |
146 | /**
147 | * Sets message type, and returns message object.
148 | *
149 | * @param $type
150 | * @return \Krucas\Notification\Message
151 | */
152 | public function setType($type)
153 | {
154 | $this->type = $type;
155 |
156 | return $this;
157 | }
158 |
159 | /**
160 | * Returns message position.
161 | *
162 | * @return int|null
163 | */
164 | public function getPosition()
165 | {
166 | return $this->position;
167 | }
168 |
169 | /**
170 | * Sets message position.
171 | *
172 | * @param $position
173 | * @return \Krucas\Notification\Message
174 | */
175 | public function setPosition($position)
176 | {
177 | $this->position = $position;
178 |
179 | return $this;
180 | }
181 |
182 | /**
183 | * Set message.
184 | * Shortcut for `setMessage()`
185 | *
186 | * @param $message
187 | * @return \Krucas\Notification\Message
188 | */
189 | public function message($message)
190 | {
191 | $this->setMessage($message);
192 |
193 | return $this;
194 | }
195 |
196 | /**
197 | * Set format.
198 | * Shortcut for `setFormat()`
199 | *
200 | * @param $format
201 | * @return \Krucas\Notification\Message
202 | */
203 | public function format($format)
204 | {
205 | $this->setFormat($format);
206 |
207 | return $this;
208 | }
209 |
210 | /**
211 | * Set message to be instant.
212 | * Shortcut for `setFlash()`
213 | *
214 | * @return \Krucas\Notification\Message
215 | */
216 | public function instant()
217 | {
218 | $this->setFlash(false);
219 |
220 | return $this;
221 | }
222 |
223 | /**
224 | * Set message to be flashable.
225 | * Shortcut for `setFlash()`
226 | *
227 | * @return \Krucas\Notification\Message
228 | */
229 | public function flash()
230 | {
231 | $this->setFlash(true);
232 |
233 | return $this;
234 | }
235 |
236 | /**
237 | * Set message position.
238 | * Shortcut for `setPosition()`
239 | *
240 | * @param $position
241 | * @return \Krucas\Notification\Message
242 | */
243 | public function position($position)
244 | {
245 | $this->setPosition($position);
246 |
247 | return $this;
248 | }
249 |
250 | /**
251 | * Get the evaluated contents of the object.
252 | *
253 | * @return string
254 | */
255 | public function render()
256 | {
257 | if (is_null($this->getMessage())) {
258 | return '';
259 | }
260 | return str_replace([':message', ':type'], [$this->getMessage(), $this->getType()], $this->getFormat());
261 | }
262 |
263 | /**
264 | * Convert the object to its JSON representation.
265 | *
266 | * @param int $options
267 | * @return string
268 | */
269 | public function toJson($options = 0)
270 | {
271 | return json_encode($this->toArray(), $options);
272 | }
273 |
274 | /**
275 | * Get the instance as an array.
276 | *
277 | * @return array
278 | */
279 | public function toArray()
280 | {
281 | return get_object_vars($this);
282 | }
283 |
284 | /**
285 | * Evaluates object as string.
286 | *
287 | * @return string
288 | */
289 | public function __toString()
290 | {
291 | return $this->render();
292 | }
293 | }
294 |
--------------------------------------------------------------------------------
/src/Krucas/Notification/Middleware/NotificationMiddleware.php:
--------------------------------------------------------------------------------
1 | session = $session;
32 | $this->notification = $notification;
33 | $this->key = $key;
34 | }
35 |
36 | /**
37 | * Handle an incoming request.
38 | *
39 | * @param \Illuminate\Http\Request $request
40 | * @param \Closure $next
41 | * @return mixed
42 | */
43 | public function handle($request, Closure $next)
44 | {
45 | $containers = $this->session->get($this->key, []);
46 |
47 | if (count($containers) > 0) {
48 | foreach ($containers as $name => $messages) {
49 | /** @var \Krucas\Notification\Message $message */
50 | foreach ($messages as $message) {
51 | $this->notification->container($name)->add($message->getType(), $message, false);
52 | }
53 | }
54 | }
55 |
56 | $this->session->forget($this->key);
57 |
58 | return $next($request);
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/src/Krucas/Notification/Notification.php:
--------------------------------------------------------------------------------
1 | defaultContainer = $defaultContainer;
92 | $this->defaultTypes = $defaultTypes;
93 | $this->types = $types;
94 | $this->defaultFormat = $defaultFormat;
95 | $this->format = $format;
96 | $this->defaultFormats = $defaultFormats;
97 | $this->formats = $formats;
98 | }
99 |
100 | /**
101 | * Return name of default container.
102 | *
103 | * @return string
104 | */
105 | public function getDefaultContainerName()
106 | {
107 | return $this->defaultContainer;
108 | }
109 |
110 | /**
111 | * Set types for a container.
112 | *
113 | * @param string $container
114 | * @param array $types
115 | * @return \Krucas\Notification\Notification
116 | */
117 | public function setContainerTypes($container, $types = [])
118 | {
119 | $this->types[$container] = $types;
120 |
121 | return $this;
122 | }
123 |
124 | /**
125 | * Return types for a container.
126 | *
127 | * @param $container
128 | * @return array
129 | */
130 | public function getContainerTypes($container)
131 | {
132 | if (isset($this->types[$container])) {
133 | return $this->types[$container];
134 | }
135 |
136 | return $this->defaultTypes;
137 | }
138 |
139 | /**
140 | * Set format for a container.
141 | *
142 | * @param $container
143 | * @param null $format
144 | * @return \Krucas\Notification\Notification
145 | */
146 | public function setContainerFormat($container, $format = null)
147 | {
148 | $this->format[$container] = $format;
149 |
150 | return $this;
151 | }
152 |
153 | /**
154 | * Return format for a container.
155 | *
156 | * @param $container
157 | * @return string|null
158 | */
159 | public function getContainerFormat($container)
160 | {
161 | if (isset($this->format[$container])) {
162 | return $this->format[$container];
163 | }
164 |
165 | return $this->defaultFormat;
166 | }
167 |
168 | /**
169 | * Set formats for a container.
170 | *
171 | * @param $container
172 | * @param array $formats
173 | * @return \Krucas\Notification\Notification
174 | */
175 | public function setContainerFormats($container, $formats = array())
176 | {
177 | $this->formats[$container] = $formats;
178 |
179 | return $this;
180 | }
181 |
182 | /**
183 | * Return formats for a container.
184 | *
185 | * @param $container
186 | * @return array
187 | */
188 | public function getContainerFormats($container)
189 | {
190 | if (isset($this->formats[$container])) {
191 | return $this->formats[$container];
192 | }
193 |
194 | return $this->defaultFormats;
195 | }
196 |
197 | /**
198 | * Add new container.
199 | *
200 | * @param string $container
201 | * @param array $types
202 | * @param null $defaultFormat
203 | * @param array $formats
204 | * @return \Krucas\Notification\Notification
205 | */
206 | public function addContainer($container, $types = [], $defaultFormat = null, $formats = [])
207 | {
208 | if (isset($this->containers[$container])) {
209 | return $this;
210 | }
211 |
212 | $this->containers[$container] = new NotificationsBag($container, $types, $defaultFormat, $formats);
213 | $this->containers[$container]->setNotification($this);
214 |
215 | return $this;
216 | }
217 |
218 | /**
219 | * Return array of available containers.
220 | *
221 | * @return array
222 | */
223 | public function getContainers()
224 | {
225 | return $this->containers;
226 | }
227 |
228 | /**
229 | * Returns container instance.
230 | *
231 | * @param null|string $container
232 | * @param callable $callback
233 | * @return \Krucas\Notification\NotificationsBag
234 | */
235 | public function container($container = null, Closure $callback = null)
236 | {
237 | $container = is_null($container) ? $this->defaultContainer : $container;
238 |
239 | if (!isset($this->containers[$container])) {
240 | $this->addContainer(
241 | $container,
242 | $this->getContainerTypes($container),
243 | $this->getContainerFormat($container),
244 | $this->getContainerFormats($container)
245 | );
246 | }
247 |
248 | if (is_callable($callback)) {
249 | $callback($this->containers[$container]);
250 | }
251 |
252 | return $this->containers[$container];
253 | }
254 |
255 | /**
256 | * Create new message instance.
257 | *
258 | * @param null $message
259 | * @return \Krucas\Notification\Message
260 | */
261 | public function message($message = null)
262 | {
263 | $m = new Message();
264 | $m->setMessage($message);
265 | return $m;
266 | }
267 |
268 | /**
269 | * Fire given event.
270 | *
271 | * @param $event
272 | * @param \Krucas\Notification\NotificationsBag $notificationBag
273 | * @param \Krucas\Notification\Message $message
274 | * @return array|bool|null
275 | */
276 | public function fire($event, NotificationsBag $notificationBag, Message $message)
277 | {
278 | if (!isset(static::$dispatcher)) {
279 | return true;
280 | }
281 |
282 | $event = "notification.{$event}: ".$notificationBag->getName();
283 |
284 | return static::$dispatcher->dispatch($event, array($this, $notificationBag, $message));
285 | }
286 |
287 | /**
288 | * Get the event dispatcher instance.
289 | *
290 | * @return \Illuminate\Contracts\Events\Dispatcher
291 | */
292 | public static function getEventDispatcher()
293 | {
294 | return static::$dispatcher;
295 | }
296 |
297 | /**
298 | * Set the event dispatcher instance.
299 | *
300 | * @param \Illuminate\Contracts\Events\Dispatcher $dispatcher
301 | * @return void
302 | */
303 | public static function setEventDispatcher(Dispatcher $dispatcher)
304 | {
305 | static::$dispatcher = $dispatcher;
306 | }
307 |
308 | /**
309 | * Unset the event dispatcher for models.
310 | *
311 | * @return void
312 | */
313 | public static function unsetEventDispatcher()
314 | {
315 | static::$dispatcher = null;
316 | }
317 |
318 | /**
319 | * Calls NotificationBag function for a default container.
320 | *
321 | * @param $name
322 | * @param $arguments
323 | * @return \Krucas\Notification\NotificationsBag|null
324 | */
325 | public function __call($name, $arguments)
326 | {
327 | return call_user_func_array(array($this->container(null), $name), $arguments);
328 | }
329 | }
330 |
--------------------------------------------------------------------------------
/src/Krucas/Notification/NotificationServiceProvider.php:
--------------------------------------------------------------------------------
1 | publishes(array(
25 | __DIR__ . '/../../config/notification.php' => config_path('notification.php'),
26 | ), 'config');
27 |
28 | $dispatcher->subscribe('Krucas\Notification\Subscriber');
29 |
30 | $this->app->afterResolving('blade.compiler', function ($bladeCompiler) {
31 | $bladeCompiler->directive('notification', function ($container = null) {
32 | if (strcasecmp('()', $container) === 0) {
33 | $container = null;
34 | }
35 |
36 | return "container({$container})->show(); ?>";
37 | });
38 | });
39 | }
40 |
41 | /**
42 | * Register the service provider.
43 | *
44 | * @return void
45 | */
46 | public function register()
47 | {
48 | $this->mergeConfigFrom(__DIR__ . '/../../config/notification.php', 'notification');
49 |
50 | $this->app->singleton('notification', function ($app) {
51 | $config = $app['config'];
52 |
53 | $notification = new Notification(
54 | $config->get('notification.default_container'),
55 | $config->get('notification.default_types'),
56 | $config->get('notification.types'),
57 | $config->get('notification.default_format'),
58 | $config->get('notification.format'),
59 | $config->get('notification.default_formats'),
60 | $config->get('notification.formats')
61 | );
62 |
63 | $notification->setEventDispatcher($app['events']);
64 |
65 | return $notification;
66 | });
67 |
68 | $this->app->alias('notification', 'Krucas\Notification\Notification');
69 |
70 | $this->app->singleton('Krucas\Notification\Subscriber', function ($app) {
71 | return new Subscriber($app['session.store'], $app['config']['notification.session_key']);
72 | });
73 |
74 | $this->app->singleton('Krucas\Notification\Middleware\NotificationMiddleware', function ($app) {
75 | return new NotificationMiddleware(
76 | $app['session.store'],
77 | $app['notification'],
78 | $app['config']->get('notification.session_key')
79 | );
80 | });
81 | }
82 |
83 | /**
84 | * Get the services provided by the provider.
85 | *
86 | * @return array
87 | */
88 | public function provides()
89 | {
90 | return array(
91 | 'Krucas\Notification\Notification',
92 | 'Krucas\Notification\Subscriber',
93 | 'notification',
94 | );
95 | }
96 | }
--------------------------------------------------------------------------------
/src/Krucas/Notification/NotificationsBag.php:
--------------------------------------------------------------------------------
1 | '{type}',
31 | 'instant' => '{type}Instant',
32 | 'clear' => 'clear{uType}',
33 | 'show' => 'show{uType}',
34 | );
35 |
36 | /**
37 | * Default format for all message types.
38 | *
39 | * @var string
40 | */
41 | protected $defaultFormat = null;
42 |
43 | /**
44 | * Default formats for types.
45 | *
46 | * @var array
47 | */
48 | protected $formats = array();
49 |
50 | /**
51 | * Collection to store all instant notification messages.
52 | *
53 | * @var \Krucas\Notification\Collection|null
54 | */
55 | protected $notifications;
56 |
57 | /**
58 | * Sequence of how messages should be rendered by its type.
59 | *
60 | * @var array
61 | */
62 | protected $groupForRender = array();
63 |
64 | /**
65 | * Notification library instance.
66 | *
67 | * @var \Krucas\Notification\Notification
68 | */
69 | protected $notification;
70 |
71 | /**
72 | * Creates new NotificationBag object.
73 | *
74 | * @param $container
75 | * @param array $types
76 | * @param null $defaultFormat
77 | * @param array $formats
78 | */
79 | public function __construct($container, $types = array(), $defaultFormat = null, $formats = array())
80 | {
81 | $this->container = $container;
82 | $this->addType($types);
83 | $this->setDefaultFormat($defaultFormat);
84 | $this->setFormats($formats);
85 | $this->notifications = new Collection();
86 | }
87 |
88 | /**
89 | * Returns assigned container name.
90 | *
91 | * @return string
92 | */
93 | public function getName()
94 | {
95 | return $this->container;
96 | }
97 |
98 | /**
99 | * Add new available type of message to bag.
100 | *
101 | * @param $type
102 | * @return \Krucas\Notification\NotificationsBag
103 | */
104 | public function addType($type)
105 | {
106 | if (func_num_args() > 1) {
107 | foreach (func_get_args() as $t) {
108 | $this->addType($t);
109 | }
110 | } else {
111 | if (is_array($type)) {
112 | foreach ($type as $t) {
113 | $this->addType($t);
114 | }
115 | } else {
116 | if (!$this->typeIsAvailable($type)) {
117 | $this->types[] = $type;
118 | }
119 | }
120 | }
121 |
122 | return $this;
123 | }
124 |
125 | /**
126 | * Return available types of messages in container.
127 | *
128 | * @return array
129 | */
130 | public function getTypes()
131 | {
132 | return $this->types;
133 | }
134 |
135 | /**
136 | * Determines if type is available in container.
137 | *
138 | * @param $type
139 | * @return bool
140 | */
141 | public function typeIsAvailable($type)
142 | {
143 | return in_array($type, array_values($this->types)) ? true : false;
144 | }
145 |
146 | /**
147 | * Resets types values.
148 | *
149 | * @return \Krucas\Notification\NotificationsBag
150 | */
151 | public function clearTypes()
152 | {
153 | $this->types = array();
154 |
155 | return $this;
156 | }
157 |
158 | /**
159 | * Extract type from a given string.
160 | *
161 | * @param $name
162 | * @return bool|array
163 | */
164 | protected function extractType($name)
165 | {
166 | if (count($this->types) <= 0) {
167 | return false;
168 | }
169 |
170 | foreach ($this->types as $type) {
171 | foreach ($this->matcher as $function => $pattern) {
172 | if (str_replace(array('{type}', '{uType}'), array($type, ucfirst($type)), $pattern) === $name) {
173 | return array($type, $function);
174 | }
175 | }
176 | }
177 |
178 | return false;
179 | }
180 |
181 | /**
182 | * Set default format for all message types.
183 | *
184 | * @param $format
185 | * @return \Krucas\Notification\NotificationsBag
186 | */
187 | public function setDefaultFormat($format)
188 | {
189 | $this->defaultFormat = $format;
190 |
191 | return $this;
192 | }
193 |
194 | /**
195 | * Return default format.
196 | *
197 | * @return string
198 | */
199 | public function getDefaultFormat()
200 | {
201 | return $this->defaultFormat;
202 | }
203 |
204 | /**
205 | * Set formats for a given types.
206 | *
207 | * @param $formats
208 | * @return \Krucas\Notification\NotificationsBag
209 | */
210 | public function setFormats($formats)
211 | {
212 | foreach ($formats as $type => $format) {
213 | $this->setFormat($type, $format);
214 | }
215 |
216 | return $this;
217 | }
218 |
219 | /**
220 | * Set format for a given type.
221 | *
222 | * @param $type
223 | * @param $format
224 | * @return \Krucas\Notification\NotificationsBag
225 | */
226 | public function setFormat($type, $format)
227 | {
228 | if ($this->typeIsAvailable($type)) {
229 | $this->formats[$type] = $format;
230 | }
231 |
232 | return $this;
233 | }
234 |
235 | /**
236 | * Return format for a given type.
237 | *
238 | * @param $type
239 | * @return bool|string
240 | */
241 | public function getFormat($type)
242 | {
243 | if (!$this->typeIsAvailable($type)) {
244 | return false;
245 | }
246 |
247 | if (isset($this->formats[$type])) {
248 | return $this->formats[$type];
249 | }
250 |
251 | if (!is_null($this->getDefaultFormat())) {
252 | return $this->getDefaultFormat();
253 | }
254 |
255 | return false;
256 | }
257 |
258 | /**
259 | * Clear format for a given type.
260 | *
261 | * @param $type
262 | * @return \Krucas\Notification\NotificationsBag
263 | */
264 | public function clearFormat($type)
265 | {
266 | unset($this->formats[$type]);
267 |
268 | return $this;
269 | }
270 |
271 | /**
272 | * Clear all formats.
273 | *
274 | * @return \Krucas\Notification\NotificationsBag
275 | */
276 | public function clearFormats()
277 | {
278 | $this->formats = array();
279 |
280 | return $this;
281 | }
282 |
283 | /**
284 | * Returns valid format.
285 | *
286 | * @param $format
287 | * @param null $type
288 | * @return null
289 | */
290 | protected function checkFormat($format, $type = null)
291 | {
292 | return !is_null($format) ? $format : $this->getFormat($type);
293 | }
294 |
295 | /**
296 | * Adds new notification message to one of collections.
297 | * If message is array, adds multiple messages.
298 | * Message can be string, array (array can contain string for message, or array of message and format).
299 | * Flashes flashable messages.
300 | *
301 | * @param $type
302 | * @param string|\Krucas\Notification\Message|\Closure $message
303 | * @param bool $flash
304 | * @param null $format
305 | * @return \Krucas\Notification\NotificationsBag
306 | */
307 | public function add($type, $message, $flash = true, $format = null)
308 | {
309 | if (!$this->typeIsAvailable($type)) {
310 | return $this;
311 | }
312 |
313 | if ($message instanceof \Krucas\Notification\Message) {
314 | $m = $message;
315 | $this->addInstance($m, $type, $flash, $format);
316 | } elseif ($message instanceof Closure) {
317 | $m = new Message($type, null, $flash, $format);
318 | call_user_func_array($message, [$m]);
319 | $this->addInstance($m, $type, $flash, $format);
320 | } else {
321 | $m = new Message($type, $message, $flash, $this->checkFormat($format, $type));
322 | }
323 |
324 | if (!$m->isFlash()) {
325 | $this->notifications->add($m);
326 | $this->fireEvent('added', $m);
327 | } else {
328 | $this->fireEvent('flash', $m);
329 | }
330 |
331 | return $this;
332 | }
333 |
334 | /**
335 | * Add message by instance.
336 | *
337 | * @param \Krucas\Notification\Message $message
338 | * @param string $type
339 | * @param bool $flash
340 | * @param null $format
341 | */
342 | protected function addInstance(Message $message, $type, $flash = true, $format = null)
343 | {
344 | $message->setType($type);
345 | if ($message->isFlash() != $flash) {
346 | $message->setFlash($flash);
347 | }
348 | if (is_null($message->getFormat())) {
349 | $message->setFormat($this->getFormat($type));
350 | }
351 | if (!is_null($format)) {
352 | $message->setFormat($this->checkFormat($format, $type));
353 | }
354 | }
355 |
356 | /**
357 | * Returns all messages for given type.
358 | *
359 | * @param $type
360 | * @return \Krucas\Notification\Collection
361 | */
362 | public function get($type)
363 | {
364 | $collection = new Collection();
365 |
366 | foreach ($this->notifications as $key => $message) {
367 | if ($message->getType() == $type) {
368 | $collection->add($message);
369 | }
370 | }
371 |
372 | return $collection;
373 | }
374 |
375 | /**
376 | * Clears message for a given type.
377 | *
378 | * @param null $type
379 | * @return \Krucas\Notification\NotificationsBag
380 | */
381 | public function clear($type = null)
382 | {
383 | if (is_null($type)) {
384 | $this->notifications = new Collection();
385 | } else {
386 | $notifications = new Collection();
387 |
388 | foreach ($this->notifications as $message) {
389 | if ($message->getType() != $type) {
390 | $notifications->add($message);
391 | }
392 | }
393 |
394 | $this->notifications = $notifications;
395 | }
396 |
397 | return $this;
398 | }
399 |
400 | /**
401 | * Clears all messages.
402 | * Alias for clear(null).
403 | *
404 | * @return \Krucas\Notification\NotificationsBag
405 | */
406 | public function clearAll()
407 | {
408 | return $this->clear(null);
409 | }
410 |
411 | /**
412 | * Returns all messages in bag.
413 | *
414 | * @return \Krucas\Notification\Collection
415 | */
416 | public function all()
417 | {
418 | return $this->notifications;
419 | }
420 |
421 | /**
422 | * Returns first message object for given type.
423 | *
424 | * @return \Krucas\Notification\Message
425 | */
426 | public function first()
427 | {
428 | return $this->notifications->first();
429 | }
430 |
431 | /**
432 | * Returns generated output of non flash messages.
433 | *
434 | * @param null $type
435 | * @param null $format
436 | * @return string
437 | */
438 | public function show($type = null, $format = null)
439 | {
440 | $messages = $this->getMessagesForRender($type);
441 |
442 | $this->groupForRender = array();
443 |
444 | $output = '';
445 |
446 | foreach ($messages as $message) {
447 | if (!$message->isFlash()) {
448 | if (!is_null($format)) {
449 | $message->setFormat($format);
450 | }
451 |
452 | $output .= $message->render();
453 | }
454 | }
455 |
456 | return $output;
457 | }
458 |
459 | /**
460 | * Renders all messages.
461 | *
462 | * @param null $format
463 | * @return string
464 | */
465 | public function showAll($format = null)
466 | {
467 | return $this->show(null, $format);
468 | }
469 |
470 | /**
471 | * Resolves which messages should be returned for rendering.
472 | *
473 | * @param null $type
474 | * @return \Krucas\Notification\Collection
475 | */
476 | protected function getMessagesForRender($type = null)
477 | {
478 | if (is_null($type)) {
479 | if (count($this->groupForRender) > 0) {
480 | $messages = array();
481 |
482 | foreach ($this->groupForRender as $typeToRender) {
483 | $messages = array_merge($messages, $this->get($typeToRender)->all());
484 | }
485 |
486 | return new Collection($messages);
487 | }
488 |
489 | return $this->all();
490 | }
491 | return $this->get($type);
492 | }
493 |
494 | /**
495 | * Return array with groups list for rendering.
496 | *
497 | * @return array
498 | */
499 | public function getGroupingForRender()
500 | {
501 | return $this->groupForRender;
502 | }
503 |
504 | /**
505 | * Set order to render types.
506 | * Call this method: group('success', 'info', ...)
507 | *
508 | * @return \Krucas\Notification\NotificationsBag
509 | */
510 | public function group()
511 | {
512 | if (func_num_args() > 0) {
513 | $types = func_get_args();
514 | $this->groupForRender = array();
515 | foreach ($types as $type) {
516 | $this->addToGrouping($type);
517 | }
518 | }
519 |
520 | return $this;
521 | }
522 |
523 | /**
524 | * Adds type for rendering.
525 | *
526 | * @param $type
527 | * @return \Krucas\Notification\NotificationsBag
528 | */
529 | public function addToGrouping($type)
530 | {
531 | if (!$this->typeIsAvailable($type)) {
532 | return $this;
533 | }
534 |
535 | if (!in_array($type, $this->groupForRender)) {
536 | $this->groupForRender[] = $type;
537 | }
538 |
539 | return $this;
540 | }
541 |
542 | /**
543 | * Removes type from rendering.
544 | *
545 | * @param $type
546 | * @return \Krucas\Notification\NotificationsBag
547 | */
548 | public function removeFromGrouping($type)
549 | {
550 | foreach ($this->groupForRender as $key => $typeToRender) {
551 | if ($type == $typeToRender) {
552 | unset($this->groupForRender[$key]);
553 | }
554 | }
555 |
556 | $this->groupForRender = array_values($this->groupForRender);
557 |
558 | return $this;
559 | }
560 |
561 | /**
562 | * Get the instance as an array.
563 | *
564 | * @return array
565 | */
566 | public function toArray()
567 | {
568 | $arr = array
569 | (
570 | 'container' => $this->container,
571 | 'format' => $this->getDefaultFormat(),
572 | 'types' => $this->getTypes(),
573 | 'notifications' => $this->notifications->toArray()
574 | );
575 |
576 | return $arr;
577 | }
578 |
579 | /**
580 | * Convert the object to its JSON representation.
581 | *
582 | * @param int $options
583 | * @return string
584 | */
585 | public function toJson($options = 0)
586 | {
587 | return json_encode($this->toArray(), $options);
588 | }
589 |
590 | /**
591 | * Convert the Bag to its string representation.
592 | *
593 | * @return string
594 | */
595 | public function __toString()
596 | {
597 | return (string) $this->notifications;
598 | }
599 |
600 | /**
601 | * Count the number of colections.
602 | *
603 | * @return int
604 | */
605 | public function count()
606 | {
607 | return count($this->notifications);
608 | }
609 |
610 | /**
611 | * Check if a message is set for given type.
612 | *
613 | * @param $type
614 | * @return bool
615 | */
616 | public function has($type = null)
617 | {
618 | if ($this->count() <= 0) {
619 | return false;
620 | }
621 |
622 | if (is_null($type)) {
623 | return true;
624 | }
625 |
626 | if (!$this->typeIsAvailable($type)) {
627 | return false;
628 | }
629 |
630 | foreach ($this->notifications as $key => $message) {
631 | if ($message->getType() == $type) {
632 | return true;
633 | }
634 | }
635 |
636 | return false;
637 | }
638 |
639 | /**
640 | * Fire event for a given message.
641 | *
642 | * @param $event
643 | * @param $message
644 | * @return boolean
645 | */
646 | protected function fireEvent($event, $message)
647 | {
648 | if (!isset($this->notification)) {
649 | return true;
650 | }
651 |
652 | return $this->getNotification()->fire($event, $this, $message);
653 | }
654 |
655 | /**
656 | * Set notification instance.
657 | *
658 | * @param \Krucas\Notification\Notification $notification
659 | * @return void
660 | */
661 | public function setNotification(Notification $notification)
662 | {
663 | $this->notification = $notification;
664 | }
665 |
666 | /**
667 | * Get notification instance.
668 | *
669 | * @return \Krucas\Notification\Notification
670 | */
671 | public function getNotification()
672 | {
673 | return $this->notification;
674 | }
675 |
676 | /**
677 | * Unset notification instance.
678 | *
679 | * @return void
680 | */
681 | public function unsetNotification()
682 | {
683 | $this->notification = null;
684 | }
685 |
686 | /**
687 | * Execute short version of function calls.
688 | *
689 | * @param $name
690 | * @param $arguments
691 | * @return \Krucas\Notification\NotificationsBag|string
692 | */
693 | public function __call($name, $arguments)
694 | {
695 | if (($extracted = $this->extractType($name)) !== false) {
696 | switch($extracted[1]) {
697 | case 'add':
698 | return $this->add(
699 | $extracted[0],
700 | isset($arguments[0]) ? $arguments[0] : null,
701 | true,
702 | isset($arguments[1]) ? $arguments[1] : null
703 | );
704 | break;
705 |
706 | case 'instant':
707 | return $this->add(
708 | $extracted[0],
709 | isset($arguments[0]) ? $arguments[0] : null,
710 | false,
711 | isset($arguments[1]) ? $arguments[1] : null
712 | );
713 | break;
714 |
715 | case 'clear':
716 | return $this->clear($extracted[0]);
717 | break;
718 |
719 | case 'show':
720 | return $this->show($extracted[0], isset($arguments[0]) ? $arguments[0] : null);
721 | break;
722 | }
723 | }
724 | }
725 | }
726 |
--------------------------------------------------------------------------------
/src/Krucas/Notification/Subscriber.php:
--------------------------------------------------------------------------------
1 | session = $session;
30 | $this->key = $key;
31 | }
32 |
33 | /**
34 | * Get session instance.
35 | *
36 | * @return \Illuminate\Session\Store
37 | */
38 | public function getSession()
39 | {
40 | return $this->session;
41 | }
42 |
43 | /**
44 | * Get session key.
45 | *
46 | * @return string
47 | */
48 | public function getKey()
49 | {
50 | return $this->key;
51 | }
52 |
53 | /**
54 | * Execute this event to flash messages.
55 | *
56 | * @param string $eventName
57 | * @param array $data Event payload. Should be an array containing 3 elements:
58 | * [ Notification, NotificationsBag, Message ]
59 | * @return bool
60 | */
61 | public function onFlash($eventName, array $data)
62 | {
63 | $this->validateEventData($data);
64 |
65 | list($notification, $notificationBag, $message) = $data;
66 |
67 | $key = implode('.', [$this->key, $notificationBag->getName()]);
68 |
69 | $this->session->push($key, $message);
70 |
71 | return true;
72 | }
73 |
74 | /**
75 | * Register the listeners for the subscriber.
76 | *
77 | * @param \Illuminate\Contracts\Events\Dispatcher $events
78 | * @return array
79 | */
80 | public function subscribe($events)
81 | {
82 | $events->listen('notification.flash: *', 'Krucas\Notification\Subscriber@onFlash');
83 | }
84 |
85 | /**
86 | * Validates that the correct event data has been passed to self::onFlash()
87 | *
88 | * Data array should have 3 elements with sequential keys: Notification, NotificationsBag and Message
89 | *
90 | * @param array $data
91 | * @throws InvalidArgumentException If the event data is invalid.
92 | */
93 | private function validateEventData(array $data)
94 | {
95 | if ( ! array_key_exists(0, $data) || ! array_key_exists(1, $data) || ! array_key_exists(2, $data)) {
96 | throw new \InvalidArgumentException(sprintf(
97 | '%s expects 3 elements in data array, %s given.',
98 | sprintf('%s::onFlash', __CLASS__),
99 | count($data)
100 | ));
101 | }
102 |
103 | if ( ! $data[0] instanceof Notification || ! $data[1] instanceof NotificationsBag || ! $data[2] instanceof Message) {
104 | $expected = [Notification::class, NotificationsBag::class, Message::class];
105 |
106 | $actual = array_map(function ($element) {
107 | return is_object($element) ? get_class($element) : '{' . gettype($element) . '}';
108 | }, $data);
109 |
110 | throw new \InvalidArgumentException(sprintf(
111 | '%s expects a data array containing [%s], actually given [%s]',
112 | sprintf('%s::onFlash', __CLASS__),
113 | implode(', ', $expected),
114 | implode(', ', $actual)
115 | ));
116 | }
117 | }
118 | }
119 |
--------------------------------------------------------------------------------
/src/config/.gitkeep:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/edvinaskrucas/notification/7ef6bd45c2845160b4b4340e2a14f476cd952739/src/config/.gitkeep
--------------------------------------------------------------------------------
/src/config/notification.php:
--------------------------------------------------------------------------------
1 | 'notifications',
14 |
15 | /*
16 | |--------------------------------------------------------------------------
17 | | Default container name
18 | |--------------------------------------------------------------------------
19 | |
20 | | This name will be used to name default container (when calling it with null value).
21 | |
22 | */
23 | 'default_container' => 'default',
24 |
25 | /*
26 | |--------------------------------------------------------------------------
27 | | Default types for dynamic containers.
28 | |--------------------------------------------------------------------------
29 | |
30 | | These types will be added for new containers.
31 | |
32 | */
33 | 'default_types' => array('info', 'success', 'warning', 'error'),
34 |
35 | /*
36 | |--------------------------------------------------------------------------
37 | | Types for containers
38 | |--------------------------------------------------------------------------
39 | |
40 | | Specify available types for each container separately.
41 | |
42 | */
43 | 'types' => array(),
44 |
45 | /*
46 | |--------------------------------------------------------------------------
47 | | Default message format
48 | |--------------------------------------------------------------------------
49 | |
50 | | This format will be used when no format is specified.
51 | | Available place holders:
52 | |
53 | | :type - type of message (error, warning, info, success).
54 | | :message - message text.
55 | |
56 | */
57 | 'default_format' => '
:message
',
58 |
59 | /*
60 | |--------------------------------------------------------------------------
61 | | Default message format for containers
62 | |--------------------------------------------------------------------------
63 | |
64 | | This format will be used to override global default format for each container.
65 | |
66 | | 'format' => array(
67 | | 'myContainer' => ':message - :type',
68 | | )
69 | |
70 | |
71 | | Available place holders:
72 | |
73 | | :type - type of message (error, warning, info, success).
74 | | :message - message text.
75 | |
76 | */
77 | 'format' => array(),
78 |
79 | /*
80 | |--------------------------------------------------------------------------
81 | | Default message formats for types
82 | |--------------------------------------------------------------------------
83 | |
84 | | These formats can override default format for each type of message (error, warning, info, success).
85 | | Available place holders:
86 | |
87 | | :type - type of message (error, warning, info, success).
88 | | :message - message text.
89 | |
90 | */
91 | 'default_formats' => array(
92 | 'info' => ':message
',
93 | 'success' => ':message
',
94 | 'warning' => ':message
',
95 | 'error' => ':message
',
96 | ),
97 |
98 | /*
99 | |--------------------------------------------------------------------------
100 | | Message formats for types and container types
101 | |--------------------------------------------------------------------------
102 | |
103 | | These formats can override default format for each type of message (error, warning, info, success).
104 | | You can set formats for each container by using this syntax:
105 | |
106 | | 'formats' => array(
107 | | 'myContainer' => array(
108 | | 'info' => ':key - :message'
109 | | )
110 | | )
111 | |
112 | | Available place holders:
113 | |
114 | | :type - type of message (error, warning, info, success).
115 | | :message - message text.
116 | |
117 | */
118 | 'formats' => array(),
119 |
120 | );
121 |
--------------------------------------------------------------------------------
/tests/.gitkeep:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/edvinaskrucas/notification/7ef6bd45c2845160b4b4340e2a14f476cd952739/tests/.gitkeep
--------------------------------------------------------------------------------
/tests/CollectionTest.php:
--------------------------------------------------------------------------------
1 | getCollection();
10 |
11 | $this->assertInstanceOf('Krucas\Notification\Collection', $collection);
12 | $this->assertCount(0, $collection);
13 | }
14 |
15 | public function testAddingMessagesToCollection()
16 | {
17 | $collection = $this->getCollection();
18 | $this->assertCount(0, $collection);
19 |
20 | $collection->add(new \Krucas\Notification\Message());
21 | $this->assertCount(1, $collection);
22 |
23 | $collection->add(new \Krucas\Notification\Message());
24 | $this->assertCount(2, $collection);
25 | }
26 |
27 | public function testContainsMethod()
28 | {
29 | $collection = $this->getCollection();
30 |
31 | $collection->add(new \Krucas\Notification\Message());
32 |
33 | $this->assertCount(1, $collection);
34 | $this->assertTrue($collection->contains(new \Krucas\Notification\Message()));
35 | $this->assertFalse($collection->contains(new \Krucas\Notification\Message('error')));
36 | }
37 |
38 | public function testCollectionRender()
39 | {
40 | $collection = $this->getCollection();
41 |
42 | $collection->add(new \Krucas\Notification\Message('error', 'error message', false, ':type: :message'));
43 | $collection->add(new \Krucas\Notification\Message('warning', 'w', false, ':message'));
44 |
45 | $this->assertCount(2, $collection);
46 | $this->assertEquals('error: error messagew', $collection->render());
47 | }
48 |
49 | public function testCollectionToString()
50 | {
51 | $collection = $this->getCollection();
52 |
53 | $collection->add(new \Krucas\Notification\Message('error', 'error message', false, ':type: :message'));
54 | $collection->add(new \Krucas\Notification\Message('warning', 'w', false, ':message'));
55 |
56 | $this->assertCount(2, $collection);
57 | $this->assertEquals('error: error messagew', (string)$collection);
58 | }
59 |
60 | public function testSetAtPosition()
61 | {
62 | $collection = $this->getCollection();
63 |
64 | $message1 = new \Krucas\Notification\Message();
65 | $message1->setPosition(2);
66 |
67 | $message2 = new \Krucas\Notification\Message();
68 | $message2->setPosition(1);
69 |
70 | $collection
71 | ->add($message1)
72 | ->add($message2);
73 |
74 | $this->assertEquals($message2, $collection[0]);
75 | $this->assertEquals($message1, $collection[1]);
76 | }
77 |
78 | protected function getCollection()
79 | {
80 | return new \Krucas\Notification\Collection();
81 | }
82 | }
83 |
--------------------------------------------------------------------------------
/tests/MessageTest.php:
--------------------------------------------------------------------------------
1 | assertInstanceOf('Krucas\Notification\Message', $message);
12 | $this->assertEquals('error', $message->getType());
13 | $this->assertEquals('test message', $message->getMessage());
14 | $this->assertEquals(':type: :message', $message->getFormat());
15 | $this->assertFalse($message->isFlash());
16 | $this->assertEquals(4, $message->getPosition());
17 | }
18 |
19 | public function testMethodChaining()
20 | {
21 | $message = new \Krucas\Notification\Message();
22 |
23 | $this->assertInstanceOf('Krucas\Notification\Message', $message);
24 | $this->assertNull($message->getType());
25 | $this->assertNull($message->getMessage());
26 | $this->assertNull($message->getFormat());
27 | $this->assertTrue($message->isFlash());
28 | $this->assertNull($message->getPosition());
29 |
30 | $message->setFlash(false)
31 | ->setFormat('Test: :message')
32 | ->setType('warning')
33 | ->setMessage('test')
34 | ->setPosition(5);
35 |
36 | $this->assertEquals('warning', $message->getType());
37 | $this->assertEquals('test', $message->getMessage());
38 | $this->assertEquals('Test: :message', $message->getFormat());
39 | $this->assertFalse($message->isFlash());
40 | $this->assertEquals(5, $message->getPosition());
41 | }
42 |
43 | public function testToStringMethod()
44 | {
45 | $message = new \Krucas\Notification\Message('error', 'test message', false, ':type: :message');
46 |
47 | $this->assertEquals('error: test message', (string)$message);
48 | }
49 |
50 | public function testMessageRendering()
51 | {
52 | $message = new \Krucas\Notification\Message('error', 'test message', false, ':type: :message');
53 |
54 | $this->assertEquals('error: test message', $message->render());
55 | }
56 |
57 | public function testMessageToArray()
58 | {
59 | $message = new \Krucas\Notification\Message('error', 'test message', false, ':type: :message');
60 |
61 | $this->assertEquals(array(
62 | 'message' => 'test message',
63 | 'format' => ':type: :message',
64 | 'type' => 'error',
65 | 'flash' => false,
66 | 'position' => null
67 | ), $message->toArray());
68 | }
69 |
70 | public function testMessageToJson()
71 | {
72 | $message = new \Krucas\Notification\Message('error', 'test message', false, ':type: :message');
73 |
74 | $this->assertEquals(
75 | '{"message":"test message","format":":type: :message","type":"error","flash":false,"position":null}',
76 | $message->toJson()
77 | );
78 | }
79 |
80 | public function testMethodsShortcuts()
81 | {
82 | $message = new \Krucas\Notification\Message();
83 | $this->assertNull($message->getMessage());
84 | $this->assertNull($message->getFormat());
85 | $this->assertNull($message->getPosition());
86 | $this->assertTrue($message->isFlash());
87 |
88 | $message->message('test')->format(':message')->position(5);
89 | $this->assertEquals('test', $message->getMessage());
90 | $this->assertEquals(':message', $message->getFormat());
91 | $this->assertEquals(5, $message->getPosition());
92 | $this->assertTrue($message->isFlash());
93 |
94 | $message->instant();
95 | $this->assertFalse($message->isFlash());
96 |
97 | $message->flash();
98 | $this->assertTrue($message->isFlash());
99 | }
100 | }
--------------------------------------------------------------------------------
/tests/Middleware/NotificationMiddlewareTest.php:
--------------------------------------------------------------------------------
1 | getNotificationsBag();
17 | $notificationsBag->shouldReceive('add')->once()->with('error', $messages[0], false);
18 | $notificationsBag->shouldReceive('add')->once()->with('info', $messages[1], false);
19 |
20 | $session = $this->getSessionStore();
21 | $notification = $this->getNotification();
22 | $notification->shouldReceive('container')->twice()->with('test')->andReturn($notificationsBag);
23 | $prefix = 'notifications';
24 |
25 | $middleware = new \Krucas\Notification\Middleware\NotificationMiddleware($session, $notification, $prefix);
26 | $session->shouldReceive('get')->once()->with('notifications', array())->andReturn(array('test' => $messages));
27 | $session->shouldReceive('forget')->once()->with('notifications');
28 |
29 | $middleware->handle(m::mock('Illuminate\Http\Request'), function() {});
30 | }
31 |
32 | protected function getSessionStore()
33 | {
34 | return m::mock('Illuminate\Session\Store');
35 | }
36 |
37 | protected function getNotification()
38 | {
39 | return m::mock('Krucas\Notification\Notification');
40 | }
41 |
42 | protected function getNotificationsBag()
43 | {
44 | return m::mock('Krucas\Notification\NotificationsBag');
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/tests/Mocks/NotificationsBagMock.php:
--------------------------------------------------------------------------------
1 | getNotificationBag();
17 | $this->assertEquals('test', $notificationBag->getName());
18 | $this->assertEquals(array(), $notificationBag->getTypes());
19 | $this->assertNull($notificationBag->getDefaultFormat());
20 | }
21 |
22 | public function testAddType()
23 | {
24 | $notificationBag = $this->getNotificationBag();
25 | $this->assertEquals(array(), $notificationBag->getTypes());
26 |
27 | $notificationBag->addType('warning');
28 | $this->assertEquals(array('warning'), $notificationBag->getTypes());
29 | }
30 |
31 | public function testAddTypesArray()
32 | {
33 | $notificationBag = $this->getNotificationBag();
34 | $this->assertEquals(array(), $notificationBag->getTypes());
35 |
36 | $notificationBag->addType(array('info', 'danger'));
37 | $this->assertEquals(array('info', 'danger'), $notificationBag->getTypes());
38 | }
39 |
40 | public function testAddTypesArrayAsIndividualParam()
41 | {
42 | $notificationBag = $this->getNotificationBag();
43 | $this->assertEquals(array(), $notificationBag->getTypes());
44 |
45 | $notificationBag->addType('info', 'danger');
46 | $this->assertEquals(array('info', 'danger'), $notificationBag->getTypes());
47 | }
48 |
49 | public function testAddExistingType()
50 | {
51 | $notificationBag = $this->getNotificationBag();
52 |
53 | $notificationBag->addType('warning');
54 | $this->assertEquals(array('warning'), $notificationBag->getTypes());
55 |
56 | $notificationBag->addType('warning');
57 | $this->assertEquals(array('warning'), $notificationBag->getTypes());
58 | }
59 |
60 | public function testCheckIfTypeIsAvailable()
61 | {
62 | $notificationBag = $this->getNotificationBag();
63 | $this->assertFalse($notificationBag->typeIsAvailable('warning'));
64 |
65 | $notificationBag->addType('warning');
66 | $this->assertTrue($notificationBag->typeIsAvailable('warning'));
67 | }
68 |
69 | public function testClearTypes()
70 | {
71 | $notificationBag = $this->getNotificationBag();
72 | $this->assertEquals(array(), $notificationBag->getTypes());
73 |
74 | $notificationBag->addType('warning');
75 | $this->assertEquals(array('warning'), $notificationBag->getTypes());
76 |
77 | $notificationBag->clearTypes();
78 | $this->assertEquals(array(), $notificationBag->getTypes());
79 | }
80 |
81 | public function testExtractType()
82 | {
83 | $notificationBag = $this->getNotificationBag();
84 | $this->assertFalse($notificationBag->extractType('info'));
85 |
86 | $notificationBag->addType(array('info', 'success'));
87 | $this->assertEquals(array('info', 'add'), $notificationBag->extractType('info'));
88 | $this->assertEquals(array('info', 'instant'), $notificationBag->extractType('infoInstant'));
89 | $this->assertEquals(array('info', 'clear'), $notificationBag->extractType('clearInfo'));
90 | $this->assertEquals(array('info', 'show'), $notificationBag->extractType('showInfo'));
91 | $this->assertEquals(array('success', 'add'), $notificationBag->extractType('success'));
92 | }
93 |
94 | public function testExtractTypeInvalid()
95 | {
96 | $notificationBag = $this->getNotificationBag();
97 |
98 | $notificationBag->addType(array('info', 'success'));
99 | $this->assertFalse($notificationBag->extractType('ShowInfo'));
100 | $this->assertFalse($notificationBag->extractType('infoinstant'));
101 | }
102 |
103 | public function testSetDefaultFormat()
104 | {
105 | $notificationBag = $this->getNotificationBag();
106 | $notificationBag->setDefaultFormat(':type - :message');
107 | $this->assertEquals(':type - :message', $notificationBag->getDefaultFormat());
108 | }
109 |
110 | public function testSetFormatForType()
111 | {
112 | $notificationBag = $this->getNotificationBag();
113 | $notificationBag->addType('success');
114 | $this->assertFalse($notificationBag->getFormat('success'));
115 |
116 | $notificationBag->setFormat('success', 'OK - :message');
117 | $this->assertEquals('OK - :message', $notificationBag->getFormat('success'));
118 | }
119 |
120 | public function testSetFormatsArray()
121 | {
122 | $notificationBag = $this->getNotificationBag();
123 | $notificationBag->addType(array('success', 'info'));
124 | $this->assertFalse($notificationBag->getFormat('success'));
125 | $this->assertFalse($notificationBag->getFormat('info'));
126 |
127 | $notificationBag->setFormats(
128 | array(
129 | 'success' => 'OK - :message',
130 | 'info' => 'INFO - :message',
131 | )
132 | );
133 | $this->assertEquals('OK - :message', $notificationBag->getFormat('success'));
134 | $this->assertEquals('INFO - :message', $notificationBag->getFormat('info'));
135 | }
136 |
137 | public function testSetFormatForNonExistingType()
138 | {
139 | $notificationBag = $this->getNotificationBag();
140 | $this->assertFalse($notificationBag->getFormat('success'));
141 |
142 | $notificationBag->setFormat('success', 'OK - :message');
143 | $this->assertFalse($notificationBag->getFormat('success'));
144 | }
145 |
146 | public function testGetFormatWhenJustDefaultIsSet()
147 | {
148 | $notificationBag = $this->getNotificationBag();
149 | $notificationBag->addType('success');
150 | $this->assertFalse($notificationBag->getFormat('success'));
151 | $notificationBag->setDefaultFormat(':type - :message');
152 | $this->assertEquals(':type - :message', $notificationBag->getFormat('success'));
153 | }
154 |
155 | public function testClearFormat()
156 | {
157 | $notificationBag = $this->getNotificationBag();
158 | $notificationBag->addType(array('success', 'info'));
159 | $this->assertFalse(false, $notificationBag->getFormat('success'));
160 | $this->assertFalse(false, $notificationBag->getFormat('info'));
161 |
162 | $notificationBag->setFormats(
163 | array(
164 | 'success' => 'OK - :message',
165 | 'info' => 'INFO - :message',
166 | )
167 | );
168 | $this->assertEquals('OK - :message', $notificationBag->getFormat('success'));
169 | $this->assertEquals('INFO - :message', $notificationBag->getFormat('info'));
170 |
171 | $notificationBag->clearFormat('success');
172 | $this->assertFalse($notificationBag->getFormat('success'));
173 | $this->assertEquals('INFO - :message', $notificationBag->getFormat('info'));
174 | }
175 |
176 | public function testClearAllFormats()
177 | {
178 | $notificationBag = $this->getNotificationBag();
179 | $notificationBag->addType(array('success', 'info'));
180 | $this->assertFalse(false, $notificationBag->getFormat('success'));
181 | $this->assertFalse(false, $notificationBag->getFormat('info'));
182 |
183 | $notificationBag->setFormats(
184 | array(
185 | 'success' => 'OK - :message',
186 | 'info' => 'INFO - :message',
187 | )
188 | );
189 | $this->assertEquals('OK - :message', $notificationBag->getFormat('success'));
190 | $this->assertEquals('INFO - :message', $notificationBag->getFormat('info'));
191 |
192 | $notificationBag->clearFormats();
193 | $this->assertFalse($notificationBag->getFormat('success'));
194 | $this->assertFalse($notificationBag->getFormat('info'));
195 | }
196 |
197 | public function testAddMessageViaClosure()
198 | {
199 | $notificationBag = $this->getNotificationBag();
200 | $notificationBag->addType(array('info'));
201 |
202 | $notificationBag->add('info', function (\Krucas\Notification\Message $message) {
203 | $message->setMessage('test');
204 | }, false, null);
205 |
206 | $this->assertCount(1, $notificationBag);
207 | }
208 |
209 | public function testAddMessageWithCustomFormat()
210 | {
211 | $notificationBag = $this->getNotificationBag();
212 | $notificationBag->addType('success');
213 | $this->assertCount(0, $notificationBag);
214 |
215 | $notificationBag->add('success', 'test', false, 'customFormat');
216 | $this->assertCount(1, $notificationBag);
217 | $notifications = $notificationBag->all();
218 | $this->assertEquals('customFormat', $notifications[0]->getFormat());
219 | }
220 |
221 | public function testAddInstantMessageUsingNamedMethod()
222 | {
223 | $notificationBag = $this->getNotificationBag();
224 | $notificationBag->addType('success');
225 | $this->assertCount(0, $notificationBag);
226 |
227 | $notificationBag->successInstant('test');
228 | $this->assertCount(1, $notificationBag);
229 | }
230 |
231 | public function testAddMessageForNonExistingType()
232 | {
233 | $notificationBag = $this->getNotificationBag();
234 | $this->assertCount(0, $notificationBag);
235 |
236 | $notificationBag->add('success', 'test', false);
237 | $this->assertCount(0, $notificationBag);
238 | }
239 |
240 | public function testAddMessagesForMultipleTypes()
241 | {
242 | $notificationBag = $this->getNotificationBag();
243 | $notificationBag->addType(array('success', 'info'));
244 | $this->assertCount(0, $notificationBag);
245 |
246 | $notificationBag->add('success', 'test', false);
247 | $notificationBag->add('info', 'test', false);
248 | $this->assertCount(2, $notificationBag);
249 | }
250 |
251 | public function testAddMessagesForMultipleTypesUsingNamedMethods()
252 | {
253 | $notificationBag = $this->getNotificationBag();
254 | $notificationBag->addType(array('success', 'info'));
255 | $this->assertCount(0, $notificationBag);
256 |
257 | $notificationBag->add('success', 'test', false);
258 | $notificationBag->add('info', 'test', false);
259 | $this->assertCount(2, $notificationBag);
260 | }
261 |
262 | public function testAddInstantMessageWithMessageInstance()
263 | {
264 | $notificationBag = $this->getNotificationBag();
265 | $notificationBag->addType('info');
266 | $this->assertCount(0, $notificationBag);
267 |
268 | $message = $this->getMessage();
269 | $message->shouldReceive('setType')->with('info')->andReturn($message);
270 | $message->shouldReceive('isFlash')->andReturn(false);
271 | $message->shouldReceive('getPosition')->andReturn(null);
272 | $message->shouldReceive('getFormat')->andReturn(':message');
273 | $notificationBag->add('info', $message, false);
274 | $this->assertCount(1, $notificationBag);
275 | }
276 |
277 | public function testAddFlashMessageWithMessageInstance()
278 | {
279 | $notificationBag = $this->getNotificationBag();
280 | $notificationBag->addType('info');
281 | $message = $this->getMessage();
282 | $message->shouldReceive('setType')->with('info')->andReturn($message);
283 | $message->shouldReceive('isFlash')->andReturn(true);
284 | $message->shouldReceive('getPosition')->andReturn(null);
285 | $message->shouldReceive('getFormat')->andReturn(':message');
286 | $this->assertCount(0, $notificationBag);
287 |
288 | $notificationBag->add('info', $message);
289 | $this->assertCount(0, $notificationBag);
290 | }
291 |
292 | public function testAddInstantMessageWithMessageInstanceUsingNamedMethods()
293 | {
294 | $notificationBag = $this->getNotificationBag();
295 | $notificationBag->addType('info');
296 | $this->assertCount(0, $notificationBag);
297 |
298 | $message = $this->getMessage();
299 | $message->shouldReceive('setType')->with('info')->andReturn($message);
300 | $message->shouldReceive('isFlash')->andReturn(false);
301 | $message->shouldReceive('getPosition')->andReturn(null);
302 | $message->shouldReceive('getFormat')->andReturn(':message');
303 | $notificationBag->infoInstant($message);
304 | $this->assertCount(1, $notificationBag);
305 | }
306 |
307 | public function testAddInstantMessageWithMessageInstanceUsingNamedMethodsOverrideFlashStatus()
308 | {
309 | $notificationBag = $this->getNotificationBag();
310 | $notificationBag->addType('info');
311 | $this->assertCount(0, $notificationBag);
312 |
313 | $message = $this->getMessage();
314 | $message->shouldReceive('setType')->with('info')->andReturn($message);
315 | $message->shouldReceive('isFlash')->andReturn(true, false);
316 | $message->shouldReceive('setFlash')->with(false)->andReturn($message);
317 | $message->shouldReceive('getPosition')->andReturn(null);
318 | $message->shouldReceive('getFormat')->andReturn(':message');
319 | $notificationBag->infoInstant($message);
320 | $this->assertCount(1, $notificationBag);
321 | }
322 |
323 | public function testAddInstantMessageWithMessageInstanceSetFormatIfNotSet()
324 | {
325 | $notificationBag = $this->getNotificationBag();
326 | $notificationBag->addType('info');
327 | $notificationBag->setDefaultFormat(':message');
328 | $this->assertCount(0, $notificationBag);
329 |
330 | $message = $this->getMessage();
331 | $message->shouldReceive('setType')->with('info')->andReturn($message);
332 | $message->shouldReceive('isFlash')->andReturn(false);
333 | $message->shouldReceive('getFormat')->andReturn(null);
334 | $message->shouldReceive('setFormat')->once()->with(':message')->andReturn($message);
335 | $message->shouldReceive('getPosition')->andReturn(null);
336 | $notificationBag->add('info', $message, false);
337 | $this->assertCount(1, $notificationBag);
338 | }
339 |
340 | public function testAddInstantMessageWithMessageInstanceAndOverrideFormat()
341 | {
342 | $notificationBag = $this->getNotificationBag();
343 | $notificationBag->addType('info');
344 | $this->assertCount(0, $notificationBag);
345 |
346 | $message = $this->getMessage();
347 | $message->shouldReceive('setType')->with('info')->andReturn($message);
348 | $message->shouldReceive('isFlash')->andReturn(false);
349 | $message->shouldReceive('getFormat')->andReturn('m');
350 | $message->shouldReceive('setFormat')->with(':message')->andReturn($message);
351 | $message->shouldReceive('getPosition')->andReturn(null);
352 | $notificationBag->add('info', $message, false, ':message');
353 | $this->assertCount(1, $notificationBag);
354 | }
355 |
356 | public function testGetInstantMessagesForGivenType()
357 | {
358 | $notificationBag = $this->getNotificationBag();
359 | $notificationBag->addType(array('success', 'info'));
360 | $this->assertCount(0, $notificationBag);
361 |
362 | $notificationBag->successInstant('test');
363 | $notificationBag->successInstant('test2');
364 | $notificationBag->infoInstant('test');
365 | $this->assertCount(3, $notificationBag);
366 | $this->assertCount(2, $notificationBag->get('success'));
367 | $this->assertCount(1, $notificationBag->get('info'));
368 | }
369 |
370 | public function testGetInstantMessagesForGivenTypeWhenMessageHasPosition()
371 | {
372 | $notificationBag = $this->getNotificationBag();
373 | $notificationBag->addType(array('info', 'danger'));
374 | $this->assertCount(0, $notificationBag);
375 |
376 | $message = $this->getMessage();
377 | $message->shouldReceive('setType')->with('info')->andReturn($message);
378 | $message->shouldReceive('getType')->andReturn('info');
379 | $message->shouldReceive('isFlash')->andReturn(false);
380 | $message->shouldReceive('getPosition')->andReturn(5);
381 | $message->shouldReceive('getFormat')->andReturn(':message');
382 |
383 | $notificationBag->add('info', $message, false);
384 | $notificationBag->add('danger', 'test', false);
385 | $this->assertCount(2, $notificationBag);
386 | $this->assertCount(1, $notificationBag->get('info'));
387 | $this->assertEquals($message, $notificationBag->get('info')[0]);
388 | }
389 |
390 | public function testClearMessagesForGivenType()
391 | {
392 | $notificationBag = $this->getNotificationBag();
393 | $notificationBag->addType(array('success', 'info'));
394 | $this->assertCount(0, $notificationBag->get('success'));
395 | $this->assertCount(0, $notificationBag->get('info'));
396 |
397 | $notificationBag->add('success', 'test', false);
398 | $notificationBag->add('info', 'test', false);
399 | $this->assertCount(1, $notificationBag->get('success'));
400 | $this->assertCount(1, $notificationBag->get('info'));
401 |
402 | $notificationBag->clear('success');
403 | $this->assertCount(0, $notificationBag->get('success'));
404 | $this->assertCount(1, $notificationBag->get('info'));
405 |
406 | $notificationBag->clear('info');
407 | $this->assertCount(0, $notificationBag->get('success'));
408 | $this->assertCount(0, $notificationBag->get('info'));
409 | }
410 |
411 | public function testClearMessagesForGivenTypeUsingNamedMethod()
412 | {
413 | $notificationBag = $this->getNotificationBag();
414 | $notificationBag->addType(array('success', 'info'));
415 | $this->assertCount(0, $notificationBag->get('success'));
416 | $this->assertCount(0, $notificationBag->get('info'));
417 |
418 | $notificationBag->add('success', 'test', false);
419 | $notificationBag->add('info', 'test', false);
420 | $this->assertCount(1, $notificationBag->get('success'));
421 | $this->assertCount(1, $notificationBag->get('info'));
422 |
423 | $notificationBag->clearSuccess();
424 | $this->assertCount(0, $notificationBag->get('success'));
425 | $this->assertCount(1, $notificationBag->get('info'));
426 |
427 | $notificationBag->clearInfo();
428 | $this->assertCount(0, $notificationBag->get('success'));
429 | $this->assertCount(0, $notificationBag->get('info'));
430 | }
431 |
432 | public function testClearAllMessages()
433 | {
434 | $notificationBag = $this->getNotificationBag();
435 | $notificationBag->addType(array('success', 'info'));
436 | $this->assertCount(0, $notificationBag->get('success'));
437 | $this->assertCount(0, $notificationBag->get('info'));
438 |
439 | $notificationBag->add('success', 'test', false);
440 | $notificationBag->add('info', 'test', false);
441 | $this->assertCount(1, $notificationBag->get('success'));
442 | $this->assertCount(1, $notificationBag->get('info'));
443 |
444 | $notificationBag->clearAll();
445 | $this->assertCount(0, $notificationBag->get('success'));
446 | $this->assertCount(0, $notificationBag->get('info'));
447 | }
448 |
449 | public function testClearAllMessageWithoutGivenType()
450 | {
451 | $notificationBag = $this->getNotificationBag();
452 | $notificationBag->addType(array('success', 'info'));
453 | $this->assertCount(0, $notificationBag->get('success'));
454 | $this->assertCount(0, $notificationBag->get('info'));
455 |
456 | $notificationBag->add('success', 'test', false);
457 | $notificationBag->add('info', 'test', false);
458 | $this->assertCount(1, $notificationBag->get('success'));
459 | $this->assertCount(1, $notificationBag->get('info'));
460 |
461 | $notificationBag->clear();
462 | $this->assertCount(0, $notificationBag->get('success'));
463 | $this->assertCount(0, $notificationBag->get('info'));
464 | }
465 |
466 | public function testGetAllMessages()
467 | {
468 | $notificationBag = $this->getNotificationBag();
469 | $notificationBag->addType(array('success', 'info'));
470 | $this->assertCount(0, $notificationBag);
471 |
472 | $notificationBag->add('success', 'test', false);
473 | $notificationBag->add('info', 'test', false);
474 | $this->assertCount(2, $notificationBag->all());
475 | }
476 |
477 | public function testGetFirstMessage()
478 | {
479 | $notificationBag = $this->getNotificationBag();
480 | $notificationBag->addType(array('success', 'info'));
481 | $this->assertCount(0, $notificationBag);
482 | $this->assertNull($notificationBag->first());
483 |
484 | $notificationBag->add('success', 'test', false);
485 | $notificationBag->add('info', 'test', false);
486 | $this->assertCount(2, $notificationBag);
487 | $this->assertEquals('success', $notificationBag->first()->getType());
488 | $this->assertEquals('test', $notificationBag->first()->getMessage());
489 | }
490 |
491 | public function testShowMessagesForGivenType()
492 | {
493 | $notificationBag = $this->getNotificationBag();
494 | $notificationBag->addType(array('success', 'info'));
495 | $notificationBag->setDefaultFormat(':type - :message');
496 | $this->assertCount(0, $notificationBag);
497 |
498 | $notificationBag->add('success', 'test', false);
499 | $notificationBag->add('info', 'test', false);
500 | $this->assertCount(2, $notificationBag);
501 | $this->assertEquals('success - test', $notificationBag->show('success'));
502 | $this->assertEquals('info - test', $notificationBag->show('info'));
503 | }
504 |
505 | public function testShowMessagesForGivenTypeWithCustomFormat()
506 | {
507 | $notificationBag = $this->getNotificationBag();
508 | $notificationBag->addType('success');
509 | $notificationBag->setDefaultFormat(':type - :message');
510 | $this->assertCount(0, $notificationBag);
511 |
512 | $notificationBag->add('success', 'test', false);
513 | $this->assertCount(1, $notificationBag);
514 | $this->assertEquals('test - OK', $notificationBag->show('success', ':message - OK'));
515 | }
516 |
517 | public function testShowMessagesForGivenTypeUsingNamedMethods()
518 | {
519 | $notificationBag = $this->getNotificationBag();
520 | $notificationBag->addType(array('success', 'info'));
521 | $notificationBag->setDefaultFormat(':type - :message');
522 | $this->assertCount(0, $notificationBag);
523 |
524 | $notificationBag->add('success', 'test', false);
525 | $notificationBag->add('info', 'test', false);
526 | $this->assertCount(2, $notificationBag);
527 | $this->assertEquals('success - test', $notificationBag->showSuccess());
528 | $this->assertEquals('info - test', $notificationBag->showInfo());
529 | }
530 |
531 | public function testShowAllMessages()
532 | {
533 | $notificationBag = $this->getNotificationBag();
534 | $notificationBag->addType(array('success', 'info'));
535 | $notificationBag->setDefaultFormat(':type - :message');
536 | $this->assertCount(0, $notificationBag);
537 |
538 | $notificationBag->add('success', 'test', false);
539 | $notificationBag->add('info', 'test', false);
540 | $this->assertCount(2, $notificationBag);
541 | $this->assertEquals('success - testinfo - test', $notificationBag->show());
542 | $this->assertEquals('success - testinfo - test', $notificationBag->showAll());
543 | }
544 |
545 | public function testAddTypesForGroupedRendering()
546 | {
547 | $notificationBag = $this->getNotificationBag();
548 | $notificationBag->addType(array('success', 'info'));
549 | $this->assertEquals(array(), $notificationBag->getGroupingForRender());
550 |
551 | $notificationBag->addToGrouping('success');
552 | $this->assertEquals(array('success'), $notificationBag->getGroupingForRender());
553 |
554 | $notificationBag->addToGrouping('info');
555 | $this->assertEquals(array('success', 'info'), $notificationBag->getGroupingForRender());
556 | }
557 |
558 | public function testAddAndRemoveTypesForGroupedRendering()
559 | {
560 | $notificationBag = $this->getNotificationBag();
561 | $notificationBag->addType(array('success', 'info'));
562 | $this->assertEquals(array(), $notificationBag->getGroupingForRender());
563 |
564 | $notificationBag->addToGrouping('success');
565 | $this->assertEquals(array('success'), $notificationBag->getGroupingForRender());
566 |
567 | $notificationBag->addToGrouping('info');
568 | $this->assertEquals(array('success', 'info'), $notificationBag->getGroupingForRender());
569 |
570 | $notificationBag->removeFromGrouping('success');
571 | $this->assertEquals(array('info'), $notificationBag->getGroupingForRender());
572 | }
573 |
574 | public function testAddTypesForGroupedRenderingInvalidType()
575 | {
576 | $notificationBag = $this->getNotificationBag();
577 | $this->assertEquals(array(), $notificationBag->getGroupingForRender());
578 |
579 | $notificationBag->addToGrouping('success');
580 | $this->assertEquals(array(), $notificationBag->getGroupingForRender());
581 | }
582 |
583 | public function testShowGroupedMessages()
584 | {
585 | $notificationBag = $this->getNotificationBag();
586 | $notificationBag->addType(array('success', 'info'));
587 | $notificationBag->setDefaultFormat(':type - :message');
588 | $notificationBag->add('success', 'test', false);
589 | $notificationBag->add('info', 'test2', false);
590 | $this->assertEquals('success - test', $notificationBag->group('success')->show());
591 | $this->assertEquals('info - test2success - test', $notificationBag->group('info', 'success')->show());
592 | }
593 |
594 | public function testSetNotificationInstance()
595 | {
596 | $notificationBag = $this->getNotificationBag();
597 | $this->assertNull($notificationBag->getEventDispatcher());
598 | $notificationBag->setNotification($notification = m::mock('Krucas\Notification\Notification'));
599 | $this->assertEquals($notification, $notificationBag->getNotification());
600 | $notificationBag->unsetNotification();
601 | $this->assertNull($notificationBag->getNotification());
602 | }
603 |
604 | public function testToString()
605 | {
606 | $notificationBag = $this->getNotificationBag();
607 | $notificationBag->addType('info');
608 | $notificationBag->setDefaultFormat(':type - :message');
609 | $notificationBag->add('info', 'ok', false);
610 | $this->assertEquals('info - ok', (string) $notificationBag);
611 | }
612 |
613 | public function testToArray()
614 | {
615 | $notificationBag = $this->getNotificationBag();
616 | $notificationBag->addType('info');
617 | $notificationBag->setDefaultFormat(':message');
618 | $notificationBag->add('info', 'test', false);
619 |
620 | $this->assertEquals(
621 | array(
622 | 'container' => 'test',
623 | 'format' => ':message',
624 | 'types' => array('info'),
625 | 'notifications' => array(
626 | array(
627 | 'message' => 'test',
628 | 'format' => ':message',
629 | 'type' => 'info',
630 | 'flash' => false,
631 | 'position' => null
632 | )
633 | )
634 | ),
635 | $notificationBag->toArray()
636 | );
637 | }
638 |
639 | public function testToJson()
640 | {
641 | $notificationBag = $this->getNotificationBag();
642 | $notificationBag->addType('info');
643 | $notificationBag->setDefaultFormat(':message');
644 | $notificationBag->add('info', 'test', false);
645 |
646 | $this->assertEquals(
647 | json_encode(
648 | array(
649 | 'container' => 'test',
650 | 'format' => ':message',
651 | 'types' => array('info'),
652 | 'notifications' => array(
653 | array(
654 | 'message' => 'test',
655 | 'format' => ':message',
656 | 'type' => 'info',
657 | 'flash' => false,
658 | 'position' => null
659 | )
660 | )
661 | )
662 | ),
663 | $notificationBag->toJson()
664 | );
665 | }
666 |
667 |
668 | protected function getNotificationBag()
669 | {
670 | return new NotificationsBagMock('test');
671 | }
672 |
673 | protected function getMessage()
674 | {
675 | $message = m::mock('Krucas\Notification\Message');
676 | return $message;
677 | }
678 | }
679 |
--------------------------------------------------------------------------------
/tests/NotificationTest.php:
--------------------------------------------------------------------------------
1 | getNotification();
15 | $this->assertEquals('default', $notification->getDefaultContainerName());
16 | $this->assertCount(0, $notification->getContainers());
17 | }
18 |
19 | public function testGetDefaultContainerName()
20 | {
21 | $notification = $this->getNotification();
22 | $this->assertEquals('default', $notification->getDefaultContainerName());
23 | }
24 |
25 | public function testSetContainerTypes()
26 | {
27 | $notification = $this->getNotification();
28 | $this->assertEquals(array(), $notification->getContainerTypes('default'));
29 |
30 | $notification->setContainerTypes('default', array('success', 'info'));
31 | $this->assertEquals(array('success', 'info'), $notification->getContainerTypes('default'));
32 | }
33 |
34 | public function testGetTypesContainerForContainer()
35 | {
36 | $notification = $this->getNotification();
37 | $this->assertEquals(array(), $notification->getContainerTypes('default'));
38 | $this->assertEquals(array(), $notification->getContainerTypes('test'));
39 |
40 | $notification->setContainerTypes('default', array('success', 'info'));
41 | $this->assertEquals(array('success', 'info'), $notification->getContainerTypes('default'));
42 | $this->assertEquals(array(), $notification->getContainerTypes('test'));
43 | }
44 |
45 | public function testSetContainerFormat()
46 | {
47 | $notification = $this->getNotification();
48 | $this->assertNull($notification->getContainerFormat('default'));
49 |
50 | $notification->setContainerFormat('default', ':message');
51 | $this->assertEquals(':message', $notification->getContainerFormat('default'));
52 | }
53 |
54 | public function testGetContainerFormatForContainer()
55 | {
56 | $notification = $this->getNotification();
57 | $this->assertNull($notification->getContainerFormat('default'));
58 | $this->assertNull($notification->getContainerFormat('test'));
59 |
60 | $notification->setContainerFormat('default', ':message');
61 | $this->assertEquals(':message', $notification->getContainerFormat('default'));
62 | $this->assertNull($notification->getContainerFormat('test'));
63 | }
64 |
65 | public function testSetContainerContainerFormats()
66 | {
67 | $notification = $this->getNotification();
68 | $this->assertEquals(array(), $notification->getContainerFormats('default'));
69 |
70 | $notification->setContainerFormats('default', array('info' => ':message'));
71 | $this->assertEquals(array('info' => ':message'), $notification->getContainerFormats('default'));
72 | }
73 |
74 | public function testGetContainerFormatsForContainer()
75 | {
76 | $notification = $this->getNotification();
77 | $this->assertEquals(array(), $notification->getContainerFormats('default'));
78 | $this->assertEquals(array(), $notification->getContainerFormats('test'));
79 |
80 | $notification->setContainerFormats('default', array('info' => ':message'));
81 | $this->assertEquals(array('info' => ':message'), $notification->getContainerFormats('default'));
82 | $this->assertEquals(array(), $notification->getContainerFormats('test'));
83 | }
84 |
85 | public function testAddContainer()
86 | {
87 | $notification = $this->getNotification();
88 | $this->assertCount(0, $notification->getContainers());
89 |
90 | $notification->addContainer('test');
91 | $this->assertCount(1, $notification->getContainers());
92 | }
93 |
94 | public function testAddExistingContainer()
95 | {
96 | $notification = $this->getNotification();
97 | $this->assertCount(0, $notification->getContainers());
98 |
99 | $notification->addContainer('test');
100 | $this->assertCount(1, $notification->getContainers());
101 |
102 | $notification->addContainer('test');
103 | $this->assertCount(1, $notification->getContainers());
104 | }
105 |
106 | public function testSetNotificationInstanceOnNewContainer()
107 | {
108 | $notification = $this->getNotification();
109 | $this->assertCount(0, $notification->getContainers());
110 |
111 | $notification->addContainer('test');
112 | $this->assertCount(1, $notification->getContainers());
113 | $this->assertEquals($notification, $notification->container('test')->getNotification());
114 | }
115 |
116 | public function testNotificationBagInstanceOnDefaultContainer()
117 | {
118 | $notification = $this->getNotification();
119 | $this->assertInstanceOf('Krucas\Notification\NotificationsBag', $notification->container());
120 | }
121 |
122 | public function testNotificationBagInstanceOnDefaultContainerUsingMagicMethod()
123 | {
124 | $notification = $this->getNotification();
125 | $this->assertEquals('default', $notification->getName());
126 | }
127 |
128 | public function testNotificationBagInstanceOnAddedContainer()
129 | {
130 | $notification = $this->getNotification();
131 | $notification->addContainer('test');
132 | $this->assertInstanceOf('Krucas\Notification\NotificationsBag', $notification->container('test'));
133 | }
134 |
135 | public function testNotificationBagInstanceOnNonExistingContainer()
136 | {
137 | $notification = $this->getNotification();
138 | $this->assertInstanceOf('Krucas\Notification\NotificationsBag', $notification->container('test'));
139 | }
140 |
141 | public function testNotificationBagInstanceOnNonExistingContainerWithResolvedParams()
142 | {
143 | $notification = $this->getNotification();
144 | $notification->setContainerTypes('test', array('success'));
145 | $notification->setContainerFormat('test', ':message');
146 | $notification->setContainerFormats('test', array('success' => ':message - OK'));
147 | $container = $notification->container('test');
148 | $this->assertInstanceOf('Krucas\Notification\NotificationsBag', $container);
149 | $this->assertEquals(array('success'), $container->getTypes());
150 | $this->assertEquals(':message', $container->getDefaultFormat());
151 | $this->assertEquals(':message - OK', $container->getFormat('success'));
152 | }
153 |
154 | public function testCallbackOnNotificationBag()
155 | {
156 | $notification = $this->getNotification();
157 | $this->assertEquals(array(), $notification->container('default')->getTypes());
158 | $notification->container('default', function ($container) {
159 | $container->addType('info');
160 | });
161 | $this->assertEquals(array('info'), $notification->container('default')->getTypes());
162 | }
163 |
164 | public function testCreateNewEmptyMessageInstance()
165 | {
166 | $notification = $this->getNotification();
167 | $message = $notification->message();
168 | $this->assertInstanceOf('Krucas\Notification\Message', $message);
169 | $this->assertNull($message->getMessage());
170 | }
171 |
172 | public function testCreateNewMessageInstanceWithMessage()
173 | {
174 | $notification = $this->getNotification();
175 | $message = $notification->message('test');
176 | $this->assertInstanceOf('Krucas\Notification\Message', $message);
177 | $this->assertEquals('test', $message->getMessage());
178 | }
179 |
180 | public function testSetEventDispatcher()
181 | {
182 | $notification = $this->getNotification();
183 | $this->assertNull($notification->getEventDispatcher());
184 | $notification->setEventDispatcher($events = m::mock('Illuminate\Contracts\Events\Dispatcher'));
185 | $this->assertEquals($events, $notification->getEventDispatcher());
186 | $notification->unsetEventDispatcher();
187 | $this->assertNull($notification->getEventDispatcher());
188 | }
189 |
190 | public function testAddFlashMessageProcess()
191 | {
192 | $notification = $this->getNotification();
193 | $notification->setContainerTypes('default', array('info'));
194 | $notification->setEventDispatcher($events = m::mock('Illuminate\Contracts\Events\Dispatcher'));
195 |
196 | $message = $this->getMessage();
197 | $message->shouldReceive('setType')->with('info')->andReturn($message);
198 | $message->shouldReceive('isFlash')->andReturn(true);
199 | $message->shouldReceive('getPosition')->andReturn(null);
200 | $message->shouldReceive('getFormat')->andReturn(':message');
201 |
202 | $events->shouldReceive('dispatch')->once()->with('notification.flash: default', array($notification, $notification->container(), $message));
203 | $notification->container()->info($message);
204 | }
205 |
206 | public function testAddInstantMessageProcess()
207 | {
208 | $notification = $this->getNotification();
209 | $notification->setContainerTypes('default', array('info'));
210 | $notification->setEventDispatcher($events = m::mock('Illuminate\Contracts\Events\Dispatcher'));
211 |
212 | $message = $this->getMessage();
213 | $message->shouldReceive('setType')->with('info')->andReturn($message);
214 | $message->shouldReceive('isFlash')->andReturn(false);
215 | $message->shouldReceive('getPosition')->andReturn(null);
216 | $message->shouldReceive('getFormat')->andReturn(':message');
217 |
218 | $events->shouldReceive('dispatch')->once()->with('notification.added: default', array($notification, $notification->container(), $message));
219 | $notification->container()->infoInstant($message);
220 | }
221 |
222 | public function testCreateNewContainerWithDefaults()
223 | {
224 | $notification = new \Krucas\Notification\Notification(
225 | 'default',
226 | ['info', 'warning', 'success', 'error'],
227 | [],
228 | ':type :message',
229 | [],
230 | [],
231 | []
232 | );
233 |
234 | $container = $notification->container('test');
235 |
236 | $this->assertEquals('test', $container->getName());
237 | $this->assertEquals(':type :message', $container->getDefaultFormat());
238 | $this->assertEquals(['info', 'warning', 'success', 'error'], $container->getTypes());
239 | }
240 |
241 | public function testCreateNewContainerFromDefined()
242 | {
243 | $notification = new \Krucas\Notification\Notification(
244 | 'default',
245 | ['info', 'warning', 'success', 'error'],
246 | [
247 | 'test' => ['info']
248 | ],
249 | ':type :message',
250 | [
251 | 'test' => ':message',
252 | ],
253 | [],
254 | [
255 | 'test' => [
256 | 'info' => 'info :message',
257 | ]
258 | ]
259 | );
260 |
261 | $container = $notification->container('test');
262 |
263 | $this->assertEquals('test', $container->getName());
264 | $this->assertEquals(':message', $container->getDefaultFormat());
265 | $this->assertEquals(['info'], $container->getTypes());
266 | $this->assertEquals('info :message', $container->getFormat('info'));
267 | }
268 |
269 | protected function getNotification()
270 | {
271 | return new \Krucas\Notification\Notification('default', [], [], null, [], [], []);
272 | }
273 |
274 | protected function getMessage()
275 | {
276 | $message = m::mock('Krucas\Notification\Message');
277 | return $message;
278 | }
279 | }
280 |
--------------------------------------------------------------------------------
/tests/SubscriberTest.php:
--------------------------------------------------------------------------------
1 | getSubscriber();
15 | $this->assertInstanceOf('Illuminate\Session\Store', $subscriber->getSession());
16 | $this->assertEquals('notifications', $subscriber->getKey());
17 | }
18 |
19 | public function testSubscribe()
20 | {
21 | $subscriber = $this->getSubscriber();
22 | $events = m::mock('Illuminate\Contracts\Events\Dispatcher');
23 | $events->shouldReceive('listen')->once()->with('notification.flash: *', 'Krucas\Notification\Subscriber@onFlash');
24 | $this->assertNull($subscriber->subscribe($events));
25 | }
26 |
27 | public function testOnFlash()
28 | {
29 | $subscriber = $this->getSubscriber();
30 |
31 | $notification = $this->getNotification();
32 | $notificationsBag = $this->getNotificationsBag();
33 | $message = $this->getMessage();
34 |
35 | $notificationsBag->shouldReceive('getName')->once()->andReturn('my_test_bag');
36 | $subscriber->getSession()->shouldReceive('push')->once()->with('notifications.my_test_bag', $message);
37 |
38 | // As of Laravel 5.4 wildcard event subscribers now receive the event
39 | // name as their first argument and the array of event data as their
40 | // second argument
41 | $this->assertTrue($subscriber->onFlash('notification.flash: default', [$notification, $notificationsBag, $message]));
42 | }
43 |
44 | public function testOnFlashValidation()
45 | {
46 | $subscriber = $this->getSubscriber();
47 |
48 | try {
49 | $subscriber->onFlash('notification.flash: default', [new \stdClass]);
50 | $this->fail('Failed to throw expected InvalidArgumentException when passing incorrect number of event data array elements to Krucas\Notification\Subscriber::onFlash');
51 | } catch (\InvalidArgumentException $e) {
52 | $this->assertEquals('Krucas\Notification\Subscriber::onFlash expects 3 elements in data array, 1 given.', $e->getMessage());
53 | }
54 |
55 | try {
56 | $subscriber->onFlash('notification.flash: default', [new \stdClass, new \stdClass, new \stdClass]);
57 | $this->fail('Failed to throw expected InvalidArgumentException when passing incorrect type of event data array elements to Krucas\Notification\Subscriber::onFlash');
58 | } catch (\InvalidArgumentException $e) {
59 | $this->assertEquals('Krucas\Notification\Subscriber::onFlash expects a data array containing [Krucas\Notification\Notification, Krucas\Notification\NotificationsBag, Krucas\Notification\Message], actually given [stdClass, stdClass, stdClass]', $e->getMessage());
60 | }
61 | }
62 |
63 | protected function getSubscriber()
64 | {
65 | return new \Krucas\Notification\Subscriber($this->getSessionStore(), 'notifications');
66 | }
67 |
68 | protected function getSessionStore()
69 | {
70 | return m::mock('Illuminate\Session\Store');
71 | }
72 |
73 | protected function getNotification()
74 | {
75 | return m::mock('Krucas\Notification\Notification');
76 | }
77 |
78 | protected function getNotificationsBag()
79 | {
80 | return m::mock('Krucas\Notification\NotificationsBag');
81 | }
82 |
83 | protected function getMessage()
84 | {
85 | return m::mock('Krucas\Notification\Message');
86 | }
87 | }
88 |
--------------------------------------------------------------------------------