Skip to content

use entity manager and repository instead of object manager/repository #19439

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions form/unit_testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -147,27 +147,27 @@ make sure the ``FormRegistry`` uses the created instance::
namespace App\Tests\Form\Type;

use App\Form\Type\TestedType;
use Doctrine\Persistence\ObjectManager;
use Doctrine\ORM\EntityManager;
use Symfony\Component\Form\PreloadedExtension;
use Symfony\Component\Form\Test\TypeTestCase;
// ...

class TestedTypeTest extends TypeTestCase
{
private MockObject|ObjectManager $objectManager;
private MockObject&EntityManager $entityManager;

protected function setUp(): void
{
// mock any dependencies
$this->objectManager = $this->createMock(ObjectManager::class);
$this->entityManager = $this->createMock(EntityManager::class);

parent::setUp();
}

protected function getExtensions(): array
{
// create a type instance with the mocked dependencies
$type = new TestedType($this->objectManager);
$type = new TestedType($this->entityManager);

return [
// register the type instances with the PreloadedExtension
Expand Down
4 changes: 2 additions & 2 deletions service_container/parent_services.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ you may have multiple repository classes which need the
// src/Repository/BaseDoctrineRepository.php
namespace App\Repository;

use Doctrine\Persistence\ObjectManager;
use Doctrine\ORM\EntityManager;
use Psr\Log\LoggerInterface;

// ...
Expand All @@ -18,7 +18,7 @@ you may have multiple repository classes which need the
protected LoggerInterface $logger;

public function __construct(
protected ObjectManager $objectManager,
protected EntityManager $entityManager,
) {
}

Expand Down
24 changes: 12 additions & 12 deletions testing/database.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@ Suppose the class you want to test looks like this::
namespace App\Salary;

use App\Entity\Employee;
use Doctrine\Persistence\ObjectManager;
use Doctrine\ORM\EntityManager;

class SalaryCalculator
{
public function __construct(
private ObjectManager $objectManager,
private EntityManager $entityManager,
) {
}

public function calculateTotalSalary(int $id): int
{
$employeeRepository = $this->objectManager
$employeeRepository = $this->entityManager
->getRepository(Employee::class);
$employee = $employeeRepository->find($id);

Expand All @@ -47,8 +47,8 @@ constructor, you can pass a mock object within a test::

use App\Entity\Employee;
use App\Salary\SalaryCalculator;
use Doctrine\Persistence\ObjectManager;
use Doctrine\Persistence\ObjectRepository;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityRepository;
use PHPUnit\Framework\TestCase;

class SalaryCalculatorTest extends TestCase
Expand All @@ -60,20 +60,20 @@ constructor, you can pass a mock object within a test::
$employee->setBonus(1100);

// Now, mock the repository so it returns the mock of the employee
$employeeRepository = $this->createMock(ObjectRepository::class);
$employeeRepository = $this->createMock(EntityRepository::class);
$employeeRepository->expects($this->any())
->method('find')
->willReturn($employee);

// Last, mock the EntityManager to return the mock of the repository
// (this is not needed if the class being tested injects the
// repository it uses instead of the entire object manager)
$objectManager = $this->createMock(ObjectManager::class);
$objectManager->expects($this->any())
// repository it uses instead of the entire entity manager)
$entityManager = $this->createMock(EntityManager::class);
$entityManager->expects($this->any())
->method('getRepository')
->willReturn($employeeRepository);

$salaryCalculator = new SalaryCalculator($objectManager);
$salaryCalculator = new SalaryCalculator($entityManager);
$this->assertEquals(2100, $salaryCalculator->calculateTotalSalary(1));
}
}
Expand All @@ -94,12 +94,12 @@ so, get the entity manager via the service container as follows::
namespace App\Tests\Repository;

use App\Entity\Product;
use Doctrine\Persistence\ObjectManager;
use Doctrine\ORM\EntityManager;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

class ProductRepositoryTest extends KernelTestCase
{
private ObjectManager $entityManager;
private EntityManager $entityManager;

protected function setUp(): void
{
Expand Down