Skip to content

Commit 5fa2d2a

Browse files
committed
Implement testing helper trait
1 parent 8056ccb commit 5fa2d2a

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

src/Traits/WithToastr.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace ActivismeBe\Toastr\Traits;
4+
5+
/**
6+
* Trait WithToastr
7+
* -----
8+
* Trait for checking the toastr session's in phpunit functions.
9+
*
10+
* @author Tim Joosten <tim@activisme.be>
11+
* @copyright 2018 Tim Joosten <MIT license>
12+
* @package ActivismeBe\Toastr\Testing
13+
*/
14+
trait WithToastr
15+
{
16+
/**
17+
* Custom assertions for toastr flash messages.
18+
*
19+
* @param string $type The type of the toastr notification.
20+
* @param string $title The title of the toastr notification.
21+
* @param string $message The actual toastr message.
22+
* @param array $options The user defined configuration for the toastr message.
23+
* @return void
24+
*/
25+
protected function assertHasToastr(string $type, string $title, string $message, array $options = []): void
26+
{
27+
$expectedNotification = ['type' => $type, 'title' => $title, 'message' => $message, 'options' => $options];
28+
$flashNotifications = json_decode(json_encode(session('toastr::notifications')), true);
29+
$assertMessage = 'Failed asserting that the toastr message '. $message .' is present';
30+
31+
if (! $flashNotifications) {
32+
$this->fail('Failed asserting that a flash message was sent.');
33+
}
34+
35+
$this->assertContains($expectedNotification, $flashNotifications, $assertMessage);
36+
}
37+
}

0 commit comments

Comments
 (0)