-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestCase.php
121 lines (96 loc) · 3.35 KB
/
TestCase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
/*
This file is part of Erebot.
Erebot is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Erebot is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Erebot. If not, see <http://www.gnu.org/licenses/>.
*/
require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'Utils.php');
abstract class Erebot_Testenv_TestCase
extends \PHPUnit\Framework\TestCase
{
private $expectedLogs = NULL;
private $logStream = NULL;
public function getExpectedLogs()
{
return $this->expectedLogs;
}
public function addExpectedLog($logLine)
{
if (!is_string($logLine)) {
throw new Exception();
}
if ($this->expectedLogs === NULL) {
$this->expectedLogs = array();
}
$this->expectedLogs[] = $logLine;
}
public function setExpectedLogs($logLines)
{
if (is_string($logLines)) {
$logLines = trim($logLines);
$logLines = preg_split('/\\r?\\n/', $logLines);
}
if ($logLines === NULL) {
return;
}
if (!is_array($logLines)) {
throw new Exception();
}
$this->expectedLogs = array();
foreach ($logLines as $logLine) {
$this->addExpectedLog($logLine);
}
}
protected function setExpectedLogsFromAnnotations()
{
try {
$expectedLogs = Erebot_Testenv_Utils::getExpectedLogs(
get_class($this),
$this->getName()
);
$this->setExpectedLogs($expectedLogs);
}
catch (ReflectionException $e) {
}
}
protected function runTest()
{
$this->setExpectedLogsFromAnnotations();
$logging = \Plop\Plop::getInstance();
$this->logStream = fopen('php://temp', 'a+');
$handlers = new \Plop\HandlersCollection();
$handler = new \Plop\Handler\Stream($this->logStream);
$handler->setFormatter(
new \Plop\Formatter('%(levelname)s:%(message)s')
);
$handlers[] = $handler;
$logging->getLogger()->setHandlers($handlers);
$result = parent::runTest();
if ($this->expectedLogs !== NULL) {
fseek($this->logStream, 0);
$actualLogs = stream_get_contents($this->logStream);
fclose($this->logStream);
$actualLogs = array_map('rtrim', explode("\n", $actualLogs));
$actualLogs = array_values(array_filter($actualLogs, 'strlen'));
$this->addToAssertionCount(1);
if (count($this->expectedLogs)) {
$this->assertEquals($this->expectedLogs, $actualLogs);
}
else if (count($actualLogs)) {
$this->fail(
"No logs expected, but we received:\n" .
var_export($actualLogs, TRUE)
);
}
}
return $result;
}
}