Completed
Push — master ( 9eae13...e56856 )
by Antonio
04:26
created

testEnqueueDequeueAndAcknowledge()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 25
rs 8.8571
cc 1
eloc 13
nc 1
nop 0
1
<?php
2
namespace Da\Mailer\Test\Queue\Backend\Pdo;
3
4
use Da\Mailer\Model\MailMessage;
5
use Da\Mailer\Queue\Backend\Pdo\PdoQueueStoreAdapter;
6
use Da\Mailer\Test\AbstractMySqlDatabaseTestCase;
7
use Da\Mailer\Test\Fixture\FixtureHelper;
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
    public function testEnqueueDequeueAndAcknowledge()
24
    {
25
        $mailJob = FixtureHelper::getPdoMailJob();
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::getPdoMailJob();
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::getPdoMailJob();
75
        $this->pdoQueueStore->ack($mailJob);
76
    }
77
}
78