Completed
Pull Request — master (#73)
by Helpful
02:27
created

QueuedJobsTest::testQueueJob()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 24
rs 8.9714
cc 3
eloc 15
nc 3
nop 0
1
<?php
2
3
/**
4
 *
5
 *
6
 * @author Marcus Nyeholt <[email protected]>
7
 */
8
class QueuedJobsTest extends SapphireTest
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
9
{
10
11
    public function setUp()
12
    {
13
        parent::setUp();
14
15
        Config::nest();
16
        // Two restarts are allowed per job
17
        Config::inst()->update('QueuedJobService', 'stall_threshold', 2);
18
    }
19
20
    public function tearDown()
21
    {
22
        Config::unnest();
23
        parent::tearDown();
24
    }
25
26
27
    /**
28
     * @return QueuedJobService
29
     */
30
    protected function getService()
31
    {
32
        return singleton("TestQJService");
33
    }
34
35
    public function testQueueJob()
36
    {
37
        $svc = $this->getService();
38
39
        // lets create a new job and add it tio the queue
40
        $job = new TestQueuedJob();
41
        $jobId = $svc->queueJob($job);
42
        $list = $svc->getJobList();
43
44
        $this->assertEquals(1, $list->count());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<QueuedJobsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
45
46
        $myJob = null;
47
        foreach ($list as $job) {
48
            if ($job->Implementation == 'TestQueuedJob') {
49
                $myJob = $job;
50
                break;
51
            }
52
        }
53
54
        $this->assertNotNull($myJob);
0 ignored issues
show
Bug introduced by
The method assertNotNull() does not seem to exist on object<QueuedJobsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
55
        $this->assertTrue($jobId > 0);
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<QueuedJobsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
56
        $this->assertEquals('TestQueuedJob', $myJob->Implementation);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<QueuedJobsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
57
        $this->assertNotNull($myJob->SavedJobData);
0 ignored issues
show
Bug introduced by
The method assertNotNull() does not seem to exist on object<QueuedJobsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
58
    }
59
60
    public function testJobRunAs()
61
    {
62
        $svc = $this->getService();
63
        $list = $svc->getJobList();
64
        foreach ($list as $job) {
65
            $job->delete();
66
        }
67
68
        $this->logInWithPermission('DUMMY');
69
70
        // lets create a new job and add it tio the queue
71
        $job = new TestQueuedJob();
72
        $job->runningAs = "DUMMY";
0 ignored issues
show
Documentation introduced by
The property runningAs does not exist on object<TestQueuedJob>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
73
        $jobId = $svc->queueJob($job);
0 ignored issues
show
Unused Code introduced by
$jobId is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
74
        $list = $svc->getJobList();
75
76
        $myJob = $list->First();
77
78
        $this->assertEquals("[email protected]", $myJob->RunAs()->Email);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<QueuedJobsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
79
    }
80
81
    public function testQueueSignature()
82
    {
83
        $svc = $this->getService();
84
85
        // lets create a new job and add it tio the queue
86
        $job = new TestQueuedJob();
87
        $jobId = $svc->queueJob($job);
88
89
        $newJob = new TestQueuedJob();
90
        $newId = $svc->queueJob($newJob);
91
92
        $this->assertEquals($jobId, $newId);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<QueuedJobsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
93
94
        // now try another, but with different params
95
        $newJob = new TestQueuedJob();
96
        $newJob->randomParam = 'stuff';
0 ignored issues
show
Documentation introduced by
The property randomParam does not exist on object<TestQueuedJob>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
97
        $newId = $svc->queueJob($newJob);
98
99
        $this->assertNotEquals($jobId, $newId);
0 ignored issues
show
Bug introduced by
The method assertNotEquals() does not seem to exist on object<QueuedJobsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
100
    }
101
102
    public function testProcessJob()
103
    {
104
        $job = new TestQueuedJob();
105
        $job->setup();
106
        $job->process();
107
        // we should now have some  data
108
        $data = $job->getJobData();
109
        $this->assertNotNull($data->messages);
0 ignored issues
show
Bug introduced by
The method assertNotNull() does not seem to exist on object<QueuedJobsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
110
        $this->assertFalse($data->isComplete);
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<QueuedJobsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
111
112
        $jd = $data->jobData;
113
        $this->assertTrue(isset($jd->times));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<QueuedJobsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
114
        $this->assertEquals(1, count($jd->times));
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<QueuedJobsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
115
116
        // now take the 'saved' data and try restoring the job
117
    }
118
119
    public function testResumeJob()
120
    {
121
        $job = new TestQueuedJob();
122
        $job->setup();
123
        $job->process();
124
        // we should now have some  data
125
        $data = $job->getJobData();
126
127
        // so create a new job and restore it from this data
128
129
        $job = new TestQueuedJob();
130
        $job->setup();
131
132
        $job->setJobData($data->totalSteps, $data->currentStep, $data->isComplete, $data->jobData, $data->messages);
133
        $job->process();
134
135
        $data = $job->getJobData();
136
        $this->assertFalse($data->isComplete);
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<QueuedJobsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
137
        $jd = $data->jobData;
138
        $this->assertTrue(isset($jd->times));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<QueuedJobsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
139
        $this->assertEquals(2, count($jd->times));
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<QueuedJobsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
140
    }
141
142
    public function testInitialiseJob()
143
    {
144
        // okay, lets test it out on the actual service
145
        $svc = $this->getService();
146
        // lets create a new job and add it to the queue
147
        $job = new TestQueuedJob();
148
        $id = $svc->queueJob($job);
149
150
        $descriptor = DataObject::get_by_id('QueuedJobDescriptor', $id);
151
152
        $job = $svc->testInit($descriptor);
153
        $this->assertInstanceOf('TestQueuedJob', $job, 'Job has been triggered');
0 ignored issues
show
Bug introduced by
The method assertInstanceOf() does not seem to exist on object<QueuedJobsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
154
155
        $descriptor = DataObject::get_by_id('QueuedJobDescriptor', $id);
156
157
        $this->assertEquals(QueuedJob::STATUS_INIT, $descriptor->JobStatus);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<QueuedJobsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
158
    }
159
160
    public function testStartJob()
161
    {
162
        // okay, lets test it out on the actual service
163
        $svc = $this->getService();
164
        // lets create a new job and add it to the queue
165
166
        $this->logInWithPermission('DUMMYUSER');
167
168
        $job = new TestQueuedJob();
169
        $job->testingStartJob = true;
0 ignored issues
show
Documentation introduced by
The property testingStartJob does not exist on object<TestQueuedJob>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
170
        $id = $svc->queueJob($job);
171
172
        $this->logInWithPermission('ADMIN');
173
174
        $result = $svc->runJob($id);
175
        $this->assertTrue($result);
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<QueuedJobsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
176
177
        // we want to make sure that the current user is the runas user of the job
178
        $descriptor = DataObject::get_by_id('QueuedJobDescriptor', $id);
179
        $this->assertEquals('Complete', $descriptor->JobStatus);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<QueuedJobsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
180
    }
181
182
    public function testImmediateQueuedJob()
183
    {
184
        // okay, lets test it out on the actual service
185
        $svc = $this->getService();
186
        // lets create a new job and add it to the queue
187
188
        $job = new TestQueuedJob(QueuedJob::IMMEDIATE);
189
        $job->firstJob = true;
0 ignored issues
show
Documentation introduced by
The property firstJob does not exist on object<TestQueuedJob>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
190
        $id = $svc->queueJob($job);
0 ignored issues
show
Unused Code introduced by
$id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
191
192
        $job = new TestQueuedJob(QueuedJob::IMMEDIATE);
193
        $job->secondJob = true;
0 ignored issues
show
Documentation introduced by
The property secondJob does not exist on object<TestQueuedJob>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
194
        $id = $svc->queueJob($job);
0 ignored issues
show
Unused Code introduced by
$id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
195
196
        $jobs = $svc->getJobList(QueuedJob::IMMEDIATE);
197
        $this->assertEquals(2, $jobs->count());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<QueuedJobsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
198
199
        // now fake a shutdown
200
        $svc->onShutdown();
201
202
        $jobs = $svc->getJobList(QueuedJob::IMMEDIATE);
203
        $this->assertInstanceOf('DataList', $jobs);
0 ignored issues
show
Bug introduced by
The method assertInstanceOf() does not seem to exist on object<QueuedJobsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
204
        $this->assertEquals(0, $jobs->count());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<QueuedJobsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
205
    }
206
207
    public function testNextJob()
208
    {
209
        $svc = $this->getService();
210
        $list = $svc->getJobList();
211
212
        foreach ($list as $job) {
213
            $job->delete();
214
        }
215
216
        $list = $svc->getJobList();
217
        $this->assertEquals(0, $list->count());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<QueuedJobsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
218
219
        $job = new TestQueuedJob();
220
        $id1 = $svc->queueJob($job);
221
222
        $job = new TestQueuedJob();
223
        // to get around the signature checks
224
        $job->randomParam = 'me';
0 ignored issues
show
Documentation introduced by
The property randomParam does not exist on object<TestQueuedJob>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
225
        $id2 = $svc->queueJob($job);
0 ignored issues
show
Unused Code introduced by
$id2 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
226
227
        $job = new TestQueuedJob();
228
        // to get around the signature checks
229
        $job->randomParam = 'mo';
0 ignored issues
show
Documentation introduced by
The property randomParam does not exist on object<TestQueuedJob>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
230
        $id3 = $svc->queueJob($job);
231
232
        $this->assertEquals(2, $id3 - $id1);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<QueuedJobsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
233
234
        $list = $svc->getJobList();
235
        $this->assertEquals(3, $list->count());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<QueuedJobsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
236
237
        // okay, lets get the first one and initialise it, then make sure that a subsequent init attempt fails
238
        $job = $svc->getNextPendingJob();
239
240
        $this->assertEquals($id1, $job->ID);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<QueuedJobsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
241
        $svc->testInit($job);
242
243
        // now try and get another, it should be === false
244
        $next = $svc->getNextPendingJob();
245
246
        $this->assertFalse($next);
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<QueuedJobsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
247
    }
248
249
    /**
250
     * Verify that broken jobs are correctly verified for health and restarted as necessary
251
     *
252
     * Order of checkJobHealth() and getNextPendingJob() is important
253
     *
254
     * Execution of this job is broken into several "loops", each of which represents one invocation
255
     * of ProcessJobQueueTask
256
     */
257
    public function testJobHealthCheck()
258
    {
259
        // Create a job and add it to the queue
260
        $svc = $this->getService();
261
        $job = new TestQueuedJob(QueuedJob::IMMEDIATE);
262
        $job->firstJob = true;
0 ignored issues
show
Documentation introduced by
The property firstJob does not exist on object<TestQueuedJob>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
263
        $id = $svc->queueJob($job);
264
        $descriptor = QueuedJobDescriptor::get()->byID($id);
265
266
        // Verify initial state is new and LastProcessedCount is not marked yet
267
        $this->assertEquals(QueuedJob::STATUS_NEW, $descriptor->JobStatus);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<QueuedJobsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
268
        $this->assertEquals(0, $descriptor->StepsProcessed);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<QueuedJobsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
269
        $this->assertEquals(-1, $descriptor->LastProcessedCount);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<QueuedJobsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
270
        $this->assertEquals(0, $descriptor->ResumeCounts);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<QueuedJobsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
271
272
        // Loop 1 - Pick up new job and attempt to run it
273
        // Job health should not attempt to cleanup unstarted jobs
274
        $svc->checkJobHealth();
275
        $nextJob = $svc->getNextPendingJob(QueuedJob::IMMEDIATE);
276
277
        // Ensure that this is the next job ready to go
278
        $descriptor = QueuedJobDescriptor::get()->byID($id);
279
        $this->assertEquals($nextJob->ID, $descriptor->ID);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<QueuedJobsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
280
        $this->assertEquals(QueuedJob::STATUS_NEW, $descriptor->JobStatus);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<QueuedJobsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
281
        $this->assertEquals(0, $descriptor->StepsProcessed);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<QueuedJobsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
282
        $this->assertEquals(-1, $descriptor->LastProcessedCount);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<QueuedJobsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
283
        $this->assertEquals(0, $descriptor->ResumeCounts);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<QueuedJobsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
284
285
        // Run 1 - Start the job (no work is done)
286
        $descriptor->JobStatus = QueuedJob::STATUS_INIT;
287
        $descriptor->write();
288
289
        // Assume that something bad happens at this point, the process dies during execution, and
290
        // the task is re-initiated somewhere down the track
291
292
        // Loop 2 - Detect broken job, and mark it for future checking.
293
        $svc->checkJobHealth();
294
        $nextJob = $svc->getNextPendingJob(QueuedJob::IMMEDIATE);
295
296
        // Note that we don't immediately try to restart it until StepsProcessed = LastProcessedCount
297
        $descriptor = QueuedJobDescriptor::get()->byID($id);
298
        $this->assertFalse($nextJob); // Don't run it this round please!
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<QueuedJobsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
299
        $this->assertEquals(QueuedJob::STATUS_INIT, $descriptor->JobStatus);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<QueuedJobsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
300
        $this->assertEquals(0, $descriptor->StepsProcessed);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<QueuedJobsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
301
        $this->assertEquals(0, $descriptor->LastProcessedCount);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<QueuedJobsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
302
        $this->assertEquals(0, $descriptor->ResumeCounts);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<QueuedJobsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
303
304
        // Loop 3 - We've previously marked this job as broken, so restart it this round
305
        // If no more work has been done on the job at this point, assume that we are able to
306
        // restart it
307
        $svc->checkJobHealth();
308
        $nextJob = $svc->getNextPendingJob(QueuedJob::IMMEDIATE);
309
310
        // This job is resumed and exeuction is attempted this round
311
        $descriptor = QueuedJobDescriptor::get()->byID($id);
312
        $this->assertEquals($nextJob->ID, $descriptor->ID);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<QueuedJobsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
313
        $this->assertEquals(QueuedJob::STATUS_WAIT, $descriptor->JobStatus);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<QueuedJobsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
314
        $this->assertEquals(0, $descriptor->StepsProcessed);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<QueuedJobsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
315
        $this->assertEquals(0, $descriptor->LastProcessedCount);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<QueuedJobsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
316
        $this->assertEquals(1, $descriptor->ResumeCounts);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<QueuedJobsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
317
318
        // Run 2 - First restart (work is done)
319
        $descriptor->JobStatus = QueuedJob::STATUS_RUN;
320
        $descriptor->StepsProcessed++; // Essentially delays the next restart by 1 loop
321
        $descriptor->write();
322
323
        // Once again, at this point, assume the job fails and crashes
324
325
        // Loop 4 - Assuming a job has LastProcessedCount < StepsProcessed we are in the same
326
        // situation as step 2.
327
        // Because the last time the loop ran, StepsProcessed was incremented,
328
        // this indicates that it's likely that another task could be working on this job, so
329
        // don't run this.
330
        $svc->checkJobHealth();
331
        $nextJob = $svc->getNextPendingJob(QueuedJob::IMMEDIATE);
332
333
        $descriptor = QueuedJobDescriptor::get()->byID($id);
334
        $this->assertFalse($nextJob); // Don't run jobs we aren't sure should be restarted
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<QueuedJobsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
335
        $this->assertEquals(QueuedJob::STATUS_RUN, $descriptor->JobStatus);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<QueuedJobsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
336
        $this->assertEquals(1, $descriptor->StepsProcessed);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<QueuedJobsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
337
        $this->assertEquals(1, $descriptor->LastProcessedCount);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<QueuedJobsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
338
        $this->assertEquals(1, $descriptor->ResumeCounts);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<QueuedJobsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
339
340
        // Loop 5 - Job is again found to not have been restarted since last iteration, so perform second
341
        // restart. The job should be attempted to run this loop
342
        $svc->checkJobHealth();
343
        $nextJob = $svc->getNextPendingJob(QueuedJob::IMMEDIATE);
344
345
        // This job is resumed and exeuction is attempted this round
346
        $descriptor = QueuedJobDescriptor::get()->byID($id);
347
        $this->assertEquals($nextJob->ID, $descriptor->ID);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<QueuedJobsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
348
        $this->assertEquals(QueuedJob::STATUS_WAIT, $descriptor->JobStatus);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<QueuedJobsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
349
        $this->assertEquals(1, $descriptor->StepsProcessed);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<QueuedJobsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
350
        $this->assertEquals(1, $descriptor->LastProcessedCount);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<QueuedJobsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
351
        $this->assertEquals(2, $descriptor->ResumeCounts);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<QueuedJobsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
352
353
        // Run 3 - Second and last restart (no work is done)
354
        $descriptor->JobStatus = QueuedJob::STATUS_RUN;
355
        $descriptor->write();
356
357
        // Loop 6 - As no progress has been made since loop 3, we can mark this as dead
358
        $svc->checkJobHealth();
359
        $nextJob = $svc->getNextPendingJob(QueuedJob::IMMEDIATE);
360
361
        // Since no StepsProcessed has been done, don't wait another loop to mark this as dead
362
        $descriptor = QueuedJobDescriptor::get()->byID($id);
363
        $this->assertEquals(QueuedJob::STATUS_PAUSED, $descriptor->JobStatus);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<QueuedJobsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
364
        $this->assertEmpty($nextJob);
0 ignored issues
show
Bug introduced by
The method assertEmpty() does not seem to exist on object<QueuedJobsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
365
    }
366
}
367
368
// stub class to be able to call init from an external context
369
class TestQJService extends QueuedJobService
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
370
{
371
    public function testInit($descriptor)
372
    {
373
        return $this->initialiseJob($descriptor);
374
    }
375
}
376
377
class TestQueuedJob extends AbstractQueuedJob implements QueuedJob
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
378
{
379
    private $type = QueuedJob::QUEUED;
380
381
    public function __construct($type = null)
382
    {
383
        if ($type) {
384
            $this->type = $type;
385
        }
386
        $this->times = array();
0 ignored issues
show
Documentation introduced by
The property times does not exist on object<TestQueuedJob>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
387
    }
388
389
    public function getJobType()
390
    {
391
        return $this->type;
392
    }
393
394
    public function getTitle()
395
    {
396
        return "A Test job";
397
    }
398
399
    public function setup()
400
    {
401
        $this->totalSteps = 5;
402
    }
403
404 View Code Duplication
    public function process()
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...
405
    {
406
        $times = $this->times;
0 ignored issues
show
Documentation introduced by
The property times does not exist on object<TestQueuedJob>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
407
        // needed due to quirks with __set
408
        $times[] = date('Y-m-d H:i:s');
409
        $this->times = $times;
0 ignored issues
show
Documentation introduced by
The property times does not exist on object<TestQueuedJob>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
410
411
        $this->addMessage("Updated time to " . date('Y-m-d H:i:s'));
412
        sleep(1);
413
414
        // make sure we're incrementing
415
        $this->currentStep++;
416
417
        // and checking whether we're complete
418
        if ($this->currentStep == 5) {
419
            $this->isComplete = true;
420
        }
421
    }
422
}
423