Skip to content

Commit

Permalink
Create tests for project dbfill
Browse files Browse the repository at this point in the history
  • Loading branch information
SenseException committed Jul 11, 2024
1 parent 80e3587 commit 869c12b
Show file tree
Hide file tree
Showing 4 changed files with 307 additions and 11 deletions.
14 changes: 5 additions & 9 deletions lib/DataSources/DbPrefill/Projects.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ private function createProject(array $projectData): void
foreach ($projectData['versions'] ?? [] as $version) {
$projectVersion = new ProjectVersion($version);

foreach ($version['tags'] ?? [] as $tag) {
$tag = new Tag($tag['name'], new DateTimeImmutable($tag['date']));
$projectVersion->addTag($tag);
}

$tagVersion = $projectVersion->getLatestTag()?->getName();
if (isset($projectData['versionsGreaterThan']) && $tagVersion !== null && version_compare($projectData['versionsGreaterThan'], $tagVersion, '>')) {
continue;
Expand All @@ -61,15 +66,6 @@ private function createProject(array $projectData): void
foreach ($version['docsLanguages'] ?? [] as $language) {
$rstLanguage = new RSTLanguage($language['code'], $language['path']);
$projectVersion->addDocsLanguage($rstLanguage);

$this->entityManager->persist($rstLanguage);
}

foreach ($version['tags'] ?? [] as $tag) {
$tag = new Tag($tag['name'], new DateTimeImmutable($tag['date']));
$projectVersion->addTag($tag);

$this->entityManager->persist($tag);
}

$this->entityManager->persist($projectVersion);
Expand Down
4 changes: 2 additions & 2 deletions lib/Model/ProjectVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ class ProjectVersion
private bool $hasDocs;

/** @var Collection<int, RSTLanguage> */
#[ORM\OneToMany(targetEntity: RSTLanguage::class, fetch: 'EAGER', mappedBy: 'projectVersion', orphanRemoval: true)]
#[ORM\OneToMany(targetEntity: RSTLanguage::class, fetch: 'EAGER', mappedBy: 'projectVersion', orphanRemoval: true, cascade: ['persist'])]
private Collection $docsLanguages;

/** @var string[] */
#[ORM\Column(type: 'simple_array', nullable: true)]
private array $aliases;

/** @var Collection<int, Tag> */
#[ORM\OneToMany(targetEntity: Tag::class, fetch: 'EAGER', mappedBy: 'projectVersion', orphanRemoval: true)]
#[ORM\OneToMany(targetEntity: Tag::class, fetch: 'EAGER', mappedBy: 'projectVersion', orphanRemoval: true, cascade: ['persist'])]
private Collection $tags;

/** @param mixed[] $version */
Expand Down
98 changes: 98 additions & 0 deletions tests/DataSources/DbPrefill/ProjectsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

declare(strict_types=1);

namespace Doctrine\Website\Tests\DataSources\DbPrefill;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Website\DataSources\DataSource;
use Doctrine\Website\DataSources\DbPrefill\Projects;
use Doctrine\Website\Model\Project;
use Doctrine\Website\Tests\TestCase;

use function assert;
use function file_get_contents;
use function is_dir;
use function json_decode;

class ProjectsTest extends TestCase
{
protected function setUp(): void
{
$buildDir = __DIR__ . '/../../../build-test';

if (is_dir($buildDir)) {
return;
}

self::markTestSkipped('This test requires ./bin/console build-website to have been ran.');
}

protected function tearDown(): void
{
parent::tearDown();

$entityManager = $this->getEntityManager();
$repository = $entityManager->getRepository(Project::class);
$project = $repository->find('testproject');

assert($project instanceof Project);

$entityManager->remove($project);
$entityManager->flush();
}

public function testPopulate(): void
{
$projectFixture = __DIR__ . '/fixtures/projects.json';
$fixture = json_decode((string) file_get_contents($projectFixture), true);

$entityManager = $this->getEntityManager();

$dataSource = $this->createMock(DataSource::class);
$dataSource->method('getSourceRows')->willReturn($fixture);

$dbFill = new Projects($dataSource, $entityManager);
$dbFill->populate();

$this->assertProjectIsComplete($entityManager);
}

private function assertProjectIsComplete(EntityManagerInterface $entityManager): void
{
$entityManager->clear();

$repository = $entityManager->getRepository(Project::class);
$project = $repository->find('testproject');

assert($project instanceof Project);

self::assertSame('Testproject', $project->getName());
self::assertSame('testproject', $project->getSlug());
self::assertSame('Testproject', $project->getShortName());
self::assertTrue($project->isActive());
self::assertFalse($project->isArchived());
self::assertSame('doctrine-testproject', $project->getDocsSlug());
self::assertSame('testproject', $project->getDocsRepositoryName());
self::assertSame('/docs', $project->getDocsPath());
self::assertSame('/lib', $project->getCodePath());
self::assertSame('doctrine/testproject', $project->getComposerPackageName());
self::assertSame('testproject', $project->getRepositoryName());
self::assertFalse($project->isIntegration());
self::assertSame('', $project->getIntegrationFor());
self::assertSame('It\'s a testproject', $project->getDescription());
self::assertSame(['testproject', 'docblock', 'parser'], $project->getKeywords());

$versions = $project->getVersions();

self::assertCount(1, $versions);
}

private function getEntityManager(): EntityManagerInterface
{
$entityManager = $this->getContainer()->get(EntityManagerInterface::class);
assert($entityManager instanceof EntityManagerInterface);

return $entityManager;
}
}
202 changes: 202 additions & 0 deletions tests/DataSources/DbPrefill/fixtures/projects.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
[
{
"active": true,
"archived": false,
"integration": false,
"name": "Testproject",
"repositoryName": "testproject",
"docsPath": "/docs",
"codePath": "/lib",
"slug": "testproject",
"versionsGreaterThan": "1.0.1",
"versions": [
{
"name": "2.0",
"branchName": "2.0.x",
"aliases": [
"current",
"stable"
],
"current": true,
"maintained": true,
"tags": [
{
"name": "2.0.0",
"date": "2022-12-19 18:24:23"
}
],
"hasDocs": true,
"docsLanguages": [
{
"code": "en",
"path": "projects/testproject/docs/en"
}
]
},
{
"name": "1.0",
"slug": "1.0",
"branchName": null,
"tags": [
{
"name": "v1.0",
"date": "2013-01-12 20:26:03"
}
],
"maintained": false,
"hasDocs": false,
"docsLanguages": []
}
],
"composerPackageName": "doctrine/testproject",
"description": "It's a testproject",
"keywords": [
"testproject",
"docblock",
"parser"
],
"docsSlug": "doctrine-testproject",
"packagistData": {
"package": {
"name": "doctrine/testproject",
"description": "It's a testproject",
"time": "2013-01-12T19:24:37+00:00",
"maintainers": [
{
"name": "beberlei",
"avatar_url": "https://www.gravatar.com/avatar/75f5fb3ddda052e46f1daed314ae69ab?d=identicon"
}
],
"versions": {
"2.0.0": {
"name": "doctrine/testproject",
"description": "It's a testproject",
"keywords": [
"testproject",
"parser",
"docblock"
],
"homepage": "https://www.doctrine-project.org/projects/testproject.html",
"version": "2.0.0",
"version_normalized": "2.0.0.0",
"license": [
"MIT"
],
"authors": [
{
"name": "John Doe",
"email": "[email protected]",
"role": "Creator"
}
],
"source": {
"url": "https://github.com/doctrine/testproject.git",
"type": "git",
"reference": "d02c9f3742044e17d5fa8d28d8402a2d95c33302"
},
"dist": {
"url": "https://api.github.com/repos/doctrine/testproject/zipball/d02c9f3742044e17d5fa8d28d8402a2d95c33302",
"type": "zip",
"shasum": "",
"reference": "d02c9f3742044e17d5fa8d28d8402a2d95c33302"
},
"type": "library",
"support": {
"issues": "https://github.com/doctrine/testproject/issues",
"source": "https://github.com/doctrine/testproject/tree/2.0.0"
},
"funding": [],
"time": "2022-12-19T18:17:20+00:00",
"autoload": {
"psr-4": {
"Doctrine\\Common\\Testproject\\": "lib/Doctrine/Common/Testproject"
}
},
"require": {
"php": "^7.2 || ^8.0",
"ext-tokenizer": "*",
"doctrine/lexer": "^2 || ^3",
"psr/cache": "^1 || ^2 || ^3"
},
"require-dev": {
"doctrine/cache": "^2.0",
"doctrine/coding-standard": "^10",
"phpstan/phpstan": "^1.8.0",
"phpunit/phpunit": "^7.5 || ^8.5 || ^9.5",
"symfony/cache": "^5.4 || ^6",
"vimeo/psalm": "^4.10"
},
"suggest": {
"php": "PHP 8.0 or higher comes with attributes, a native replacement for testproject"
}
},
"v1.0": {
"name": "doctrine/testproject",
"description": "It's a testproject",
"keywords": [
"testproject",
"parser",
"docblock"
],
"homepage": "http://www.doctrine-project.org",
"version": "v1.0",
"version_normalized": "1.0.0.0",
"license": [
"MIT"
],
"authors": [
{
"name": "John Doe",
"email": "[email protected]",
"role": "Creator"
}
],
"source": {
"url": "https://github.com/doctrine/testproject.git",
"type": "git",
"reference": "fae359b3efd908e407a0105ff8956b5c94ddca8e"
},
"dist": {
"url": "https://api.github.com/repos/doctrine/testproject/zipball/fae359b3efd908e407a0105ff8956b5c94ddca8e",
"type": "zip",
"shasum": "",
"reference": "fae359b3efd908e407a0105ff8956b5c94ddca8e"
},
"type": "library",
"support": {
"source": "https://github.com/doctrine/testproject/tree/v1.0"
},
"time": "2013-01-12T19:23:32+00:00",
"autoload": {
"psr-0": {
"Doctrine\\Common\\Testproject\\": "lib/"
}
},
"require": {
"php": ">=5.3.2",
"doctrine/lexer": "1.*"
},
"require-dev": {
"doctrine/cache": "1.*"
}
}
},
"type": "library",
"repository": "https://github.com/doctrine/testproject",
"github_stars": 6729,
"github_watchers": 41,
"github_forks": 236,
"github_open_issues": 30,
"language": "PHP",
"dependents": 2349,
"suggesters": 74,
"downloads": {
"total": 426454499,
"monthly": 6487217,
"daily": 260080
},
"favers": 6768
}
}
}
]

0 comments on commit 869c12b

Please sign in to comment.