-
Notifications
You must be signed in to change notification settings - Fork 189
Expand file tree
/
Copy pathBaseTestCase.php
More file actions
139 lines (120 loc) · 4.3 KB
/
BaseTestCase.php
File metadata and controls
139 lines (120 loc) · 4.3 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
<?php
declare(strict_types=1);
namespace ShopifyTest;
use GuzzleHttp\Psr7\Response;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Http\Client\ClientInterface;
use Shopify\ApiVersion;
use Shopify\Clients\HttpClientFactory;
use Shopify\Context;
use Shopify\Exception\HttpRequestException;
use ShopifyTest\Auth\MockSessionStorage;
use ShopifyTest\Clients\MockRequest;
define('RUNNING_SHOPIFY_TESTS', 1);
class BaseTestCase extends TestCase
{
/**
* API version to use for tests. Uses the latest available version.
*/
protected const TEST_API_VERSION = ApiVersion::APRIL_2026;
protected const TEST_API_SECRET = '7008c5f4da4718b9b45d26d3fcbbb157';
protected const TEST_API_SECRET_ALT = 'b4f15c37e89a23d6c701f4e82a9d5f0b';
/** @var string */
protected $domain = 'test-shop.myshopify.io';
/** @var string */
protected $version;
public function setUp(): void
{
// Initialize Context before each test
Context::initialize(
apiKey: 'ash',
apiSecretKey: self::TEST_API_SECRET,
scopes: ['sleepy', 'kitty'],
hostName: 'www.my-friends-cats.com',
sessionStorage: new MockSessionStorage(),
apiVersion: self::TEST_API_VERSION,
);
Context::$RETRY_TIME_IN_SECONDS = 0;
$this->version = require dirname(__FILE__) . '/../src/version.php';
// Make sure we always mock the transport layer so we don't accidentally make real requests
$this->mockTransportRequests([]);
}
/**
* Builds a mock HTTP response that can optionally also validate the parameters of the cURL call.
*
* @param int|null $statusCode The HTTP status code to return
* @param string|array|null $body The body of the HTTP response
* @param array $headers The headers expected in the response
* @param string|null $error The cURL error message to return
*
* @return array
*/
protected function buildMockHttpResponse(
?int $statusCode = null,
$body = null,
array $headers = [],
?string $error = null
): array {
if ($body && !is_string($body)) {
$body = json_encode($body);
}
return [
'statusCode' => $statusCode,
'body' => $body,
'headers' => $headers,
'error' => $error,
];
}
/**
* Sets up a transport layer mock that expects the given requests to happen.
*
* @param MockRequest[] $requests
*/
public function mockTransportRequests(array $requests): void
{
$requestMatchers = [];
$newResponses = [];
foreach ($requests as $request) {
$matcher = new HttpRequestMatcher(
$request->url,
$request->method,
"/$request->userAgent/",
$request->headers,
$request->body ?? "",
true,
$request->identicalBody
);
$requestMatchers[] = $matcher;
$newResponses[] = $request->error ? 'TEST EXCEPTION' : new Response(
$request->response['statusCode'],
$request->response['headers'],
$request->response['body'],
);
}
/** @var MockObject */
$client = $this->createMock(ClientInterface::class);
$i = 0;
$client->expects($this->exactly(count($requestMatchers)))
->method('sendRequest')
->with(self::callback(function ($request) use (&$i, $requestMatchers) {
return $requestMatchers[$i]->matches($request);
}))
->willReturnCallback(
function () use (&$i, $newResponses) {
$response = $newResponses[$i++];
if ($response === 'TEST EXCEPTION') {
throw new HttpRequestException();
} else {
return $response;
}
}
);
/** @var MockObject */
$factory = $this->createMock(HttpClientFactory::class);
$factory->expects($this->any())
->method('client')
->willReturn($client);
Context::$HTTP_CLIENT_FACTORY = $factory;
}
}