1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the NatsBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Issel Guberna <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Octante\NatsBundle\Tests\Unit\Logger; |
13
|
|
|
|
14
|
|
|
use Octante\NatsBundle\Logger\NatsLogger; |
15
|
|
|
|
16
|
|
|
class NatsLoggerTest extends \PHPUnit_Framework_TestCase |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* when: callLogCommand |
20
|
|
|
* with: natsMessage |
21
|
|
|
* should: sendMessageToLogger |
22
|
|
|
*/ |
23
|
|
View Code Duplication |
public function test_callLogCommand_natsMessage_sendMessageToLogger() |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
$loggerMock = $this->getMockBuilder('Monolog\Logger') |
26
|
|
|
->disableOriginalConstructor() |
27
|
|
|
->setMethods(array('info')) |
28
|
|
|
->getMock(); |
29
|
|
|
|
30
|
|
|
$loggerMock->expects($this->once()) |
31
|
|
|
->method('info') |
32
|
|
|
->with('Executing "PUBLISH: test"'); |
33
|
|
|
|
34
|
|
|
$msg = 'PUBLISH: test'; |
35
|
|
|
$duration = 0.5; |
36
|
|
|
$connection = $this->getMockBuilder('Octante\NatsBundle\Connection\ConnectionWrapper') |
37
|
|
|
->disableOriginalConstructor(); |
38
|
|
|
$sut = new NatsLogger($loggerMock); |
39
|
|
|
$sut->logCommand($msg, $duration, $connection); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* when: callLogCommand |
44
|
|
|
* with: natsMessage |
45
|
|
|
* should: sendErrorToLogger |
46
|
|
|
*/ |
47
|
|
View Code Duplication |
public function test_callLogCommand_natsMessage_sendErrorToLogger() |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
$loggerMock = $this->getMockBuilder('Monolog\Logger') |
50
|
|
|
->disableOriginalConstructor() |
51
|
|
|
->setMethods(array('error')) |
52
|
|
|
->getMock(); |
53
|
|
|
|
54
|
|
|
$loggerMock->expects($this->once()) |
55
|
|
|
->method('error') |
56
|
|
|
->with('Executing "PUBLISH: test" failed (1)'); |
57
|
|
|
|
58
|
|
|
$msg = 'PUBLISH: test'; |
59
|
|
|
$duration = 0.5; |
60
|
|
|
$connection = $this->getMockBuilder('Octante\NatsBundle\Connection\ConnectionWrapper') |
61
|
|
|
->disableOriginalConstructor(); |
62
|
|
|
$sut = new NatsLogger($loggerMock); |
63
|
|
|
$sut->logCommand($msg, $duration, $connection, true); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* when: callGetNbCommand |
68
|
|
|
* with: callingTwoCommands |
69
|
|
|
* should: returnValueTwo |
70
|
|
|
*/ |
71
|
|
|
public function test_callGetNbCommand_callingTwoCommands_returnValueTwo() |
72
|
|
|
{ |
73
|
|
|
$loggerMock = $this->getMockBuilder('Monolog\Logger') |
74
|
|
|
->disableOriginalConstructor() |
75
|
|
|
->getMock(); |
76
|
|
|
|
77
|
|
|
$msg = 'PUBLISH: test'; |
78
|
|
|
$duration = 0.5; |
79
|
|
|
$connection = $this->getMockBuilder('Octante\NatsBundle\Connection\ConnectionWrapper') |
80
|
|
|
->disableOriginalConstructor(); |
81
|
|
|
$sut = new NatsLogger($loggerMock); |
82
|
|
|
$sut->logCommand($msg, $duration, $connection, false); |
83
|
|
|
$sut->logCommand($msg, $duration, $connection, false); |
84
|
|
|
|
85
|
|
|
$this->assertEquals(2, $sut->getNbCommands()); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
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.