Skip to content

Commit

Permalink
Merge pull request #37 from acelaya-forks/feature/create-repo
Browse files Browse the repository at this point in the history
Add DatabaseTestCase::createRepository() method
  • Loading branch information
acelaya authored Nov 9, 2024
2 parents 8846204 + 37f7b22 commit d233428
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
3 changes: 2 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
parameters:
checkMissingIterableValueType: false
ignoreErrors:
- identifier: missingType.iterableValue
21 changes: 21 additions & 0 deletions src/DbTest/DatabaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<TEntity>
* @param class-string<TEntity> $entityName
* @param class-string<TRepo>|null $repositoryName
* @return ($repositoryName is null ? ObjectRepository<TEntity> : 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
{
Expand Down

0 comments on commit d233428

Please sign in to comment.