Skip to content

Commit

Permalink
fix implementation, PHP 7.4 minimum, tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MasterRO94 committed May 17, 2021
1 parent 3ad7cf2 commit a22f919
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 96 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
/vendor/
composer.lock
.phpunit.result.cache
9 changes: 9 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/"
Expand All @@ -21,6 +25,11 @@
"src/helpers.php"
]
},
"autoload-dev": {
"psr-4": {
"MasterRO\\Flash\\Tests\\": "tests/"
}
},
"extra": {
"laravel": {
"providers": [
Expand Down
20 changes: 20 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
verbose="true"
>
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix="Test.php">./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
138 changes: 51 additions & 87 deletions src/Flash.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')]);
}
}
4 changes: 3 additions & 1 deletion src/FlashFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace MasterRO\Flash;

class FlashFacade
use Illuminate\Support\Facades\Facade;

class FlashFacade extends Facade
{
/**
* @return string
Expand Down
14 changes: 6 additions & 8 deletions src/helpers.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
<?php

if (! function_exists('flash')) {

/**
* @param string $message
* @param string $type
* @param bool $session
*/
function flash($message = null, $type = 'success', $session = true)
function flash(string $message = null, string $type = 'success', bool $session = true)
{
(new \MasterRO\Flash\Flash($message, $type, $session))->push();
if (!$message) {
return \MasterRO\Flash\Flash::make();
}

return \MasterRO\Flash\Flash::make($message, $type, $session)->push();
}
}
110 changes: 110 additions & 0 deletions tests/FlashTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

declare(strict_types=1);

namespace MasterRO\Flash\Tests;

use Flash;
use MasterRO\Flash\FlashFacade;
use Orchestra\Testbench\TestCase;

class FlashTest extends TestCase
{
protected function getPackageAliases($app)
{
return [
'Flash' => 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']);
}
}
}

0 comments on commit a22f919

Please sign in to comment.