Skip to content

Commit d233428

Browse files
authored
Merge pull request #37 from acelaya-forks/feature/create-repo
Add DatabaseTestCase::createRepository() method
2 parents 8846204 + 37f7b22 commit d233428

File tree

5 files changed

+30
-8
lines changed

5 files changed

+30
-8
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ jobs:
2222
php-version: ${{ matrix.php-version }}
2323
tools: composer
2424
coverage: none
25-
- run: composer install --no-interaction --prefer-dist ${{ matrix.php-version == '8.3' && '--ignore-platform-req=php' || '' }}
25+
- run: composer install --no-interaction --prefer-dist
2626
- run: composer ${{ matrix.command }}

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ All notable changes to this project will be documented in this file.
44

55
The format is based on [Keep a Changelog](https://keepachangelog.com), and this project adheres to [Semantic Versioning](https://semver.org).
66

7-
## [Unreleased]
7+
## [4.2.0] - 2024-11-09
88
### Added
9-
* *Nothing*
9+
* Add new `DatabaseTestCase::createRepository()` protected method to create default or custom repositories without duplicating code.
1010

1111
### Changed
1212
* Update shlinkio coding standard to v2.4

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,21 @@
1313
],
1414
"require": {
1515
"php": "^8.2",
16-
"doctrine/data-fixtures": "^1.7",
16+
"doctrine/data-fixtures": "^1.8",
1717
"doctrine/orm": "^3.3",
1818
"fig/http-message-util": "^1.1",
19-
"guzzlehttp/guzzle": "^7.8",
19+
"guzzlehttp/guzzle": "^7.9",
2020
"phpunit/php-code-coverage": "^11.0",
2121
"phpunit/phpunit": "^11.4",
2222
"psr/container": "^2.0 || ^1.0",
2323
"psr/http-server-middleware": "^1.0",
24-
"shlinkio/shlink-json": "^1.0",
24+
"shlinkio/shlink-json": "^1.2",
2525
"symfony/console": "^7.1",
2626
"symfony/event-dispatcher": "^7.1",
2727
"symfony/process": "^7.1"
2828
},
2929
"require-dev": {
30-
"phpstan/phpstan": "^1.10",
30+
"phpstan/phpstan": "^1.12",
3131
"roave/security-advisories": "dev-master",
3232
"shlinkio/php-coding-standard": "~2.4.0"
3333
},

phpstan.neon

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
parameters:
2-
checkMissingIterableValueType: false
2+
ignoreErrors:
3+
- identifier: missingType.iterableValue

src/DbTest/DatabaseTestCase.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Shlinkio\Shlink\TestUtils\DbTest;
66

77
use Doctrine\ORM\EntityManagerInterface;
8+
use Doctrine\Persistence\ObjectRepository;
89
use PHPUnit\Framework\Attributes\After;
910
use PHPUnit\Framework\Attributes\Before;
1011
use PHPUnit\Framework\TestCase;
@@ -28,6 +29,26 @@ final protected function getEntityManager(): EntityManagerInterface
2829
return self::$em;
2930
}
3031

32+
/**
33+
* Create a repository instance for provided empty.
34+
* If $repositoryName is not provided, default repository will be returned via $em->getRepository($entityName)
35+
*
36+
* @template TEntity of object
37+
* @template TRepo of ObjectRepository<TEntity>
38+
* @param class-string<TEntity> $entityName
39+
* @param class-string<TRepo>|null $repositoryName
40+
* @return ($repositoryName is null ? ObjectRepository<TEntity> : TRepo)
41+
*/
42+
final protected function createRepository(string $entityName, string|null $repositoryName = null): ObjectRepository
43+
{
44+
$em = $this->getEntityManager();
45+
if ($repositoryName === null) {
46+
return $em->getRepository($entityName);
47+
}
48+
49+
return new $repositoryName($em, $em->getClassMetadata($entityName));
50+
}
51+
3152
#[Before]
3253
final public function beginTransaction(): void
3354
{

0 commit comments

Comments
 (0)