1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spiral\Tests\LogViewer; |
4
|
|
|
|
5
|
|
|
use Psr\Log\LogLevel; |
6
|
|
|
use Spiral\Debug\Traits\LoggerTrait; |
7
|
|
|
use Spiral\LogViewer\Entities\LogFile; |
8
|
|
|
use Spiral\LogViewer\Services\LogService; |
9
|
|
|
use Spiral\Tests\HttpTest; |
10
|
|
|
|
11
|
|
|
class LogServiceTest extends HttpTest |
12
|
|
|
{ |
13
|
|
|
use LoggerTrait; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Can get all logs. |
17
|
|
|
*/ |
18
|
|
View Code Duplication |
public function testGetLogs() |
|
|
|
|
19
|
|
|
{ |
20
|
|
|
/** @var LogService $service */ |
21
|
|
|
$service = $this->container->get(LogService::class); |
22
|
|
|
|
23
|
|
|
$this->assertCount(0, $service->getLogs()); |
24
|
|
|
|
25
|
|
|
$snapshot = $this->makeSnapshot('error', 123); |
26
|
|
|
$snapshot->report(); |
27
|
|
|
|
28
|
|
|
$this->assertCount(1, $service->getLogs()); |
29
|
|
|
|
30
|
|
|
$this->get('/controller/action'); |
31
|
|
|
$this->assertCount(2, $service->getLogs()); |
32
|
|
|
|
33
|
|
|
$log = current($service->getLogs()->iterate()); |
34
|
|
|
$this->assertInstanceOf(LogFile::class, $log); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Take really last log. |
39
|
|
|
*/ |
40
|
|
View Code Duplication |
public function testLastLog() |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
/** @var LogService $service */ |
43
|
|
|
$service = $this->container->get(LogService::class); |
44
|
|
|
|
45
|
|
|
$this->assertEmpty($service->lastLog()); |
46
|
|
|
|
47
|
|
|
$snapshot = $this->makeSnapshot('error', 123); |
48
|
|
|
$snapshot->report(); |
49
|
|
|
|
50
|
|
|
/** @var LogFile $last */ |
51
|
|
|
$last = $service->lastLog(); |
52
|
|
|
|
53
|
|
|
$this->assertNotEmpty($last); |
54
|
|
|
$this->assertInstanceOf(LogFile::class, $last); |
55
|
|
|
|
56
|
|
|
sleep(1); |
57
|
|
|
$this->get('/controller/action'); |
58
|
|
|
|
59
|
|
|
/** @var LogFile $last2 */ |
60
|
|
|
$last2 = $service->lastLog(); |
61
|
|
|
$this->assertNotEmpty($last2); |
62
|
|
|
|
63
|
|
|
$this->assertNotEquals($last->name(), $last2->name()); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get log by name is really what we want |
68
|
|
|
*/ |
69
|
|
View Code Duplication |
public function testGetLogByName() |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
/** @var LogService $service */ |
72
|
|
|
$service = $this->container->get(LogService::class); |
73
|
|
|
|
74
|
|
|
$snapshot = $this->makeSnapshot('error', 123); |
75
|
|
|
$snapshot->report(); |
76
|
|
|
|
77
|
|
|
$this->assertEmpty($service->getLogByName('some-name')); |
78
|
|
|
|
79
|
|
|
/** @var LogFile $last */ |
80
|
|
|
$last = $service->lastLog(); |
81
|
|
|
$this->assertNotEmpty($last); |
82
|
|
|
|
83
|
|
|
$log = $service->getLogByName($last->name()); |
84
|
|
|
|
85
|
|
|
$this->assertNotEmpty($log); |
86
|
|
|
$this->assertInstanceOf(LogFile::class, $log); |
87
|
|
|
|
88
|
|
|
$this->assertEquals($last->name(), $log->name()); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Remove all logs |
93
|
|
|
*/ |
94
|
|
View Code Duplication |
public function testRemoveAll() |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
/** @var LogService $service */ |
97
|
|
|
$service = $this->container->get(LogService::class); |
98
|
|
|
|
99
|
|
|
$this->assertCount(0, $service->getLogs()); |
100
|
|
|
|
101
|
|
|
$snapshot = $this->makeSnapshot('error', 123); |
102
|
|
|
$snapshot->report(); |
103
|
|
|
|
104
|
|
|
$this->assertCount(1, $service->getLogs()); |
105
|
|
|
|
106
|
|
|
$this->get('/controller/action'); |
107
|
|
|
$this->assertCount(2, $service->getLogs()); |
108
|
|
|
|
109
|
|
|
$service->removeAll(); |
110
|
|
|
|
111
|
|
|
$this->assertEmpty($service->getLogs()); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Remove |
116
|
|
|
*/ |
117
|
|
|
public function testRemove() |
118
|
|
|
{ |
119
|
|
|
/** @var LogService $service */ |
120
|
|
|
$service = $this->container->get(LogService::class); |
121
|
|
|
|
122
|
|
|
$this->assertCount(0, $service->getLogs()); |
123
|
|
|
|
124
|
|
|
$snapshot = $this->makeSnapshot('error', 123); |
125
|
|
|
$snapshot->report(); |
126
|
|
|
|
127
|
|
|
$this->assertCount(1, $service->getLogs()); |
128
|
|
|
|
129
|
|
|
/** @var LogFile $last */ |
130
|
|
|
$last = $service->lastLog(); |
131
|
|
|
$this->assertNotEmpty($last); |
132
|
|
|
|
133
|
|
|
$service->removeLog(new LogFile('some-name')); |
134
|
|
|
|
135
|
|
|
$this->assertCount(1, $service->getLogs()); |
136
|
|
|
|
137
|
|
|
$service->removeLog($last); |
138
|
|
|
|
139
|
|
|
$this->assertEmpty($service->getLogs()); |
140
|
|
|
} |
141
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.