You are here

public function BasicTrackerTest::testExceptionHandling in Search API 8

Tests whether a method properly handles exceptions.

@dataProvider exceptionHandlingDataProvider

Parameters

string $tracker_method: The method to test.

array $args: (optional) The arguments to pass to the method.

File

tests/src/Kernel/BasicTrackerTest.php, line 297

Class

BasicTrackerTest
Tests the "default" tracker plugin.

Namespace

Drupal\Tests\search_api\Kernel

Code

public function testExceptionHandling($tracker_method, array $args = []) {

  /** @var \PHPUnit\Framework\MockObject\MockObject|\Drupal\Core\Database\Connection $connection */
  $connection = $this
    ->getMockBuilder(Connection::class)
    ->disableOriginalConstructor()
    ->getMock();
  foreach ([
    'select',
    'insert',
    'update',
    'delete',
  ] as $method) {
    $connection
      ->method($method)
      ->willThrowException(new \Exception());
  }
  $transaction = $this
    ->getMockBuilder(Transaction::class)
    ->disableOriginalConstructor()
    ->getMock();
  $rolled_back = FALSE;
  $rollback = function () use (&$rolled_back) {
    $rolled_back = TRUE;
  };
  $transaction
    ->method('rollback')
    ->willReturnCallback($rollback);
  $connection
    ->method('startTransaction')
    ->willReturn($transaction);
  $this->tracker
    ->setDatabaseConnection($connection);

  /** @var \PHPUnit\Framework\MockObject\MockObject|\Psr\Log\LoggerInterface $logger */
  $logger = $this
    ->createMock(LoggerInterface::class);
  $log = [];
  $logger
    ->method('log')
    ->willReturnCallback(function () use (&$log) {
    $log[] = func_get_args();
  });
  $this->tracker
    ->setLogger($logger);
  call_user_func_array([
    $this->tracker,
    $tracker_method,
  ], $args);
  $this
    ->assertCount(1, $log);
  $this
    ->assertStringStartsWith('%type', $log[0][1]);
  $this
    ->assertFalse($rolled_back);
}