|
1
|
|
|
<?php |
|
2
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the package neoblack/free-at-home-api. |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please read the |
|
9
|
|
|
* LICENSE file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace NeoBlack\FreeAtHomeApi\Test\Unit\Service; |
|
13
|
|
|
|
|
14
|
|
|
use NeoBlack\FreeAtHomeApi\Client; |
|
15
|
|
|
use NeoBlack\FreeAtHomeApi\Exception\InvalidEndpointException; |
|
16
|
|
|
use NeoBlack\FreeAtHomeApi\Exception\MissingCredentialsException; |
|
17
|
|
|
use NeoBlack\FreeAtHomeApi\Exception\MissingEndpointException; |
|
18
|
|
|
use NeoBlack\FreeAtHomeApi\Test\Unit\Fixtures\HttpClient; |
|
19
|
|
|
use PHPUnit\Framework\TestCase; |
|
20
|
|
|
|
|
21
|
|
|
class ClientTest extends TestCase |
|
22
|
|
|
{ |
|
23
|
|
|
protected function createTestClient(): Client |
|
24
|
|
|
{ |
|
25
|
|
|
return (new Client(new HttpClient())) |
|
26
|
|
|
->withEndpoint('https://localhost') |
|
27
|
|
|
->withCredentials('foo', 'bar') |
|
28
|
|
|
->getClient(); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @test |
|
33
|
|
|
*/ |
|
34
|
|
|
public function getClientWithoutEndpointThrowsException(): void |
|
35
|
|
|
{ |
|
36
|
|
|
$this->expectException(MissingEndpointException::class); |
|
37
|
|
|
(new Client())->getClient(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @test |
|
42
|
|
|
*/ |
|
43
|
|
|
public function getClientWithoutCredentialsThrowsException(): void |
|
44
|
|
|
{ |
|
45
|
|
|
$this->expectException(MissingCredentialsException::class); |
|
46
|
|
|
(new Client())->withEndpoint('https://localhost')->getClient(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @return array |
|
51
|
|
|
*/ |
|
52
|
|
|
public function invalidEndpointDataProvider(): array |
|
53
|
|
|
{ |
|
54
|
|
|
return [ |
|
55
|
|
|
'/' => ['/', false], |
|
56
|
|
|
'/foo' => ['/foo', false], |
|
57
|
|
|
'foo' => ['foo', false], |
|
58
|
|
|
'http://localhost' => ['http://localhost', true], |
|
59
|
|
|
'https://localhost' => ['https://localhost', true], |
|
60
|
|
|
'http://localhost.foo' => ['http://localhost.foo', true], |
|
61
|
|
|
'https://localhost.foo' => ['https://localhost.foo', true], |
|
62
|
|
|
'http://localhost.foo/foo/bar' => ['http://localhost.foo/foo/bar', true], |
|
63
|
|
|
'https://localhost.foo/foo/bar' => ['https://localhost.foo/foo/bar', true], |
|
64
|
|
|
'http://localhost.foo:8888/foo/bar' => ['http://localhost.foo:8888/foo/bar', true], |
|
65
|
|
|
'https://localhost.foo:8888/foo/bar' => ['https://localhost.foo:8888/foo/bar', true], |
|
66
|
|
|
]; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @test |
|
71
|
|
|
* @dataProvider invalidEndpointDataProvider |
|
72
|
|
|
*/ |
|
73
|
|
|
public function withEndpointThrowsExceptionForInvalidValues(string $endpoint, $isValid): void |
|
74
|
|
|
{ |
|
75
|
|
|
if (!$isValid) { |
|
76
|
|
|
$this->expectException(InvalidEndpointException::class); |
|
77
|
|
|
} |
|
78
|
|
|
/** @noinspection UnnecessaryAssertionInspection */ |
|
79
|
|
|
$this->assertInstanceOf(Client::class, (new Client())->withEndpoint($endpoint)); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @test |
|
84
|
|
|
*/ |
|
85
|
|
|
public function getClientReturnClient(): void |
|
86
|
|
|
{ |
|
87
|
|
|
/** @noinspection UnnecessaryAssertionInspection */ |
|
88
|
|
|
$this->assertInstanceOf(Client::class, (new Client()) |
|
89
|
|
|
->withEndpoint('https://localhost') |
|
90
|
|
|
->withCredentials('foo', 'bar') |
|
91
|
|
|
->getClient()); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @test |
|
96
|
|
|
*/ |
|
97
|
|
|
public function getDevices(): void |
|
98
|
|
|
{ |
|
99
|
|
|
$this->assertSame([], $this->createTestClient()->getDevices()); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @test |
|
104
|
|
|
*/ |
|
105
|
|
|
public function getDevice(): void |
|
106
|
|
|
{ |
|
107
|
|
|
$this->assertSame([], $this->createTestClient()->getDevice('4711-foo-bar')); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @test |
|
112
|
|
|
*/ |
|
113
|
|
|
public function updateDevice(): void |
|
114
|
|
|
{ |
|
115
|
|
|
$this->assertSame([], $this->createTestClient()->updateDevice('4711-foo-bar', ['name' => 'New Name'])); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @test |
|
120
|
|
|
*/ |
|
121
|
|
|
public function deleteDevice(): void |
|
122
|
|
|
{ |
|
123
|
|
|
$this->assertSame([], $this->createTestClient()->deleteDevice('4711-foo-bar')); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|