-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathClientTest.php
More file actions
130 lines (114 loc) · 3.81 KB
/
ClientTest.php
File metadata and controls
130 lines (114 loc) · 3.81 KB
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
<?php
use PHPUnit\Framework\TestCase;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Client as GuzzleClient;
use OpenComponents\Client;
use OpenComponents\ComponentDataRetriever;
class ClientTest extends TestCase
{
public function testConstructor()
{
// Testing options initialization
$client = new Client([
"serverRendering" => "https://some-registry.com"
], [
"hello" => '1.2.3',
"world" => '~2.2.2',
"nover" => ''
]);
$this->assertInstanceOf(
Client::class,
$client
);
}
public function testRenderComponents()
{
$config = [
"registries" => [
"serverRendering" => "https://some-registry.com"
],
"components" => [
"hello" => '1.2.3',
"world" => '~2.2.2'
]
];
// Instance initialization
$client = new Client($config['registries'], $config['components']);
$client->setComponentDataRetriever(
$this->mockComponentDataRetriever($config)
);
$components = $client->renderComponents([
[
"name" => "oc-client"
]
]);
$this->assertNotNull($components);
$this->assertTrue(isset($components['errors']));
$this->assertTrue(is_array($components['errors']));
$this->assertTrue(isset($components['html']));
$this->assertTrue(is_string($components['html']));
$components = $client->renderComponents([
[
"name" => "oc-client"
],
[
"name" => "world"
]
]);
$this->assertEquals(2, count($components['html']));
$this->assertMatchesRegularExpression('/script/', $components['html'][0]);
}
public function mockComponentDataRetriever($config)
{
$componentResponse = '{
"type": "oc-component",
"version": "0.36.15",
"requestVersion": "",
"name": "oc-client",
"renderMode": "rendered",
"href": "https://some-repo/oc-client",
"html": "<script src=\"//s3-eu-west-1.amazonaws.com/somebucket/components/oc-client/0.36.15/src/oc-client.min.js\" type=\"text/javascript\"></script>"
}';
$componentsResponse = '[
{
"status": 200,
"headers": {},
"response": {
"type": "oc-component",
"version": "0.36.15",
"requestVersion": "",
"name": "oc-client",
"renderMode": "rendered",
"href": "https://some-repo/oc-client",
"html": "<script src=\"//s3-eu-west-1.amazonaws.com/somebucket/components/oc-client/0.36.15/src/oc-client.min.js\" type=\"text/javascript\"></script>"
}
},
{
"status": 200,
"headers": {},
"response": {
"type": "oc-component",
"version": "0.9.0",
"requestVersion": "",
"name": "some-widget",
"renderMode": "rendered",
"href": "https://some-repo/some-widget",
"html": "whatever"
}
}
]';
$mock = new MockHandler([
new Response(200, [], $componentResponse),
new Response(200, [], $componentsResponse),
new Response(200, [], $componentResponse)
]);
$handler = HandlerStack::create($mock);
$httpClient = new GuzzleClient([
'handler' => $handler
]);
$componentDataRetriever = new ComponentDataRetriever($config);
return $componentDataRetriever->setHttpClient($httpClient);
}
}