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 |
||
27 | class PageControllerTest extends TestCase { |
||
28 | |||
29 | private $appName; |
||
30 | private $request; |
||
31 | private $userId; |
||
32 | private $mailAccountMapper; |
||
33 | private $urlGenerator; |
||
34 | private $config; |
||
35 | private $controller; |
||
36 | private $accountService; |
||
37 | private $aliasesService; |
||
38 | private $userSession; |
||
39 | |||
40 | protected function setUp() { |
||
41 | parent::setUp(); |
||
42 | |||
43 | $this->appName = 'mail'; |
||
44 | $this->userId = 'george'; |
||
45 | $this->request = $this->getMockBuilder('\OCP\IRequest')->getMock(); |
||
46 | $this->urlGenerator = $this->getMockBuilder('OCP\IURLGenerator')->getMock(); |
||
47 | $this->config = $this->getMockBuilder('OCP\IConfig')->getMock(); |
||
48 | $this->accountService = $this->getMockBuilder('OCA\Mail\Service\AccountService') |
||
49 | ->disableOriginalConstructor() |
||
50 | ->getMock(); |
||
51 | $this->aliasesService = $this->getMockBuilder('\OCA\Mail\Service\AliasesService') |
||
52 | ->disableOriginalConstructor() |
||
53 | ->getMock(); |
||
54 | $this->userSession = $this->getMockBuilder('OCP\IUserSession')->getMock(); |
||
55 | $this->controller = new PageController($this->appName, $this->request, |
||
56 | $this->urlGenerator, $this->config, $this->accountService, |
||
57 | $this->aliasesService, $this->userId, $this->userSession); |
||
58 | } |
||
59 | |||
60 | public function testIndex() { |
||
61 | $account1 = $this->getMock('OCA\Mail\Service\IAccount'); |
||
62 | $account2 = $this->getMock('OCA\Mail\Service\IAccount'); |
||
63 | |||
64 | $this->accountService->expects($this->once()) |
||
65 | ->method('findByUserId') |
||
66 | ->with($this->userId) |
||
67 | ->will($this->returnValue([ |
||
68 | $account1, |
||
69 | $account2, |
||
70 | ])); |
||
71 | $account1->expects($this->once()) |
||
72 | ->method('getConfiguration') |
||
73 | ->will($this->returnValue([ |
||
74 | 'accountId' => 1, |
||
75 | ])); |
||
76 | $account2->expects($this->once()) |
||
77 | ->method('getConfiguration') |
||
78 | ->will($this->returnValue([ |
||
79 | 'accountId' => 2, |
||
80 | ])); |
||
81 | $this->aliasesService->expects($this->exactly(2)) |
||
82 | ->method('findAll') |
||
83 | ->will($this->returnValueMap([ |
||
84 | [1, $this->userId, ['a11', 'a12']], |
||
85 | [2, $this->userId, ['a21', 'a22']], |
||
86 | ])); |
||
87 | $accountsJson = [ |
||
88 | [ |
||
89 | 'accountId' => 1, |
||
90 | 'aliases' => [ |
||
91 | 'a11', |
||
92 | 'a12', |
||
93 | ] |
||
94 | ], |
||
95 | [ |
||
96 | 'accountId' => 2, |
||
97 | 'aliases' => [ |
||
98 | 'a21', |
||
99 | 'a22', |
||
100 | ] |
||
101 | ], |
||
102 | ]; |
||
103 | |||
104 | $user = $this->getMockBuilder('\OCP\IUser')->getMock(); |
||
105 | $this->userSession->expects($this->once()) |
||
106 | ->method('getUser') |
||
107 | ->will($this->returnValue($user)); |
||
108 | $this->config->expects($this->once()) |
||
109 | ->method('getSystemValue') |
||
110 | ->with('debug', false) |
||
111 | ->will($this->returnValue(true)); |
||
112 | $this->config->expects($this->once()) |
||
113 | ->method('getAppValue') |
||
114 | ->with('mail', 'installed_version') |
||
115 | ->will($this->returnValue('1.2.3')); |
||
116 | $user->expects($this->once()) |
||
117 | ->method('getDisplayName') |
||
118 | ->will($this->returnValue('Jane Doe')); |
||
119 | $user->expects($this->once()) |
||
120 | ->method('getUID') |
||
121 | ->will($this->returnValue('jane')); |
||
122 | $this->config->expects($this->once()) |
||
123 | ->method('getUserValue') |
||
124 | ->with($this->equalTo('jane'), $this->equalTo('settings'), $this->equalTo('email'), $this->equalTo('')) |
||
125 | ->will($this->returnValue('[email protected]')); |
||
126 | |||
127 | $expected = new TemplateResponse($this->appName, 'index', [ |
||
128 | 'debug' => true, |
||
129 | 'app-version' => '1.2.3', |
||
130 | 'accounts' => base64_encode(json_encode($accountsJson)), |
||
131 | 'prefill_displayName' => 'Jane Doe', |
||
132 | 'prefill_email' => '[email protected]', |
||
133 | ]); |
||
134 | // set csp rules for ownCloud 8.1 |
||
135 | if (class_exists('OCP\AppFramework\Http\ContentSecurityPolicy')) { |
||
136 | $csp = new ContentSecurityPolicy(); |
||
137 | $csp->addAllowedFrameDomain('\'self\''); |
||
138 | $expected->setContentSecurityPolicy($csp); |
||
139 | } |
||
140 | |||
141 | $response = $this->controller->index(); |
||
142 | |||
143 | $this->assertEquals($expected, $response); |
||
144 | } |
||
145 | |||
146 | public function testComposeSimple() { |
||
147 | $address = '[email protected]'; |
||
148 | $uri = "mailto:$address"; |
||
149 | |||
150 | $expected = new RedirectResponse('#mailto?to=' . urlencode($address)); |
||
151 | |||
152 | $response = $this->controller->compose($uri); |
||
153 | |||
154 | $this->assertEquals($expected, $response); |
||
155 | } |
||
156 | |||
157 | public function testComposeWithSubject() { |
||
158 | $address = '[email protected]'; |
||
159 | $subject = 'hello there'; |
||
160 | $uri = "mailto:$address?subject=$subject"; |
||
161 | |||
162 | $expected = new RedirectResponse('#mailto?to=' . urlencode($address) |
||
163 | . '&subject=' . urlencode($subject)); |
||
164 | |||
165 | $response = $this->controller->compose($uri); |
||
166 | |||
167 | $this->assertEquals($expected, $response); |
||
168 | } |
||
169 | |||
170 | public function testComposeWithCc() { |
||
171 | $address = '[email protected]'; |
||
172 | $cc = '[email protected]'; |
||
173 | $uri = "mailto:$address?cc=$cc"; |
||
174 | |||
175 | $expected = new RedirectResponse('#mailto?to=' . urlencode($address) |
||
176 | . '&cc=' . urlencode($cc)); |
||
177 | |||
178 | $response = $this->controller->compose($uri); |
||
179 | |||
180 | $this->assertEquals($expected, $response); |
||
181 | } |
||
182 | |||
183 | public function testComposeWithBcc() { |
||
184 | $address = '[email protected]'; |
||
185 | $bcc = '[email protected]'; |
||
186 | $uri = "mailto:$address?bcc=$bcc"; |
||
187 | |||
188 | $expected = new RedirectResponse('#mailto?to=' . urlencode($address) |
||
189 | . '&bcc=' . urlencode($bcc)); |
||
190 | |||
191 | $response = $this->controller->compose($uri); |
||
192 | |||
193 | $this->assertEquals($expected, $response); |
||
194 | } |
||
195 | |||
196 | public function testComposeWithMultilineBody() { |
||
197 | $address = '[email protected]'; |
||
198 | $body = 'Hi!\nWhat\'s up?\nAnother line'; |
||
199 | $uri = "mailto:$address?body=$body"; |
||
200 | |||
201 | $expected = new RedirectResponse('#mailto?to=' . urlencode($address) |
||
202 | . '&body=' . urlencode($body)); |
||
203 | |||
204 | $response = $this->controller->compose($uri); |
||
205 | |||
206 | $this->assertEquals($expected, $response); |
||
207 | } |
||
208 | |||
209 | } |
||
210 |