Increase code coverage for Projects namespace

This commit is contained in:
Claudio Zizza
2023-09-16 01:45:56 +02:00
committed by Claudio Zizza
parent 9b1312e857
commit 2c19b2693a
7 changed files with 183 additions and 3 deletions

View File

@@ -12,15 +12,19 @@ class GetProjectPackagistData
{
private const PACKAGIST_URL_FORMAT = 'https://packagist.org/packages/%s.json';
public function __construct(private string $packagistUrlFormat = self::PACKAGIST_URL_FORMAT)
{
}
/** @return mixed[] */
public function __invoke(string $composerPackageName): array
{
$packagistUrl = sprintf(self::PACKAGIST_URL_FORMAT, $composerPackageName);
$packagistUrl = sprintf($this->packagistUrlFormat, $composerPackageName);
$response = file_get_contents($packagistUrl);
$projectPackagistData = $response !== false ? json_decode($response, true) : [];
return $projectPackagistData !== false ? $projectPackagistData : [];
return $projectPackagistData ?? [];
}
}

View File

@@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
namespace Doctrine\Website\Tests\Projects;
use Doctrine\Website\Projects\GetProjectPackagistData;
use org\bovigo\vfs\vfsStream;
use PHPUnit\Framework\TestCase;
class GetProjectPackagistDataTest extends TestCase
{
private string $packagistUrl;
protected function setUp(): void
{
vfsStream::setup('url', null, [
'packages' => [
'orm.json' => '{}',
'broken.json' => '{',
],
]);
$this->packagistUrl = vfsStream::url('url') . '/packages/%s.json';
}
public function testFetchingPackagistData(): void
{
$projectPackagistData = new GetProjectPackagistData($this->packagistUrl);
self::assertSame([], $projectPackagistData('orm'));
}
public function testInvalidJson(): void
{
$projectPackagistData = new GetProjectPackagistData($this->packagistUrl);
self::assertSame([], $projectPackagistData('broken'));
}
}

View File

@@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
namespace Doctrine\Website\Tests\Projects;
use Doctrine\Website\Projects\GetTotalDownloads;
use Doctrine\Website\Repositories\ProjectRepository;
use Doctrine\Website\Tests\TestCase;
class GetTotalDownloadsTest extends TestCase
{
public function testGetTotalDownloadsTest(): void
{
$project1 = $this->createProject($this->createProjectData(21));
$project2 = $this->createProject($this->createProjectData(13));
$project3 = $this->createProject($this->createProjectData(8));
$projectRepository = self::createStub(ProjectRepository::class);
$projectRepository->method('findAll')
->willReturn([$project1, $project2, $project3]);
$getTotalDownloads = new GetTotalDownloads($projectRepository);
self::assertSame(42, $getTotalDownloads());
}
/** @return array<string, mixed> */
private function createProjectData(int $totalDownloads): array
{
return [
'packagistData' => [
'package' => [
'downloads' => ['total' => $totalDownloads],
],
],
'versions' => [],
];
}
}

View File

@@ -79,6 +79,50 @@ class ProjectDataReaderTest extends TestCase
self::assertSame($this->projectIntegrationTypes['symfony'], $projectData['integrationType']);
}
public function testReadNoIntegrationType(): void
{
$projectDataReader = new ProjectDataReader(
__DIR__ . '/../test-projects',
[
[
'repositoryName' => 'test-integration-project',
'integration' => true,
],
],
[],
);
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Project integration test-integration-project requires a type.');
$projectDataReader->read('test-integration-project');
}
public function testReadIntegrationTypeDoesNotExist(): void
{
$projectDataReader = new ProjectDataReader(
__DIR__ . '/../test-projects',
[
[
'repositoryName' => 'test-integration-project',
'integration' => true,
'integrationType' => 'symfony',
],
],
[],
);
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Project integration test-integration-project has a type of symfony which does not exist.');
$projectDataReader->read('test-integration-project');
}
public function testReadEmptyJson(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('composer.json file exists in repository empty-json but does not contain any valid data.');
$this->projectDataReader->read('empty-json');
}
protected function setUp(): void
{
$this->projectIntegrationTypes = [

View File

@@ -0,0 +1,21 @@
<?php
declare(strict_types=1);
namespace Doctrine\Website\Tests\Projects;
use Doctrine\Website\Projects\ProjectDataRepository;
use PHPUnit\Framework\TestCase;
class ProjectDataRepositoryTest extends TestCase
{
public function testGetProjectRepositoryNames(): void
{
$projectDataRepository = new ProjectDataRepository([
['repositoryName' => 'orm'],
['repositoryName' => 'dbal'],
]);
self::assertSame(['orm', 'dbal'], $projectDataRepository->getProjectRepositoryNames());
}
}

View File

@@ -10,6 +10,7 @@ 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;
use function sprintf;
@@ -26,8 +27,12 @@ class ProjectGitSyncerTest extends TestCase
protected function setUp(): void
{
vfsStream::setup('projects', null, [
'orm' => ['.git' => []],
]);
$this->processFactory = $this->createMock(ProcessFactory::class);
$this->projectsDir = __DIR__;
$this->projectsDir = vfsStream::url('projects');
$this->githubRepo = $this->createMock(Repo::class);
$githubClientProvider = $this->createMock(GithubClientProvider::class);
$githubClient = $this->getMockBuilder(Client::class)
@@ -64,6 +69,16 @@ class ProjectGitSyncerTest extends TestCase
$this->projectGitSyncer->initRepository($repositoryName);
}
public function testInitRepositoryAlreadyInitialized(): void
{
$repositoryName = 'orm';
$this->processFactory->expects(self::never())
->method('run');
$this->projectGitSyncer->initRepository($repositoryName);
}
public function testSyncRepository(): void
{
$repositoryName = 'example-project';
@@ -111,4 +126,19 @@ class ProjectGitSyncerTest extends TestCase
$this->projectGitSyncer->checkoutBranch($repositoryName, $branchName);
}
public function testIsRepositoryInitialized(): void
{
self::assertTrue($this->projectGitSyncer->isRepositoryInitialized('orm'));
self::assertFalse($this->projectGitSyncer->isRepositoryInitialized('foo'));
}
public function testCheckoutTag(): void
{
$this->processFactory->expects(self::once())
->method('run')
->with('cd \'vfs://projects/example-project\' && git clean -xdf && git checkout tags/\'1.0.0\'');
$this->projectGitSyncer->checkoutTag('example-project', '1.0.0');
}
}

View File

@@ -0,0 +1 @@
42