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 |
||
22 | class BeerCommandTest extends BaseCommandTestCase |
||
23 | { |
||
24 | /** |
||
25 | * |
||
26 | */ |
||
27 | public function tearDown() |
||
28 | { |
||
29 | DriverManager::getConnection($GLOBALS['config']['db'], new Configuration()) |
||
30 | ->delete('beers', [ |
||
31 | 'to_user' => 'U23456', |
||
32 | 'from_user' => 'U12345', |
||
33 | ]); |
||
34 | parent::tearDown(); |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * @test |
||
39 | */ |
||
40 | public function processForReturnsCorrectResponseForWrongUsername() |
||
41 | { |
||
42 | $this->initCommandWithPayload(BeerCommand::class, [ |
||
43 | 'user' => 'U54321', |
||
44 | 'text' => 'beer:for max', |
||
45 | ]); |
||
46 | $result = $this->command->process(); |
||
47 | static::assertEquals('*Sorry, a username must start with a @-sign:*', $result); |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * @test |
||
52 | */ |
||
53 | public function processForReturnsCorrectResponseForCorrectUsername() |
||
54 | { |
||
55 | $this->initCommandWithPayload(BeerCommand::class, [ |
||
56 | 'user' => 'U12345', |
||
57 | 'text' => 'beer:for <@U23456>', |
||
58 | ]); |
||
59 | $result = $this->command->process(); |
||
60 | static::assertStringStartsWith('Yeah, one more :t3beer: for <@U23456>', $result); |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * @test |
||
65 | */ |
||
66 | public function processForReturnsCorrectResponseForCorrectUsernameWithin24Hours() |
||
67 | { |
||
68 | $this->initCommandWithPayload(BeerCommand::class, [ |
||
69 | 'user' => 'U12345', |
||
70 | 'text' => 'beer:for <@U23456>', |
||
71 | ]); |
||
72 | $this->command->process(); |
||
73 | $result = $this->command->process(); |
||
74 | static::assertStringStartsWith('You spend one :t3beer: to <@U23456> within in last 24 hours. Too much beer is unhealthy ;)', $result); |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * @test |
||
79 | */ |
||
80 | public function processAllReturnsCorrectResponse() |
||
81 | { |
||
82 | $this->initCommandWithPayload(BeerCommand::class, [ |
||
83 | 'user' => 'U12345', |
||
84 | 'text' => 'beer:all', |
||
85 | ]); |
||
86 | $result = $this->command->process(); |
||
87 | static::assertRegExp('/Yeah, ([0-9]*) :t3beer: spend to all people/', $result); |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * @test |
||
92 | */ |
||
93 | public function processTop10ReturnsCorrectResponse() |
||
94 | { |
||
95 | $this->initCommandWithPayload(BeerCommand::class, [ |
||
96 | 'user' => 'U12345', |
||
97 | 'text' => 'beer:top10', |
||
98 | ]); |
||
99 | $result = $this->command->process(); |
||
100 | static::assertStringStartsWith('*Yeah, here are the TOP 10*', $result); |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * @test |
||
105 | */ |
||
106 | public function processStatsReturnsCorrectResponseForCorrectUsername() |
||
107 | { |
||
108 | $this->initCommandWithPayload(BeerCommand::class, [ |
||
109 | 'user' => 'U12345', |
||
110 | 'text' => 'beer:stats <@U23456>', |
||
111 | ]); |
||
112 | $result = $this->command->process(); |
||
113 | static::assertRegExp('/<@U23456> has received ([0-9]*) :t3beer: so far/', $result); |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * @test |
||
118 | */ |
||
119 | public function processStatsReturnsCorrectResponseForinvalidUsername() |
||
120 | { |
||
121 | $this->initCommandWithPayload(BeerCommand::class, [ |
||
122 | 'user' => 'U12345', |
||
123 | 'text' => 'beer:stats foo', |
||
124 | ]); |
||
125 | $result = $this->command->process(); |
||
126 | static::assertEquals('*Sorry, a username must start with a @-sign:*', $result); |
||
127 | } |
||
128 | } |
||
129 |