-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 46bf3d3
Showing
9 changed files
with
1,133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
vendor/ | ||
composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
{ | ||
"name": "timacdonald/log-fake", | ||
"description": "A drop in fake logger for testing with the Laravel framework.", | ||
"license": "MIT", | ||
"keywords": [ | ||
"fake", | ||
"log", | ||
"logger", | ||
"testing", | ||
"laravel" | ||
], | ||
"authors": [ | ||
{ | ||
"name": "Tim MacDonald", | ||
"email": "[email protected]", | ||
"homepage": "https://timacdonald.me" | ||
} | ||
], | ||
"require": { | ||
"php": "^7.1.3", | ||
"illuminate/config": "~5.6", | ||
"illuminate/container": "~5.6", | ||
"illuminate/support": "~5.6", | ||
"phpunit/phpunit": "^7.0", | ||
"psr/log": "^1.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"TiMacDonald\\Log\\": "src" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"Tests\\": "tests/" | ||
}, | ||
"files": [ | ||
"tests/helpers.php" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2017 Tim MacDonald | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnFailure="false"> | ||
<testsuites> | ||
<testsuite name="Test Suite"> | ||
<directory>./tests/</directory> | ||
</testsuite> | ||
</testsuites> | ||
<filter> | ||
<whitelist> | ||
<directory suffix=".php">app/</directory> | ||
</whitelist> | ||
</filter> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
# Log fake for Laravel | ||
|
||
[![Latest Stable Version](https://poser.pugx.org/timacdonald/log-fake/v/stable)](https://packagist.org/packages/timacdonald/log-fake) [![Total Downloads](https://poser.pugx.org/timacdonald/log-fake/downloads)](https://packagist.org/packages/timacdonald/log-fake) [![License](https://poser.pugx.org/timacdonald/log-fake/license)](https://packagist.org/packages/timacdonald/log-fake) | ||
|
||
A bunch of Laravel facades / services are able to be faked, such as the Dispatcher with `Bus::fake()`, to help with testing and assertions. This package gives you the ability to fake the logger in your app, and includes the ability to make assertions against channels and stacks introduced in logging overhaul in Laravel `5.6`. | ||
|
||
## Installation | ||
|
||
You can install using [composer](https://getcomposer.org/) from [Packagist](https://packagist.org/packages/timacdonald/log-fake) | ||
|
||
``` | ||
$ composer require timacdonald/log-fake | ||
``` | ||
|
||
## Basic usage | ||
|
||
```php | ||
use TiMacDonald\Log\LogFake; | ||
use Illuminate\Support\Facades\Log; | ||
|
||
//... | ||
|
||
Log::swap(new LogFake); | ||
|
||
Log::info('Donuts have arrived'); | ||
|
||
Log::assertLogged('info', function ($message, $context) { | ||
return str_contains($message, 'Donuts'); | ||
}); | ||
``` | ||
|
||
## Channels | ||
|
||
If you are logging to a specific channel in your app, such as Slack with `Log::channel('slack')->critical('It is 5pm, go home')`, you need to also prefix your assertions in the same manner. | ||
|
||
```php | ||
use TiMacDonald\Log\LogFake; | ||
use Illuminate\Support\Facades\Log; | ||
|
||
//... | ||
|
||
Log::swap(new LogFake); | ||
|
||
Log::channel('slack')->alert('It is 5pm, go home'); | ||
|
||
Log::channel('slack')->assertLogged('alert'); // ✅ passes | ||
|
||
// without the channel prefix... | ||
|
||
Log::assertLogged('alert'); // ❌ fails | ||
``` | ||
|
||
## Stacks | ||
|
||
If you are logging to a stack in your app, like with channels, you will need to prefix your assertions. Note that the order of the stack does not matter. | ||
|
||
```php | ||
use TiMacDonald\Log\LogFake; | ||
use Illuminate\Support\Facades\Log; | ||
|
||
//... | ||
|
||
Log::swap(new LogFake); | ||
|
||
Log::stack(['bugsnag', 'sentry'])->critical('Perform evasive maneuvers'); | ||
|
||
|
||
Log::stack(['bugsnag', 'sentry'])->assertLogged('critical'); // ✅ passes | ||
|
||
// without the stack prefix... | ||
|
||
Log::assertLogged('critical'); // ❌ fails | ||
``` | ||
|
||
## Available assertions | ||
|
||
All assertions are relative to the channel or stack as shown in the previous examples. | ||
|
||
### assertLogged($level, $callback = null) | ||
|
||
```php | ||
Log::assertLogged('info'); | ||
|
||
Log::channel('slack')->assertLogged('alert'); | ||
|
||
Log::stack(['bugsnag', 'sentry'])->assertLogged('critical'); | ||
|
||
// with a callback | ||
|
||
Log::assertLogged('info', function ($message, $context) { | ||
return str_contains($message, 'Donuts'); | ||
}); | ||
|
||
Log::channel('slack')->assertLogged('alert', function ($message, $context) { | ||
return str_contains($message, '5pm'); | ||
}); | ||
|
||
Log::stack(['bugsnag', 'sentry'])->assertLogged('critical', function ($message, $context) { | ||
return str_contains($message, 'evasive maneuvers'); | ||
}); | ||
``` | ||
|
||
### assertLoggedTimes($level, $times = 1) | ||
|
||
```php | ||
Log::assertLoggedTimes('info', 5); | ||
|
||
Log::channel('slack')->assertLoggedTimes('alert', 5); | ||
|
||
Log::stack(['bugsnag', 'sentry'])->assertLoggedTimes('critical', 5); | ||
``` | ||
|
||
### assertNotLogged($level, $callback = null) | ||
|
||
```php | ||
Log::assertNotLogged('info'); | ||
|
||
Log::channel('slack')->assertNotLogged('alert'); | ||
|
||
Log::stack(['bugsnag', 'sentry'])->assertNotLogged('critical'); | ||
|
||
// with a callback | ||
|
||
Log::assertNotLogged('info', function ($message, $context) { | ||
return str_contains($message, 'Donuts'); | ||
}); | ||
|
||
Log::channel('slack')->assertNotLogged('alert' , function ($message, $context) { | ||
return str_contains($message, '5pm'); | ||
}); | ||
|
||
Log::stack(['bugsnag', 'sentry'])->assertNotLogged('critical', function ($message, $context) { | ||
return str_contains($message, 'evasive maneuvers'); | ||
}); | ||
``` | ||
|
||
### assertNothingLogged() | ||
|
||
```php | ||
Log::assertNothingLogged('info'); | ||
|
||
Log::channel('slack')->assertNothingLogged('alert'); | ||
|
||
Log::stack(['bugsnag', 'sentry'])->assertNothingLogged('critical'); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace TiMacDonald\Log; | ||
|
||
class ChannelFake | ||
{ | ||
protected $log; | ||
|
||
protected $name; | ||
|
||
public function __construct($log, $name) | ||
{ | ||
$this->log = $log; | ||
|
||
$this->name = $name; | ||
} | ||
|
||
public function __call($method, $arguments) | ||
{ | ||
$this->log->setCurrentChannel($this->name); | ||
|
||
$result = $this->log->{$method}(...$arguments); | ||
|
||
$this->log->setCurrentChannel(null); | ||
|
||
return $result; | ||
} | ||
} |
Oops, something went wrong.