Completed
Pull Request — master (#43)
by Harry
10:13
created

ArrayAdapterTest::testRejectOne()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
dl 8
loc 8
c 0
b 0
f 0
rs 9.4285
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 View Code Duplication
    public function testAcknowledgeOne()
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...
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 testReject()
75
    {
76
        $this->adapter->reject($this->messages);
0 ignored issues
show
Documentation introduced by
$this->messages is of type array<integer,object<Mockery\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...
77
78
        $iterator = $this->adapter->dequeue($this->factory, 10);
79
80
        assertThat(iterator_to_array($iterator), is(identicalTo($this->messages)));
81
    }
82
83 View Code Duplication
    public function testRejectOne()
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...
84
    {
85
        $this->adapter->reject([$this->messageA]);
0 ignored issues
show
Documentation introduced by
array($this->messageA) is of type array<integer,object<Gra...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...
86
87
        $iterator = $this->adapter->dequeue($this->factory, 10);
88
89
        assertThat(iterator_to_array($iterator), is(identicalTo($this->messages)));
90
    }
91
92
    public function testDequeue()
93
    {
94
        $iterator = $this->adapter->dequeue($this->factory, 10);
95
96
        assertThat(iterator_to_array($iterator), is(identicalTo($this->messages)));
97
    }
98
99
    public function testDequeueWithLimit()
100
    {
101
        $iterator = $this->adapter->dequeue($this->factory, 1);
102
103
        assertThat(iterator_to_array($iterator), is(identicalTo([$this->messageA])));
104
    }
105
106
    public function testDequeueWithPollingLimit()
107
    {
108
        $iterator = $this->adapter->dequeue($this->factory, null);
109
110
        assertThat(iterator_to_array($iterator), is(identicalTo($this->messages)));
111
    }
112
113 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...
114
    {
115
        $adapter = new ArrayAdapter();
116
117
        $iterator = $adapter->dequeue($this->factory, null);
118
119
        assertThat(iterator_to_array($iterator), is(emptyArray()));
120
    }
121
122 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...
123
    {
124
        $adapter = new ArrayAdapter();
125
126
        $iterator = $adapter->dequeue($this->factory, 10);
127
128
        assertThat(iterator_to_array($iterator), is(emptyArray()));
129
    }
130
131
    public function testEnqueue()
132
    {
133
        $messageA = m::mock(MessageInterface::class);
134
        $messageB = m::mock(MessageInterface::class);
135
        $messageC = m::mock(MessageInterface::class);
136
        $messages = [$messageA, $messageB, $messageC];
137
        $merged = array_merge($this->messages, $messages);
138
139
        $this->adapter->enqueue($messages);
140
141
        $iterator = $this->adapter->dequeue($this->factory, 10);
142
143
        assertThat(iterator_to_array($iterator), is(identicalTo($merged)));
144
    }
145
146 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...
147
    {
148
        $iterator = $this->adapter->dequeue($this->factory, 10);
149
150
        assertThat(iterator_to_array($iterator), is(nonEmptyArray()));
151
152
        $this->adapter->purge();
153
154
        $iterator = $this->adapter->dequeue($this->factory, 10);
155
156
        assertThat(iterator_to_array($iterator), is(emptyArray()));
157
    }
158
159 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...
160
    {
161
        $iterator = $this->adapter->dequeue($this->factory, 10);
162
163
        assertThat(iterator_to_array($iterator), is(nonEmptyArray()));
164
165
        $this->adapter->delete();
166
167
        $iterator = $this->adapter->dequeue($this->factory, 10);
168
169
        assertThat(iterator_to_array($iterator), is(emptyArray()));
170
    }
171
}
172