|
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)) |
|
|
|
|
|
|
63
|
|
|
->withVersion('34-43-23') |
|
64
|
|
|
->createConfiguredClient(); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.