QueuedJobsAdminTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 5
dl 0
loc 62
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 12 2
A testConstructorParamsShouldBeATextarea() 0 5 1
A testCreateJobWithConstructorParams() 0 15 2
1
<?php
2
3
use SilverStripe\Dev\FunctionalTest;
4
use SilverStripe\Forms\FieldList;
5
use Symbiote\QueuedJobs\Controllers\QueuedJobsAdmin;
6
use Symbiote\QueuedJobs\Jobs\PublishItemsJob;
7
8
/**
9
 * Tests for the QueuedJobsAdmin ModelAdmin clas
10
 *
11
 * @coversDefaultClass \Symbiote\QueuedJobs\Controllers\QueuedJobsAdmin
12
 * @package queuedjobs
13
 * @author  Robbie Averill <[email protected]>
14
 */
15
class QueuedJobsAdminTest extends FunctionalTest
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...
16
{
17
    /**
18
     * {@inheritDoc}
19
     * @var string
20
     */
21
    // protected static $fixture_file = 'QueuedJobsAdminTest.yml';
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
22
23
    /**
24
     * @var QueuedJobsAdmin
25
     */
26
    protected $admin;
27
28
    /**
29
     * Get a test class, and mock the job queue for it
30
     *
31
     * {@inheritDoc}
32
     */
33
    public function setUp()
34
    {
35
        parent::setUp();
36
        $this->admin = new QueuedJobsAdmin;
37
38
        // Compatible with both PHPUnit 3 and PHPUnit 5+
39
        $mockQueue = method_exists($this, 'createMock') ? $this->createMock('Symbiote\\QueuedJobs\\Services\\QueuedJobService') : $this->getMock('Symbiote\\QueuedJobs\\Services\\QueuedJobService');
40
        $this->admin->jobQueue = $mockQueue;
41
42
        $this->logInWithPermission('ADMIN');
43
        $this->admin->doInit();
44
    }
45
46
    /**
47
     * Ensure that the JobParams field is added as a Textarea
48
     */
49
    public function testConstructorParamsShouldBeATextarea()
50
    {
51
        $fields = $this->admin->getEditForm('foo', new FieldList)->Fields();
0 ignored issues
show
Documentation introduced by
new \SilverStripe\Forms\FieldList() is of type object<SilverStripe\Forms\FieldList>, but the function expects a object<Symbiote\QueuedJo...rollers\FieldList>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
52
        $this->assertInstanceOf('SilverStripe\\Forms\\TextareaField', $fields->fieldByName('JobParams'));
53
    }
54
55
    /**
56
     * Ensure that when a multi-line value is entered for JobParams, it is split by new line and each value
57
     * passed to the constructor of the JobType that is created by the reflection in createjob()
58
     *
59
     * @covers ::createjob
60
     */
61
    public function testCreateJobWithConstructorParams()
62
    {
63
        $this->admin->jobQueue
64
            ->expects($this->once())
65
            ->method('queueJob')
66
            ->with($this->callback(function ($job) {
67
                return $job instanceof PublishItemsJob && $job->rootID === 'foo123';
0 ignored issues
show
Documentation introduced by
The property rootID does not exist on object<Symbiote\QueuedJobs\Jobs\PublishItemsJob>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
68
            }));
69
70
        $form = $this->admin->getEditForm('foo', new FieldList);
0 ignored issues
show
Documentation introduced by
new \SilverStripe\Forms\FieldList() is of type object<SilverStripe\Forms\FieldList>, but the function expects a object<Symbiote\QueuedJo...rollers\FieldList>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
71
        $form->Fields()->fieldByName('JobParams')->setValue(implode(PHP_EOL, ['foo123', 'bar']));
72
        $form->Fields()->fieldByName('JobType')->setValue(PublishItemsJob::class);
73
74
        $this->admin->createjob($form->getData(), $form);
75
    }
76
}
77