-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSupabaseTest.php
More file actions
289 lines (227 loc) · 8.18 KB
/
Copy pathSupabaseTest.php
File metadata and controls
289 lines (227 loc) · 8.18 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<?php
namespace Utopia\Tests\E2E\Sources;
use Utopia\Migration\Destination;
use Utopia\Migration\Resource;
use Utopia\Migration\Source;
use Utopia\Migration\Sources\Supabase;
use Utopia\Migration\Transfer;
use Utopia\Tests\E2E\Adapters\Mock;
class SupabaseTest extends Base
{
protected ?Source $source = null;
protected ?Transfer $transfer = null;
protected ?Destination $destination = null;
protected function setUp(): void
{
// Check DB is online and ready
$pdo = null;
$tries = 5;
while ($tries > 0) {
try {
$pdo = new \PDO('pgsql:host=supabase-db'.';port=5432;dbname=postgres', 'postgres', 'postgres');
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
if ($pdo && $pdo->query('SELECT 1')->fetchColumn() === 1) {
break;
} else {
var_dump('DB was offline, waiting 1s then retrying.');
}
} catch (\PDOException $e) {
}
sleep(1);
$tries--;
}
if (! $pdo || $tries === 0) {
throw new \Exception('DB was offline after 5 tries');
}
$this->source = new Supabase(
'http://supabase-api',
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'supabase-db',
'postgres',
'postgres',
'postgres'
);
$this->destination = new Mock();
$this->transfer = new Transfer($this->source, $this->destination);
}
/**
* @group Supabase
*/
public function testSourceReport()
{
// Test report all
$report = $this->source->report();
$this->assertNotEmpty($report);
return [
'report' => $report,
];
}
/**
* @depends testSourceReport
*
* @group Supabase
*/
public function testRunTransfer($state)
{
$this->transfer->run($this->source->getSupportedResources(),
function () {
}
);
$this->assertEquals(0, count($this->transfer->getReport('error')));
return array_merge($state, [
'transfer' => $this->transfer,
'source' => $this->source,
]);
}
/**
* @depends testRunTransfer
*
* @group Supabase
*/
public function testValidateTransfer($state)
{
$statusCounters = $state['transfer']->getStatusCounters();
$this->assertNotEmpty($statusCounters);
foreach ($statusCounters as $resource => $counters) {
$this->assertNotEmpty($counters);
if ($counters[Resource::STATUS_ERROR] > 0) {
$this->fail('Resource '.$resource.' has '.$counters[Resource::STATUS_ERROR].' errors');
return;
}
}
return $state;
}
/**
* @depends testValidateTransfer
*
* @group Supabase
*/
public function testValidateUserTransfer($state): void
{
// Find known user
$users = $state['source']->cache->get(Resource::TYPE_USER);
$this->assertGreaterThan(0, count($users));
$foundUser = null;
foreach ($users as $user) {
/** @var \Utopia\Migration\Resources\Auth\User $user */
if ($user->getEmail() == 'albert.kihn95@yahoo.com') {
$foundUser = $user;
break;
}
}
if (! $foundUser) {
$this->fail('User "albert.kihn95@yahoo.com" not found');
return;
}
$this->assertEquals('success', $foundUser->getStatus());
$this->assertEquals('$2a$10$NGZAAOfXeheUoH9V3dnRoeR.r3J5ynnSZ6KjvHxOUlV8XUrulJzQa', $foundUser->getPasswordHash()->getHash());
$this->assertEquals('bcrypt', $foundUser->getPasswordHash()->getAlgorithm());
$this->assertEquals(['email'], $foundUser->getTypes());
}
/**
* @depends testValidateTransfer
*
* @group Supabase
*/
public function testValidateDatabaseTransfer($state): void
{
// Find known database
$databases = $state['source']->cache->get(Resource::TYPE_DATABASE);
$this->assertGreaterThan(0, count($databases));
$foundDatabase = null;
foreach ($databases as $database) {
/** @var \Utopia\Migration\Resources\Database $database */
if ($database->getDBName() === 'public') {
$foundDatabase = $database;
}
break;
}
if (! $foundDatabase) {
$this->fail('Database "public" not found');
return;
}
$this->assertEquals('success', $foundDatabase->getStatus());
$this->assertEquals('public', $foundDatabase->getDBName());
$this->assertEquals('public', $foundDatabase->getId());
// Find Known Collections
$collections = $state['source']->cache->get(Resource::TYPE_COLLECTION);
$this->assertGreaterThan(0, count($collections));
$foundCollection = null;
foreach ($collections as $collection) {
/** @var \Utopia\Migration\Resources\Database\Collection $collection */
if ($collection->getDatabase()->getDBName() === 'public' && $collection->getCollectionName() === 'test') {
$foundCollection = $collection;
break;
}
}
if (! $foundCollection) {
$this->fail('Collection "test" not found');
return;
}
$this->assertEquals('success', $foundCollection->getStatus());
$this->assertEquals('test', $foundCollection->getCollectionName());
$this->assertEquals('public', $foundCollection->getDatabase()->getDBName());
$this->assertEquals('public', $foundCollection->getDatabase()->getId());
// Find Known Documents
$documents = $state['source']->cache->get(Resource::TYPE_DOCUMENT);
$this->assertGreaterThan(0, count($documents));
$foundDocument = null;
foreach ($documents as $document) {
/** @var \Utopia\Migration\Resources\Database\Document $document */
if ($document->getCollection()->getDatabase()->getDBName() === 'public' && $document->getCollection()->getCollectionName() === 'test') {
$foundDocument = $document;
}
break;
}
if (! $foundDocument) {
$this->fail('Document "1" not found');
return;
}
$this->assertEquals('success', $foundDocument->getStatus());
}
/**
* @depends testValidateTransfer
*
* @group Supabase
*/
public function testValidateStorageTransfer($state): void
{
// Find known bucket
$buckets = $state['source']->cache->get(Resource::TYPE_BUCKET);
$this->assertGreaterThan(0, count($buckets));
$foundBucket = null;
foreach ($buckets as $bucket) {
/** @var \Utopia\Migration\Resources\Storage\Bucket $bucket */
if ($bucket->getBucketName() === 'Test Bucket 1') {
$foundBucket = $bucket;
}
break;
}
if (! $foundBucket) {
$this->fail('Bucket "Test Bucket 1" not found');
return;
}
$this->assertEquals('success', $foundBucket->getStatus());
// Find known file
$files = $state['source']->cache->get(Resource::TYPE_FILE);
$this->assertGreaterThan(0, count($files));
$foundFile = null;
foreach ($files as $file) {
/** @var \Utopia\Migration\Resources\File $file */
if ($file->getFileName() === 'tulips.png') {
$foundFile = $file;
}
break;
}
if (! $foundFile) {
$this->fail('File "tulips.png" not found');
return;
}
/** @var \Utopia\Migration\Resources\Storage\File $foundFile */
$this->assertEquals('success', $foundFile->getStatus());
$this->assertEquals('tulips.png', $foundFile->getFileName());
$this->assertEquals('image/png', $foundFile->getMimeType());
$this->assertEquals(679233, $foundFile->getSize());
$this->assertEquals('', $foundFile->getData()); // Memory Leak Check
}
}