1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PSFS\tests\base; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
use PSFS\base\Service; |
7
|
|
|
use PSFS\base\Singleton; |
8
|
|
|
use PSFS\base\types\CurlService; |
9
|
|
|
use PSFS\tests\examples\AuthServiceTest; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class ServiceTest |
13
|
|
|
* @package PSFS\tests |
14
|
|
|
*/ |
15
|
|
|
class ServiceTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
private function hasInternet() |
19
|
|
|
{ |
20
|
|
|
// use 80 for http or 443 for https protocol |
21
|
|
|
$connected = @fsockopen("https://github.com", 443); |
22
|
|
|
if ($connected) { |
23
|
|
|
fclose($connected); |
24
|
|
|
return true; |
25
|
|
|
} |
26
|
|
|
return false; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
protected function getServiceInstance(): Service |
30
|
|
|
{ |
31
|
|
|
$srv = Service::getInstance(); |
32
|
|
|
// Check instance |
33
|
|
|
$this->assertInstanceOf(Service::class, $srv, '$srv is not a Service class'); |
34
|
|
|
// Check Singleton |
35
|
|
|
$this->assertInstanceOf(Singleton::class, $srv, '$srv is not a Singleton class'); |
36
|
|
|
// Check initialization |
37
|
|
|
$this->assertNull($srv->getUrl(), 'Service has previous url set'); |
38
|
|
|
$srv->setUrl('www.example.com'); |
39
|
|
|
$this->assertNotEmpty($srv->getUrl(), 'Service has empty url'); |
40
|
|
|
$srv->setUrl('www.google.com'); |
41
|
|
|
$this->assertNotEquals('www.example.com', $srv->getUrl(), 'Service does not update url'); |
42
|
|
|
return $srv; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
protected function checkParams(Service $srv) |
46
|
|
|
{ |
47
|
|
|
$paramName = uniqid('test'); |
48
|
|
|
$paramValue = microtime(true); |
49
|
|
|
// CHeck when param didn't exist |
50
|
|
|
$param = $srv->getParam($paramName); |
51
|
|
|
$this->assertNull($param, 'Param exists before test'); |
52
|
|
|
// Set params with bulk method |
53
|
|
|
$srv->setParams([$paramName => $paramValue]); |
54
|
|
|
$this->assertNotEmpty($srv->getParams(), 'Service params are empty'); |
55
|
|
|
$param = $srv->getParam($paramName); |
56
|
|
|
$this->assertNotNull($param, 'Param did not exists'); |
57
|
|
|
// Check to remove params |
58
|
|
|
$count = count($srv->getParams()); |
59
|
|
|
$srv->dropParam($paramName); |
60
|
|
|
$this->assertNotEquals(count($srv->getParams()), $count, 'Param not dropped'); |
61
|
|
|
// Check adding one param |
62
|
|
|
$srv->addParam($paramName, $paramValue); |
63
|
|
|
$param = $srv->getParam($paramName); |
64
|
|
|
$this->assertNotNull($param, 'Param did not exists'); |
65
|
|
|
$this->assertEquals($paramValue, $param, 'Different param value'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
protected function checkOptions(Service $srv) |
69
|
|
|
{ |
70
|
|
|
// Clean data |
71
|
|
|
$srv->setOptions([]); |
72
|
|
|
// CHeck when option didn't exist |
73
|
|
|
$option = $srv->getOption(CURLOPT_CONNECTTIMEOUT); |
74
|
|
|
$this->assertNull($option, 'Option exists before test'); |
75
|
|
|
// Set option with bulk method |
76
|
|
|
$srv->setOptions([CURLOPT_CONNECTTIMEOUT => 30]); |
77
|
|
|
$this->assertNotEmpty($srv->getOptions(), 'Service options are empty'); |
78
|
|
|
$option = $srv->getOption(CURLOPT_CONNECTTIMEOUT); |
79
|
|
|
$this->assertNotNull($option, 'Option did not exists'); |
80
|
|
|
// Check to remove options |
81
|
|
|
$count = count($srv->getOptions()); |
82
|
|
|
$srv->dropOption(CURLOPT_CONNECTTIMEOUT); |
83
|
|
|
$this->assertNotEquals(count($srv->getOptions()), $count, 'Option not dropped'); |
84
|
|
|
// Check adding one option |
85
|
|
|
$srv->addOption(CURLOPT_CONNECTTIMEOUT, 30); |
86
|
|
|
$option = $srv->getOption(CURLOPT_CONNECTTIMEOUT); |
87
|
|
|
$this->assertNotNull($option, 'Option did not exists'); |
88
|
|
|
$this->assertEquals(30, $option, 'Different option value'); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testServiceTraits() |
92
|
|
|
{ |
93
|
|
|
$srv = $this->getServiceInstance(); |
94
|
|
|
$this->assertInstanceOf(Service::class, $srv, '$srv is not a Service class'); |
95
|
|
|
$this->checkParams($srv); |
96
|
|
|
$this->checkOptions($srv); |
97
|
|
|
|
98
|
|
|
// Initialize url, with default second param, the service has to clean all variables |
99
|
|
|
$srv->setUrl('https://example.com'); |
100
|
|
|
|
101
|
|
|
// Tests has to be passed again |
102
|
|
|
$this->checkParams($srv); |
103
|
|
|
|
104
|
|
|
// Initialize service without cleaning params and options |
105
|
|
|
$srv->setUrl('https://google.com', false); |
106
|
|
|
$this->assertNotEquals('https://example.com', $srv->getUrl(), 'Service does not update url'); |
107
|
|
|
$this->assertEquals('https://google.com', $srv->getUrl(), 'Service does not update url'); |
108
|
|
|
$this->assertNotEmpty($srv->getParams(), 'Params are empty'); |
109
|
|
|
$this->assertNotEmpty($srv->getOptions(), 'Options are empty'); |
110
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @covers |
115
|
|
|
* @return void |
116
|
|
|
*/ |
117
|
|
|
public function testSimpleCall() |
118
|
|
|
{ |
119
|
|
|
if ($this->hasInternet()) { |
120
|
|
|
$this->markTestIncomplete('Pending make tests'); |
121
|
|
|
} else { |
122
|
|
|
$this->assertTrue(true, 'Not connected to internet'); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function testAuthorizedCall() |
127
|
|
|
{ |
128
|
|
|
$authSrv = AuthServiceTest::getInstance(); |
129
|
|
|
// Generate random user and password |
130
|
|
|
$user = uniqid('user'); |
131
|
|
|
$password = sha1(microtime(true)); |
132
|
|
|
$basicAuth = "{$user}:{$password}"; |
133
|
|
|
// Apply auth to a example service |
134
|
|
|
$authSrv->test($user, $password); |
135
|
|
|
// Check options created into curl resource |
136
|
|
|
$curl = $authSrv->getCon(); |
137
|
|
|
$this->assertInstanceOf(\CurlHandle::class, $curl, 'Curl resource was not created'); |
138
|
|
|
$callInfo = $authSrv->getCallInfo(); |
139
|
|
|
$jsonResponse = $authSrv->getResult(); |
140
|
|
|
// Get specific curl options that have to be set |
141
|
|
|
$authType = $authSrv->getOption(CURLOPT_HTTPAUTH); |
142
|
|
|
$this->assertNotNull($authType, 'Auth not set'); |
143
|
|
|
$this->assertEquals(CURLAUTH_BASIC, $authType, 'Auth basic not set'); |
144
|
|
|
$authString = $authSrv->getOption(CURLOPT_USERPWD); |
145
|
|
|
$this->assertNotNull($authString, 'Basic auth string not set'); |
146
|
|
|
$this->assertEquals($basicAuth, $authString, 'Different auth string'); |
147
|
|
|
// Check request response |
148
|
|
|
$this->assertIsArray($jsonResponse, 'Bad json decoding'); |
149
|
|
|
$this->assertArrayHasKey('request_header', $callInfo, 'Verbose mode not activated'); |
150
|
|
|
if (array_key_exists('request_header', $callInfo)) { |
151
|
|
|
$rawHeaders = explode("\r\n", $callInfo['request_header']); |
152
|
|
|
$headers = []; |
153
|
|
|
foreach ($rawHeaders as $rawHeader) { |
154
|
|
|
$data = explode(": ", $rawHeader, 2); |
155
|
|
|
if (count($data) === 2) { |
156
|
|
|
$headers[strtolower($data[0])] = $data[1]; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
$this->assertArrayHasKey(strtolower('Authorization'), $headers, 'Auth header not set'); |
160
|
|
|
$this->assertArrayHasKey(strtolower(CurlService::PSFS_AUTH_HEADER), $headers, 'PSFS Security header not set'); |
161
|
|
|
if (array_key_exists(strtolower('Authorization'), $headers)) { |
162
|
|
|
$authorization = base64_decode(str_replace('Basic ', '', $headers[strtolower('Authorization')])); |
163
|
|
|
$this->assertEquals($basicAuth, $authorization, 'Basic header different than expected'); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
} |
169
|
|
|
|