SlackLoggerTest::logWithoutException()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace DominionEnterprisesTest\Psr\Log;
4
5
use DominionEnterprises\Psr\Log\SlackLogger;
6
use Psr\Log\LogLevel;
7
8
/**
9
 * @coversDefaultClass \DominionEnterprises\Psr\Log\SlackLogger
10
 * @covers ::__construct
11
 * @covers ::<private>
12
 */
13
final class SlackLoggerTest extends \PHPUnit\Framework\TestCase
14
{
15
    /**
16
     * @var string
17
     */
18
    private $webHookUrl = 'http://localhost/';
19
20
    /**
21
     * Verify behavior of log() when level is not included when constructed.
22
     *
23
     * @test
24
     * @covers ::log
25
     *
26
     * @return void
27
     */
28
    public function logIgnoredLevel()
29
    {
30
        $mock = $this->getMockBuilder('\\GuzzleHttp\\ClientInterface')->getMock();
31
        $mock->method('post')->will(
32
            $this->throwException(new \Exception('post() should not have been called.'))
33
        );
34
        $logger = $this->getLogger($mock);
35
        $this->assertNull($logger->log(LogLevel::INFO, 'test message'));
36
    }
37
38
    /**
39
     * Verify behavior of log() without an execption.
40
     *
41
     * @test
42
     * @covers ::log
43
     *
44
     * @return void
45
     */
46
    public function logWithoutException()
47
    {
48
        $text = '*[emergency]* test message';
49
        $logger = $this->getLogger($this->getGuzzleClientMock($text));
50
        $logger->log(LogLevel::EMERGENCY, 'test message');
51
    }
52
53
    /**
54
     * Verify behavior of log() with Throwable.
55
     *
56
     * @param \Throwable $throwable The exception or error to be logged in the test.
57
     *
58
     * @test
59
     * @covers ::log
60
     * @dataProvider provideThrowables
61
     *
62
     * @return void
63
     */
64
    public function logThrowable(\Throwable $throwable)
65
    {
66
        $text = $this->buildExpectedPayloadText($throwable);
67
        $logger = $this->getLogger($this->getGuzzleClientMock($text));
68
        $logger->log(LogLevel::EMERGENCY, 'test message', ['exception' => $throwable]);
69
    }
70
71
    /**
72
     * Data provider for ensure all types of exceptions can be logged.
73
     *
74
     * @return array
75
     */
76
    public function provideThrowables() : array
77
    {
78
        return [
79
            'runtimeException' => [new \RuntimeException('a runtime exception')],
80
            'typeError' => [new \TypeError('a type error')],
0 ignored issues
show
Unused Code introduced by
The call to TypeError::__construct() has too many arguments starting with 'a type error'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
81
        ];
82
    }
83
84
    private function buildExpectedPayloadText(\Throwable $throwable) : string
85
    {
86
        $class = get_class($throwable);
87
        return "*[emergency]* test message\n*Exception:* {$class}\n*Message:* {$throwable->getMessage()}\n*File:*"
88
            . " {$throwable->getFile()}\n*Line:* {$throwable->getLine()}";
89
    }
90
91
    private function getGuzzleClientMock(string $expectedPayloadText)
92
    {
93
        $body = ['payload' => json_encode(['text' => $expectedPayloadText, 'mrkdwn' => true])];
94
        $mock = $this->getMockBuilder('\\GuzzleHttp\\ClientInterface')->getMock();
95
        $mock->expects($this->once())->method('post')->with(
96
            $this->equalTo($this->webHookUrl),
97
            $this->equalTo(['body' => $body])
98
        );
99
100
        return $mock;
101
    }
102
103
    private function getLogger(\GuzzleHttp\ClientInterface $client) : SlackLogger
104
    {
105
        return new SlackLogger($client, $this->webHookUrl);
106
    }
107
}
108