This repository was archived by the owner on Nov 30, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 375
/
Copy pathUtilTest.php
126 lines (101 loc) · 3.81 KB
/
UtilTest.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
<?php
namespace Osiset\ShopifyApp\Test;
use Illuminate\Support\Facades\Config;
use LogicException;
use Osiset\ShopifyApp\Util;
use stdClass;
class UtilTest extends TestCase
{
public function testHmacCreator(): void
{
// Set the secret to use for HMAC creations
$secret = 'hello';
// Raw data
$data = 'one-two-three';
$this->assertSame(
hash_hmac('sha256', $data, $secret, true),
Util::createHmac(['data' => $data, 'raw' => true], $secret)->toNative()
);
// Raw data encoded
$data = 'one-two-three';
$this->assertSame(
base64_encode(hash_hmac('sha256', $data, $secret, true)),
Util::createHmac(['data' => $data, 'raw' => true, 'encode' => true], $secret)->toNative()
);
// Query build (sorts array and builds query string)
$data = ['one' => 1, 'two' => 2, 'three' => 3];
$this->assertSame(
hash_hmac('sha256', 'one=1three=3two=2', $secret, false),
Util::createHmac(['data' => $data, 'buildQuery' => true], $secret)->toNative()
);
}
public function testRegisterPackageRoutes(): void
{
$this->expectExceptionObject(new LogicException('Excluded routes must be an array', 0));
// Routes to exclude
$routes = explode(',', 'home,billing');
$this->assertTrue(Util::registerPackageRoute('authenticate', false));
$this->assertTrue(Util::registerPackageRoute('authenticate', []));
$this->assertTrue(Util::registerPackageRoute('authenticate', $routes));
$this->assertFalse(Util::registerPackageRoute('home', $routes));
Util::registerPackageRoute('home', stdClass::class);
}
public function testRouteNames(): void
{
// non-dot-notation route name
$this->assertSame(
Util::getShopifyConfig('route_names.home'),
'home'
);
// dot-notation route name
$this->assertSame(
Util::getShopifyConfig('route_names.authenticate.token'),
'authenticate.token'
);
}
public function testGetShopifyConfig(): void
{
$this->app['config']->set('shopify-app.config_api_callback', function (string $key, $shop) {
if ($key === 'api_secret') {
return 'hello world';
}
return Config::get("shopify-app.{$key}");
});
$secret = Util::getShopifyConfig('api_secret');
$grantMode = Util::getShopifyConfig('api_grant_mode');
$this->assertSame('hello world', $secret);
$this->assertSame('OFFLINE', $grantMode);
}
public function testGraphQLWebhookTopic(): void
{
// REST-format topics are changed to the GraphQL format
$topics = [
'app/uninstalled' => 'APP_UNINSTALLED',
'orders/partially_fulfilled' => 'ORDERS_PARTIALLY_FULFILLED',
'order_transactions/create' => 'ORDER_TRANSACTIONS_CREATE',
];
foreach ($topics as $restTopic => $graphQLTopic) {
$this->assertEquals(
$graphQLTopic,
Util::getGraphQLWebhookTopic($restTopic)
);
}
// GraphQL-format topics are unchanged
$this->assertEquals(
'ORDERS_PARTIALLY_FULFILLED',
Util::getGraphQLWebhookTopic('ORDERS_PARTIALLY_FULFILLED')
);
}
public function testUseNativeAppBridgeIsTrue(): void
{
$this->app['config']->set('shopify-app.frontend_engine', 'VUE');
$result = Util::useNativeAppBridge();
$this->assertTrue($result);
}
public function testUseNativeAppBridgeIsFalse(): void
{
$this->app['config']->set('shopify-app.frontend_engine', 'REACT');
$result = Util::useNativeAppBridge();
$this->assertFalse($result);
}
}