10
10
*/
11
11
class ContrivedControllerTest extends UnitTestCase {
12
12
13
+ /**
14
+ * Data provider for testAdd().
15
+ */
13
16
public function provideTestAdd () {
14
17
return [
15
18
[4 , 2 , 2 ],
16
- [0 , NULL , '' ]
19
+ [0 , NULL , '' ],
17
20
];
18
21
}
19
22
@@ -29,4 +32,76 @@ public function testAdd($expected, $first, $second) {
29
32
$ this ->assertEquals ($ expected , $ ref_add ->invokeArgs ($ controller , [$ first , $ second ]));
30
33
}
31
34
35
+ /**
36
+ * Data provider for testHandCount().
37
+ */
38
+ public function provideTestHandCount () {
39
+ return [
40
+ ['I can count these on one hand. ' , 0 , 0 ],
41
+ ['I can count these on one hand. ' , 1 , 0 ],
42
+ ['I can count these on one hand. ' , 0 , 1 ],
43
+ ['I need two hands to count these. ' , 5 , 5 ],
44
+ ['That \'s just too many numbers to count. ' , 5 , 6 ],
45
+ ['That \'s just too many numbers to count. ' , 6 , 5 ],
46
+ ];
47
+ }
48
+
49
+ /**
50
+ * @dataProvider provideTestHandCount
51
+ */
52
+ public function testHandCount ($ expected , $ first , $ second ) {
53
+ // Get a mock translation service.
54
+ $ mock_translation = $ this ->getStringTranslationStub ();
55
+ // Create a new controller with our mocked translation service.
56
+ $ controller = new ContrivedController ($ mock_translation );
57
+
58
+ // Set up a reflection for handCount().
59
+ $ ref_hand_count = new \ReflectionMethod ($ controller , 'handCount ' );
60
+ // Set handCount() to be public.
61
+ $ ref_hand_count ->setAccessible (TRUE );
62
+ // Check out whether handCount() meets our expectation.
63
+ $ message = $ ref_hand_count ->invokeArgs ($ controller , [$ first , $ second ]);
64
+ $ this ->assertEquals ($ expected , (string ) $ message );
65
+ }
66
+
67
+ /**
68
+ * Data provider for testHandCountIsolated().
69
+ */
70
+ public function providerTestHandCountIsolated () {
71
+ return [
72
+ ['I can count these on one hand. ' , 0 ],
73
+ ['I can count these on one hand. ' , 5 ],
74
+ ['I need two hands to count these. ' , 10 ],
75
+ ['That \'s just too many numbers to count. ' , 11 ],
76
+ ];
77
+ }
78
+
79
+ /**
80
+ * @dataProvider providerTestHandCountIsolated
81
+ */
82
+ public function testHandCountIsolated ($ expected , $ sum ) {
83
+ // Mock a ContrivedController, using a mocked translation service.
84
+ $ controller = $ this ->getMockBuilder (ContrivedController::class)
85
+ ->setConstructorArgs ([$ this ->getStringTranslationStub ()])
86
+ // Specify that we'll also mock add().
87
+ ->setMethods (['add ' ])
88
+ ->getMock ();
89
+
90
+ // Mock add() so that it returns our $sum when it's called with (0,0).
91
+ $ controller ->expects ($ this ->once ())
92
+ ->method ('add ' )
93
+ ->with ($ this ->equalTo (0 ), $ this ->equalTo (0 ))
94
+ ->willReturn ($ sum );
95
+
96
+ // Use reflection to make handCount() public.
97
+ $ ref_hand_count = new \ReflectionMethod ($ controller , 'handCount ' );
98
+ $ ref_hand_count ->setAccessible (TRUE );
99
+
100
+ // Invoke handCount().
101
+ $ message = (string ) $ ref_hand_count ->invokeArgs ($ controller , [0 , 0 ]);
102
+
103
+ // Assert our expectations.
104
+ $ this ->assertEquals ($ expected , $ message );
105
+ }
106
+
32
107
}
0 commit comments