Passed
Pull Request — master (#6)
by Wilmer
11:29
created

ProfilerTest::testBeginEndIsNull()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 10
rs 10
cc 1
nc 1
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Yiisoft\Profiler\Tests;
5
6
use Psr\Log\NullLogger;
7
use Yiisoft\Profiler\LogTarget;
8
use Yiisoft\Profiler\Profiler;
9
use Yiisoft\Profiler\Target;
10
11
class ProfilerTest extends TestCase
12
{
13
    /**
14
     * @covers \Yiisoft\Profiler\Profiler::setTargets()
15
     * @covers \Yiisoft\Profiler\Profiler::getTargets()
16
     */
17
    public function testSetupTarget(): void
18
    {
19
        $profiler = new Profiler($this->logger);
20
21
        $target = new LogTarget(new NullLogger());
22
23
        $profiler->setTargets([$target]);
24
25
        $this->assertEquals([$target], $profiler->getTargets());
26
        $this->assertSame($target, $profiler->getTargets()[0]);
27
28
        $profiler->setTargets([
29
            [
30
                '__class' => LogTarget::class,
31
                'logger' => new NullLogger(),
32
                'level' => 'test'
33
            ],
34
        ]);
35
36
        $target = $profiler->getTargets()[0];
37
38
        $this->assertInstanceOf(LogTarget::class, $target);
39
        $this->assertEquals('test', $target->getLogLevel());
40
    }
41
42
    /**
43
     * @depends testSetupTarget
44
     *
45
     * @covers \Yiisoft\Profiler\Profiler::addTarget()
46
     */
47
    public function testAddTarget(): void
48
    {
49
        $profiler = new Profiler($this->logger);
50
51
        $target = $this->getMockBuilder(Target::class)->getMockForAbstractClass();
52
        $profiler->setTargets([$target]);
53
54
        $namedTarget = $this->getMockBuilder(Target::class)->getMockForAbstractClass();
55
        $profiler->addTarget($namedTarget, 'test-target');
56
57
        $targets = $profiler->getTargets();
58
59
        $this->assertCount(2, $targets);
60
        $this->assertTrue(isset($targets['test-target']));
61
        $this->assertSame($namedTarget, $targets['test-target']);
62
63
        $namelessTarget = $this->getMockBuilder(Target::class)->getMockForAbstractClass();
64
        $profiler->addTarget($namelessTarget);
65
        $targets = $profiler->getTargets();
66
67
        $this->assertCount(3, $targets);
68
        $this->assertSame($namelessTarget, array_pop($targets));
69
    }
70
71
    public function testEnabled(): void
72
    {
73
        $profiler = new Profiler($this->logger);
74
75
        $profiler->setEnabled(false);
76
77
        $profiler->begin('test');
78
        $profiler->end('test');
79
80
        $this->assertEmpty($profiler->getMessages());
81
82
        $profiler->setEnabled(true);
83
84
        $profiler->begin('test');
85
        $profiler->end('test');
86
87
        $this->assertCount(1, $profiler->getMessages());
88
    }
89
90
    public function testBeginEndIsNull(): void
91
    {
92
        $profiler = new Profiler($this->logger);
93
94
        $profiler->setEnabled(true);
95
96
        $profiler->begin(null);
97
        $profiler->end(null);
98
99
        $this->assertCount(1, $profiler->getMessages());
100
    }
101
102
    /**
103
     * @covers \Yiisoft\Profiler\Profiler::flush()
104
     */
105
    public function testFlushWithDispatch(): void
106
    {
107
        /* @var $profiler Profiler|\PHPUnit_Framework_MockObject_MockObject */
108
        $profiler = $this->getMockBuilder(Profiler::class)
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\MockOb...ckBuilder::setMethods() has been deprecated: https://github.com/sebastianbergmann/phpunit/pull/3687 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

108
        $profiler = /** @scrutinizer ignore-deprecated */ $this->getMockBuilder(Profiler::class)

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
109
            ->setConstructorArgs([$this->logger])
110
            ->setMethods(['dispatch'])
111
            ->getMock();
112
113
        $message = ['anything'];
114
115
        $profiler->setMessages($message);
116
117
        $profiler->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Yiisoft\Profiler\Profiler. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

117
        $profiler->/** @scrutinizer ignore-call */ 
118
                   expects($this->once())

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
118
            ->method('dispatch')
119
            ->with($this->equalTo($message));
120
121
        $profiler->flush();
122
123
        $this->assertEmpty($profiler->getMessages());
124
    }
125
126
    public function testNestedMessages(): void
127
    {
128
        $profiler = new Profiler($this->logger);
129
130
        $profiler->begin('test');
131
        $profiler->begin('test');
132
        $profiler->end('test');
133
        $profiler->end('test');
134
135
        $this->assertCount(2, $profiler->getMessages());
136
    }
137
138
    /**
139
     * @depends testNestedMessages
140
     */
141
    public function testNestedLevel(): void
142
    {
143
        $profiler = new Profiler($this->logger);
144
145
        $profiler->begin('outer');
146
        $profiler->begin('inner');
147
        $profiler->end('inner');
148
        $profiler->end('outer');
149
        $profiler->begin('not-nested');
150
        $profiler->end('not-nested');
151
152
        $outerMessage = null;
153
        $innerMessage = null;
154
        $notNestedMessage = null;
155
156
        foreach ($profiler->getmessages() as $message) {
157
            if ($message['token'] === 'outer') {
158
                $outerMessage = $message;
159
                continue;
160
            }
161
            if ($message['token'] === 'inner') {
162
                $innerMessage = $message;
163
                continue;
164
            }
165
            if ($message['token'] === 'not-nested') {
166
                $notNestedMessage = $message;
167
                continue;
168
            }
169
        }
170
171
        $this->assertSame(0, $outerMessage['nestedLevel']);
172
        $this->assertSame(1, $innerMessage['nestedLevel']);
173
        $this->assertSame(0, $notNestedMessage['nestedLevel']);
174
    }
175
}
176