Completed
Push — master ( c1b2ad...3084e3 )
by Harry
02:22
created

FirehoseAdapterTest::testEnqueue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 1
eloc 14
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 Aws\ResultInterface;
19
use Aws\Firehose\FirehoseClient;
20
use Graze\Queue\Adapter\Exception\MethodNotSupportedException;
21
use Graze\Queue\Message\MessageFactoryInterface;
22
use Graze\Queue\Message\MessageInterface;
23
use Mockery as m;
24
use Mockery\MockInterface;
25
use PHPUnit_Framework_TestCase as TestCase;
26
27
class FirehoseAdapterTest extends TestCase
28
{
29
    /** @var MessageInterface|MockInterface */
30
    private $messageA;
31
    /** @var MessageInterface|MockInterface */
32
    private $messageB;
33
    /** @var MessageInterface|MockInterface */
34
    private $messageC;
35
    /** @var MessageInterface[]|MockInterface[] */
36
    private $messages;
37
    /** @var ResultInterface|MockInterface */
38
    private $model;
39
    /** @var MessageFactoryInterface|MockInterface */
40
    private $factory;
41
    /** @var FirehoseClient */
42
    private $client;
43
44 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...
45
    {
46
        $this->client = m::mock(FirehoseClient::class);
47
        $this->model = m::mock(ResultInterface::class);
48
        $this->factory = m::mock(MessageFactoryInterface::class);
49
50
        $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...
51
        $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...
52
        $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...
53
        $this->messages = [$a, $b, $c];
54
    }
55
56
    public function testInterface()
57
    {
58
        assertThat(new FirehoseAdapter($this->client, 'foo'), is(anInstanceOf('Graze\Queue\Adapter\AdapterInterface')));
59
    }
60
61
    public function testEnqueue()
62
    {
63
        $adapter = new FirehoseAdapter($this->client, 'foo');
64
65
        $this->messageA->shouldReceive('getBody')->once()->withNoArgs()->andReturn('foo');
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Graze\Queue\Message\MessageInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
66
        $this->messageB->shouldReceive('getBody')->once()->withNoArgs()->andReturn('bar');
67
        $this->messageC->shouldReceive('getBody')->once()->withNoArgs()->andReturn('baz');
68
69
        $this->model->shouldReceive('get')->once()->with('RequestResponses')->andReturn([]);
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Aws\ResultInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
70
71
        $this->client->shouldReceive('putRecordBatch')->once()->with([
72
            'DeliveryStreamName' => 'foo',
73
            'Records' => [
74
                ['Data' => 'foo'],
75
                ['Data' => 'bar'],
76
                ['Data' => 'baz'],
77
            ],
78
        ])->andReturn($this->model);
79
80
        $adapter->enqueue($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...
81
    }
82
83
    /**
84
     * @expectedException \Graze\Queue\Adapter\Exception\MethodNotSupportedException
85
     */
86
    public function testAcknowledge()
87
    {
88
        $adapter = new FirehoseAdapter($this->client, 'foo');
89
        $adapter->acknowledge($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...
90
    }
91
92
    /**
93
     * @expectedException \Graze\Queue\Adapter\Exception\MethodNotSupportedException
94
     */
95
    public function testDequeue()
96
    {
97
        $adapter = new FirehoseAdapter($this->client, 'foo');
98
        $adapter->dequeue($this->factory, 10);
99
    }
100
101
    /**
102
     * @expectedException \Graze\Queue\Adapter\Exception\MethodNotSupportedException
103
     */
104
    public function testPurge()
105
    {
106
        $adapter = new FirehoseAdapter($this->client, 'foo');
107
        $adapter->purge();
108
    }
109
110
    /**
111
     * @expectedException \Graze\Queue\Adapter\Exception\MethodNotSupportedException
112
     */
113
    public function testDelete()
114
    {
115
        $adapter = new FirehoseAdapter($this->client, 'foo');
116
        $adapter->delete();
117
    }
118
}
119