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 |
||
29 | class EncryptionTest extends TestCase { |
||
30 | |||
31 | /** @var Encryption */ |
||
32 | private $instance; |
||
33 | |||
34 | /** @var \PHPUnit_Framework_MockObject_MockObject */ |
||
35 | private $keyManagerMock; |
||
36 | |||
37 | /** @var \PHPUnit_Framework_MockObject_MockObject */ |
||
38 | private $encryptAllMock; |
||
39 | |||
40 | /** @var \PHPUnit_Framework_MockObject_MockObject */ |
||
41 | private $decryptAllMock; |
||
42 | |||
43 | /** @var \PHPUnit_Framework_MockObject_MockObject */ |
||
44 | private $sessionMock; |
||
45 | |||
46 | /** @var \PHPUnit_Framework_MockObject_MockObject */ |
||
47 | private $cryptMock; |
||
48 | |||
49 | /** @var \PHPUnit_Framework_MockObject_MockObject */ |
||
50 | private $utilMock; |
||
51 | |||
52 | /** @var \PHPUnit_Framework_MockObject_MockObject */ |
||
53 | private $loggerMock; |
||
54 | |||
55 | /** @var \PHPUnit_Framework_MockObject_MockObject */ |
||
56 | private $l10nMock; |
||
57 | |||
58 | 23 | public function setUp() { |
|
59 | 23 | parent::setUp(); |
|
60 | |||
61 | 23 | $this->cryptMock = $this->getMockBuilder('OCA\Encryption\Crypto\Crypt') |
|
62 | 23 | ->disableOriginalConstructor() |
|
63 | 23 | ->getMock(); |
|
64 | 23 | $this->utilMock = $this->getMockBuilder('OCA\Encryption\Util') |
|
65 | 23 | ->disableOriginalConstructor() |
|
66 | 23 | ->getMock(); |
|
67 | 23 | $this->keyManagerMock = $this->getMockBuilder('OCA\Encryption\KeyManager') |
|
68 | 23 | ->disableOriginalConstructor() |
|
69 | 23 | ->getMock(); |
|
70 | 23 | $this->sessionMock = $this->getMockBuilder('OCA\Encryption\Session') |
|
71 | 23 | ->disableOriginalConstructor() |
|
72 | 23 | ->getMock(); |
|
73 | 23 | $this->encryptAllMock = $this->getMockBuilder('OCA\Encryption\Crypto\EncryptAll') |
|
74 | 23 | ->disableOriginalConstructor() |
|
75 | 23 | ->getMock(); |
|
76 | 23 | $this->decryptAllMock = $this->getMockBuilder('OCA\Encryption\Crypto\DecryptAll') |
|
77 | 23 | ->disableOriginalConstructor() |
|
78 | 23 | ->getMock(); |
|
79 | 23 | $this->loggerMock = $this->getMockBuilder('OCP\ILogger') |
|
80 | 23 | ->disableOriginalConstructor() |
|
81 | 23 | ->getMock(); |
|
82 | 23 | $this->l10nMock = $this->getMockBuilder('OCP\IL10N') |
|
83 | 23 | ->disableOriginalConstructor() |
|
84 | 23 | ->getMock(); |
|
85 | 23 | $this->l10nMock->expects($this->any()) |
|
86 | 23 | ->method('t') |
|
87 | 23 | ->with($this->anything()) |
|
88 | 23 | ->willReturnArgument(0); |
|
89 | |||
90 | 23 | $this->instance = new Encryption( |
|
91 | 23 | $this->cryptMock, |
|
92 | 23 | $this->keyManagerMock, |
|
93 | 23 | $this->utilMock, |
|
94 | 23 | $this->sessionMock, |
|
95 | 23 | $this->encryptAllMock, |
|
96 | 23 | $this->decryptAllMock, |
|
97 | 23 | $this->loggerMock, |
|
98 | 23 | $this->l10nMock |
|
99 | 23 | ); |
|
100 | |||
101 | 23 | } |
|
102 | |||
103 | /** |
||
104 | * test if public key from one of the recipients is missing |
||
105 | */ |
||
106 | 1 | public function testEndUser1() { |
|
110 | |||
111 | /** |
||
112 | * test if public key from owner is missing |
||
113 | * |
||
114 | * @expectedException \OCA\Encryption\Exceptions\PublicKeyMissingException |
||
115 | */ |
||
116 | 1 | public function testEndUser2() { |
|
120 | |||
121 | /** |
||
122 | * common part of testEndUser1 and testEndUser2 |
||
123 | * |
||
124 | * @throws PublicKeyMissingException |
||
125 | */ |
||
126 | 2 | public function endTest() { |
|
146 | |||
147 | |||
148 | 2 | public function getPublicKeyCallback($uid) { |
|
154 | |||
155 | 1 | public function addSystemKeysCallback($accessList, $publicKeys) { |
|
161 | |||
162 | /** |
||
163 | * @dataProvider dataProviderForTestGetPathToRealFile |
||
164 | */ |
||
165 | 4 | public function testGetPathToRealFile($path, $expected) { |
|
170 | |||
171 | View Code Duplication | public function dataProviderForTestGetPathToRealFile() { |
|
179 | |||
180 | /** |
||
181 | * @dataProvider dataTestBegin |
||
182 | */ |
||
183 | 4 | public function testBegin($mode, $header, $legacyCipher, $defaultCipher, $fileKey, $expected) { |
|
224 | |||
225 | public function dataTestBegin() { |
||
233 | |||
234 | |||
235 | /** |
||
236 | * test begin() if decryptAll mode was activated |
||
237 | */ |
||
238 | 1 | public function testBeginDecryptAll() { |
|
276 | |||
277 | /** |
||
278 | * @dataProvider dataTestUpdate |
||
279 | * |
||
280 | * @param string $fileKey |
||
281 | * @param boolean $expected |
||
282 | */ |
||
283 | 2 | public function testUpdate($fileKey, $expected) { |
|
300 | 2 | ||
301 | public function dataTestUpdate() { |
||
307 | |||
308 | /** |
||
309 | * Test case if the public key is missing. ownCloud should still encrypt |
||
310 | * the file for the remaining users |
||
311 | */ |
||
312 | public function testUpdateMissingPublicKey() { |
||
344 | 1 | ||
345 | /** |
||
346 | 1 | * by default the encryption module should encrypt regular files, files in |
|
347 | 1 | * files_versions and files in files_trashbin |
|
348 | * |
||
349 | 1 | * @dataProvider dataTestShouldEncrypt |
|
350 | 1 | */ |
|
351 | public function testShouldEncrypt($path, $expected) { |
||
356 | |||
357 | View Code Duplication | public function dataTestShouldEncrypt() { |
|
369 | |||
370 | /** |
||
371 | * @expectedException \OC\Encryption\Exceptions\DecryptionFailedException |
||
372 | * @expectedExceptionMessage Can not decrypt this file, probably this is a shared file. Please ask the file owner to reshare the file with you. |
||
373 | */ |
||
374 | public function testDecrypt() { |
||
377 | |||
378 | public function testPrepareDecryptAll() { |
||
387 | |||
388 | } |
||
389 |