Skip to content

Commit

Permalink
Implement testing helper trait
Browse files Browse the repository at this point in the history
  • Loading branch information
Tjoosten committed Jun 26, 2018
1 parent 8056ccb commit 5fa2d2a
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/Traits/WithToastr.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace ActivismeBe\Toastr\Traits;

/**
* Trait WithToastr
* -----
* Trait for checking the toastr session's in phpunit functions.
*
* @author Tim Joosten <[email protected]>
* @copyright 2018 Tim Joosten <MIT license>
* @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);
}
}

0 comments on commit 5fa2d2a

Please sign in to comment.