From 5fa2d2a7f0a14f4a9351bfa1e93f22029a61bdb7 Mon Sep 17 00:00:00 2001 From: Tjoosten Date: Tue, 26 Jun 2018 13:48:07 +0200 Subject: [PATCH] Implement testing helper trait --- src/Traits/WithToastr.php | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/Traits/WithToastr.php diff --git a/src/Traits/WithToastr.php b/src/Traits/WithToastr.php new file mode 100644 index 0000000..49c40e7 --- /dev/null +++ b/src/Traits/WithToastr.php @@ -0,0 +1,37 @@ + + * @copyright 2018 Tim Joosten + * @package ActivismeBe\Toastr\Testing + */ +trait WithToastr +{ + /** + * Custom assertions for toastr flash messages. + * + * @param string $type The type of the toastr notification. + * @param string $title The title of the toastr notification. + * @param string $message The actual toastr message. + * @param array $options The user defined configuration for the toastr message. + * @return void + */ + protected function assertHasToastr(string $type, string $title, string $message, array $options = []): void + { + $expectedNotification = ['type' => $type, 'title' => $title, 'message' => $message, 'options' => $options]; + $flashNotifications = json_decode(json_encode(session('toastr::notifications')), true); + $assertMessage = 'Failed asserting that the toastr message '. $message .' is present'; + + if (! $flashNotifications) { + $this->fail('Failed asserting that a flash message was sent.'); + } + + $this->assertContains($expectedNotification, $flashNotifications, $assertMessage); + } +} \ No newline at end of file