|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the NatsBundle package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Issel Guberna <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Octante\NatsBundle\Tests\Unit\DataCollector; |
|
12
|
|
|
|
|
13
|
|
|
use Octante\NatsBundle\DataCollector\NatsDataCollector; |
|
14
|
|
|
|
|
15
|
|
|
class NatsDataCollectorTest extends \PHPUnit_Framework_TestCase |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* when: collectIsCalled |
|
19
|
|
|
* should: returnDataWithCommandsArrayKey |
|
20
|
|
|
*/ |
|
21
|
|
|
public function test_collectIsCalled__returnDataWithCommandsArrayKey() |
|
22
|
|
|
{ |
|
23
|
|
|
$natsLoggerMock = $this->getMock('Octante\NatsBundle\Logger\NatsLogger'); |
|
24
|
|
|
$requestMock = $this->getMock('Symfony\Component\HttpFoundation\Request'); |
|
25
|
|
|
$responseMock = $this->getMock('Symfony\Component\HttpFoundation\Response'); |
|
26
|
|
|
|
|
27
|
|
|
$sut = new NatsDataCollector($natsLoggerMock); |
|
28
|
|
|
$sut->collect($requestMock, $responseMock); |
|
29
|
|
|
$this->assertNull($sut->getCommands()); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* when: callGetCommandCount |
|
34
|
|
|
* with: twoCommands |
|
35
|
|
|
* should: returnTwo |
|
36
|
|
|
*/ |
|
37
|
|
View Code Duplication |
public function test_callGetCommandCount_twoCommands_returnTwo() |
|
|
|
|
|
|
38
|
|
|
{ |
|
39
|
|
|
$natsLoggerMock = $this->getMockBuilder('Octante\NatsBundle\Logger\NatsLogger') |
|
40
|
|
|
->disableOriginalConstructor() |
|
41
|
|
|
->getMock(); |
|
42
|
|
|
|
|
43
|
|
|
$natsLoggerMock->expects($this->once()) |
|
44
|
|
|
->method('getCommands') |
|
45
|
|
|
->willReturn(array(1,2)); |
|
46
|
|
|
|
|
47
|
|
|
$requestMock = $this->getMock('Symfony\Component\HttpFoundation\Request'); |
|
48
|
|
|
$responseMock = $this->getMock('Symfony\Component\HttpFoundation\Response'); |
|
49
|
|
|
|
|
50
|
|
|
$sut = new NatsDataCollector($natsLoggerMock); |
|
51
|
|
|
$sut->collect($requestMock, $responseMock); |
|
52
|
|
|
$this->assertEquals(2, $sut->getCommandCount()); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* when: callGetErrorCommandsCount |
|
57
|
|
|
* with: aLoggerWithAnError |
|
58
|
|
|
* should: returnOne |
|
59
|
|
|
*/ |
|
60
|
|
View Code Duplication |
public function test_callGetErrorCommandsCount_aLoggerWithAnError_returnOne() |
|
|
|
|
|
|
61
|
|
|
{ |
|
62
|
|
|
$natsLoggerMock = $this->getMockBuilder('Octante\NatsBundle\Logger\NatsLogger') |
|
63
|
|
|
->disableOriginalConstructor() |
|
64
|
|
|
->getMock(); |
|
65
|
|
|
|
|
66
|
|
|
$natsLoggerMock->expects($this->once()) |
|
67
|
|
|
->method('getCommands') |
|
68
|
|
|
->willReturn( |
|
69
|
|
|
array( |
|
70
|
|
|
'commands' => array('error' => null) |
|
71
|
|
|
) |
|
72
|
|
|
); |
|
73
|
|
|
|
|
74
|
|
|
$requestMock = $this->getMock('Symfony\Component\HttpFoundation\Request'); |
|
75
|
|
|
$responseMock = $this->getMock('Symfony\Component\HttpFoundation\Response'); |
|
76
|
|
|
|
|
77
|
|
|
$sut = new NatsDataCollector($natsLoggerMock); |
|
78
|
|
|
$sut->collect($requestMock, $responseMock); |
|
79
|
|
|
$this->assertEquals(1, $sut->getErrorCommandsCount()); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* when: getTimeIsCalled |
|
84
|
|
|
* with: oneCommandInLogger |
|
85
|
|
|
* should: returnTime |
|
86
|
|
|
*/ |
|
87
|
|
View Code Duplication |
public function test_getTimeIsCalled_oneCommandInLogger_returnTime() |
|
|
|
|
|
|
88
|
|
|
{ |
|
89
|
|
|
$natsLoggerMock = $this->getMockBuilder('Octante\NatsBundle\Logger\NatsLogger') |
|
90
|
|
|
->disableOriginalConstructor() |
|
91
|
|
|
->getMock(); |
|
92
|
|
|
|
|
93
|
|
|
$natsLoggerMock->expects($this->once()) |
|
94
|
|
|
->method('getCommands') |
|
95
|
|
|
->willReturn( |
|
96
|
|
|
array( |
|
97
|
|
|
'commands' => array('executionMS' => 1000) |
|
98
|
|
|
) |
|
99
|
|
|
); |
|
100
|
|
|
|
|
101
|
|
|
$requestMock = $this->getMock('Symfony\Component\HttpFoundation\Request'); |
|
102
|
|
|
$responseMock = $this->getMock('Symfony\Component\HttpFoundation\Response'); |
|
103
|
|
|
|
|
104
|
|
|
$sut = new NatsDataCollector($natsLoggerMock); |
|
105
|
|
|
$sut->collect($requestMock, $responseMock); |
|
106
|
|
|
$this->assertEquals(1000, $sut->getTime()); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.