Completed
Push — develop ( b68cde...86f0dd )
by Adam
57s queued 52s
created

AbstractRequestTest_MockAbstractRequest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 11
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 2
dl 0
loc 11
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getData() 0 3 1
A sendData() 0 4 1
1
<?php
2
3
4
namespace IBM\Watson\Common\Tests\Message;
5
6
use GuzzleHttp\Client;
7
use IBM\Watson\Common\Message\AbstractRequest;
8
use Mockery as m;
9
use Symfony\Component\HttpFoundation\Request;
10
11
class AbstractRequestTest extends \PHPUnit_Framework_TestCase
12
{
13
    protected $request;
14
15
    public function setUp()
16
    {
17
        $this->request = m::mock('\IBM\Watson\Common\Message\AbstractRequest')->makePartial();
18
        $this->request->initialize();
19
    }
20
21
    public function testConstruct()
22
    {
23
        $this->request = new AbstractRequestTest_MockAbstractRequest(new Client(), new Request());
24
        $this->assertSame([], $this->request->getParameters());
25
    }
26
27
    public function testInitializeWithParameters()
28
    {
29
        $this->assertSame($this->request, $this->request->initialize(['username' => 'adam', 'password' => '01234']));
0 ignored issues
show
Bug introduced by
The method initialize does only exist in IBM\Watson\Common\Tests\...est_MockAbstractRequest, but not in Mockery\Mock.

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...
30
        $this->assertSame('adam', $this->request->getUsername());
0 ignored issues
show
Bug introduced by
The method getUsername does only exist in IBM\Watson\Common\Tests\...est_MockAbstractRequest, but not in Mockery\Mock.

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...
31
        $this->assertSame('01234', $this->request->getPassword());
0 ignored issues
show
Bug introduced by
The method getPassword does only exist in IBM\Watson\Common\Tests\...est_MockAbstractRequest, but not in Mockery\Mock.

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...
32
    }
33
34
    /**
35
     * @expectedException \IBM\Watson\Common\Exception\RuntimeException
36
     * @expectedExceptionMessage Request cannot be modified after it has been sent!
37
     */
38
    public function testInitializeAfterRequestSent()
39
    {
40
        $this->request = new AbstractRequestTest_MockAbstractRequest(new Client(), new Request());
41
        $this->request->send();
42
43
        $this->request->initialize();
44
    }
45
46
    /**
47
     * @expectedException \IBM\Watson\Common\Exception\RuntimeException
48
     * @expectedExceptionMessage Request cannot be modified after it has been sent!
49
     */
50
    public function testSetParameterAfterRequestSent()
51
    {
52
        $this->request = new AbstractRequestTest_MockAbstractRequest(new Client(), new Request());
53
        $this->request->send();
54
55
        $this->request->setUsername('adam');
56
    }
57
58
    /**
59
     * @expectedException \IBM\Watson\Common\Exception\RuntimeException
60
     * @expectedExceptionMessage You must call send() before accessing the Response!
61
     */
62
    public function testGetResponseBeforeRequestSent()
63
    {
64
        $this->request = new AbstractRequestTest_MockAbstractRequest(new Client(), new Request());
65
        $this->request->getResponse();
66
    }
67
68
    public function testGetResponseAfterRequestSent()
69
    {
70
        $this->request = new AbstractRequestTest_MockAbstractRequest(new Client(), new Request());
71
        $this->request->send();
72
73
        $response = $this->request->getResponse();
74
        $this->assertInstanceOf('\IBM\Watson\Common\Message\ResponseInterface', $response);
75
    }
76
}
77
78
class AbstractRequestTest_MockAbstractRequest extends AbstractRequest
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
79
{
80
    public function getData()
81
    {
82
    }
83
84
    public function sendData($data)
85
    {
86
        $this->response = m::mock('\IBM\Watson\Common\Message\AbstractResponse');
87
    }
88
}
89