Skip to content

Commit 92cbe19

Browse files
author
Your Name
committed
updated with quality suggestion
1 parent 3668a5a commit 92cbe19

2 files changed

Lines changed: 60 additions & 70 deletions

File tree

tests/VCS/Adapter/GiteaTest.php

Lines changed: 6 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use Utopia\Tests\Base;
99
use Utopia\VCS\Adapter\Git;
1010
use Utopia\VCS\Adapter\Git\Gitea;
11-
use Utopia\Fetch\Client;
1211

1312
class GiteaTest extends Base
1413
{
@@ -57,71 +56,6 @@ private function setupGitea(): void
5756
}
5857
}
5958

60-
private function configureWebhook(string $owner, string $repositoryName, string $secret): int
61-
{
62-
$catcherUrl = System::getEnv('TESTS_GITEA_REQUEST_CATCHER_URL', 'http://request-catcher:5000') ?? '';
63-
64-
return $this->vcsAdapter->createWebhook(
65-
$owner,
66-
$repositoryName,
67-
$catcherUrl . '/webhook',
68-
$secret
69-
);
70-
}
71-
72-
/** @return array<mixed> */
73-
private function getLastWebhookRequest(): array
74-
{
75-
$catcherUrl = System::getEnv('TESTS_GITEA_REQUEST_CATCHER_URL', 'http://request-catcher:5000') ?? '';
76-
77-
$client = new Client();
78-
$response = $client->fetch(
79-
url: "{$catcherUrl}/__last_request__",
80-
method: 'GET'
81-
);
82-
83-
if ($response->getStatusCode() < 200 || $response->getStatusCode() >= 300) {
84-
return [];
85-
}
86-
87-
$body = $response->text();
88-
89-
if (empty($body)) {
90-
return [];
91-
}
92-
93-
return json_decode($body, true) ?? [];
94-
}
95-
96-
private function assertEventually(callable $probe, int $timeoutMs = 15000, int $waitMs = 500): void
97-
{
98-
$start = microtime(true) * 1000;
99-
$lastException = null;
100-
101-
while ((microtime(true) * 1000 - $start) < $timeoutMs) {
102-
try {
103-
$probe();
104-
return;
105-
} catch (\Throwable $e) {
106-
$lastException = $e;
107-
usleep($waitMs * 1000);
108-
}
109-
}
110-
111-
throw $lastException ?? new \Exception('assertEventually timed out');
112-
}
113-
114-
private function clearWebhookRequests(): void
115-
{
116-
$catcherUrl = System::getEnv('TESTS_GITEA_REQUEST_CATCHER_URL', 'http://request-catcher:5000') ?? '';
117-
118-
$client = new Client();
119-
$client->fetch(
120-
url: "{$catcherUrl}/__clear__",
121-
method: 'DELETE'
122-
);
123-
}
124-
12559
public function testCreateRepository(): void
12660
{
12761
$owner = self::$owner;
@@ -1308,8 +1242,9 @@ public function testWebhookPushEvent(): void
13081242
$this->vcsAdapter->createRepository(self::$owner, $repositoryName, false);
13091243

13101244
try {
1311-
$this->clearWebhookRequests();
1312-
$this->configureWebhook(self::$owner, $repositoryName, $secret);
1245+
$catcherUrl = System::getEnv('TESTS_GITEA_REQUEST_CATCHER_URL', 'http://request-catcher:5000') ?? '';
1246+
$this->deleteLastWebhookRequest();
1247+
$this->vcsAdapter->createWebhook(self::$owner, $repositoryName, $catcherUrl . '/webhook', $secret);
13131248

13141249
// Trigger a real push by creating a file
13151250
$this->vcsAdapter->createFile(
@@ -1364,10 +1299,11 @@ public function testWebhookPullRequestEvent(): void
13641299
$this->vcsAdapter->createBranch(self::$owner, $repositoryName, 'feature-branch', 'main');
13651300
$this->vcsAdapter->createFile(self::$owner, $repositoryName, 'feature.txt', 'content', 'Add feature', 'feature-branch');
13661301

1367-
$this->configureWebhook(self::$owner, $repositoryName, $secret);
1302+
$catcherUrl = System::getEnv('TESTS_GITEA_REQUEST_CATCHER_URL', 'http://request-catcher:5000') ?? '';
1303+
$this->vcsAdapter->createWebhook(self::$owner, $repositoryName, $catcherUrl . '/webhook', $secret);
13681304

13691305
// Clear after setup so only PR event will arrive
1370-
$this->clearWebhookRequests();
1306+
$this->deleteLastWebhookRequest();
13711307

13721308
// Trigger real PR event
13731309
$this->vcsAdapter->createPullRequest(

tests/VCS/Base.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Exception;
66
use PHPUnit\Framework\TestCase;
7+
use Utopia\Fetch\Client;
78
use Utopia\System\System;
89
use Utopia\VCS\Adapter\Git;
910
use Utopia\VCS\Adapter\Git\GitHub;
@@ -33,6 +34,59 @@ abstract public function testGetPullRequest(): void;
3334

3435
abstract public function testGetRepositoryTree(): void;
3536

37+
/** @return array<mixed> */
38+
protected function getLastWebhookRequest(): array
39+
{
40+
$catcherUrl = System::getEnv('TESTS_GITEA_REQUEST_CATCHER_URL', 'http://request-catcher:5000') ?? '';
41+
42+
$client = new Client();
43+
$response = $client->fetch(
44+
url: "{$catcherUrl}/__last_request__",
45+
method: 'GET'
46+
);
47+
48+
if ($response->getStatusCode() < 200 || $response->getStatusCode() >= 300) {
49+
return [];
50+
}
51+
52+
$body = $response->text();
53+
54+
if (empty($body)) {
55+
return [];
56+
}
57+
58+
return json_decode($body, true) ?? [];
59+
}
60+
61+
protected function assertEventually(callable $probe, int $timeoutMs = 15000, int $waitMs = 500): void
62+
{
63+
$start = microtime(true) * 1000;
64+
$lastException = null;
65+
66+
while ((microtime(true) * 1000 - $start) < $timeoutMs) {
67+
try {
68+
$probe();
69+
return;
70+
} catch (\Throwable $e) {
71+
$lastException = $e;
72+
usleep($waitMs * 1000);
73+
}
74+
}
75+
76+
throw $lastException ?? new \Exception('assertEventually timed out');
77+
}
78+
79+
protected function deleteLastWebhookRequest(): void
80+
{
81+
$catcherUrl = System::getEnv('TESTS_GITEA_REQUEST_CATCHER_URL', 'http://request-catcher:5000') ?? '';
82+
83+
$client = new Client();
84+
$client->fetch(
85+
url: "{$catcherUrl}/__clear__",
86+
method: 'DELETE'
87+
);
88+
}
89+
3690
public function testGetPullRequestFromBranch(): void
3791
{
3892
$result = $this->vcsAdapter->getPullRequestFromBranch('vermakhushboo', 'basic-js-crud', 'test');

0 commit comments

Comments
 (0)