Completed
Push — master ( 64551a...f3ef88 )
by Simon
02:13
created

PartialSubmissionJobTest   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 190
Duplicated Lines 25.79 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 15
c 3
b 1
f 0
lcom 1
cbo 10
dl 49
loc 190
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetTitle() 0 4 1
A testSetup() 0 6 1
A testProcess() 0 13 1
A testProcessNoMail() 0 16 2
A testIsSend() 0 14 1
A testIsDeleted() 0 17 1
A testFilesRemoved() 12 12 1
A testNewJobCreated() 0 18 1
A testInvalidEmail() 0 18 1
A testCommaSeparatedUsers() 14 14 1
A testFromAddressSet() 12 12 1
A testFromAddressNotSet() 11 11 1
A setUp() 0 9 1
A tearDown() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

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
3
namespace Firesphere\PartialUserforms\Tests;
4
5
use Firesphere\PartialUserforms\Jobs\PartialSubmissionJob;
6
use Firesphere\PartialUserforms\Models\PartialFormSubmission;
7
use SilverStripe\Control\Director;
8
use SilverStripe\Core\Config\Config;
9
use SilverStripe\Core\Injector\Injector;
10
use SilverStripe\Dev\Debug;
11
use SilverStripe\Dev\SapphireTest;
12
use SilverStripe\ORM\FieldType\DBDatetime;
13
use SilverStripe\Security\Security;
14
use SilverStripe\SiteConfig\SiteConfig;
15
use Symbiote\QueuedJobs\DataObjects\QueuedJobDescriptor;
16
use Symbiote\QueuedJobs\Services\QueuedJobService;
17
18
class PartialSubmissionJobTest extends SapphireTest
19
{
20
    protected static $fixture_file = '../fixtures/submission.yml';
21
    /**
22
     * @var PartialSubmissionJob
23
     */
24
    protected $job;
25
26
    public function testGetTitle()
27
    {
28
        $this->assertEquals('Export partial submissions to Email', $this->job->getTitle());
29
    }
30
31
    public function testSetup()
32
    {
33
        $this->job->setup();
34
35
        $this->assertInstanceOf(SiteConfig::class, $this->job->getConfig());
36
    }
37
38
    public function testProcess()
39
    {
40
        $this->assertTrue(method_exists($this->job, 'process'));
41
        $config = SiteConfig::current_site_config();
42
        $config->SendDailyEmail = true;
43
        $config->SendMailTo = '[email protected]';
44
        $config->write();
45
        $this->job->setup();
46
        $this->job->process();
47
48
        $this->assertEmailSent('[email protected]');
49
        $this->assertFileExists('/tmp/Export of TestForm - 2018-01-01 12:00:00.csv');
50
    }
51
52
    public function testProcessNoMail()
53
    {
54
        $this->assertTrue(method_exists($this->job, 'process'));
55
        $config = SiteConfig::current_site_config();
56
        $config->SendDailyEmail = true;
57
        $config->SendMailTo = '';
58
        Security::setCurrentUser(null);
59
        $config->write();
60
        $this->job->setup();
61
        $this->job->process();
62
63
        $messages = $this->job->getMessages();
64
        foreach ($messages as $message) {
65
            $this->assertContains('Can not process without valid email', $message);
66
        }
67
    }
68
69
    public function testIsSend()
70
    {
71
        $submission = $this->objFromFixture(PartialFormSubmission::class, 'submission1');
72
        $config = SiteConfig::current_site_config();
73
        $config->SendDailyEmail = true;
74
        $config->SendMailTo = '[email protected]';
75
        $config->write();
76
        $this->job->setup();
77
        $this->job->process();
78
79
        $processedSubmission = $submission->get()->byID($submission->ID);
80
81
        $this->assertTrue((bool)$processedSubmission->IsSend);
82
    }
83
84
    public function testIsDeleted()
85
    {
86
        $submission = $this->objFromFixture(PartialFormSubmission::class, 'submission1');
87
        $id = $submission->write();
88
        $config = SiteConfig::current_site_config();
89
        $config->SendDailyEmail = true;
90
        $config->CleanupAfterSend = true;
91
        $config->SendMailTo = '[email protected]';
92
        $config->write();
93
        $this->job->setup();
94
        $this->job->process();
95
        $this->job->afterComplete();
96
97
        $processedSubmission = $submission->get()->byID($id);
98
99
        $this->assertNull($processedSubmission);
100
    }
101
102 View Code Duplication
    public function testFilesRemoved()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
103
    {
104
        $config = SiteConfig::current_site_config();
105
        $config->SendDailyEmail = true;
106
        $config->SendMailTo = '[email protected]';
107
        $config->write();
108
        $this->job->setup();
109
        $this->job->process();
110
        $this->job->afterComplete();
111
112
        $this->assertFileNotExists('/tmp/Export of TestForm - 2018-01-01 12:00:00.csv');
113
    }
114
115
    public function testNewJobCreated()
116
    {
117
        $config = SiteConfig::current_site_config();
118
        $config->SendDailyEmail = true;
119
        $config->SendMailTo = '[email protected]';
120
        $config->write();
121
        $this->job->setup();
122
        $this->job->process();
123
        $this->job->afterComplete();
124
125
        $jobs = QueuedJobDescriptor::get()->filter([
126
            'Implementation'         => PartialSubmissionJob::class,
127
            'StartAfter:GreaterThan' => DBDatetime::now()->Format(DBDatetime::ISO_DATETIME)
128
        ]);
129
130
        $this->assertEquals(1, $jobs->count());
131
        $this->assertEquals('2018-01-02 00:00:00', $jobs->first()->StartAfter);
132
    }
133
134
    public function testInvalidEmail()
135
    {
136
        $config = SiteConfig::current_site_config();
137
        $config->SendDailyEmail = true;
138
        $config->SendMailTo = '[email protected], error, non-existing, [email protected]';
139
        $config->write();
140
141
        /** @var PartialSubmissionJob $job */
142
        $job = new PartialSubmissionJob();
143
        $job->setup();
144
        $emails = $job->getAddresses();
145
146
        $expected = [
147
            '[email protected]',
148
            '[email protected]'
149
        ];
150
        $this->assertEquals($expected, $emails);
151
    }
152
153 View Code Duplication
    public function testCommaSeparatedUsers()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
154
    {
155
        $config = SiteConfig::current_site_config();
156
        $config->SendDailyEmail = true;
157
        $config->SendMailTo = '[email protected], [email protected] , [email protected]';
158
        $config->write();
159
160
        $this->job->setup();
161
        $this->job->process();
162
163
        $this->assertEmailSent('[email protected]');
164
        $this->assertEmailSent('[email protected]');
165
        $this->assertEmailSent('[email protected]');
166
    }
167
168 View Code Duplication
    public function testFromAddressSet()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
169
    {
170
        $config = SiteConfig::current_site_config();
171
        $config->SendDailyEmail = true;
172
        $config->SendMailTo = '[email protected]';
173
        $config->SendMailFrom = '[email protected]';
174
        $config->write();
175
176
        $this->job->setup();
177
        $this->job->process();
178
        $this->assertEmailSent('[email protected]', '[email protected]');
179
    }
180
181 View Code Duplication
    public function testFromAddressNotSet()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
182
    {
183
        $config = SiteConfig::current_site_config();
184
        $config->SendDailyEmail = true;
185
        $config->SendMailTo = '[email protected]';
186
        $config->write();
187
188
        $this->job->setup();
189
        $this->job->process();
190
        $this->assertEmailSent('[email protected]', 'site@' . Director::host());
191
    }
192
193
    protected function setUp()
194
    {
195
        parent::setUp();
196
        $this->usesDatabase = true;
197
        DBDatetime::set_mock_now('2018-01-01 12:00:00');
198
        /** @var PartialSubmissionJob $job */
199
        $this->job = new PartialSubmissionJob();
200
        Config::modify()->set(QueuedJobService::class, 'use_shutdown_function', false);
201
    }
202
203
    protected function tearDown()
204
    {
205
        parent::tearDown();
206
    }
207
}
208