diff --git a/config/services.xml b/config/services.xml index b027511..d639085 100644 --- a/config/services.xml +++ b/config/services.xml @@ -33,16 +33,14 @@ + %doctrine.website.github.http_token% - + - - - diff --git a/lib/Github/GithubClientProvider.php b/lib/Github/GithubClientProvider.php index 1970b2d..c98fa54 100644 --- a/lib/Github/GithubClientProvider.php +++ b/lib/Github/GithubClientProvider.php @@ -4,25 +4,40 @@ declare(strict_types=1); namespace Doctrine\Website\Github; +use Github\Api\Repo; use Github\AuthMethod; use Github\Client; use InvalidArgumentException; +use Psr\Cache\CacheItemPoolInterface; -/** @final */ -class GithubClientProvider +use function assert; + +final class GithubClientProvider { private bool $authenticated = false; public function __construct( private readonly Client $githubClient, + CacheItemPoolInterface $cache, private readonly string $githubHttpToken, ) { if ($githubHttpToken === '') { throw new InvalidArgumentException('You must configure a Github http token.'); } + + $this->githubClient->addCache($cache); } - public function getGithubClient(): Client + public function repositories(): Repo + { + $repositories = $this->getGithubClient()->api('repo'); + + assert($repositories instanceof Repo); + + return $repositories; + } + + private function getGithubClient(): Client { if ($this->authenticated === false) { $this->githubClient->authenticate($this->githubHttpToken, '', AuthMethod::ACCESS_TOKEN); diff --git a/lib/Projects/ProjectGitSyncer.php b/lib/Projects/ProjectGitSyncer.php index 07da306..d7f85b6 100644 --- a/lib/Projects/ProjectGitSyncer.php +++ b/lib/Projects/ProjectGitSyncer.php @@ -4,7 +4,6 @@ declare(strict_types=1); namespace Doctrine\Website\Projects; -use Doctrine\Website\Github\GithubClientProvider; use Doctrine\Website\ProcessFactory; use Github\Api\Repo; @@ -15,15 +14,11 @@ use function sprintf; /** @final */ class ProjectGitSyncer { - private readonly Repo $githubRepo; - public function __construct( private readonly ProcessFactory $processFactory, - GithubClientProvider $githubClientProvider, + private readonly Repo $githubRepo, private readonly string $projectsDir, ) { - // TODO Inject Repo instead of GithubClientProvider - $this->githubRepo = $githubClientProvider->getGithubClient()->repo(); } public function isRepositoryInitialized(string $repositoryName): bool diff --git a/tests/Github/GithubClientProviderTest.php b/tests/Github/GithubClientProviderTest.php index f9b5bd2..d44db70 100644 --- a/tests/Github/GithubClientProviderTest.php +++ b/tests/Github/GithubClientProviderTest.php @@ -6,39 +6,47 @@ namespace Doctrine\Website\Tests\Github; use Doctrine\Website\Github\GithubClientProvider; use Doctrine\Website\Tests\TestCase; +use Github\Api\Repo; use Github\AuthMethod; use Github\Client; use InvalidArgumentException; +use Psr\Cache\CacheItemPoolInterface; class GithubClientProviderTest extends TestCase { - public function testGetGithubClient(): void + public function testRepositories(): void { + $githubRepo = $this->createMock(Repo::class); $githubClient = $this->createMock(Client::class); + $cache = $this->createMock(CacheItemPoolInterface::class); $githubHttpToken = '1234'; - $githubClientProvider = new GithubClientProvider($githubClient, $githubHttpToken); - $githubClient->expects(self::once()) ->method('authenticate') ->with($githubHttpToken, '', AuthMethod::ACCESS_TOKEN); + $githubClient->expects(self::once()) + ->method('addCache') + ->with($cache); + $githubClient->expects(self::once()) + ->method('api') + ->with('repo') + ->willReturn($githubRepo); - $githubClientResult = $githubClientProvider->getGithubClient(); + $githubClientProvider = new GithubClientProvider($githubClient, $cache, $githubHttpToken); - self::assertSame($githubClient, $githubClientResult); + $githubClientResult = $githubClientProvider->repositories(); - $githubClientResult = $githubClientProvider->getGithubClient(); - - self::assertSame($githubClient, $githubClientResult); + self::assertSame($githubRepo, $githubClientResult); } public function testGetGithubClientWithMissingToken(): void { $githubClient = $this->createMock(Client::class); + $cache = $this->createMock(CacheItemPoolInterface::class); $githubHttpToken = ''; $this->expectException(InvalidArgumentException::class); - new GithubClientProvider($githubClient, $githubHttpToken); + new GithubClientProvider($githubClient, $cache, $githubHttpToken); } } diff --git a/tests/Projects/ProjectGitSyncerTest.php b/tests/Projects/ProjectGitSyncerTest.php index bba6150..6c16259 100644 --- a/tests/Projects/ProjectGitSyncerTest.php +++ b/tests/Projects/ProjectGitSyncerTest.php @@ -4,12 +4,10 @@ declare(strict_types=1); namespace Doctrine\Website\Tests\Projects; -use Doctrine\Website\Github\GithubClientProvider; use Doctrine\Website\ProcessFactory; use Doctrine\Website\Projects\ProjectGitSyncer; use Doctrine\Website\Tests\TestCase; use Github\Api\Repo; -use Github\Client; use org\bovigo\vfs\vfsStream; use PHPUnit\Framework\MockObject\MockObject; @@ -34,21 +32,10 @@ class ProjectGitSyncerTest extends TestCase $this->processFactory = $this->createMock(ProcessFactory::class); $this->projectsDir = vfsStream::url('projects'); $this->githubRepo = $this->createMock(Repo::class); - $githubClientProvider = $this->createMock(GithubClientProvider::class); - $githubClient = $this->getMockBuilder(Client::class) - ->disableOriginalConstructor() - ->addMethods(['repo']) - ->getMock(); - - $githubClient->method('repo') - ->willReturn($this->githubRepo); - - $githubClientProvider->method('getGithubClient') - ->willReturn($githubClient); $this->projectGitSyncer = new ProjectGitSyncer( $this->processFactory, - $githubClientProvider, + $this->githubRepo, $this->projectsDir, ); }