View source
<?php
namespace Drupal\Tests\testing_example\Unit\Controller;
use Drupal\Tests\UnitTestCase;
use Drupal\testing_example\Controller\ContrivedController;
class ContrivedControllerTest extends UnitTestCase {
public function provideTestAdd() {
return [
[
4,
2,
2,
],
];
}
public function testAdd($expected, $first, $second) {
$controller = $this
->getMockBuilder(ContrivedController::class)
->disableOriginalConstructor()
->getMock();
$ref_add = new \ReflectionMethod($controller, 'add');
$ref_add
->setAccessible(TRUE);
$this
->assertEquals($expected, $ref_add
->invokeArgs($controller, [
$first,
$second,
]));
}
public function provideTestHandCount() {
return [
[
'I can count these on one hand.',
0,
0,
],
[
'I can count these on one hand.',
1,
0,
],
[
'I can count these on one hand.',
0,
1,
],
[
'I need two hands to count these.',
5,
5,
],
[
'That\'s just too many numbers to count.',
5,
6,
],
[
'That\'s just too many numbers to count.',
6,
5,
],
];
}
public function testHandCount($expected, $first, $second) {
$mock_translation = $this
->getStringTranslationStub();
$controller = new ContrivedController($mock_translation);
$ref_hand_count = new \ReflectionMethod($controller, 'handCount');
$ref_hand_count
->setAccessible(TRUE);
$message = $ref_hand_count
->invokeArgs($controller, [
$first,
$second,
]);
$this
->assertEquals($expected, (string) $message);
}
public function providerTestHandCountIsolated() {
$data = [];
foreach (range(0, 5) as $sum) {
$data[] = [
'I can count these on one hand.',
$sum,
];
}
foreach (range(6, 10) as $sum) {
$data[] = [
'I need two hands to count these.',
$sum,
];
}
foreach (range(11, 15) as $sum) {
$data[] = [
'That\'s just too many numbers to count.',
$sum,
];
}
return $data;
}
public function testHandCountIsolated($expected, $sum) {
$controller = $this
->getMockBuilder(ContrivedController::class)
->setConstructorArgs([
$this
->getStringTranslationStub(),
])
->setMethods([
'add',
])
->getMock();
$controller
->expects($this
->once())
->method('add')
->with($this
->equalTo(0), $this
->equalTo(0))
->willReturn($sum);
$ref_hand_count = new \ReflectionMethod($controller, 'handCount');
$ref_hand_count
->setAccessible(TRUE);
$message = (string) $ref_hand_count
->invokeArgs($controller, [
0,
0,
]);
$this
->assertEquals($expected, $message);
}
}