Completed
Push — develop ( c8d2e9...9b1d71 )
by Adam
03:00
created

BuilderTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 55
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A testCreateConfiguredClient() 0 10 1
A testCanAddPlugins() 0 11 1
A testWithCredentials() 0 9 1
A testInvalidVersionThrowsException() 0 6 1
1
<?php
2
3
namespace IBM\Watson\Common\tests\HttpClient;
4
5
use Http\Client\Common\Plugin;
6
use Http\Client\HttpClient;
7
use Http\Message\UriFactory;
8
use IBM\Watson\Common\HttpClient\Builder;
9
use PHPUnit\Framework\TestCase;
10
use Mockery as m;
11
12
class BuilderTest extends TestCase
13
{
14
    private $httpClient;
15
    private $uriFactory;
16
17
    public function setUp()
18
    {
19
        $this->httpClient = m::mock(HttpClient::class);
20
        $this->uriFactory = m::mock(UriFactory::class);
21
    }
22
23
    public function testCreateConfiguredClient()
24
    {
25
        // Test builder can use preconfigured http client.
26
        $client = (new Builder($this->httpClient))->createConfiguredClient();
27
        $this->assertInstanceOf(HttpClient::class, $client);
28
29
        // Test builder discovers http client when not specified.
30
        $client = (new Builder())->createConfiguredClient();
31
        $this->assertInstanceOf(HttpClient::class, $client);
32
    }
33
34
    public function testCanAddPlugins()
35
    {
36
        $firstPlugin = m::mock(Plugin::class);
37
        $secondPlugin = m::mock(Plugin::class);
38
        $thirdPlugin = m::mock(Plugin::class);
39
40
        $builder = new Builder($this->httpClient);
41
        $builder->addPlugin($firstPlugin, $secondPlugin, $thirdPlugin);
42
43
        $this->assertInstanceOf(HttpClient::class, $builder->createConfiguredClient());
44
    }
45
46
    public function testWithCredentials()
47
    {
48
        $client = (new Builder($this->httpClient))
49
            ->withCredentials('username', 'password')
50
            ->withHost('https://api.host.com/')
51
            ->createConfiguredClient();
52
53
        $this->assertInstanceOf(HttpClient::class, $client);
54
    }
55
56
    /**
57
     * @expectedException \IBM\Watson\Common\Exception\Api\InvalidArgumentException
58
     * @expectedExceptionMessage Version must be a date in the Y-m-d format
59
     */
60
    public function testInvalidVersionThrowsException()
61
    {
62
        $client = (new Builder($this->httpClient))
0 ignored issues
show
Unused Code introduced by
$client is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
63
            ->withVersion('34-43-23')
64
            ->createConfiguredClient();
65
    }
66
}
67