You are here

public function ContrivedControllerTest::testHandCountIsolated in Examples for Developers 8

Same name and namespace in other branches
  1. 3.x modules/testing_example/tests/src/Unit/Controller/ContrivedControllerTest.php \Drupal\Tests\testing_example\Unit\Controller\ContrivedControllerTest::testHandCountIsolated()

Test hand count isolated.

@dataProvider providerTestHandCountIsolated

File

testing_example/tests/src/Unit/Controller/ContrivedControllerTest.php, line 101

Class

ContrivedControllerTest
The class to test ContrivedController.

Namespace

Drupal\Tests\testing_example\Unit\Controller

Code

public function testHandCountIsolated($expected, $sum) {

  // Mock a ContrivedController, using a mocked translation service.
  $controller = $this
    ->getMockBuilder(ContrivedController::class)
    ->setConstructorArgs([
    $this
      ->getStringTranslationStub(),
  ])
    ->setMethods([
    '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');
  $ref_hand_count
    ->setAccessible(TRUE);

  // Invoke handCount().
  $message = (string) $ref_hand_count
    ->invokeArgs($controller, [
    0,
    0,
  ]);

  // Assert our expectations.
  $this
    ->assertEquals($expected, $message);
}