This repository has been archived by the owner on Oct 31, 2018. It is now read-only.
/
AssertionHelpers.php
117 lines (109 loc) · 3.37 KB
/
AssertionHelpers.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
/**
* Helper asserts for mocks. Add to your base PHPUnit\Framework\TestCase.
*/
trait FBMock_AssertionHelpers {
/**
* Assert that the method was called a certain number of times on a mock
*
* @param $mock a mock object
* @param $method_name name of method to check
* @param $expected_num_calls expected number of calls
* @param $msg message for assert (optional)
*/
public static function assertNumCalls(
FBMock_Mock $mock,
$method_name,
$expected_num_calls,
$msg = '') {
FBMock_Utils::assertString($method_name);
FBMock_Utils::assertInt($expected_num_calls);
$call_count = count($mock->mockGetCalls($method_name));
PHPUnit\Framework\TestCase::assertEquals(
$expected_num_calls,
$call_count,
$msg ?: "$method_name called wrong number of times"
);
}
/**
* Assert that the method was called once. If $args is given, check that it
* matches the args $method_name was called with.
*
* @param $mock a mock object
* @param $method_name name of method to check
* @param $args expected arguments (optional)
* @param $msg message for assert (optional)
*/
public static function assertCalledOnce(
FBMock_Mock $mock,
$method_name,
$args=null,
$msg='') {
FBMock_Utils::assertString($method_name);
self::assertNumCalls($mock, $method_name, 1, $msg);
if ($args !== null) {
PHPUnit\Framework\TestCase::assertEquals(
$args,
$mock->mockGetCalls($method_name)[0],
$msg ?: "$method_name args are not equal"
);
}
}
/**
* Assert that the method was not called.
*
* @param $mock a mock object
* @param $method_name name of method to check
* @param $msg message for assert (optional)
*/
public static function assertNotCalled(
FBMock_Mock $mock,
$method_name,
$msg='') {
FBMock_Utils::assertString($method_name);
self::assertNumCalls($mock, $method_name, 0, $msg);
}
/**
* Assert that the method calls match the array of calls.
*
* Example usage:
*
* // Code under test calls method
* $mock->testMethod(1,2,3);
* $mock->testMethod('a', 'b', 'c');
*
* // Test asserts calls
* self::assertCalls(
* $mock,
* 'testMethod',
* array(1,2,3),
* array('a', 'b', 'c')
* );
*
* @param $mock a mock object
* @param $method_name name of method to check
* @param ... arrays of expected arguments for each call
* @param $msg message for assert (optional)
*/
public static function assertCalls(
FBMock_Mock $mock,
$method_name
/* array $expected_first_call, array $expected_second_call, $msg = ''*/) {
FBMock_Utils::assertString($method_name);
$args = func_get_args();
$msg = '';
if (is_string(end($args))) {
$msg = array_pop($args);
}
$expected_calls = array_slice($args, 2);
self::assertNumCalls($mock, $method_name, count($expected_calls), $msg);
$actual_calls = $mock->mockGetCalls($method_name);
foreach ($expected_calls as $i => $call) {
PHPUnit\Framework\TestCase::assertEquals(
$call,
$actual_calls[$i],
$msg ?: "Call $i for method $method_name did not match expected call"
);
}
}
}