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 |
||
2 | class DMSTest extends FunctionalTest |
||
3 | { |
||
4 | protected static $fixture_file = 'dmstest.yml'; |
||
5 | |||
6 | /** |
||
7 | * Stub PDF files for testing |
||
8 | * |
||
9 | * @var string |
||
10 | */ |
||
11 | public static $testFile = 'dms/tests/DMS-test-lorum-file.pdf'; |
||
12 | public static $testFile2 = 'dms/tests/DMS-test-document-2.pdf'; |
||
13 | |||
14 | /** |
||
15 | * The test folder to write assets into |
||
16 | * |
||
17 | * @var string |
||
18 | */ |
||
19 | protected $testDmsPath = 'assets/_dms-assets-test-1234'; |
||
20 | |||
21 | /** |
||
22 | * @var DMSInterace |
||
23 | */ |
||
24 | protected $dms; |
||
25 | |||
26 | /** |
||
27 | * Use a test DMS folder, so we don't overwrite the live one, and clear it out in case of previous broken tests |
||
28 | * |
||
29 | * {@inheritDoc} |
||
30 | */ |
||
31 | public function setUp() |
||
32 | { |
||
33 | parent::setUp(); |
||
34 | Config::inst()->update('DMS', 'folder_name', $this->testDmsPath); |
||
35 | DMSFilesystemTestHelper::delete($this->testDmsPath); |
||
36 | $this->dms = DMS::inst(); |
||
|
|||
37 | } |
||
38 | |||
39 | /** |
||
40 | * Delete the test folder after the test runs |
||
41 | * |
||
42 | * {@inheritDoc} |
||
43 | */ |
||
44 | public function tearDown() |
||
45 | { |
||
46 | parent::tearDown(); |
||
47 | DMSFilesystemTestHelper::delete($this->testDmsPath); |
||
48 | } |
||
49 | |||
50 | public function testDMSStorage() |
||
51 | { |
||
52 | $file = self::$testFile; |
||
53 | $document = $this->dms->storeDocument($file); |
||
54 | |||
55 | $this->assertNotNull($document, "Document object created"); |
||
56 | $this->assertTrue( |
||
57 | file_exists( |
||
58 | DMS::inst()->getStoragePath() . DIRECTORY_SEPARATOR . $document->Folder |
||
59 | . DIRECTORY_SEPARATOR . $document->Filename |
||
60 | ), |
||
61 | "Document file copied into DMS folder" |
||
62 | ); |
||
63 | } |
||
64 | |||
65 | public function testDMSFolderSpanning() |
||
66 | { |
||
67 | Config::inst()->update('DMS', 'folder_size', 5); |
||
68 | $file = self::$testFile; |
||
69 | |||
70 | $documents = array(); |
||
71 | for ($i = 0; $i <= 16; $i++) { |
||
72 | $document = $this->dms->storeDocument($file); |
||
73 | $this->assertNotNull($document, "Document object created on run number: $i"); |
||
74 | $this->assertTrue(file_exists($document->getFullPath())); |
||
75 | $documents[] = $document; |
||
76 | } |
||
77 | |||
78 | // Test document objects have their folders set |
||
79 | $folders = array(); |
||
80 | for ($i = 0; $i <= 16; $i++) { |
||
81 | $folderName = $documents[$i]->Folder; |
||
82 | $this->assertTrue( |
||
83 | strpos($documents[$i]->getFullPath(), DIRECTORY_SEPARATOR . $folderName . DIRECTORY_SEPARATOR) !== false, |
||
84 | "Correct folder name for the documents. Document path contains reference to folder name '$folderName'" |
||
85 | ); |
||
86 | $folders[] = $folderName; |
||
87 | } |
||
88 | |||
89 | // Test we created 4 folder to contain the 17 files |
||
90 | foreach ($folders as $f) { |
||
91 | $this->assertTrue( |
||
92 | is_dir(DMS::inst()->getStoragePath() . DIRECTORY_SEPARATOR . $f), |
||
93 | "Document folder '$f' exists" |
||
94 | ); |
||
95 | } |
||
96 | } |
||
97 | |||
98 | public function testReplaceDocument() |
||
99 | { |
||
100 | // Store the first document |
||
101 | $document = $this->dms->storeDocument(self::$testFile); |
||
102 | $document->Title = "My custom title"; |
||
103 | $document->Description = "My custom description"; |
||
104 | $document->write(); |
||
105 | |||
106 | // Then overwrite with a second document |
||
107 | $document = $document->replaceDocument(self::$testFile2); |
||
108 | |||
109 | $this->assertNotNull($document, "Document object created"); |
||
110 | $this->assertTrue( |
||
111 | file_exists( |
||
112 | DMS::inst()->getStoragePath() . DIRECTORY_SEPARATOR . $document->Folder |
||
113 | . DIRECTORY_SEPARATOR . $document->Filename |
||
114 | ), |
||
115 | "Document file copied into DMS folder" |
||
116 | ); |
||
117 | $this->assertContains( |
||
118 | "DMS-test-document-2", |
||
119 | $document->Filename, |
||
120 | "Original document filename is contain in the new filename" |
||
121 | ); |
||
122 | $this->assertEquals("My custom title", $document->Title, "Custom title not modified"); |
||
123 | $this->assertEquals("My custom description", $document->Description, "Custom description not modified"); |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * Test that documents can be returned by a given page |
||
128 | */ |
||
129 | View Code Duplication | public function testGetByPageWithoutEmbargoes() |
|
137 | |||
138 | /** |
||
139 | * Test that embargoed documents are excluded from getByPage |
||
140 | */ |
||
141 | View Code Duplication | public function testGetByPageWithEmbargoedDocuments() |
|
149 | |||
150 | /** |
||
151 | * Ensure the shortcode handler key is configurable |
||
152 | */ |
||
153 | public function testShortcodeHandlerKeyIsConfigurable() |
||
158 | |||
159 | /** |
||
160 | * Test that document sets can be retrieved for a given page |
||
161 | */ |
||
162 | public function testGetDocumentSetsByPage() |
||
169 | |||
170 | /** |
||
171 | * Ensure that assets/* folders are not included in filesystem sync operations |
||
172 | */ |
||
173 | public function testFolderExcludedFromFilesystemSync() |
||
183 | } |
||
184 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..