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

ClientTest::testInterface()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
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;
17
18
use ArrayIterator;
19
use Graze\Queue\Adapter\AdapterInterface;
20
use Graze\Queue\Handler\AbstractAcknowledgementHandler;
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 ClientTest extends TestCase
28
{
29
    /** @var AdapterInterface|MockInterface */
30
    private $adapter;
31
    /** @var MessageFactoryInterface|MockInterface */
32
    private $factory;
33
    /** @var AbstractAcknowledgementHandler|MockInterface */
34
    private $handler;
35
    /** @var MessageInterface|MockInterface */
36
    private $messageA;
37
    /** @var MessageInterface|MockInterface */
38
    private $messageB;
39
    /** @var MessageInterface|MockInterface */
40
    private $messageC;
41
    /** @var MessageInterface[]|MockInterface[] */
42
    private $messages;
43
    /** @var Client */
44
    private $client;
45
46
    public function setUp()
47
    {
48
        $this->adapter = m::mock(AdapterInterface::class);
49
        $this->factory = m::mock(MessageFactoryInterface::class);
50
        $this->handler = m::mock(AbstractAcknowledgementHandler::class);
51
52
        $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...
53
        $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...
54
        $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...
55
        $this->messages = [$a, $b, $c];
56
57
        $this->client = new Client($this->adapter, [
58
            'handler'         => $this->handler,
59
            'message_factory' => $this->factory,
60
        ]);
61
    }
62
63
    public function testInterface()
64
    {
65
        assertThat($this->client, is(anInstanceOf(ConsumerInterface::class)));
66
        assertThat($this->client, is(anInstanceOf(DeleterInterface::class)));
67
        assertThat($this->client, is(anInstanceOf(ProducerInterface::class)));
68
        assertThat($this->client, is(anInstanceOf(PurgerInterface::class)));
69
    }
70
71
    public function testCreate()
72
    {
73
        $this->factory->shouldReceive('createMessage')->once()->with('foo', ['bar'])->andReturn($this->messageA);
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Graze\Queue\Message\MessageFactoryInterface.

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...
74
75
        assertThat($this->client->create('foo', ['bar']), is(identicalTo($this->messageA)));
76
    }
77
78
    public function testSend()
79
    {
80
        $this->adapter->shouldReceive('enqueue')->once()->with($this->messages);
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Graze\Queue\Adapter\AdapterInterface.

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...
81
82
        $this->client->send($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...
83
    }
84
85
    public function testReceive()
86
    {
87
        $worker = function () {
88
        };
89
90
        $messages = new ArrayIterator($this->messages);
91
92
        $this->adapter->shouldReceive('dequeue')->once()->with($this->factory, 1)->andReturn($messages);
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Graze\Queue\Adapter\AdapterInterface.

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...
93
        $this->handler->shouldReceive('__invoke')->once()->with($messages, $this->adapter, $worker);
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Graze\Queue\Handler\AbstractAcknowledgementHandler.

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...
94
95
        $this->client->receive($worker);
96
    }
97
98
    public function testPurge()
99
    {
100
        $this->adapter->shouldReceive('purge')->once();
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Graze\Queue\Adapter\AdapterInterface.

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...
101
        $this->client->purge();
102
    }
103
104
    public function testDelete()
105
    {
106
        $this->adapter->shouldReceive('delete')->once();
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Graze\Queue\Adapter\AdapterInterface.

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...
107
        $this->client->delete();
108
    }
109
}
110