Skip to content

Commit

Permalink
Add helper class with test assertions.
Browse files Browse the repository at this point in the history
  • Loading branch information
gbuckingham89 committed Nov 26, 2021
1 parent 7ab5e5a commit d050da7
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 11 deletions.
51 changes: 41 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,50 +8,81 @@ The current version requires Laravel 7.0 (PHP 7.4) or greater. If you have a pro

The recommended way is using [Composer](https://getcomposer.org) - simply run this command in your terminal:

composer require gbuckingham89/laraflash
```php
composer require gbuckingham89/laraflash
```

The service provider and facade will be automatically registered for you unless you have disabled auto discovery. If this is the case, you'll need to manually register the service provider and facade:

For the service provider, add the following to the `providers` array in your `app.php` config file:

Gbuckingham89\Laraflash\ServiceProvider::class
```php
Gbuckingham89\Laraflash\ServiceProvider::class
```

For the facade, add the following to the `aliases` array in your `app.php` config file:

'Laraflash' => Gbuckingham89\Laraflash\Facade\Laraflash::class
```php
'Laraflash' => Gbuckingham89\Laraflash\Facade\Laraflash::class
```

Finally, you'll need to publish the view file (unless you want to roll your own). Do this by running this command in your terminal:

php artisan vendor:publish --provider="Gbuckingham89\Laraflash\ServiceProvider"
```php
php artisan vendor:publish --provider="Gbuckingham89\Laraflash\ServiceProvider"
```

## Use

### Setting a message

Before you redirect to another page, simply use the `Laraflash` facade to set your message. You can call the main `flash` method as follows:

Laraflash::flash("This is a flash message.", "success");
```php
Laraflash::flash("This is a flash message.", "success");
```

The first parameter is your message, the second is the priority level.

There are also four helper methods available for easier setting of priority levels:

Laraflash::success("This is a success flash message.");
```php
Laraflash::success("This is a success flash message.");

Laraflash::info("This is an info flash message.");
Laraflash::info("This is an info flash message.");

Laraflash::warning("This is a warning flash message.");
Laraflash::warning("This is a warning flash message.");

Laraflash::danger("This is a danger flash message.");
Laraflash::danger("This is a danger flash message.");
```

### Displaying the message

To display the message, simply include the bundled view (built for Bootstrap) in your view:

@include('laraflash::laraflash')
```php
@include('laraflash::laraflash')
```

Alternatively, you can access the data in the session directly (stored in `laraflash.message` and `laraflash.level`) and roll your own view.

### Testing Assertions

This package provides `Gbuckingham89\Laraflash\LaraflashAssertions` with some basic/common assertions if you have to test that your responses contain a laraflash message, or not.

```php
use Gbuckingham89\Laraflash\LaraflashAssertions;

LaraflashAssertions::assertResponseDoesntHaveLaraflash($response);

LaraflashAssertions::assertResponseHasLaraflash($response, 'info', 'The message you are expecting');

LaraflashAssertions::assertResponseHasLaraflashSuccess($response, 'The message you are expecting');
LaraflashAssertions::assertResponseHasLaraflashInfo($response, 'The message you are expecting');
LaraflashAssertions::assertResponseHasLaraflashWarning($response, 'The message you are expecting');
LaraflashAssertions::assertResponseHasLaraflashDanger($response, 'The message you are expecting');
```

## Copyright and license

Code and documentation copyright [George Buckingham](https://www.georgebuckingham.com).
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"source": "https://github.com/gbuckingham89/laraflash"
},
"require": {
"php": "^7.4|^8.0",
"php": "7.4.*|8.0.*|8.1.*",
"illuminate/testing": "^7.0|^8.0",
"illuminate/session": "^7.0|^8.0",
"illuminate/support": "^7.0|^8.0"
},
Expand Down
67 changes: 67 additions & 0 deletions src/LaraflashAssertions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Gbuckingham89\Laraflash;

use Illuminate\Testing\TestResponse;

abstract class LaraflashAssertions
{
/**
* @param \Illuminate\Testing\TestResponse $response
*/
public static function assertResponseDoesntHaveLaraflash(TestResponse $response): void
{
$response->assertSessionMissing('laraflash.level');
$response->assertSessionMissing('laraflash.message');
}

/**
* @param \Illuminate\Testing\TestResponse $response
* @param string $level
* @param string|null $message
*/
public static function assertResponseHasLaraflash(
TestResponse $response,
string $level,
?string $message = null
): void {
$response->assertSessionHas('laraflash.level', $level);
$response->assertSessionHas('laraflash.message', $message);
}

/**
* @param \Illuminate\Testing\TestResponse $response
* @param string|null $message
*/
public static function assertResponseHasLaraflashSuccess(TestResponse $response, ?string $message = null): void
{
self::assertResponseHasLaraflash($response, 'success', $message);
}

/**
* @param \Illuminate\Testing\TestResponse $response
* @param string|null $message
*/
public static function assertResponseHasLaraflashInfo(TestResponse $response, ?string $message = null): void
{
self::assertResponseHasLaraflash($response, 'info', $message);
}

/**
* @param \Illuminate\Testing\TestResponse $response
* @param string|null $message
*/
public static function assertResponseHasLaraflashWarning(TestResponse $response, ?string $message = null): void
{
self::assertResponseHasLaraflash($response, 'warning', $message);
}

/**
* @param \Illuminate\Testing\TestResponse $response
* @param string|null $message
*/
public static function assertResponseHasLaraflashDanger(TestResponse $response, ?string $message = null): void
{
self::assertResponseHasLaraflash($response, 'danger', $message);
}
}

0 comments on commit d050da7

Please sign in to comment.