From 37f7b222457c29282d70aeba6543d63ba5a3f350 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 9 Nov 2024 10:55:52 +0100 Subject: [PATCH] Add DatabaseTestCase::createRepository() method --- .github/workflows/ci.yml | 2 +- CHANGELOG.md | 4 ++-- composer.json | 8 ++++---- phpstan.neon | 3 ++- src/DbTest/DatabaseTestCase.php | 21 +++++++++++++++++++++ 5 files changed, 30 insertions(+), 8 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b63684d..171e7ce 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -22,5 +22,5 @@ jobs: php-version: ${{ matrix.php-version }} tools: composer coverage: none - - run: composer install --no-interaction --prefer-dist ${{ matrix.php-version == '8.3' && '--ignore-platform-req=php' || '' }} + - run: composer install --no-interaction --prefer-dist - run: composer ${{ matrix.command }} diff --git a/CHANGELOG.md b/CHANGELOG.md index bb547ab..cd83f0c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,9 +4,9 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com), and this project adheres to [Semantic Versioning](https://semver.org). -## [Unreleased] +## [4.2.0] - 2024-11-09 ### Added -* *Nothing* +* Add new `DatabaseTestCase::createRepository()` protected method to create default or custom repositories without duplicating code. ### Changed * Update shlinkio coding standard to v2.4 diff --git a/composer.json b/composer.json index 6b8193b..e02823a 100644 --- a/composer.json +++ b/composer.json @@ -13,21 +13,21 @@ ], "require": { "php": "^8.2", - "doctrine/data-fixtures": "^1.7", + "doctrine/data-fixtures": "^1.8", "doctrine/orm": "^3.3", "fig/http-message-util": "^1.1", - "guzzlehttp/guzzle": "^7.8", + "guzzlehttp/guzzle": "^7.9", "phpunit/php-code-coverage": "^11.0", "phpunit/phpunit": "^11.4", "psr/container": "^2.0 || ^1.0", "psr/http-server-middleware": "^1.0", - "shlinkio/shlink-json": "^1.0", + "shlinkio/shlink-json": "^1.2", "symfony/console": "^7.1", "symfony/event-dispatcher": "^7.1", "symfony/process": "^7.1" }, "require-dev": { - "phpstan/phpstan": "^1.10", + "phpstan/phpstan": "^1.12", "roave/security-advisories": "dev-master", "shlinkio/php-coding-standard": "~2.4.0" }, diff --git a/phpstan.neon b/phpstan.neon index a821b91..715b4d1 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,2 +1,3 @@ parameters: - checkMissingIterableValueType: false + ignoreErrors: + - identifier: missingType.iterableValue diff --git a/src/DbTest/DatabaseTestCase.php b/src/DbTest/DatabaseTestCase.php index 7fcc90c..3ff2e7f 100644 --- a/src/DbTest/DatabaseTestCase.php +++ b/src/DbTest/DatabaseTestCase.php @@ -5,6 +5,7 @@ namespace Shlinkio\Shlink\TestUtils\DbTest; use Doctrine\ORM\EntityManagerInterface; +use Doctrine\Persistence\ObjectRepository; use PHPUnit\Framework\Attributes\After; use PHPUnit\Framework\Attributes\Before; use PHPUnit\Framework\TestCase; @@ -28,6 +29,26 @@ final protected function getEntityManager(): EntityManagerInterface return self::$em; } + /** + * Create a repository instance for provided empty. + * If $repositoryName is not provided, default repository will be returned via $em->getRepository($entityName) + * + * @template TEntity of object + * @template TRepo of ObjectRepository + * @param class-string $entityName + * @param class-string|null $repositoryName + * @return ($repositoryName is null ? ObjectRepository : TRepo) + */ + final protected function createRepository(string $entityName, string|null $repositoryName = null): ObjectRepository + { + $em = $this->getEntityManager(); + if ($repositoryName === null) { + return $em->getRepository($entityName); + } + + return new $repositoryName($em, $em->getClassMetadata($entityName)); + } + #[Before] final public function beginTransaction(): void {