Skip to content

Commit 06e8e69

Browse files
committed
minor #19439 use entity manager and repository instead of object manager/repository (xabbuh)
This PR was merged into the 6.3 branch. Discussion ---------- use entity manager and repository instead of object manager/repository see #19394 (comment) Commits ------- cf8bed8 use entity manager and repository instead of object manager/repository
2 parents c5924fa + cf8bed8 commit 06e8e69

File tree

3 files changed

+18
-18
lines changed

3 files changed

+18
-18
lines changed

form/unit_testing.rst

+4-4
Original file line numberDiff line numberDiff line change
@@ -147,27 +147,27 @@ make sure the ``FormRegistry`` uses the created instance::
147147
namespace App\Tests\Form\Type;
148148

149149
use App\Form\Type\TestedType;
150-
use Doctrine\Persistence\ObjectManager;
150+
use Doctrine\ORM\EntityManager;
151151
use Symfony\Component\Form\PreloadedExtension;
152152
use Symfony\Component\Form\Test\TypeTestCase;
153153
// ...
154154

155155
class TestedTypeTest extends TypeTestCase
156156
{
157-
private MockObject|ObjectManager $objectManager;
157+
private MockObject&EntityManager $entityManager;
158158

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

164164
parent::setUp();
165165
}
166166

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

172172
return [
173173
// register the type instances with the PreloadedExtension

service_container/parent_services.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ you may have multiple repository classes which need the
99
// src/Repository/BaseDoctrineRepository.php
1010
namespace App\Repository;
1111

12-
use Doctrine\Persistence\ObjectManager;
12+
use Doctrine\ORM\EntityManager;
1313
use Psr\Log\LoggerInterface;
1414

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

2020
public function __construct(
21-
protected ObjectManager $objectManager,
21+
protected EntityManager $entityManager,
2222
) {
2323
}
2424

testing/database.rst

+12-12
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,18 @@ Suppose the class you want to test looks like this::
2020
namespace App\Salary;
2121

2222
use App\Entity\Employee;
23-
use Doctrine\Persistence\ObjectManager;
23+
use Doctrine\ORM\EntityManager;
2424

2525
class SalaryCalculator
2626
{
2727
public function __construct(
28-
private ObjectManager $objectManager,
28+
private EntityManager $entityManager,
2929
) {
3030
}
3131

3232
public function calculateTotalSalary(int $id): int
3333
{
34-
$employeeRepository = $this->objectManager
34+
$employeeRepository = $this->entityManager
3535
->getRepository(Employee::class);
3636
$employee = $employeeRepository->find($id);
3737

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

4848
use App\Entity\Employee;
4949
use App\Salary\SalaryCalculator;
50-
use Doctrine\Persistence\ObjectManager;
51-
use Doctrine\Persistence\ObjectRepository;
50+
use Doctrine\ORM\EntityManager;
51+
use Doctrine\ORM\EntityRepository;
5252
use PHPUnit\Framework\TestCase;
5353

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

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

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

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

9696
use App\Entity\Product;
97-
use Doctrine\Persistence\ObjectManager;
97+
use Doctrine\ORM\EntityManager;
9898
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
9999

100100
class ProductRepositoryTest extends KernelTestCase
101101
{
102-
private ObjectManager $entityManager;
102+
private EntityManager $entityManager;
103103

104104
protected function setUp(): void
105105
{

0 commit comments

Comments
 (0)