Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
25 | class TellCommandTest extends BaseCommandTestCase |
||
26 | { |
||
27 | /** |
||
28 | * |
||
29 | */ |
||
30 | public function tearDown() |
||
31 | { |
||
32 | DriverManager::getConnection($GLOBALS['config']['db'], new Configuration()) |
||
33 | ->delete('notifications', [ |
||
34 | 'to_user' => 'U12345' |
||
35 | ]); |
||
36 | parent::tearDown(); |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * @return array |
||
41 | */ |
||
42 | public function tellDataProvider() : array |
||
43 | { |
||
44 | return [ |
||
45 | 'tell <@U12345> about review:47640' => ['tell <@U12345> about review:47640', 'OK, I will tell <@U12345> about your message'], |
||
46 | 'tell <@U12345> about forge:23456' => ['tell <@U12345> about forge:23456', 'OK, I will tell <@U12345> about your message'], |
||
47 | 'tell <@U12345> you are so nice' => ['tell <@U12345> you are so nice', 'OK, I will tell <@U12345> about your message'], |
||
48 | ]; |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * @test |
||
53 | * @dataProvider tellDataProvider |
||
54 | * |
||
55 | * @param string $message |
||
56 | * @param string $expectedMessage |
||
57 | */ |
||
58 | public function processTellReturnsCorrectResponse($message, $expectedMessage) |
||
59 | { |
||
60 | $this->initCommandWithPayload(TellCommand::class, [ |
||
61 | 'user' => 'U54321', |
||
62 | 'text' => $message, |
||
63 | ]); |
||
64 | $result = $this->command->process(); |
||
65 | static::assertEquals($expectedMessage, $result); |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * @test |
||
70 | */ |
||
71 | public function processPresenceChangeReturnsCorrectResponseForPresenceAway() |
||
72 | { |
||
73 | $loop = $this->getMock(LoopInterface::class); |
||
74 | /** @var Payload $payload */ |
||
75 | $payload = new Payload([ |
||
76 | 'user' => 'U12345', |
||
77 | 'presence' => 'away', |
||
78 | ]); |
||
79 | /** @var RealTimeClient $client */ |
||
80 | $client = $this->getMock(RealTimeClient::class, [], [$loop]); |
||
81 | /** @var TellCommand|\PHPUnit_Framework_MockObject_MockObject $command */ |
||
82 | $command = $this->getMock(TellCommand::class, ['sendResponse'], [$payload, $client]); |
||
83 | $command->expects(static::exactly(0)) |
||
84 | ->method('sendResponse'); |
||
85 | $command->processPresenceChange('U12345', 'away'); |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * @test |
||
90 | * @dataProvider tellDataProvider |
||
91 | */ |
||
92 | public function processPresenceChangeReturnsCorrectResponseForPresenceActive($message) |
||
93 | { |
||
94 | $this->initCommandWithPayload(TellCommand::class, [ |
||
95 | 'user' => 'U54321', |
||
96 | 'text' => $message, |
||
97 | ]); |
||
98 | $this->command->process(); |
||
99 | |||
100 | $loop = $this->getMock(LoopInterface::class); |
||
101 | $payload = new Payload([ |
||
102 | 'user' => 'U12345', |
||
103 | 'presence' => 'active', |
||
104 | ]); |
||
105 | $client = $this->getMock(RealTimeClient::class, [], [$loop]); |
||
106 | /** @var TellCommand|\PHPUnit_Framework_MockObject_MockObject $command */ |
||
107 | $command = $this->getMock(TellCommand::class, ['sendResponse'], [$payload, $client]); |
||
108 | $command |
||
109 | ->expects(static::exactly(1)) |
||
110 | ->method('sendResponse'); |
||
111 | $command->processPresenceChange('U12345', 'active'); |
||
112 | } |
||
113 | } |
||
114 |