1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* (c) Mantas Varatiejus <[email protected]> |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace MVar\Apache2LogParser\Tests; |
11
|
|
|
|
12
|
|
|
use MVar\Apache2LogParser\Exception\NoMatchesException; |
13
|
|
|
use MVar\Apache2LogParser\LineParserInterface; |
14
|
|
|
use MVar\Apache2LogParser\LogIterator; |
15
|
|
|
|
16
|
|
|
class LogIteratorTest extends \PHPUnit_Framework_TestCase |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Creates and returns instance of parser mock. |
20
|
|
|
* |
21
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|LineParserInterface |
22
|
|
|
*/ |
23
|
|
|
protected function getParser() |
24
|
|
|
{ |
25
|
|
|
return $this->getMock('\\MVar\\Apache2LogParser\\LineParserInterface'); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Test for log iterator. |
30
|
|
|
* |
31
|
|
|
* @param string $logfile |
32
|
|
|
* @param int $rowsCount |
33
|
|
|
* |
34
|
|
|
* @dataProvider getTestIteratorData() |
35
|
|
|
*/ |
36
|
|
|
public function testIterator($logfile, $rowsCount) |
37
|
|
|
{ |
38
|
|
|
$parser = $this->getParser(); |
39
|
|
|
$expectedData = 'parsed_line'; |
40
|
|
|
|
41
|
|
|
// Test if parser was called as many times as expected |
42
|
|
|
$parser->expects($this->exactly($rowsCount)) |
|
|
|
|
43
|
|
|
->method('parseLine') |
44
|
|
|
->willReturn($expectedData); |
45
|
|
|
|
46
|
|
|
$iterator = new LogIterator($logfile, $parser); |
|
|
|
|
47
|
|
|
|
48
|
|
|
foreach ($iterator as $line => $data) { |
49
|
|
|
$this->assertTrue(is_string($line)); |
50
|
|
|
$this->assertEquals($data, $expectedData); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Test for iterator in case of empty lines in log. |
56
|
|
|
*/ |
57
|
|
|
public function testIteratorWithEmptyLines() |
58
|
|
|
{ |
59
|
|
|
$parser = $this->getParser(); |
60
|
|
|
|
61
|
|
|
$parser->expects($this->exactly(3)) |
|
|
|
|
62
|
|
|
->method('parseLine') |
63
|
|
|
->will( |
64
|
|
|
$this->onConsecutiveCalls( |
65
|
|
|
$this->returnValue('parsed_line'), |
66
|
|
|
$this->throwException(new NoMatchesException()), |
|
|
|
|
67
|
|
|
$this->returnValue('parsed_line') |
68
|
|
|
) |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
$iterator = new LogIterator(__DIR__ . '/Fixtures/access.log', $parser, false); |
|
|
|
|
72
|
|
|
|
73
|
|
|
$result = []; |
74
|
|
|
foreach ($iterator as $data) { |
75
|
|
|
$result[] = $data; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
// Test if empty line was not parsed (NULL) |
79
|
|
|
$expectedResult = ['parsed_line', null, 'parsed_line']; |
80
|
|
|
|
81
|
|
|
$this->assertEquals($expectedResult, $result); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Test for iterator in case of file handler exception. |
86
|
|
|
* |
87
|
|
|
* @expectedException \MVar\Apache2LogParser\Exception\ParserException |
88
|
|
|
* @expectedExceptionMessage Can not open log file |
89
|
|
|
*/ |
90
|
|
|
public function testIteratorFileException() |
91
|
|
|
{ |
92
|
|
|
$iterator = new LogIterator(__DIR__ . '/Fixtures/non_existing_file.log', $this->getParser()); |
|
|
|
|
93
|
|
|
$iterator->rewind(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Data provider for testIterator(). |
98
|
|
|
* |
99
|
|
|
* @return array[] |
100
|
|
|
*/ |
101
|
|
|
public function getTestIteratorData() |
102
|
|
|
{ |
103
|
|
|
$data = []; |
104
|
|
|
|
105
|
|
|
// Simple log |
106
|
|
|
$data[] = [__DIR__ . '/Fixtures/access.log', 2]; |
107
|
|
|
|
108
|
|
|
// Compressed log |
109
|
|
|
$data[] = ['compress.zlib://file://' . __DIR__ . '/Fixtures/access_compressed.gz', 4]; |
110
|
|
|
|
111
|
|
|
return $data; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: