CleanupJobTest::testByStatus()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 12

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
dl 15
loc 15
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 12
nc 1
nop 0
1
<?php
2
3
use SilverStripe\Dev\SapphireTest;
4
use SilverStripe\Core\Config\Config;
5
use SilverStripe\ORM\FieldType\DBDatetime;
6
use Symbiote\QueuedJobs\Jobs\CleanupJob;
7
8
/**
9
 * @author Andrew Aitken-Fincham <[email protected]>
10
 */
11
class CleanupJobTest 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...
12
{
13
    /**
14
     * {@inheritDoc}
15
     * @var string
16
     */
17
    protected static $fixture_file = 'CleanupJobFixture.yml';
18
19
    /**
20
     * {@inheritDoc}
21
     */
22
    public function setUp()
23
    {
24
        // Have to set a fake time here to work with
25
        // the LastEdited dates in the fixture
26
        DBDatetime::set_mock_now("2002-02-03 02:02:02");
27
        parent::setUp();
28
    }
29
30
    /**
31
     * {@inheritDoc}
32
     */
33
    public function tearDown()
34
    {
35
        parent::tearDown();
36
        DBDatetime::clear_mock_now();
37
    }
38
39 View Code Duplication
    public function testByDays()
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...
40
    {
41
        $job = new CleanupJob();
42
        Config::modify()->set('Symbiote\\QueuedJobs\\Jobs\\CleanupJob', 'cleanup_method', 'age');
43
        Config::modify()->set('Symbiote\\QueuedJobs\\Jobs\\CleanupJob', 'cleanup_value', 30);
44
        Config::inst()->remove('Symbiote\\QueuedJobs\\Jobs\\CleanupJob', 'cleanup_statuses');
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface SilverStripe\Config\Coll...nfigCollectionInterface as the method remove() does only exist in the following implementations of said interface: SilverStripe\Config\Coll...\MemoryConfigCollection.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
45
        Config::modify()->set(
46
            'Symbiote\\QueuedJobs\\Jobs\\CleanupJob',
47
            'cleanup_statuses',
48
            array('Broken', 'Complete')
49
        );
50
        $job->process();
51
        $data = $job->getJobData();
52
        $this->assertContains("2 jobs cleaned up.", $data->messages[0]);
53
    }
54
55 View Code Duplication
    public function testByNumber()
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...
56
    {
57
        $job = new CleanupJob();
58
        Config::modify()->set('Symbiote\\QueuedJobs\\Jobs\\CleanupJob', 'cleanup_method', 'number');
59
        Config::modify()->set('Symbiote\\QueuedJobs\\Jobs\\CleanupJob', 'cleanup_value', 3);
60
        Config::inst()->remove('Symbiote\\QueuedJobs\\Jobs\\CleanupJob', 'cleanup_statuses');
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface SilverStripe\Config\Coll...nfigCollectionInterface as the method remove() does only exist in the following implementations of said interface: SilverStripe\Config\Coll...\MemoryConfigCollection.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
61
        Config::modify()->set(
62
            'Symbiote\\QueuedJobs\\Jobs\\CleanupJob',
63
            'cleanup_statuses',
64
            array('Broken', 'Complete')
65
        );
66
        $job->process();
67
        $data = $job->getJobData();
68
        $this->assertContains("2 jobs cleaned up.", $data->messages[0]);
69
    }
70
71 View Code Duplication
    public function testByStatus()
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...
72
    {
73
        $job = new CleanupJob();
74
        Config::modify()->set('Symbiote\\QueuedJobs\\Jobs\\CleanupJob', 'cleanup_method', 'number');
75
        Config::modify()->set('Symbiote\\QueuedJobs\\Jobs\\CleanupJob', 'cleanup_value', 3);
76
        Config::inst()->remove('Symbiote\\QueuedJobs\\Jobs\\CleanupJob', 'cleanup_statuses');
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface SilverStripe\Config\Coll...nfigCollectionInterface as the method remove() does only exist in the following implementations of said interface: SilverStripe\Config\Coll...\MemoryConfigCollection.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
77
        Config::modify()->set(
78
            'Symbiote\\QueuedJobs\\Jobs\\CleanupJob',
79
            'cleanup_statuses',
80
            array('Broken', 'Complete', 'New')
81
        );
82
        $job->process();
83
        $data = $job->getJobData();
84
        $this->assertContains("3 jobs cleaned up.", $data->messages[0]);
85
    }
86
87 View Code Duplication
    public function testNoCleanup()
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...
88
    {
89
        $job = new CleanupJob();
90
        Config::modify()->set('Symbiote\\QueuedJobs\\Jobs\\CleanupJob', 'cleanup_method', 'number');
91
        Config::modify()->set('Symbiote\\QueuedJobs\\Jobs\\CleanupJob', 'cleanup_value', 6);
92
        Config::inst()->remove('Symbiote\\QueuedJobs\\Jobs\\CleanupJob', 'cleanup_statuses');
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface SilverStripe\Config\Coll...nfigCollectionInterface as the method remove() does only exist in the following implementations of said interface: SilverStripe\Config\Coll...\MemoryConfigCollection.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
93
        Config::modify()->set(
94
            'Symbiote\\QueuedJobs\\Jobs\\CleanupJob',
95
            'cleanup_statuses',
96
            array('Broken', 'Complete', 'New')
97
        );
98
        $job->process();
99
        $data = $job->getJobData();
100
        $this->assertContains("No jobs to clean up.", $data->messages[0]);
101
    }
102
}
103