You are here

protected function FileLogTokenTest::setUp in File Log 8

Same name and namespace in other branches
  1. 2.0.x tests/src/Unit/FileLogTokenTest.php \Drupal\Tests\filelog\Unit\FileLogTokenTest::setUp()

Overrides UnitTestCase::setUp

File

tests/src/Unit/FileLogTokenTest.php, line 61

Class

FileLogTokenTest
Test the filelog message token integration.

Namespace

Drupal\Tests\filelog\Unit

Code

protected function setUp() : void {
  parent::setUp();

  // Get from filelog/tests/src/Unit to filelog/.
  $root = dirname(__DIR__, 3);
  require_once $root . '/filelog.tokens.inc';
  $this->logMessageParser = new LogMessageParser();
  $this->token = $this
    ->getMockBuilder(Token::class)
    ->disableOriginalConstructor()
    ->getMock();
  $this->dateFormatter = $this
    ->createMock(DateFormatterInterface::class);
  $entityTypeManager = $this
    ->createMock(EntityTypeManagerInterface::class);
  $entityTypeRepository = $this
    ->createMock(EntityTypeRepositoryInterface::class);
  $this->userStorage = $this
    ->createMock(EntityStorageInterface::class);

  // Mock the User entity type resolution.
  // InvocationMocker::with(...$arguments) incorrectly documented.
  // Suppress until phpunit/phpunit:8.2.1.

  /* @noinspection PhpParamsInspection */
  $entityTypeRepository
    ->method('getEntityTypeFromClass')
    ->with(User::class)
    ->willReturn('user');

  // Mock the user entity storage (actual user-loading mocked each test).
  // InvocationMocker::with(...$arguments) incorrectly documented.
  // Suppress until phpunit/phpunit:8.2.1.

  /* @noinspection PhpParamsInspection */
  $entityTypeManager
    ->method('getStorage')
    ->with('user')
    ->willReturn($this->userStorage);
  $this->token
    ->method('findWithPrefix')
    ->willReturnCallback([
    static::class,
    'tokenFindWithPrefix',
  ]);

  // Set up the container with the required mocks.
  $container = new ContainerBuilder();
  $container
    ->set('token', $this->token);
  $container
    ->set('date.formatter', $this->dateFormatter);
  $container
    ->set('entity_type.manager', $entityTypeManager);
  $container
    ->set('entity_type.repository', $entityTypeRepository);
  Drupal::setContainer($container);
}