From a22f9198e55b02679dd6548f3a32404368763fe7 Mon Sep 17 00:00:00 2001 From: Roman Ihoshyn Date: Mon, 17 May 2021 16:12:15 +0300 Subject: [PATCH] fix implementation, PHP 7.4 minimum, tests --- .gitignore | 2 + composer.json | 9 +++ phpunit.xml | 20 +++++++ src/Flash.php | 138 ++++++++++++++++---------------------------- src/FlashFacade.php | 4 +- src/helpers.php | 14 ++--- tests/FlashTest.php | 110 +++++++++++++++++++++++++++++++++++ 7 files changed, 201 insertions(+), 96 deletions(-) create mode 100644 phpunit.xml create mode 100644 tests/FlashTest.php diff --git a/.gitignore b/.gitignore index 57872d0..0794651 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ /vendor/ +composer.lock +.phpunit.result.cache diff --git a/composer.json b/composer.json index 409e44e..2437278 100644 --- a/composer.json +++ b/composer.json @@ -10,9 +10,13 @@ } ], "require": { + "php": "^7.4|^8.0", "illuminate/support": "^5.1|^6.0|^7.0|^8.0", "illuminate/session": "^5.1|^6.0|^7.0|^8.0" }, + "require-dev": { + "orchestra/testbench": "^5.18|^6.17" + }, "autoload": { "psr-4": { "MasterRO\\Flash\\": "src/" @@ -21,6 +25,11 @@ "src/helpers.php" ] }, + "autoload-dev": { + "psr-4": { + "MasterRO\\Flash\\Tests\\": "tests/" + } + }, "extra": { "laravel": { "providers": [ diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..76d16af --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,20 @@ + + + + + ./tests/ + + + diff --git a/src/Flash.php b/src/Flash.php index c3072bf..f429b0e 100644 --- a/src/Flash.php +++ b/src/Flash.php @@ -6,101 +6,65 @@ class Flash { - /** - * @var string - */ - public $message; + public string $message; - /** - * @var string - */ - public $type; + public string $type; - /** - * @var bool - */ - public $session; + public bool $session; - /** - * Flash constructor. - * - * @param $message - * @param string $type - * @param bool $session - */ - public function __construct($message = '', $type = 'success', $session = true) - { - $this->message = $message; - $this->type = $type; - $this->session = $session; - } + /** + * Flash constructor. + * + * @param string $message + * @param string $type + * @param bool $session + */ + public function __construct(string $message = '', string $type = 'success', bool $session = true) + { + $this->message = $message; + $this->type = $type; + $this->session = $session; + } - /** - * Success - * - * @param string $message - * @param bool $session - * - * @return mixed - */ - public static function success($message, $session = true) - { - return ((new static($message, 'success', $session))->push()); - } + public static function make(string $message = '', string $type = 'success', bool $session = true): Flash + { + return new static($message, $type, $session); + } - /** - * Warning - * - * @param string $message - * @param bool $session - * - * @return mixed - */ - public static function warning($message, $session = true) - { - return ((new static($message, 'warning', $session))->push()); - } + public function success(string $message, bool $session = true) + { + return static::make($message, 'success', $session)->push(); + } - /** - * Info - * - * @param string $message - * @param bool $session - * - * @return mixed - */ - public static function info($message, $session = true) - { - return ((new static($message, 'info', $session))->push()); - } + public function warning(string $message, bool $session = true) + { + return static::make($message, 'warning', $session)->push(); + } - /** - * Error - * - * @param string $message - * @param bool $session - * - * @return mixed - */ - public static function error($message, $session = true) - { - return ((new static($message, 'error', $session))->push()); - } + public function info(string $message, bool $session = true) + { + return static::make($message, 'info', $session)->push(); + } - /** - * Push this Flash Message to Session - * - * @return mixed - */ - public function push() - { - $type = $this->type; - $message = $this->message; + public function error(string $message, bool $session = true) + { + return static::make($message, 'error', $session)->push(); + } - if ($this->session) { - return Session::push('flash_messages', compact('type', 'message')); - } + /** + * Push this Flash Message to Session + * + * @return mixed + */ + public function push() + { + $type = $this->type; + $message = $this->message; - return Session::flash('flash_messages', [compact('type', 'message')]); - } + if ($this->session) { + return Session::push('flash_messages', compact('type', 'message')); + } + + return Session::flash('flash_messages', [compact('type', 'message')]); + } } diff --git a/src/FlashFacade.php b/src/FlashFacade.php index a23838f..8347f2c 100644 --- a/src/FlashFacade.php +++ b/src/FlashFacade.php @@ -2,7 +2,9 @@ namespace MasterRO\Flash; -class FlashFacade +use Illuminate\Support\Facades\Facade; + +class FlashFacade extends Facade { /** * @return string diff --git a/src/helpers.php b/src/helpers.php index 5bd1e97..f392162 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -1,14 +1,12 @@ push(); + if (!$message) { + return \MasterRO\Flash\Flash::make(); + } + + return \MasterRO\Flash\Flash::make($message, $type, $session)->push(); } } diff --git a/tests/FlashTest.php b/tests/FlashTest.php new file mode 100644 index 0000000..ca19538 --- /dev/null +++ b/tests/FlashTest.php @@ -0,0 +1,110 @@ + FlashFacade::class, + ]; + } + + /** @test */ + public function it_puts_message_to_session() + { + flash('Test'); + + [$sessionMessage] = session('flash_messages', [null]); + + $this->assertEquals('Test', $sessionMessage['message']); + $this->assertEquals('success', $sessionMessage['type']); + } + + /** @test */ + public function it_puts_success_message_to_session() + { + flash('Success message', 'success'); + + [$sessionMessage] = session('flash_messages', [null]); + + $this->assertEquals('Success message', $sessionMessage['message']); + $this->assertEquals('success', $sessionMessage['type']); + + session()->flush(); + + flash()->success('Success message 2'); + + [$sessionMessage] = session('flash_messages', [null]); + + $this->assertEquals('Success message 2', $sessionMessage['message']); + $this->assertEquals('success', $sessionMessage['type']); + + session()->flush(); + + Flash::success('Success message 3'); + + [$sessionMessage] = session('flash_messages', [null]); + + $this->assertEquals('Success message 3', $sessionMessage['message']); + $this->assertEquals('success', $sessionMessage['type']); + } + + /** @test */ + public function it_puts_error_message_to_session() + { + flash('Error message', 'error'); + + [$sessionMessage] = session('flash_messages', [null]); + + $this->assertEquals('Error message', $sessionMessage['message']); + $this->assertEquals('error', $sessionMessage['type']); + + session()->flush(); + + flash()->error('Error message 2'); + + [$sessionMessage] = session('flash_messages', [null]); + + $this->assertEquals('Error message 2', $sessionMessage['message']); + $this->assertEquals('error', $sessionMessage['type']); + + session()->flush(); + + Flash::error('Error message 3'); + + [$sessionMessage] = session('flash_messages', [null]); + + $this->assertEquals('Error message 3', $sessionMessage['message']); + $this->assertEquals('error', $sessionMessage['type']); + } + + public function it_puts_multiple_flash_messages() + { + flash('Error message', 'error'); + flash('Success message'); + flash()->info('Info message'); + Flash::warning('Warning message'); + + $tests = [ + ['Error message', 'error'], + ['Success message', 'success'], + ['Info message', 'info'], + ['Warning message', 'warning'], + ]; + + foreach ($tests as $key => $test) { + $sessionMessage = session('flash_messages', [])[$key] ?? null; + + $this->assertEquals($test[0], $sessionMessage['message']); + $this->assertEquals($test[1], $sessionMessage['type']); + } + } +}