Completed
Push — master ( 03ffe6...2c1c5a )
by Will
11s
created

ArrayAdapterTest::testAcknowledge()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
/**
4
 * This file is part of graze/queue.
5
 *
6
 * Copyright (c) 2015 Nature Delivered Ltd. <https://www.graze.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @license https://github.com/graze/queue/blob/master/LICENSE MIT
12
 *
13
 * @link    https://github.com/graze/queue
14
 */
15
16
namespace Graze\Queue\Adapter;
17
18
use Graze\Queue\Message\MessageFactoryInterface;
19
use Graze\Queue\Message\MessageInterface;
20
use Mockery as m;
21
use Mockery\MockInterface;
22
use PHPUnit_Framework_TestCase as TestCase;
23
24
class ArrayAdapterTest extends TestCase
25
{
26
    /** @var MessageFactoryInterface|MockInterface */
27
    private $factory;
28
    /** @var MessageInterface|MockInterface */
29
    private $messageA;
30
    /** @var MessageInterface|MockInterface */
31
    private $messageB;
32
    /** @var MessageInterface|MockInterface */
33
    private $messageC;
34
    /** @var MessageInterface[]|MockInterface[] */
35
    private $messages;
36
    /** @var ArrayAdapter */
37
    private $adapter;
38
39 View Code Duplication
    public function setUp()
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
        $this->factory = m::mock(MessageFactoryInterface::class);
42
43
        $this->messageA = $a = m::mock(MessageInterface::class);
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $a. Configured minimum length is 2.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
44
        $this->messageB = $b = m::mock(MessageInterface::class);
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $b. Configured minimum length is 2.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
45
        $this->messageC = $c = m::mock(MessageInterface::class);
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $c. Configured minimum length is 2.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
46
        $this->messages = [$a, $b, $c];
47
48
        $this->adapter = new ArrayAdapter($this->messages);
0 ignored issues
show
Documentation introduced by
$this->messages is of type array<integer,object<Moc...kery\\MockInterface>"}>, but the function expects a array<integer,object<Gra...sage\MessageInterface>>.

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...
49
    }
50
51
    public function testInterface()
52
    {
53
        assertThat($this->adapter, is(anInstanceOf(AdapterInterface::class)));
54
    }
55
56
    public function testAcknowledge()
57
    {
58
        $this->adapter->acknowledge($this->messages);
59
60
        $iterator = $this->adapter->dequeue($this->factory, 10);
61
62
        assertThat(iterator_to_array($iterator), is(identicalTo([])));
63
    }
64
65
    public function testAcknowledgeOne()
66
    {
67
        $this->adapter->acknowledge([$this->messageB]);
68
69
        $iterator = $this->adapter->dequeue($this->factory, 10);
70
71
        assertThat(iterator_to_array($iterator), is(identicalTo([$this->messageA, $this->messageC])));
72
    }
73
74
    public function testDequeue()
75
    {
76
        $iterator = $this->adapter->dequeue($this->factory, 10);
77
78
        assertThat(iterator_to_array($iterator), is(identicalTo($this->messages)));
79
    }
80
81
    public function testDequeueWithLimit()
82
    {
83
        $iterator = $this->adapter->dequeue($this->factory, 1);
84
85
        assertThat(iterator_to_array($iterator), is(identicalTo([$this->messageA])));
86
    }
87
88
    public function testDequeueWithPollingLimit()
89
    {
90
        $iterator = $this->adapter->dequeue($this->factory, null);
91
92
        assertThat(iterator_to_array($iterator), is(identicalTo($this->messages)));
93
    }
94
95 View Code Duplication
    public function testDequeueWithNoMessages()
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...
96
    {
97
        $adapter = new ArrayAdapter();
98
99
        $iterator = $adapter->dequeue($this->factory, null);
100
101
        assertThat(iterator_to_array($iterator), is(emptyArray()));
102
    }
103
104 View Code Duplication
    public function testDequeueWithLimitAndNoMessages()
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...
105
    {
106
        $adapter = new ArrayAdapter();
107
108
        $iterator = $adapter->dequeue($this->factory, 10);
109
110
        assertThat(iterator_to_array($iterator), is(emptyArray()));
111
    }
112
113
    public function testEnqueue()
114
    {
115
        $messageA = m::mock(MessageInterface::class);
116
        $messageB = m::mock(MessageInterface::class);
117
        $messageC = m::mock(MessageInterface::class);
118
        $messages = [$messageA, $messageB, $messageC];
119
        $merged = array_merge($this->messages, $messages);
120
121
        $this->adapter->enqueue($messages);
122
123
        $iterator = $this->adapter->dequeue($this->factory, 10);
124
125
        assertThat(iterator_to_array($iterator), is(identicalTo($merged)));
126
    }
127
128 View Code Duplication
    public function testPurge()
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...
129
    {
130
        $iterator = $this->adapter->dequeue($this->factory, 10);
131
132
        assertThat(iterator_to_array($iterator), is(nonEmptyArray()));
133
134
        $this->adapter->purge();
135
136
        $iterator = $this->adapter->dequeue($this->factory, 10);
137
138
        assertThat(iterator_to_array($iterator), is(emptyArray()));
139
    }
140
141 View Code Duplication
    public function testDelete()
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...
142
    {
143
        $iterator = $this->adapter->dequeue($this->factory, 10);
144
145
        assertThat(iterator_to_array($iterator), is(nonEmptyArray()));
146
147
        $this->adapter->delete();
148
149
        $iterator = $this->adapter->dequeue($this->factory, 10);
150
151
        assertThat(iterator_to_array($iterator), is(emptyArray()));
152
    }
153
}
154