beapp/repository-tester-core
Installation Add the package to your Laravel project via Composer:
composer require beapp/repository-tester-core
For Laravel, ensure compatibility by using a Symfony bridge like symfony/http-foundation if needed.
Basic Configuration
Register the bundle in config/app.php (if using Laravel's Symfony bridge) or configure it via a service provider:
// config/testing.php (example)
'repository_tester' => [
'doctrine' => [
'entity_manager' => 'default', // Match your Laravel DB connection
],
],
First Test Case
Create a test class extending BeApp\RepositoryTesterCore\TestCase\RepositoryTestCase:
use BeApp\RepositoryTesterCore\TestCase\RepositoryTestCase;
use App\Repository\UserRepository;
class UserRepositoryTest extends RepositoryTestCase
{
protected function getRepository(): UserRepository
{
return $this->getEntityManager()->getRepository(UserRepository::class);
}
public function testFindById()
{
$user = $this->getRepository()->find(1);
$this->assertInstanceOf(User::class, $user);
}
}
Key Files to Review
src/TestCase/RepositoryTestCase.php (base class)src/Traits/RepositoryTestTrait.php (shared methods)README.md on the main repository-tester.web repo.Repository Isolation
Use RepositoryTestCase to mock the entire repository layer without hitting the database:
public function testFindAll()
{
$this->mockRepository()
->method('findAll')
->willReturn([new User()]);
$result = $this->getRepository()->findAll();
$this->assertCount(1, $result);
}
Data Fixtures Load test data via YAML/array fixtures:
protected function getFixtures(): array
{
return [
'users' => [
['id' => 1, 'name' => 'John'],
['id' => 2, 'name' => 'Jane'],
],
];
}
Assertion Helpers Leverage built-in assertions for Doctrine queries:
public function testFindOneBy()
{
$this->assertOneResult(
$this->getRepository()->findBy(['name' => 'John'])
);
}
Integration with Laravel
EntityManager manually if not using Symfony:
$this->app->bind('doctrine.entity_manager', function () {
return \Doctrine\ORM\EntityManager::create([...], $config);
});
refreshDatabase() or migrate() in setUp():
public function setUp(): void
{
parent::setUp();
$this->artisan('migrate:fresh');
}
Custom Query Testing Test complex DQL/QueryBuilder logic:
public function testCustomQuery()
{
$qb = $this->getRepository()->createQueryBuilder('u');
$qb->where('u.name LIKE :name')->setParameter('name', '%John%');
$this->assertCount(1, $qb->getQuery()->getResult());
}
Doctrine vs. Eloquent
DatabaseMigrations + DatabaseTransactions.EntityManager and Eloquent’s Model::query() in the same test.Fixture Loading
$this->loadFixtures();
@ORM\Id fields).Mocking Quirks
partialMock() for repositories with complex dependencies:
$this->mockRepository()->partialMock();
Performance
Enable Doctrine Logging
Add to setUp() to debug queries:
$this->getEntityManager()->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger());
Verify Fixtures Dump loaded fixtures for debugging:
$this->getEntityManager()->createQuery('SELECT u FROM App\Entity\User u')->getResult();
Symfony Bridge Issues
symfony/dependency-injection and symfony/http-kernel are installed.HttpKernel manually:
$this->app->singleton('http_kernel', function () {
return new \Symfony\Component\HttpKernel\HttpKernel(
$this->app->make('kernel'),
'test'
);
});
Custom Assertions
Extend RepositoryTestCase to add domain-specific assertions:
class CustomRepositoryTest extends RepositoryTestCase
{
protected function assertUserExists(User $user)
{
$this->assertNotNull(
$this->getRepository()->find($user->getId())
);
}
}
Fixture Factories Create reusable fixture factories:
class UserFixtureFactory
{
public static function create(int $id, string $name): array
{
return ['id' => $id, 'name' => $name];
}
}
Hybrid Testing
Combine with Laravel’s RefreshDatabase for integration tests:
use Illuminate\Foundation\Testing\RefreshDatabase;
class UserRepositoryIntegrationTest extends RepositoryTestCase
{
use RefreshDatabase;
// Test real DB interactions here
}
Parallel Testing
Use pestphp/pest or phpunit-parallel with caution—Doctrine’s EntityManager may not be thread-safe by default. Isolate tests with unique fixtures.
How can I help you explore Laravel packages today?