Completed
Push — master ( e15eaf...3ab61b )
by Thomas Mauro
01:57
created

LoggerTest::testLogWithArrayPlaceholder()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
cc 1
eloc 16
nc 1
nop 0
1
<?php
2
3
namespace Facile\Sentry\LogTest;
4
5
use Facile\Sentry\Common\Sanitizer\SanitizerInterface;
6
use Facile\Sentry\Common\Sender\SenderInterface;
7
use Facile\Sentry\Log\ContextException;
8
use Facile\Sentry\Log\Logger;
9
use Prophecy\Argument;
10
use Psr\Log\InvalidArgumentException;
11
use Psr\Log\LogLevel;
12
13
class LoggerTest extends \PHPUnit\Framework\TestCase
14
{
15
    public function testLogWithInvalidLevel()
16
    {
17
        $this->expectException(InvalidArgumentException::class);
18
19
        $raven = $this->prophesize(\Raven_Client::class);
20
        $logger = new Logger($raven->reveal());
21
22
        $logger->log('foo', 'message');
23
    }
24
25
    public function testLogWithInvalidObject()
26
    {
27
        $this->expectException(InvalidArgumentException::class);
28
29
        $raven = $this->prophesize(\Raven_Client::class);
30
        $logger = new Logger($raven->reveal());
31
32
        $logger->log(LogLevel::ALERT, new \stdClass());
0 ignored issues
show
Documentation introduced by
new \stdClass() is of type object<stdClass>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
33
    }
34
35
    public function testLogWithObject()
36
    {
37
        $raven = $this->prophesize(\Raven_Client::class);
38
        $sender = $this->prophesize(SenderInterface::class);
39
        $sanitizer = $this->prophesize(SanitizerInterface::class);
40
        $logger = new Logger(
41
            $raven->reveal(),
42
            $sender->reveal(),
43
            $sanitizer->reveal()
44
        );
45
46
        $object = new class {
47
            public function __toString()
48
            {
49
                return 'object string';
50
            }
51
        };
52
53
        $context = [
54
            'foo' => 'name',
55
        ];
56
57
        $sanitizer->sanitize($context)->shouldBeCalled()->willReturn($context);
58
        $sender->send(\Raven_Client::ERROR, 'object string', $context)
59
            ->shouldBeCalled();
60
61
        $logger->log(LogLevel::ALERT, $object, $context);
62
    }
63
64
    public function testLog()
65
    {
66
        $raven = $this->prophesize(\Raven_Client::class);
67
        $sender = $this->prophesize(SenderInterface::class);
68
        $sanitizer = $this->prophesize(SanitizerInterface::class);
69
        $logger = new Logger(
70
            $raven->reveal(),
71
            $sender->reveal(),
72
            $sanitizer->reveal()
73
        );
74
75
        $context = [
76
            'foo' => 'name',
77
            'placeholder' => 'value'
78
        ];
79
80
        $sanitizer->sanitize($context)->shouldBeCalled()->willReturn($context);
81
        $sender->send(\Raven_Client::ERROR, 'message value', $context)
82
            ->shouldBeCalled();
83
84
        $logger->log(LogLevel::ALERT, 'message {placeholder}', $context);
85
    }
86
87
    public function testLogWithArrayPlaceholder()
88
    {
89
        $raven = $this->prophesize(\Raven_Client::class);
90
        $sender = $this->prophesize(SenderInterface::class);
91
        $sanitizer = $this->prophesize(SanitizerInterface::class);
92
        $logger = new Logger(
93
            $raven->reveal(),
94
            $sender->reveal(),
95
            $sanitizer->reveal()
96
        );
97
98
        $context = [
99
            'foo' => 'name',
100
            'placeholder' => [
101
                'foo' => 'bar',
102
            ]
103
        ];
104
105
        $sanitizer->sanitize($context)->shouldBeCalled()->willReturn($context);
106
        $sender->send(\Raven_Client::ERROR, 'message {placeholder}', $context)
107
            ->shouldBeCalled();
108
109
        $logger->log(LogLevel::ALERT, 'message {placeholder}', $context);
110
    }
111
}
112