Skip to content
This repository has been archived by the owner on Feb 17, 2022. It is now read-only.

Commit

Permalink
Merge 29762a2 into 443a0f5
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkGhostHunter committed Dec 19, 2021
2 parents 443a0f5 + 29762a2 commit 182ddfc
Show file tree
Hide file tree
Showing 11 changed files with 1,213 additions and 24 deletions.
70 changes: 70 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,76 @@ When you receive a JSON Response, you will see the alerts appended to whichever

> If your key is already present in the JSON response, Laralerts **won't overwrite the key value**. Ensure the key is never present in the response.
## Testing

To test if alerts were generated, you can use `Alert::fake()`, which works like any other faked services. It returns a fake Alert Bag that holds a copy of all alerts generated, which exposes some convenient assertion methods.

```php
use \DarkGhostHunter\Laralerts\Facades\Alert;

public function test_alert_sent()
{
$alerts = Alert::fake();

$this->post('/comment', ['body' => 'cool'])->assertOk();

$alerts->assertHasOne();
}
```

The following assertions are available:

| Method | Description |
|---------------------------|------------------------------------------------------------------------|
| `assertEmpty()` | Check if the alert bag doesn't contains alerts. |
| `assertNotEmpty()` | Check if the alert bag contains any alert. |
| `assertHasOne()` | Check if the alert bag contains only one alert. |
| `assertHas($count)` | Check if the alert bag contains the exact amount of alerts. |
| `assertHasPersistent()` | Check if the alert bag contains at least one persistent alert. |
| `assertHasNoPersistent()` | Check if the alert bag doesn't contains a persistent alert. |
| `assertPersistentCount()` | Check if the alert bag contains the exact amount of persistent alerts. |

### Asserting specific alerts

The fake Alert bag allows building conditions for the existence (or nonexistence) of alerts with specific properties, by using `assertAlert()`.

Once you build your conditions, you can use `exists()` to check if any alert matches, or `missing()` to check if no alert should match.

```php
$bag->assertAlert()->withMessage('Hello world!')->exists();

$bag->assertAlert()->withTypes('danger')->dismissible()->missing();
```

Alternatively, you can use `count()` if you expect a specific number of alerts to match the given conditions, or `unique()` for matching only one alert.

```php
$bag->assertAlert()->persisted()->count(2);

$bag->assertAlert()->notDismissible()->withTag('toast')->unique();
```

The following conditions are available:

| Method | Description |
|---------------------|---------------------------------------------------|
| `withRaw()` | Find alerts with the given raw message. |
| `withMessage()` | Find alerts with the given message. |
| `withTrans()` | Find alerts with the translated message. |
| `withTransChoice()` | Find alerts with the translated (choice) message. |
| `withAway()` | Find alerts with a link away. |
| `withTo()` | Find alerts with a link to a path. |
| `withRoute()` | Find alerts with a link to a route. |
| `withAction()` | Find alerts with a link to a controller action. |
| `withTypes()` | Find alerts with exactly the given types. |
| `persisted()` | Find alerts persisted. |
| `notPersisted()` | Find alerts not persisted. |
| `persistedAs()` | Find alerts persisted with the issued keys. |
| `dismissible()` | Find alerts dismissible. |
| `notDismissible()` | Find alerts not dismissible. |
| `withTag()` | Find alerts with all the given tags. |
| `withAnyTag()` | Find alerts with any of the given tags. |

## Security

If you discover any security related issues, please email darkghosthunter@gmail.com instead of using the issue tracker.
Expand Down
13 changes: 11 additions & 2 deletions src/Alert.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
use Illuminate\Support\Traits\Macroable;
use JsonSerializable;
use Stringable;

use function action;
use function is_array;
use function json_encode;
use function route;
use function sort;
use function strcmp;
use function trans;
use function trim;
use function url;
Expand Down Expand Up @@ -133,7 +134,7 @@ public function getTags(): array

/**
* Check if the alert contains any of the given tags.
*
*
* @param string ...$tags
* @return bool
* @internal
Expand Down Expand Up @@ -214,6 +215,8 @@ public function types(string ...$types): static
{
$this->types = $types;

sort($this->types);

return $this;
}

Expand Down Expand Up @@ -275,6 +278,10 @@ public function away(string $replace, string $url, bool $blank = true): static
'blank' => $blank,
];

usort($this->links, static function (object $first, object $second): int {
return strcmp($first->replace . $first->url, $second->replace . $second->url);
});

return $this;
}

Expand Down Expand Up @@ -329,6 +336,8 @@ public function tag(string ...$tags): static
{
$this->tags = $tags;

sort($this->tags);

return $this;
}

Expand Down
8 changes: 2 additions & 6 deletions src/Bag.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@
namespace DarkGhostHunter\Laralerts;

use Closure;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Contracts\Session\Session;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Traits\Macroable;

use function array_key_last;
use function json_decode;
use function value;

use const JSON_THROW_ON_ERROR;

/**
Expand All @@ -34,8 +30,8 @@ class Bag
/**
* Create a new Bag instance.
*
* @param \Illuminate\Contracts\Session\Session $session
* @param \Illuminate\Contracts\Config\Repository $config
* @param array $tags
* @param array $persisted
*/
public function __construct(protected array $tags, protected array $persisted = [])
{
Expand Down
24 changes: 22 additions & 2 deletions src/Facades/Alert.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

namespace DarkGhostHunter\Laralerts\Facades;

use Closure;
use DarkGhostHunter\Laralerts\Bag;
use DarkGhostHunter\Laralerts\Testing\Fakes\BagFake;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Facade;

/**
Expand All @@ -15,8 +19,8 @@
* @method static \DarkGhostHunter\Laralerts\Alert raw(string $message)
* @method static \DarkGhostHunter\Laralerts\Alert types(string ...$types)
* @method static \DarkGhostHunter\Laralerts\Alert dismiss(bool $dismissible = true)
* @method static \DarkGhostHunter\Laralerts\Alert when(\Closure|bool $condition)
* @method static \DarkGhostHunter\Laralerts\Alert unless(\Closure|bool $condition)
* @method static \DarkGhostHunter\Laralerts\Alert when(Closure|bool $condition)
* @method static \DarkGhostHunter\Laralerts\Alert unless(Closure|bool $condition)
* @method static \DarkGhostHunter\Laralerts\Alert away(string $replace, string $url, bool $blank = true)
* @method static \DarkGhostHunter\Laralerts\Alert to(string $replace, string $url, bool $blank = false)
* @method static \DarkGhostHunter\Laralerts\Alert route(string $replace, string $name, array $parameters = [], bool $blank = false)
Expand All @@ -35,4 +39,20 @@ protected static function getFacadeAccessor(): string
{
return Bag::class;
}

/**
* Creates a fake Alert Bag.
*
* @return \DarkGhostHunter\Laralerts\Testing\Fakes\BagFake
*/
public static function fake(): BagFake
{
$fake = static::$app->make(BagFake::class, [
'tags' => Arr::wrap(Config::get('laralerts.tags'))
]);

static::swap($fake);

return $fake;
}
}
2 changes: 1 addition & 1 deletion src/LaralertsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace DarkGhostHunter\Laralerts;

use Illuminate\Contracts\Http\Kernel;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Http\Kernel;
use Illuminate\Routing\Router;
use Illuminate\Support\ServiceProvider;
use Illuminate\View\Compilers\BladeCompiler;
Expand Down
Loading

0 comments on commit 182ddfc

Please sign in to comment.