Completed
Pull Request — master (#73)
by Helpful
02:23
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
    public function setUp()
11
    {
12
        parent::setUp();
13
14
        Config::nest();
15
        // Two restarts are allowed per job
16
        Config::inst()->update('QueuedJobService', 'stall_threshold', 2);
17
    }
18
19
    public function tearDown()
20
    {
21
        Config::unnest();
22
        parent::tearDown();
23
    }
24
25
26
    /**
27
     * @return QueuedJobService
28
     */
29
    protected function getService()
30
    {
31
        return singleton("TestQJService");
32
    }
33
34
    public function testQueueJob()
35
    {
36
        $svc = $this->getService();
37
38
        // lets create a new job and add it tio the queue
39
        $job = new TestQueuedJob();
40
        $jobId = $svc->queueJob($job);
41
        $list = $svc->getJobList();
42
43
        $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...
44
45
        $myJob = null;
46
        foreach ($list as $job) {
47
            if ($job->Implementation == 'TestQueuedJob') {
48
                $myJob = $job;
49
                break;
50
            }
51
        }
52
53
        $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...
54
        $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...
55
        $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...
56
        $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...
57
    }
58
59
    public function testJobRunAs()
60
    {
61
        $svc = $this->getService();
62
        $list = $svc->getJobList();
63
        foreach ($list as $job) {
64
            $job->delete();
65
        }
66
67
        $this->logInWithPermission('DUMMY');
68
69
        // lets create a new job and add it tio the queue
70
        $job = new TestQueuedJob();
71
        $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...
72
        $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...
73
        $list = $svc->getJobList();
74
75
        $myJob = $list->First();
76
77
        $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...
78
    }
79
80
    public function testQueueSignature()
81
    {
82
        $svc = $this->getService();
83
84
        // lets create a new job and add it tio the queue
85
        $job = new TestQueuedJob();
86
        $jobId = $svc->queueJob($job);
87
88
        $newJob = new TestQueuedJob();
89
        $newId = $svc->queueJob($newJob);
90
91
        $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...
92
93
        // now try another, but with different params
94
        $newJob = new TestQueuedJob();
95
        $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...
96
        $newId = $svc->queueJob($newJob);
97
98
        $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...
99
    }
100
101
    public function testProcessJob()
102
    {
103
        $job = new TestQueuedJob();
104
        $job->setup();
105
        $job->process();
106
        // we should now have some  data
107
        $data = $job->getJobData();
108
        $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...
109
        $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...
110
111
        $jd = $data->jobData;
112
        $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...
113
        $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...
114
115
        // now take the 'saved' data and try restoring the job
116
    }
117
118
    public function testResumeJob()
119
    {
120
        $job = new TestQueuedJob();
121
        $job->setup();
122
        $job->process();
123
        // we should now have some  data
124
        $data = $job->getJobData();
125
126
        // so create a new job and restore it from this data
127
128
        $job = new TestQueuedJob();
129
        $job->setup();
130
131
        $job->setJobData($data->totalSteps, $data->currentStep, $data->isComplete, $data->jobData, $data->messages);
132
        $job->process();
133
134
        $data = $job->getJobData();
135
        $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...
136
        $jd = $data->jobData;
137
        $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...
138
        $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...
139
    }
140
141
    public function testInitialiseJob()
142
    {
143
        // okay, lets test it out on the actual service
144
        $svc = $this->getService();
145
        // lets create a new job and add it to the queue
146
        $job = new TestQueuedJob();
147
        $id = $svc->queueJob($job);
148
149
        $descriptor = DataObject::get_by_id('QueuedJobDescriptor', $id);
150
151
        $job = $svc->testInit($descriptor);
152
        $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...
153
154
        $descriptor = DataObject::get_by_id('QueuedJobDescriptor', $id);
155
156
        $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...
157
    }
158
159
    public function testStartJob()
160
    {
161
        // okay, lets test it out on the actual service
162
        $svc = $this->getService();
163
        // lets create a new job and add it to the queue
164
165
        $this->logInWithPermission('DUMMYUSER');
166
167
        $job = new TestQueuedJob();
168
        $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...
169
        $id = $svc->queueJob($job);
170
171
        $this->logInWithPermission('ADMIN');
172
173
        $result = $svc->runJob($id);
174
        $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...
175
176
        // we want to make sure that the current user is the runas user of the job
177
        $descriptor = DataObject::get_by_id('QueuedJobDescriptor', $id);
178
        $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...
179
    }
180
181
    public function testImmediateQueuedJob()
182
    {
183
        // okay, lets test it out on the actual service
184
        $svc = $this->getService();
185
        // lets create a new job and add it to the queue
186
187
        $job = new TestQueuedJob(QueuedJob::IMMEDIATE);
188
        $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...
189
        $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...
190
191
        $job = new TestQueuedJob(QueuedJob::IMMEDIATE);
192
        $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...
193
        $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...
194
195
        $jobs = $svc->getJobList(QueuedJob::IMMEDIATE);
196
        $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...
197
198
        // now fake a shutdown
199
        $svc->onShutdown();
200
201
        $jobs = $svc->getJobList(QueuedJob::IMMEDIATE);
202
        $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...
203
        $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...
204
    }
205
206
    public function testNextJob()
207
    {
208
        $svc = $this->getService();
209
        $list = $svc->getJobList();
210
211
        foreach ($list as $job) {
212
            $job->delete();
213
        }
214
215
        $list = $svc->getJobList();
216
        $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...
217
218
        $job = new TestQueuedJob();
219
        $id1 = $svc->queueJob($job);
220
221
        $job = new TestQueuedJob();
222
        // to get around the signature checks
223
        $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...
224
        $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...
225
226
        $job = new TestQueuedJob();
227
        // to get around the signature checks
228
        $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...
229
        $id3 = $svc->queueJob($job);
230
231
        $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...
232
233
        $list = $svc->getJobList();
234
        $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...
235
236
        // okay, lets get the first one and initialise it, then make sure that a subsequent init attempt fails
237
        $job = $svc->getNextPendingJob();
238
239
        $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...
240
        $svc->testInit($job);
241
242
        // now try and get another, it should be === false
243
        $next = $svc->getNextPendingJob();
244
245
        $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...
246
    }
247
248
    /**
249
     * Verify that broken jobs are correctly verified for health and restarted as necessary
250
     *
251
     * Order of checkJobHealth() and getNextPendingJob() is important
252
     *
253
     * Execution of this job is broken into several "loops", each of which represents one invocation
254
     * of ProcessJobQueueTask
255
     */
256
    public function testJobHealthCheck()
257
    {
258
        // Create a job and add it to the queue
259
        $svc = $this->getService();
260
        $job = new TestQueuedJob(QueuedJob::IMMEDIATE);
261
        $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...
262
        $id = $svc->queueJob($job);
263
        $descriptor = QueuedJobDescriptor::get()->byID($id);
264
265
        // Verify initial state is new and LastProcessedCount is not marked yet
266
        $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...
267
        $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...
268
        $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...
269
        $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...
270
271
        // Loop 1 - Pick up new job and attempt to run it
272
        // Job health should not attempt to cleanup unstarted jobs
273
        $svc->checkJobHealth();
274
        $nextJob = $svc->getNextPendingJob(QueuedJob::IMMEDIATE);
275
276
        // Ensure that this is the next job ready to go
277
        $descriptor = QueuedJobDescriptor::get()->byID($id);
278
        $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...
279
        $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...
280
        $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...
281
        $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...
282
        $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...
283
284
        // Run 1 - Start the job (no work is done)
285
        $descriptor->JobStatus = QueuedJob::STATUS_INIT;
286
        $descriptor->write();
287
288
        // Assume that something bad happens at this point, the process dies during execution, and
289
        // the task is re-initiated somewhere down the track
290
291
        // Loop 2 - Detect broken job, and mark it for future checking.
292
        $svc->checkJobHealth();
293
        $nextJob = $svc->getNextPendingJob(QueuedJob::IMMEDIATE);
294
295
        // Note that we don't immediately try to restart it until StepsProcessed = LastProcessedCount
296
        $descriptor = QueuedJobDescriptor::get()->byID($id);
297
        $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...
298
        $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...
299
        $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...
300
        $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...
301
        $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...
302
303
        // Loop 3 - We've previously marked this job as broken, so restart it this round
304
        // If no more work has been done on the job at this point, assume that we are able to
305
        // restart it
306
        $svc->checkJobHealth();
307
        $nextJob = $svc->getNextPendingJob(QueuedJob::IMMEDIATE);
308
309
        // This job is resumed and exeuction is attempted this round
310
        $descriptor = QueuedJobDescriptor::get()->byID($id);
311
        $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...
312
        $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...
313
        $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...
314
        $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...
315
        $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...
316
317
        // Run 2 - First restart (work is done)
318
        $descriptor->JobStatus = QueuedJob::STATUS_RUN;
319
        $descriptor->StepsProcessed++; // Essentially delays the next restart by 1 loop
320
        $descriptor->write();
321
322
        // Once again, at this point, assume the job fails and crashes
323
324
        // Loop 4 - Assuming a job has LastProcessedCount < StepsProcessed we are in the same
325
        // situation as step 2.
326
        // Because the last time the loop ran, StepsProcessed was incremented,
327
        // this indicates that it's likely that another task could be working on this job, so
328
        // don't run this.
329
        $svc->checkJobHealth();
330
        $nextJob = $svc->getNextPendingJob(QueuedJob::IMMEDIATE);
331
332
        $descriptor = QueuedJobDescriptor::get()->byID($id);
333
        $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...
334
        $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...
335
        $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...
336
        $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...
337
        $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...
338
339
        // Loop 5 - Job is again found to not have been restarted since last iteration, so perform second
340
        // restart. The job should be attempted to run this loop
341
        $svc->checkJobHealth();
342
        $nextJob = $svc->getNextPendingJob(QueuedJob::IMMEDIATE);
343
344
        // This job is resumed and exeuction is attempted this round
345
        $descriptor = QueuedJobDescriptor::get()->byID($id);
346
        $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...
347
        $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...
348
        $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...
349
        $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...
350
        $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...
351
352
        // Run 3 - Second and last restart (no work is done)
353
        $descriptor->JobStatus = QueuedJob::STATUS_RUN;
354
        $descriptor->write();
355
356
        // Loop 6 - As no progress has been made since loop 3, we can mark this as dead
357
        $svc->checkJobHealth();
358
        $nextJob = $svc->getNextPendingJob(QueuedJob::IMMEDIATE);
359
360
        // Since no StepsProcessed has been done, don't wait another loop to mark this as dead
361
        $descriptor = QueuedJobDescriptor::get()->byID($id);
362
        $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...
363
        $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...
364
    }
365
}
366
367
// stub class to be able to call init from an external context
368
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...
369
{
370
    public function testInit($descriptor)
371
    {
372
        return $this->initialiseJob($descriptor);
373
    }
374
}
375
376
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...
377
{
378
    private $type = QueuedJob::QUEUED;
379
380
    public function __construct($type = null)
381
    {
382
        if ($type) {
383
            $this->type = $type;
384
        }
385
        $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...
386
    }
387
388
    public function getJobType()
389
    {
390
        return $this->type;
391
    }
392
393
    public function getTitle()
394
    {
395
        return "A Test job";
396
    }
397
398
    public function setup()
399
    {
400
        $this->totalSteps = 5;
401
    }
402
403 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...
404
    {
405
        $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...
406
        // needed due to quirks with __set
407
        $times[] = date('Y-m-d H:i:s');
408
        $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...
409
410
        $this->addMessage("Updated time to " . date('Y-m-d H:i:s'));
411
        sleep(1);
412
413
        // make sure we're incrementing
414
        $this->currentStep++;
415
416
        // and checking whether we're complete
417
        if ($this->currentStep == 5) {
418
            $this->isComplete = true;
419
        }
420
    }
421
}
422