RequestLogDataCollectorTest::testCollect()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 25
rs 8.8571
cc 1
eloc 18
nc 1
nop 0
1
<?php
2
3
/**
4
 * This file is a part of the Phystrix Bundle.
5
 *
6
 * Copyright 2013-2015 oDesk Corporation. All Rights Reserved.
7
 *
8
 * This file is licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 * http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
namespace Odesk\Bundle\PhystrixBundle\Tests\DataCollector;
21
22
use Odesk\Bundle\PhystrixBundle\DataCollector\RequestLogDataCollector;
23
use Symfony\Component\HttpFoundation\Request;
24
use Symfony\Component\HttpFoundation\Response;
25
26
class RequestLogDataCollectorTest extends \PHPUnit_Framework_TestCase
27
{
28
    public function testCollect()
29
    {
30
        /** @var \PHPUnit_Framework_MockObject_MockObject|\Odesk\Phystrix\RequestLog $requestLogMock */
31
        $requestLogMock = $this->getMockBuilder('Odesk\Phystrix\RequestLog')
32
            ->disableOriginalConstructor()
33
            ->getMock();
34
        $requestLogMock->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Odesk\Phystrix\RequestLog.

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...
35
            ->method('getExecutedCommands')
36
            ->willReturn(array(
37
                    $this->prepareCommandMock('Command1', 234, array('e11', 'e12')),
38
                    $this->prepareCommandMock('Command2', 345, array('e2')),
39
                    $this->prepareCommandMock('Command3', 456, array('e31', 'e32', 'e33')),
40
                ));
41
42
        $collector = new RequestLogDataCollector($requestLogMock);
43
        $collector->collect(new Request(), new Response());
44
45
        $this->assertEquals(array(
46
                array('class' => 'Command1', 'duration' => 234, 'events' => array('e11', 'e12')),
47
                array('class' => 'Command2', 'duration' => 345, 'events' => array('e2')),
48
                array('class' => 'Command3', 'duration' => 456, 'events' => array('e31', 'e32', 'e33')),
49
            ), $collector->getCommands());
50
51
        $this->assertSame('phystrix', $collector->getName());
52
    }
53
54
    /**
55
     * @param string   $name
56
     * @param int      $executionTime
57
     * @param string[] $executionEvents
58
     *
59
     * @return \PHPUnit_Framework_MockObject_MockObject|\Odesk\Phystrix\AbstractCommand
60
     */
61
    private function prepareCommandMock($name, $executionTime, $executionEvents)
62
    {
63
        $commandMock = $this->getMockBuilder('\Odesk\Phystrix\AbstractCommand')
64
            ->disableOriginalConstructor()
65
            ->setMockClassName($name)
66
            ->setMethods(array('getExecutionTimeInMilliseconds', 'getExecutionEvents'))
67
            ->getMockForAbstractClass();
68
        $commandMock->expects($this->once())
69
            ->method('getExecutionTimeInMilliseconds')
70
            ->willReturn($executionTime);
71
        $commandMock->expects($this->once())
72
            ->method('getExecutionEvents')
73
            ->willReturn($executionEvents);
74
75
        return $commandMock;
76
    }
77
}
78