|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Communibase; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
|
6
|
|
|
use GuzzleHttp\Handler\MockHandler; |
|
7
|
|
|
use GuzzleHttp\HandlerStack; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class ConnectorTest |
|
11
|
|
|
* |
|
12
|
|
|
* @todo still alot more tests should be added, these are a first start. Feel free to submit new/better tests! |
|
13
|
|
|
* @package Communibase |
|
14
|
|
|
*/ |
|
15
|
|
|
class ConnectorTest extends \PHPUnit_Framework_TestCase |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @test |
|
19
|
|
|
*/ |
|
20
|
|
|
public function constructor() |
|
21
|
|
|
{ |
|
22
|
|
|
new Connector('', ''); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @test |
|
27
|
|
|
*/ |
|
28
|
|
|
public function constructorWithClient() |
|
29
|
|
|
{ |
|
30
|
|
|
$connector = new Connector('test', '', $this->getHttpClient()); |
|
31
|
|
|
$this->assertInstanceOf(Connector::class, $connector); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @test |
|
36
|
|
|
* @expectedException Exception |
|
37
|
|
|
*/ |
|
38
|
|
|
public function getBinary() |
|
39
|
|
|
{ |
|
40
|
|
|
$connector = new Connector('test', ''); |
|
41
|
|
|
/** @noinspection UnusedFunctionResultInspection */ |
|
42
|
|
|
$connector->getBinary(''); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @test |
|
47
|
|
|
* @throws \Communibase\Exception |
|
48
|
|
|
*/ |
|
49
|
|
|
public function getByRefNeedsCorrectKeysInRef() |
|
50
|
|
|
{ |
|
51
|
|
|
$connector = new Connector('test', ''); |
|
52
|
|
|
$this->setExpectedException(\InvalidArgumentException::class); |
|
53
|
|
|
$document = $connector->getByRef([], []); |
|
|
|
|
|
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @return array |
|
58
|
|
|
*/ |
|
59
|
|
|
public function isIdProvider() |
|
60
|
|
|
{ |
|
61
|
|
|
return [ |
|
62
|
|
|
['507f1f77bcf86cd799439011', true], |
|
63
|
|
|
['507f191e810c19729de860ea', true], |
|
64
|
|
|
['54b7ed2b49726734cab0570c', true], |
|
65
|
|
|
['123c', false], |
|
66
|
|
|
['t', false], |
|
67
|
|
|
['t', false], |
|
68
|
|
|
['58a2d90012f9ae00c647d0fc((\'.,.', false], |
|
69
|
|
|
]; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @dataProvider isIdProvider |
|
74
|
|
|
* @test |
|
75
|
|
|
* @param $id |
|
76
|
|
|
* @param $isValid |
|
77
|
|
|
*/ |
|
78
|
|
|
public function isIdValid($id, $isValid) |
|
79
|
|
|
{ |
|
80
|
|
|
$this->assertSame($isValid, Connector::isIdValid($id)); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @test |
|
85
|
|
|
* @throws \Communibase\Exception |
|
86
|
|
|
*/ |
|
87
|
|
|
public function getByIdsWithEmptyArrayReturnsArray() { |
|
88
|
|
|
$connector = new Connector('test', ''); |
|
89
|
|
|
$this->assertSame([], $connector->getByIds('', [])); |
|
90
|
|
|
$this->assertSame([], $connector->getByIds('Person', [])); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @test |
|
96
|
|
|
* @depends isIdValid |
|
97
|
|
|
*/ |
|
98
|
|
|
public function generateId() |
|
99
|
|
|
{ |
|
100
|
|
|
$id = Connector::generateId(); |
|
101
|
|
|
|
|
102
|
|
|
$this->assertTrue(Connector::isIdValid($id)); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Create a new HttpClient (as a mock) |
|
107
|
|
|
* The responses can be injected thus easily reusable |
|
108
|
|
|
* |
|
109
|
|
|
* @see http://docs.guzzlephp.org/en/latest/testing.html |
|
110
|
|
|
* |
|
111
|
|
|
* @param array $responses |
|
112
|
|
|
* |
|
113
|
|
|
* @return Client |
|
114
|
|
|
*/ |
|
115
|
|
|
private function getHttpClient(array $responses = []) |
|
116
|
|
|
{ |
|
117
|
|
|
|
|
118
|
|
|
$mock = new MockHandler($responses); |
|
119
|
|
|
$handler = HandlerStack::create($mock); |
|
120
|
|
|
|
|
121
|
|
|
return new Client(['handler' => $handler]); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
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.