LogIteratorTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 98
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getParser() 0 4 1
A testIterator() 0 17 2
B testIteratorWithEmptyLines() 0 26 2
A testIteratorFileException() 0 5 1
A getTestIteratorData() 0 12 1
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))
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in MVar\Apache2LogParser\LineParserInterface.

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:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
43
            ->method('parseLine')
44
            ->willReturn($expectedData);
45
46
        $iterator = new LogIterator($logfile, $parser);
0 ignored issues
show
Bug introduced by
It seems like $parser defined by $this->getParser() on line 38 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, MVar\LogParser\LogIterator::__construct() does only seem to accept object<MVar\LogParser\LineParserInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Deprecated Code introduced by
The class MVar\Apache2LogParser\LogIterator has been deprecated with message: Will be removed in 3.0. Use \MVar\LogParser\LogIterator instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

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

Loading history...
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))
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in MVar\Apache2LogParser\LineParserInterface.

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:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
62
            ->method('parseLine')
63
            ->will(
64
                $this->onConsecutiveCalls(
65
                    $this->returnValue('parsed_line'),
66
                    $this->throwException(new NoMatchesException()),
0 ignored issues
show
Deprecated Code introduced by
The class MVar\Apache2LogParser\Exception\NoMatchesException has been deprecated with message: Will be removed in 3.0. Use \MVar\LogParser\Exception\MatchException instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

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

Loading history...
67
                    $this->returnValue('parsed_line')
68
                )
69
            );
70
71
        $iterator = new LogIterator(__DIR__ . '/Fixtures/access.log', $parser, false);
0 ignored issues
show
Bug introduced by
It seems like $parser defined by $this->getParser() on line 59 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, MVar\LogParser\LogIterator::__construct() does only seem to accept object<MVar\LogParser\LineParserInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Deprecated Code introduced by
The class MVar\Apache2LogParser\LogIterator has been deprecated with message: Will be removed in 3.0. Use \MVar\LogParser\LogIterator instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

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

Loading history...
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());
0 ignored issues
show
Bug introduced by
It seems like $this->getParser() targeting MVar\Apache2LogParser\Te...eratorTest::getParser() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, MVar\LogParser\LogIterator::__construct() does only seem to accept object<MVar\LogParser\LineParserInterface>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Deprecated Code introduced by
The class MVar\Apache2LogParser\LogIterator has been deprecated with message: Will be removed in 3.0. Use \MVar\LogParser\LogIterator instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

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

Loading history...
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