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 |
||
16 | class PartialSubmissionJobTest extends SapphireTest |
||
17 | { |
||
18 | protected static $fixture_file = '../fixtures/submission.yml'; |
||
19 | /** |
||
20 | * @var PartialSubmissionJob |
||
21 | */ |
||
22 | protected $job; |
||
23 | |||
24 | public function testGetTitle() |
||
25 | { |
||
26 | $this->assertEquals('Export partial submissions to Email', $this->job->getTitle()); |
||
27 | } |
||
28 | |||
29 | public function testSetup() |
||
30 | { |
||
31 | $this->job->setup(); |
||
32 | |||
33 | $this->assertInstanceOf(SiteConfig::class, $this->job->getConfig()); |
||
34 | } |
||
35 | |||
36 | public function testProcess() |
||
37 | { |
||
38 | $this->assertTrue(method_exists($this->job, 'process')); |
||
39 | SiteConfigHelper::setupSiteConfig('[email protected]', null, true); |
||
40 | $this->job->setup(); |
||
41 | $this->job->process(); |
||
42 | |||
43 | $this->assertEmailSent('[email protected]'); |
||
44 | $this->assertFileExists('/tmp/Export of TestForm - 2018-01-01 12:00:00.csv'); |
||
45 | } |
||
46 | |||
47 | public function testProcessNoMail() |
||
48 | { |
||
49 | $this->assertTrue(method_exists($this->job, 'process')); |
||
50 | SiteConfigHelper::setupSiteConfig('', null, true); |
||
51 | Security::setCurrentUser(null); |
||
52 | $this->job->setup(); |
||
53 | $this->job->process(); |
||
54 | |||
55 | $messages = $this->job->getMessages(); |
||
56 | foreach ($messages as $message) { |
||
57 | $this->assertContains('Can not process without valid email', $message); |
||
58 | } |
||
59 | } |
||
60 | |||
61 | public function testProcessNotSetup() |
||
62 | { |
||
63 | SiteConfigHelper::setupSiteConfig('', null, false); |
||
64 | Security::setCurrentUser(null); |
||
65 | $this->job->setup(); |
||
66 | $this->job->process(); |
||
67 | |||
68 | $messages = $this->job->getMessages(); |
||
69 | foreach ($messages as $message) { |
||
70 | $this->assertContains('Daily exports are not enabled', $message); |
||
71 | } |
||
72 | } |
||
73 | |||
74 | public function testIsSend() |
||
75 | { |
||
76 | $submission = $this->objFromFixture(PartialFormSubmission::class, 'submission1'); |
||
77 | SiteConfigHelper::setupSiteConfig('[email protected]', null, true); |
||
78 | |||
79 | $this->job->setup(); |
||
80 | $this->job->process(); |
||
81 | |||
82 | $processedSubmission = $submission->get()->byID($submission->ID); |
||
83 | |||
84 | $this->assertTrue((bool)$processedSubmission->IsSend); |
||
85 | } |
||
86 | |||
87 | public function testIsDeleted() |
||
88 | { |
||
89 | $submission = $this->objFromFixture(PartialFormSubmission::class, 'submission1'); |
||
90 | $id = $submission->write(); |
||
91 | SiteConfigHelper::setupSiteConfig('[email protected]', null, true, true); |
||
92 | $this->job->setup(); |
||
93 | $this->job->process(); |
||
94 | $this->job->afterComplete(); |
||
95 | |||
96 | $processedSubmission = $submission->get()->byID($id); |
||
97 | |||
98 | $this->assertNull($processedSubmission); |
||
99 | } |
||
100 | |||
101 | public function testFilesRemoved() |
||
102 | { |
||
103 | SiteConfigHelper::setupSiteConfig('[email protected]', null, true); |
||
104 | $this->job->setup(); |
||
105 | $this->job->process(); |
||
106 | $this->job->afterComplete(); |
||
107 | |||
108 | $this->assertFileNotExists('/tmp/Export of TestForm - 2018-01-01 12:00:00.csv'); |
||
109 | } |
||
110 | |||
111 | public function testNewJobCreated() |
||
112 | { |
||
113 | SiteConfigHelper::setupSiteConfig('[email protected]', null, true); |
||
114 | $this->job->setup(); |
||
115 | $this->job->process(); |
||
116 | $this->job->afterComplete(); |
||
117 | |||
118 | $jobs = QueuedJobDescriptor::get()->filter([ |
||
119 | 'Implementation' => PartialSubmissionJob::class, |
||
120 | 'StartAfter:GreaterThan' => DBDatetime::now()->Format(DBDatetime::ISO_DATETIME) |
||
121 | ]); |
||
122 | |||
123 | $this->assertEquals(1, $jobs->count()); |
||
124 | $this->assertEquals('2018-01-02 00:00:00', $jobs->first()->StartAfter); |
||
125 | } |
||
126 | |||
127 | public function testInvalidEmail() |
||
128 | { |
||
129 | SiteConfigHelper::setupSiteConfig('[email protected], error, non-existing, [email protected]', null, true); |
||
130 | |||
131 | /** @var PartialSubmissionJob $job */ |
||
132 | $job = new PartialSubmissionJob(); |
||
133 | $job->setup(); |
||
134 | $emails = $job->getAddresses(); |
||
135 | |||
136 | $expected = [ |
||
137 | '[email protected]', |
||
138 | '[email protected]' |
||
139 | ]; |
||
140 | $this->assertEquals($expected, $emails); |
||
141 | } |
||
142 | |||
143 | public function testCommaSeparatedUsers() |
||
144 | { |
||
145 | SiteConfigHelper::setupSiteConfig('[email protected], [email protected] , [email protected]', null, true); |
||
146 | |||
147 | $this->job->setup(); |
||
148 | $this->job->process(); |
||
149 | |||
150 | $this->assertEmailSent('[email protected]'); |
||
151 | $this->assertEmailSent('[email protected]'); |
||
152 | $this->assertEmailSent('[email protected]'); |
||
153 | } |
||
154 | |||
155 | public function testFromAddressSet() |
||
156 | { |
||
157 | SiteConfigHelper::setupSiteConfig('[email protected]', '[email protected]', true); |
||
158 | |||
159 | $this->job->setup(); |
||
160 | $this->job->process(); |
||
161 | $this->assertEmailSent('[email protected]', '[email protected]'); |
||
162 | } |
||
163 | |||
164 | public function testFromAddressNotSet() |
||
165 | { |
||
166 | SiteConfigHelper::setupSiteConfig('[email protected]', null, true); |
||
167 | |||
168 | $this->job->setup(); |
||
169 | $this->job->process(); |
||
170 | $this->assertEmailSent('[email protected]', 'site@' . Director::host()); |
||
171 | } |
||
172 | |||
173 | public function testCleanupSubmissions() |
||
174 | { |
||
175 | SiteConfigHelper::setupSiteConfig('[email protected]', null, true); |
||
176 | |||
177 | $this->job->setup(); |
||
178 | $this->job->process(); |
||
179 | $this->assertEmailSent('[email protected]', 'site@' . Director::host()); |
||
180 | } |
||
181 | |||
182 | protected function setUp() |
||
183 | { |
||
184 | parent::setUp(); |
||
185 | |||
186 | DBDatetime::set_mock_now('2018-01-01 12:00:00'); |
||
187 | /** @var PartialSubmissionJob $job */ |
||
188 | $this->job = new PartialSubmissionJob(); |
||
189 | Config::modify()->set(QueuedJobService::class, 'use_shutdown_function', false); |
||
190 | } |
||
191 | } |
||
192 |