@@ -40,16 +40,16 @@ Suppose the class you want to test looks like this::
40
40
41
41
class SalaryCalculator
42
42
{
43
- private $entityManager ;
43
+ private $objectManager ;
44
44
45
- public function __construct(ObjectManager $entityManager )
45
+ public function __construct(ObjectManager $objectManager )
46
46
{
47
- $this->entityManager = $entityManager ;
47
+ $this->objectManager = $objectManager ;
48
48
}
49
49
50
50
public function calculateTotalSalary($id)
51
51
{
52
- $employeeRepository = $this->entityManager
52
+ $employeeRepository = $this->objectManager
53
53
->getRepository(Employee::class);
54
54
$employee = $employeeRepository->find($id);
55
55
@@ -62,44 +62,35 @@ it's easy to pass a mock object within a test::
62
62
63
63
use AppBundle\Entity\Employee;
64
64
use AppBundle\Salary\SalaryCalculator;
65
- use Doctrine\ORM\EntityRepository;
66
65
use Doctrine\Common\Persistence\ObjectManager;
66
+ use Doctrine\Common\Persistence\ObjectRepository;
67
67
use PHPUnit\Framework\TestCase;
68
68
69
69
class SalaryCalculatorTest extends TestCase
70
70
{
71
71
public function testCalculateTotalSalary()
72
72
{
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);
83
76
84
77
// 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())
90
82
->method('find')
91
- ->will($this->returnValue($ employee) );
83
+ ->willReturn($ employee);
92
84
93
85
// 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())
99
90
->method('getRepository')
100
- ->will($this->returnValue($ employeeRepository) );
91
+ ->willReturn($ employeeRepository);
101
92
102
- $salaryCalculator = new SalaryCalculator($entityManager );
93
+ $salaryCalculator = new SalaryCalculator($objectManager );
103
94
$this->assertEquals(2100, $salaryCalculator->calculateTotalSalary(1));
104
95
}
105
96
}
0 commit comments