Skip to content

Commit

Permalink
Merge pull request #9 from utopia-php/refactor-vcs-tests
Browse files Browse the repository at this point in the history
Add tests for create and delete repos
  • Loading branch information
vermakhushboo committed Oct 19, 2023
2 parents be86617 + c293d06 commit d161d11
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/VCS/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ abstract public function createRepository(string $owner, string $repositoryName,
/**
* Delete repository
*/
abstract public function deleteRepository(string $owner, string $repositoryName): void;
abstract public function deleteRepository(string $owner, string $repositoryName): bool;

/**
* Get latest opened pull request with specific base branch
Expand Down
11 changes: 9 additions & 2 deletions src/VCS/Adapter/Git/GitHub.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,18 @@ public function listRepositoryContents(string $owner, string $repositoryName, st
}, $response['body']);
}

public function deleteRepository(string $owner, string $repositoryName): void
public function deleteRepository(string $owner, string $repositoryName): bool
{
$url = "/repos/{$owner}/{$repositoryName}";

$this->call(self::METHOD_DELETE, $url, ['Authorization' => "Bearer $this->accessToken"]);
$response = $this->call(self::METHOD_DELETE, $url, ['Authorization' => "Bearer $this->accessToken"]);

$statusCode = $response['headers']['status-code'];

if ($statusCode >= 400) {
throw new Exception("Deleting repository $repositoryName failed with status code $statusCode");
}
return true;
}

/**
Expand Down
19 changes: 19 additions & 0 deletions tests/VCS/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Utopia\Tests;

use Exception;
use PHPUnit\Framework\TestCase;
use Utopia\App;
use Utopia\VCS\Adapter\Git;
Expand Down Expand Up @@ -81,4 +82,22 @@ public function testListRepositoryContents(): void
$this->assertIsArray($contents);
$this->assertNotEmpty($contents);
}

public function testCreateRepository(): void
{
$repository = $this->vcsAdapter->createRepository('test-kh', 'new-repo', true);
$this->assertIsArray($repository);
$this->assertEquals('test-kh/new-repo', $repository['full_name']);
}

/**
* @depends testCreateRepository
*/
public function testDeleteRepository(): void
{
$result = $this->vcsAdapter->deleteRepository('test-kh', 'new-repo');
$this->assertEquals(true, $result);
$this->expectException(Exception::class);
$result = $this->vcsAdapter->deleteRepository('test-kh', 'new-repo-2');
}
}

0 comments on commit d161d11

Please sign in to comment.