From 869c12bb77521fd06acafcc3d79d4150ccf6f34d Mon Sep 17 00:00:00 2001 From: Claudio Zizza Date: Fri, 12 Jul 2024 00:04:12 +0200 Subject: [PATCH] Create tests for project dbfill --- lib/DataSources/DbPrefill/Projects.php | 14 +- lib/Model/ProjectVersion.php | 4 +- tests/DataSources/DbPrefill/ProjectsTest.php | 98 +++++++++ .../DbPrefill/fixtures/projects.json | 202 ++++++++++++++++++ 4 files changed, 307 insertions(+), 11 deletions(-) create mode 100644 tests/DataSources/DbPrefill/ProjectsTest.php create mode 100644 tests/DataSources/DbPrefill/fixtures/projects.json diff --git a/lib/DataSources/DbPrefill/Projects.php b/lib/DataSources/DbPrefill/Projects.php index d574c078..868a5e0f 100644 --- a/lib/DataSources/DbPrefill/Projects.php +++ b/lib/DataSources/DbPrefill/Projects.php @@ -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; @@ -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); diff --git a/lib/Model/ProjectVersion.php b/lib/Model/ProjectVersion.php index f1d90234..9a82fdd8 100644 --- a/lib/Model/ProjectVersion.php +++ b/lib/Model/ProjectVersion.php @@ -53,7 +53,7 @@ class ProjectVersion private bool $hasDocs; /** @var Collection */ - #[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[] */ @@ -61,7 +61,7 @@ class ProjectVersion private array $aliases; /** @var Collection */ - #[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 */ diff --git a/tests/DataSources/DbPrefill/ProjectsTest.php b/tests/DataSources/DbPrefill/ProjectsTest.php new file mode 100644 index 00000000..17580000 --- /dev/null +++ b/tests/DataSources/DbPrefill/ProjectsTest.php @@ -0,0 +1,98 @@ +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; + } +} diff --git a/tests/DataSources/DbPrefill/fixtures/projects.json b/tests/DataSources/DbPrefill/fixtures/projects.json new file mode 100644 index 00000000..4cbb4e69 --- /dev/null +++ b/tests/DataSources/DbPrefill/fixtures/projects.json @@ -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": "john@doe.dev", + "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": "john@doe.dev", + "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 + } + } + } +]