Skip to content

Commit 4decaf0

Browse files
committed
improve examples of how we create test doubles
1 parent ac73154 commit 4decaf0

File tree

1 file changed

+19
-28
lines changed

1 file changed

+19
-28
lines changed

testing/database.rst

Lines 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)