Skip to content

Commit 75c8541

Browse files
committed
CS, updated controller test for testing_example
1 parent 25970cd commit 75c8541

File tree

2 files changed

+80
-4
lines changed

2 files changed

+80
-4
lines changed

testing_example/src/Controller/ContrivedController.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public function __construct(TranslationInterface $translation) {
4040
* A parameter to the controller path.
4141
* @param int $second
4242
* A parameter to the controller path.
43+
*
4344
* @return string[]
4445
* A markup array.
4546
*/
@@ -61,11 +62,11 @@ public function displayAddedNumbers($first, $second) {
6162
* The translated message.
6263
*/
6364
protected function handCount($first, $second) {
64-
$sum = abs($this->add((int)$first, (int)$second));
65+
$sum = abs($this->add((int) $first, (int) $second));
6566
if ($sum <= 5) {
6667
$message = $this->t('I can count these on one hand.');
6768
}
68-
else if ($sum <= 10) {
69+
elseif ($sum <= 10) {
6970
$message = $this->t('I need two hands to count these.');
7071
}
7172
else {
@@ -89,4 +90,4 @@ protected function add($first, $second) {
8990
return $first + $second;
9091
}
9192

92-
}
93+
}

testing_example/tests/src/Unit/Controller/ContrivedControllerTest.php

+76-1
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@
1010
*/
1111
class ContrivedControllerTest extends UnitTestCase {
1212

13+
/**
14+
* Data provider for testAdd().
15+
*/
1316
public function provideTestAdd() {
1417
return [
1518
[4, 2, 2],
16-
[0, NULL, '']
19+
[0, NULL, ''],
1720
];
1821
}
1922

@@ -29,4 +32,76 @@ public function testAdd($expected, $first, $second) {
2932
$this->assertEquals($expected, $ref_add->invokeArgs($controller, [$first, $second]));
3033
}
3134

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+
32107
}

0 commit comments

Comments
 (0)