-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathTestHelper.php
More file actions
148 lines (123 loc) · 4 KB
/
TestHelper.php
File metadata and controls
148 lines (123 loc) · 4 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
namespace WorkOS;
use WorkOS\Client;
trait TestHelper
{
/**
* @var \WorkOS\RequestClient\RequestClientInterface
*/
protected $defaultRequestClient;
/**
* @var \PHPUnit\Framework\MockObject\MockObject
*/
protected $requestClientMock;
protected function setUp(): void
{
$this->defaultRequestClient = Client::requestClient();
$this->requestClientMock = $this->createMock("\WorkOS\RequestClient\RequestClientInterface");
}
protected function tearDown(): void
{
WorkOS::setApiKey(null);
WorkOS::setClientId(null);
Client::setRequestClient($this->defaultRequestClient);
}
// Configuration
protected function withApiKey($apiKey = "pk_secretsauce")
{
WorkOS::setApiKey($apiKey);
}
protected function withApiKeyAndClientId($apiKey = "pk_secretsauce", $clientId = "client_pizza")
{
WorkOS::setApiKey($apiKey);
WorkOS::setClientId($clientId);
}
// Requests
protected function mockRequest(
$method,
$path,
$headers = null,
$params = null,
$withAuth = false,
$result = null,
$responseHeaders = null,
$responseCode = 200
) {
Client::setRequestClient($this->requestClientMock);
$url = Client::generateUrl($path);
if (!$headers) {
$requestHeaders = Client::generateBaseHeaders($withAuth);
} else {
$requestHeaders = \array_merge(Client::generateBaseHeaders($withAuth), $headers);
}
if (!$result) {
$result = "{}";
}
if (!$responseHeaders) {
$responseHeaders = [];
}
$this->prepareRequestMock($method, $url, $requestHeaders, $params)
->willReturn([$result, $responseHeaders, $responseCode]);
}
protected function secondMockRequest(
$method,
$path,
$headers = null,
$params = null,
$withAuth = false,
$result = null,
$responseHeaders = null,
$responseCode = 200
) {
Client::setRequestClient($this->requestClientMock);
$url = Client::generateUrl($path);
if (!$headers) {
$requestHeaders = Client::generateBaseHeaders($withAuth);
} else {
$requestHeaders = \array_merge(Client::generateBaseHeaders(), $headers);
}
if (!$result) {
$result = "{}";
}
if (!$responseHeaders) {
$responseHeaders = [];
}
$this->prepareRequestMock($method, $url, $requestHeaders, $params)
->willReturn([$result, $responseHeaders, $responseCode]);
}
private function prepareRequestMock($method, $url, $headers, $params)
{
return $this->requestClientMock
->expects(static::atLeastOnce())->method('request')
->with(
static::identicalTo($method),
static::identicalTo($url),
static::identicalTo($headers),
static::identicalTo($params)
);
}
/**
* Asserts that a specific deprecation warning is triggered when callable is executed
*
* @param string $expected_warning The expected deprecation message
* @param callable $callable The function or method that should trigger the deprecation
* @return mixed The return value from the callable
*/
protected function assertDeprecationTriggered(string $expected_warning, callable $callable)
{
$caught = false;
set_error_handler(function ($errno, $errstr) use ($expected_warning, &$caught) {
if ($errno === E_USER_DEPRECATED && $errstr === $expected_warning) {
$caught = true;
return true;
}
return false;
});
$result = $callable();
restore_error_handler();
if (!$caught) {
$this->fail('Expected deprecation warning was not triggered: ' . $expected_warning);
}
return $result;
}
}