class ContrivedControllerTest
Same name in other branches
- 4.0.x modules/testing_example/tests/src/Unit/Controller/ContrivedControllerTest.php \Drupal\Tests\testing_example\Unit\Controller\ContrivedControllerTest
The class to test ContrivedController.
@group testing_example
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses \Drupal\Tests\Traits\PhpUnitWarnings, \Drupal\Tests\PhpUnitCompatibilityTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait
- class \Drupal\Tests\testing_example\Unit\Controller\ContrivedControllerTest extends \Drupal\Tests\UnitTestCase
Expanded class hierarchy of ContrivedControllerTest
File
-
modules/
testing_example/ tests/ src/ Unit/ Controller/ ContrivedControllerTest.php, line 13
Namespace
Drupal\Tests\testing_example\Unit\ControllerView source
class ContrivedControllerTest extends UnitTestCase {
/**
* Data provider for testAdd().
*/
public function provideTestAdd() {
return [
[
4,
2,
2,
],
];
}
/**
* Test add.
*
* @dataProvider provideTestAdd
*/
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,
]));
}
/**
* Data provider for testHandCount().
*/
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,
],
];
}
/**
* Test hand count.
*
* @dataProvider provideTestHandCount
*/
public function testHandCount($expected, $first, $second) {
// Get a mock translation service.
$mock_translation = $this->getStringTranslationStub();
// Create a new controller with our mocked translation service.
$controller = new ContrivedController($mock_translation);
// Set up a reflection for handCount().
$ref_hand_count = new \ReflectionMethod($controller, 'handCount');
// Set handCount() to be public.
$ref_hand_count->setAccessible(TRUE);
// Check out whether handCount() meets our expectation.
$message = $ref_hand_count->invokeArgs($controller, [
$first,
$second,
]);
$this->assertEquals($expected, (string) $message);
}
/**
* Data provider for testHandCountIsolated().
*/
public function providerTestHandCountIsolated() {
$data = [];
// Add one-hand data.
foreach (range(0, 5) as $sum) {
$data[] = [
'I can count these on one hand.',
$sum,
];
}
// Add two-hand data.
foreach (range(6, 10) as $sum) {
$data[] = [
'I need two hands to count these.',
$sum,
];
}
// Add too-many data.
foreach (range(11, 15) as $sum) {
$data[] = [
'That\'s just too many numbers to count.',
$sum,
];
}
return $data;
}
/**
* Test hand count isolated.
*
* @dataProvider providerTestHandCountIsolated
*/
public function testHandCountIsolated($expected, $sum) {
// Mock a ContrivedController, using a mocked translation service.
$controller = $this->getMockBuilder(ContrivedController::class)
->setConstructorArgs([
$this->getStringTranslationStub(),
])
->onlyMethods([
'add',
])
->getMock();
// Mock add() so that it returns our $sum when it's called with (0,0).
$controller->expects($this->once())
->method('add')
->with($this->equalTo(0), $this->equalTo(0))
->willReturn($sum);
// Use reflection to make handCount() public.
$ref_hand_count = new \ReflectionMethod($controller, 'handCount');
// Invoke handCount().
$message = (string) $ref_hand_count->invokeArgs($controller, [
0,
0,
]);
// Assert our expectations.
$this->assertEquals($expected, $message);
}
}
Members
Title Sort descending | Deprecated | Modifiers | Object type | Summary | Overrides |
---|---|---|---|---|---|
ContrivedControllerTest::providerTestHandCountIsolated | public | function | Data provider for testHandCountIsolated(). | ||
ContrivedControllerTest::provideTestAdd | public | function | Data provider for testAdd(). | ||
ContrivedControllerTest::provideTestHandCount | public | function | Data provider for testHandCount(). | ||
ContrivedControllerTest::testAdd | public | function | Test add. | ||
ContrivedControllerTest::testHandCount | public | function | Test hand count. | ||
ContrivedControllerTest::testHandCountIsolated | public | function | Test hand count isolated. | ||
PhpUnitWarnings::$deprecationWarnings | private static | property | Deprecation warnings from PHPUnit to raise with @trigger_error(). | ||
PhpUnitWarnings::addWarning | public | function | Converts PHPUnit deprecation warnings to E_USER_DEPRECATED. | ||
UnitTestCase::$randomGenerator | protected | property | The random generator. | ||
UnitTestCase::$root | protected | property | The app root. | 1 | |
UnitTestCase::assertArrayEquals | Deprecated | protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase::getClassResolverStub | protected | function | Returns a stub class resolver. | ||
UnitTestCase::getConfigFactoryStub | public | function | Returns a stub config factory that behaves according to the passed array. | ||
UnitTestCase::getConfigStorageStub | public | function | Returns a stub config storage that returns the supplied configuration. | ||
UnitTestCase::getContainerWithCacheTagsInvalidator | protected | function | Sets up a container with a cache tags invalidator. | ||
UnitTestCase::getRandomGenerator | protected | function | Gets the random generator for the utility methods. | ||
UnitTestCase::getStringTranslationStub | public | function | Returns a stub translation manager that just returns the passed string. | ||
UnitTestCase::randomMachineName | public | function | Generates a unique random string containing letters and numbers. | ||
UnitTestCase::setUp | protected | function | 338 | ||
UnitTestCase::setUpBeforeClass | public static | function |