Completed
Push — master ( d068c5...99fa9d )
by Antonio
03:22 queued 58s
created

PdoQueueStoreAdapterTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 69
Duplicated Lines 36.23 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 1
cbo 4
dl 25
loc 69
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 1
B testEnqueueDequeueAndAcknowledge() 25 25 1
A testAcknowledgementToUpdateMailJobs() 0 19 1
A testBadMethodCallExceptionOnAck() 0 5 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace Da\tests\Queue\Database;
3
4
use Da\Mailer\Model\MailMessage;
5
use Da\Mailer\Test\Fixture\FixtureHelper;
6
use Da\Mailer\Queue\Backend\Pdo\PdoQueueStoreAdapter;
7
use Da\Mailer\Test\AbstractMySqlDatabaseTestCase;
8
9
class PdoQueueStoreAdapterTest extends AbstractMySqlDatabaseTestCase
10
{
11
    /**
12
     * @var PdoQueueStoreAdapter
13
     */
14
    private $pdoQueueStore;
15
16
    protected function setUp()
17
    {
18
        parent::setUp();
19
20
        $this->pdoQueueStore = new PdoQueueStoreAdapter(self::getPdoQueueStoreConnection());
21
    }
22
23 View Code Duplication
    public function testEnqueueDequeueAndAcknowledge()
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...
24
    {
25
        $mailJob = FixtureHelper::getMailJob();
26
27
        $this->assertSame($this->pdoQueueStore, $this->pdoQueueStore->init());
28
29
        $this->assertTrue($this->pdoQueueStore->enqueue($mailJob));
30
31
        $this->assertTrue($this->pdoQueueStore->isEmpty() === false);
32
33
        $mailJob = $this->pdoQueueStore->dequeue();
34
35
        $this->assertTrue($this->pdoQueueStore->isEmpty() === true); // message set to 'A' on process
36
37
        $this->assertTrue(!empty($mailJob->getMessage()));
38
39
        $dequeuedMailMessage = MailMessage::fromArray(json_decode($mailJob->getMessage(), true));
40
41
        $this->assertEquals(FixtureHelper::getMailMessage(), $dequeuedMailMessage);
42
43
        $mailJob->markAsCompleted();
44
        $this->pdoQueueStore->ack($mailJob);
0 ignored issues
show
Bug introduced by
It seems like $mailJob defined by $this->pdoQueueStore->dequeue() on line 33 can be null; however, Da\Mailer\Queue\Backend\...ueueStoreAdapter::ack() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
45
46
        $this->assertTrue($this->pdoQueueStore->dequeue() === null);
47
    }
48
49
    public function testAcknowledgementToUpdateMailJobs()
50
    {
51
        $mailJob = FixtureHelper::getMailJob();
52
53
        $this->pdoQueueStore->enqueue($mailJob);
54
        $this->assertTrue($this->pdoQueueStore->isEmpty() === false);
55
        $dequedMailJob = $this->pdoQueueStore->dequeue();
56
        $this->assertTrue($this->pdoQueueStore->isEmpty() === true);
57
        // enqueue it back to be able to get it
58
        // we could actually set the time to be processed in the future :)
59
        // lets simply update the increment
60
        $dequedMailJob->incrementAttempt();
61
        $dequedMailJob->setTimeToSend(date('Y-m-d H:i:s', time() + 1));
62
        $this->assertEquals(1, $dequedMailJob->getAttempt());
63
        $dequedMailJob->markAsNew();
64
        $this->pdoQueueStore->ack($dequedMailJob);
0 ignored issues
show
Bug introduced by
It seems like $dequedMailJob defined by $this->pdoQueueStore->dequeue() on line 55 can be null; however, Da\Mailer\Queue\Backend\...ueueStoreAdapter::ack() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
65
        sleep(1);
66
        $this->assertTrue($this->pdoQueueStore->isEmpty() === false);
67
    }
68
69
    /**
70
     * @expectedException \BadMethodCallException
71
     */
72
    public function testBadMethodCallExceptionOnAck()
73
    {
74
        $mailJob = FixtureHelper::getMailJob();
75
        $this->pdoQueueStore->ack($mailJob);
76
    }
77
}
78