Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 9c02ada

Browse filesBrowse files
committed
minor symfony#8144 improve examples of how we create test doubles (xabbuh)
This PR was merged into the 2.7 branch. Discussion ---------- improve examples of how we create test doubles Commits ------- 4decaf0 improve examples of how we create test doubles
2 parents 32c72bd + 4decaf0 commit 9c02ada
Copy full SHA for 9c02ada

File tree

Expand file treeCollapse file tree

1 file changed

+19
-28
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+19
-28
lines changed

‎testing/database.rst

Copy file name to clipboardExpand all lines: testing/database.rst
+19-28Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,16 @@ Suppose the class you want to test looks like this::
4040

4141
class SalaryCalculator
4242
{
43-
private $entityManager;
43+
private $objectManager;
4444

45-
public function __construct(ObjectManager $entityManager)
45+
public function __construct(ObjectManager $objectManager)
4646
{
47-
$this->entityManager = $entityManager;
47+
$this->objectManager = $objectManager;
4848
}
4949

5050
public function calculateTotalSalary($id)
5151
{
52-
$employeeRepository = $this->entityManager
52+
$employeeRepository = $this->objectManager
5353
->getRepository(Employee::class);
5454
$employee = $employeeRepository->find($id);
5555

@@ -62,44 +62,35 @@ it's easy to pass a mock object within a test::
6262

6363
use AppBundle\Entity\Employee;
6464
use AppBundle\Salary\SalaryCalculator;
65-
use Doctrine\ORM\EntityRepository;
6665
use Doctrine\Common\Persistence\ObjectManager;
66+
use Doctrine\Common\Persistence\ObjectRepository;
6767
use PHPUnit\Framework\TestCase;
6868

6969
class SalaryCalculatorTest extends TestCase
7070
{
7171
public function testCalculateTotalSalary()
7272
{
73-
// First, mock the object to be used in the test
74-
$employee = $this->createMock(Employee::class);
75-
// use getMock() on PHPUnit 5.3 or below
76-
// $employee = $this->getMock(Employee::class);
77-
$employee->expects($this->once())
78-
->method('getSalary')
79-
->will($this->returnValue(1000));
80-
$employee->expects($this->once())
81-
->method('getBonus')
82-
->will($this->returnValue(1100));
73+
$employee = new Employee();
74+
$employee->setSalaray(1000);
75+
$employee->setBonus(1100);
8376

8477
// Now, mock the repository so it returns the mock of the employee
85-
$employeeRepository = $this
86-
->getMockBuilder(EntityRepository::class)
87-
->disableOriginalConstructor()
88-
->getMock();
89-
$employeeRepository->expects($this->once())
78+
$employeeRepository = $this->createMock(ObjectRepository::class);
79+
// use getMock() on PHPUnit 5.3 or below
80+
// $employeeRepository = $this->getMock(ObjectRepository::class);
81+
$employeeRepository->expects($this->any())
9082
->method('find')
91-
->will($this->returnValue($employee));
83+
->willReturn($employee);
9284

9385
// Last, mock the EntityManager to return the mock of the repository
94-
$entityManager = $this
95-
->getMockBuilder(ObjectManager::class)
96-
->disableOriginalConstructor()
97-
->getMock();
98-
$entityManager->expects($this->once())
86+
$objectManager = $this->createMock(ObjectManager::class);
87+
// use getMock() on PHPUnit 5.3 or below
88+
// $objectManager = $this->getMock(ObjectManager::class);
89+
$objectManager->expects($this->any())
9990
->method('getRepository')
100-
->will($this->returnValue($employeeRepository));
91+
->willReturn($employeeRepository);
10192

102-
$salaryCalculator = new SalaryCalculator($entityManager);
93+
$salaryCalculator = new SalaryCalculator($objectManager);
10394
$this->assertEquals(2100, $salaryCalculator->calculateTotalSalary(1));
10495
}
10596
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.