| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
/* |
| 6 |
* This file is part of the Monolog package. |
| 7 |
* |
| 8 |
* (c) Jordi Boggiano <[email protected]> |
| 9 |
* |
| 10 |
* For the full copyright and license information, please view the LICENSE |
| 11 |
* file that was distributed with this source code. |
| 12 |
*/ |
| 13 |
|
| 14 |
namespace Monolog\Handler; |
| 15 |
|
| 16 |
use Monolog\Test\TestCase; |
| 17 |
use Monolog\Logger; |
| 18 |
use Monolog\Formatter\LineFormatter; |
| 19 |
|
| 20 |
class RedisPubSubHandlerTest extends TestCase |
| 21 |
{ |
| 22 |
public function testConstructorShouldThrowExceptionForInvalidRedis() |
| 23 |
{ |
| 24 |
$this->expectException(\InvalidArgumentException::class); |
| 25 |
|
| 26 |
new RedisPubSubHandler(new \stdClass(), 'key'); |
| 27 |
} |
| 28 |
|
| 29 |
public function testConstructorShouldWorkWithPredis() |
| 30 |
{ |
| 31 |
$redis = $this->createMock('Predis\Client'); |
| 32 |
$this->assertInstanceof('Monolog\Handler\RedisPubSubHandler', new RedisPubSubHandler($redis, 'key')); |
| 33 |
} |
| 34 |
|
| 35 |
public function testConstructorShouldWorkWithRedis() |
| 36 |
{ |
| 37 |
if (!class_exists('Redis')) { |
| 38 |
$this->markTestSkipped('The redis ext is required to run this test'); |
| 39 |
} |
| 40 |
|
| 41 |
$redis = $this->createMock('Redis'); |
| 42 |
$this->assertInstanceof('Monolog\Handler\RedisPubSubHandler', new RedisPubSubHandler($redis, 'key')); |
| 43 |
} |
| 44 |
|
| 45 |
public function testPredisHandle() |
| 46 |
{ |
| 47 |
$redis = $this->prophesize('Predis\Client'); |
| 48 |
$redis->publish('key', 'test')->shouldBeCalled(); |
| 49 |
$redis = $redis->reveal(); |
| 50 |
|
| 51 |
$record = $this->getRecord(Logger::WARNING, 'test', ['data' => new \stdClass(), 'foo' => 34]); |
| 52 |
|
| 53 |
$handler = new RedisPubSubHandler($redis, 'key'); |
| 54 |
$handler->setFormatter(new LineFormatter("%message%")); |
| 55 |
$handler->handle($record); |
| 56 |
} |
| 57 |
|
| 58 |
public function testRedisHandle() |
| 59 |
{ |
| 60 |
if (!class_exists('Redis')) { |
| 61 |
$this->markTestSkipped('The redis ext is required to run this test'); |
| 62 |
} |
| 63 |
|
| 64 |
$redis = $this->createPartialMock('Redis', ['publish']); |
| 65 |
|
| 66 |
$redis->expects($this->once()) |
| 67 |
->method('publish') |
| 68 |
->with('key', 'test'); |
| 69 |
|
| 70 |
$record = $this->getRecord(Logger::WARNING, 'test', ['data' => new \stdClass(), 'foo' => 34]); |
| 71 |
|
| 72 |
$handler = new RedisPubSubHandler($redis, 'key'); |
| 73 |
$handler->setFormatter(new LineFormatter("%message%")); |
| 74 |
$handler->handle($record); |
| 75 |
} |
| 76 |
} |
| 77 |
|