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()) |
|
|
|
|
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
|
|
|
|
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: