You are here

public function FileTestBase::testPrepareValue in Feeds 8.3

@covers ::prepareValue @dataProvider dataProviderPrepareValue

Parameters

array $expected: The expected values.

array $values: The values to pass to prepareValue().

string $expected_exception: (optional) The name of the expected exception class.

string $expected_exception_message: (optional) The expected exception message.

File

tests/src/Kernel/Feeds/Target/FileTestBase.php, line 147

Class

FileTestBase
Base class for file field tests.

Namespace

Drupal\Tests\feeds\Kernel\Feeds\Target

Code

public function testPrepareValue(array $expected, array $values, $expected_exception = NULL, $expected_exception_message = NULL) {
  $method = $this
    ->getProtectedClosure($this->targetPlugin, 'prepareValue');

  // Add in base URL.
  if (isset($values['target_id'])) {
    $file_path = strtr($values['target_id'], [
      '[url]' => $this
        ->resourcesPath(),
    ]);
    $values['target_id'] = strtr($values['target_id'], [
      '[url]' => $this
        ->resourcesUrl(),
    ]);

    // Set guzzle client response.
    if (file_exists($file_path)) {
      $this->client
        ->request('GET', $values['target_id'], Argument::any())
        ->will(function () use ($file_path) {
        return new Response(200, [], file_get_contents($file_path));
      });
    }
    else {
      $this->client
        ->request('GET', $values['target_id'], Argument::any())
        ->will(function () {
        return new Response(404, [], '');
      });
    }
  }

  // Set expected exception if there is one expected.
  if ($expected_exception) {
    $this
      ->expectException($expected_exception);
    if ($expected_exception_message) {
      $expected_exception_message = strtr($expected_exception_message, [
        '[url]' => $this
          ->resourcesUrl(),
      ]);
      $this
        ->expectExceptionMessage($expected_exception_message);
    }
  }

  // Call prepareValue().
  $method(0, $values);

  // Asserts.
  foreach ($expected as $key => $value) {
    $this
      ->assertEquals($value, $values[$key]);
  }
}