Skip to content

Commit

Permalink
Merge pull request #16 from utopia-php/add-tests-for-list-contents
Browse files Browse the repository at this point in the history
Add tests for listContents endpoint
  • Loading branch information
christyjacob4 committed May 17, 2024
2 parents 104e47e + 3b54947 commit e538264
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/VCS/Adapter/Git/GitHub.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,15 @@ public function createRepository(string $owner, string $repositoryName, bool $pr
*
* @throws Exception
*/
public function searchRepositories(string $owner, int $page, int $per_page, string $search=''): array
public function searchRepositories(string $owner, int $page, int $per_page, string $search = ''): array
{
$url = '/search/repositories';

$response = $this->call(self::METHOD_GET, $url, ['Authorization' => "Bearer $this->accessToken"], [
'q' => "{$search} user:{$owner} fork:true",
'per_page' => $per_page,
'sort' => 'updated'
]);
]);

if (!isset($response['body']['items'])) {
throw new Exception("Repositories list missing in the response.");
Expand Down Expand Up @@ -191,9 +191,15 @@ public function listRepositoryContents(string $owner, string $repositoryName, st
return [];
}

return array_map(static function ($item) {
return $item['name'];
}, $response['body']);
if (isset($response['body'][0])) {
return array_column($response['body'], 'name');
}

if (isset($response['body']['name'])) {
return [$response['body']['name']];
}

return [];
}

public function deleteRepository(string $owner, string $repositoryName): bool
Expand Down Expand Up @@ -303,7 +309,10 @@ protected function generateAccessToken(string $privateKey, string $githubAppId):
$token = $jwt->encode($payload);
$this->jwtToken = $token;
$res = $this->call(self::METHOD_POST, '/app/installations/' . $this->installationId . '/access_tokens', ['Authorization' => 'Bearer ' . $token]);
$this->accessToken = $res['body']['token'] ?? '';
if (!isset($res['body']['token'])) {
throw new Exception('Failed to retrieve access token from GitHub API.');
}
$this->accessToken = $res['body']['token'];
}

/**
Expand Down
44 changes: 44 additions & 0 deletions tests/VCS/Adapter/GitHubTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,50 @@ public function testGetRepositoryName(): void
$this->assertEquals('basic-js-crud', $repositoryName);
}

public function testListRepositoryContents(): void
{
$owner = 'test-kh';
$repositoryName = 'test1';
$path = '';
$contents = $this->vcsAdapter->listRepositoryContents($owner, $repositoryName, $path);

$this->assertIsArray($contents);
$this->assertNotEmpty($contents);

// test for non-existent path
$path = 'non-existent-path';
$contents = $this->vcsAdapter->listRepositoryContents($owner, $repositoryName, $path);
$this->assertIsArray($contents);
$this->assertEmpty($contents);

// test for a valid folder
$path = 'src';
$contents = $this->vcsAdapter->listRepositoryContents($owner, $repositoryName, $path);
$this->assertIsArray($contents);
$this->assertNotEmpty($contents);

// test for an invalid repo
$repositoryName = 'test3';
$path = '';
$contents = $this->vcsAdapter->listRepositoryContents($owner, $repositoryName, $path);
$this->assertIsArray($contents);
$this->assertEmpty($contents);

// test for an empty repository
$repositoryName = 'test2';
$path = '';
$contents = $this->vcsAdapter->listRepositoryContents($owner, $repositoryName, $path);
$this->assertIsArray($contents);
$this->assertEmpty($contents);

// test for an absolute path
$repositoryName = 'test1';
$path = 'README.md';
$contents = $this->vcsAdapter->listRepositoryContents($owner, $repositoryName, $path);
$this->assertIsArray($contents);
$this->assertNotEmpty($contents);
}

public function testGetPullRequest(): void
{
$owner = 'vermakhushboo';
Expand Down

0 comments on commit e538264

Please sign in to comment.