Completed
Push — master ( 78baaf...bab9f0 )
by Simon
02:04
created

PartialSubmissionJobTest   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 172
Duplicated Lines 36.05 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 17
c 3
b 1
f 0
lcom 1
cbo 10
dl 62
loc 172
rs 10

15 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetTitle() 0 4 1
A testSetup() 0 6 1
A testProcess() 10 10 1
A testProcessNoMail() 13 13 2
A testProcessNotSetup() 12 12 2
A testIsSend() 0 12 1
A testIsDeleted() 0 13 1
A testFilesRemoved() 0 9 1
A testNewJobCreated() 0 15 1
A testInvalidEmail() 0 15 1
A testCommaSeparatedUsers() 11 11 1
A testFromAddressSet() 8 8 1
A testFromAddressNotSet() 8 8 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\Dev\SapphireTest;
10
use SilverStripe\ORM\FieldType\DBDatetime;
11
use SilverStripe\Security\Security;
12
use SilverStripe\SiteConfig\SiteConfig;
13
use Symbiote\QueuedJobs\DataObjects\QueuedJobDescriptor;
14
use Symbiote\QueuedJobs\Services\QueuedJobService;
15
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 View Code Duplication
    public function testProcess()
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...
37
    {
38
        $this->assertTrue(method_exists($this->job, 'process'));
39
        SetupSiteConfig::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 View Code Duplication
    public function testProcessNoMail()
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...
48
    {
49
        $this->assertTrue(method_exists($this->job, 'process'));
50
        SetupSiteConfig::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 View Code Duplication
    public function testProcessNotSetup()
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...
62
    {
63
        SetupSiteConfig::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
        SetupSiteConfig::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
        SetupSiteConfig::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
        SetupSiteConfig::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
        SetupSiteConfig::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
        SetupSiteConfig::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 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...
144
    {
145
        SetupSiteConfig::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 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...
156
    {
157
        SetupSiteConfig::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 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...
165
    {
166
        SetupSiteConfig::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
    protected function setUp()
174
    {
175
        parent::setUp();
176
        $this->usesDatabase = true;
177
        DBDatetime::set_mock_now('2018-01-01 12:00:00');
178
        /** @var PartialSubmissionJob $job */
179
        $this->job = new PartialSubmissionJob();
180
        Config::modify()->set(QueuedJobService::class, 'use_shutdown_function', false);
181
    }
182
183
    protected function tearDown()
184
    {
185
        parent::tearDown();
186
    }
187
}
188