-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathApiClientTest.php
173 lines (144 loc) · 5.29 KB
/
ApiClientTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
// Copyright 1999-2025. WebPros International GmbH.
namespace PleskXTest;
use PleskX\Api\Client\Exception;
class ApiClientTest extends AbstractTestCase
{
public function testWrongProtocol()
{
$this->expectException(\PleskX\Api\Exception::class);
$this->expectExceptionCode(1005);
$packet = static::$client->getPacket('100.0.0');
$packet->addChild('server')->addChild('get_protos');
static::$client->request($packet);
}
public function testUnknownOperator()
{
$this->expectException(\PleskX\Api\Exception::class);
$this->expectExceptionCode(1014);
$packet = static::$client->getPacket();
$packet->addChild('unknown');
static::$client->request($packet);
}
public function testInvalidXmlRequest()
{
$this->expectException(\PleskX\Api\Exception::class);
$this->expectExceptionCode(1014);
static::$client->request('<packet><wrongly formatted xml</packet>');
}
public function testInvalidCredentials()
{
$this->expectException(\PleskX\Api\Exception::class);
$this->expectExceptionCode(1001);
$host = static::$client->getHost();
$port = static::$client->getPort();
$protocol = static::$client->getProtocol();
$client = new \PleskX\Api\Client($host, $port, $protocol);
$client->setCredentials('bad-login', 'bad-password');
$packet = static::$client->getPacket();
$packet->addChild('server')->addChild('get_protos');
$client->request($packet);
}
public function testInvalidSecretKey()
{
$this->expectException(\PleskX\Api\Exception::class);
$this->expectExceptionCode(11003);
$host = static::$client->getHost();
$port = static::$client->getPort();
$protocol = static::$client->getProtocol();
$client = new \PleskX\Api\Client($host, $port, $protocol);
$client->setSecretKey('bad-key');
$packet = static::$client->getPacket();
$packet->addChild('server')->addChild('get_protos');
$client->request($packet);
}
public function testLatestMajorProtocol()
{
$packet = static::$client->getPacket('1.6');
$packet->addChild('server')->addChild('get_protos');
$result = static::$client->request($packet);
$this->assertEquals('ok', $result->status);
}
public function testLatestMinorProtocol()
{
$packet = static::$client->getPacket('1.6.5');
$packet->addChild('server')->addChild('get_protos');
$result = static::$client->request($packet);
$this->assertEquals('ok', $result->status);
}
public function testRequestShortSyntax()
{
$response = static::$client->request('server.get.gen_info');
$this->assertGreaterThan(0, strlen($response->gen_info->server_name));
}
public function testOperatorPlainRequest()
{
$response = static::$client->server()->request('get.gen_info');
$this->assertGreaterThan(0, strlen($response->gen_info->server_name));
$this->assertEquals(36, strlen($response->getValue('server_guid')));
}
public function testRequestArraySyntax()
{
$response = static::$client->request([
'server' => [
'get' => [
'gen_info' => '',
],
],
]);
$this->assertGreaterThan(0, strlen($response->gen_info->server_name));
}
public function testOperatorArraySyntax()
{
$response = static::$client->server()->request(['get' => ['gen_info' => '']]);
$this->assertGreaterThan(0, strlen($response->gen_info->server_name));
}
public function testMultiRequest()
{
$responses = static::$client->multiRequest([
'server.get_protos',
'server.get.gen_info',
]);
$this->assertCount(2, $responses);
$protos = (array) $responses[0]->protos->proto;
$generalInfo = $responses[1];
$this->assertContains('1.6.6.0', $protos);
$this->assertGreaterThan(0, strlen($generalInfo->gen_info->server_name));
}
public function testConnectionError()
{
$this->expectException(\PleskX\Api\Client\Exception::class);
$client = new \PleskX\Api\Client('invalid-host.dom');
$client->server()->getProtos();
}
public function testGetHost()
{
$client = new \PleskX\Api\Client('example.dom');
$this->assertEquals('example.dom', $client->getHost());
}
public function testGetPort()
{
$client = new \PleskX\Api\Client('example.dom', 12345);
$this->assertEquals(12345, $client->getPort());
}
public function testGetProtocol()
{
$client = new \PleskX\Api\Client('example.dom', 8880, 'http');
$this->assertEquals('http', $client->getProtocol());
}
public function testSetVerifyResponse()
{
static::$client->setVerifyResponse(function ($xml) {
if ($xml->xpath('//proto')) {
throw new Exception('proto');
}
});
try {
static::$client->server()->getProtos();
} catch (Exception $e) {
$this->assertEquals('proto', $e->getMessage());
} finally {
static::$client->setVerifyResponse();
}
}
}