From d050da701d7f794e8078153e5aacf591d0ef432b Mon Sep 17 00:00:00 2001 From: George Buckingham Date: Fri, 26 Nov 2021 17:38:06 +0000 Subject: [PATCH] Add helper class with test assertions. --- README.md | 51 ++++++++++++++++++++++------ composer.json | 3 +- src/LaraflashAssertions.php | 67 +++++++++++++++++++++++++++++++++++++ 3 files changed, 110 insertions(+), 11 deletions(-) create mode 100644 src/LaraflashAssertions.php diff --git a/README.md b/README.md index 6e28aa5..6a5a6cf 100644 --- a/README.md +++ b/README.md @@ -8,21 +8,29 @@ 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 @@ -30,28 +38,51 @@ Finally, you'll need to publish the view file (unless you want to roll your own) 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). diff --git a/composer.json b/composer.json index 10b9f63..42cb31b 100644 --- a/composer.json +++ b/composer.json @@ -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" }, diff --git a/src/LaraflashAssertions.php b/src/LaraflashAssertions.php new file mode 100644 index 0000000..0466131 --- /dev/null +++ b/src/LaraflashAssertions.php @@ -0,0 +1,67 @@ +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); + } +} \ No newline at end of file