You are here

class FileTest in Migrate Plus 8.4

Same name and namespace in other branches
  1. 8.5 tests/src/Unit/data_fetcher/FileTest.php \Drupal\Tests\migrate_plus\Unit\data_fetcher\FileTest

@coversDefaultClass \Drupal\migrate_plus\Plugin\migrate_plus\data_fetcher\File

@group migrate_plus

Hierarchy

Expanded class hierarchy of FileTest

File

tests/src/Unit/data_fetcher/FileTest.php, line 20
PHPUnit tests for the Migrate Plus File 'data fetcher' plugin.

Namespace

Drupal\Tests\migrate_plus\Unit\data_fetcher
View source
class FileTest extends MigrateTestCase {

  /**
   * Directory where test data will be created.
   *
   * @var string
   */
  const BASE_DIRECTORY = 'migration_data';

  /**
   * Minimal migration configuration data.
   *
   * @var array
   */
  private $specificMigrationConfig = [
    'source' => 'url',
    'data_fetcher_plugin' => 'file',
    'data_parser_plugin' => 'json',
    'item_selector' => 0,
    'fields' => [],
    'ids' => [
      'id' => [
        'type' => 'integer',
      ],
    ],
  ];

  /**
   * The data fetcher plugin ID being tested.
   *
   * @var string
   */
  private $dataFetcherPluginId = 'file';

  /**
   * The data fetcher plugin definition.
   *
   * @var array
   */
  private $pluginDefinition = [
    'id' => 'file',
    'title' => 'File',
  ];

  /**
   * Test data to populate a file with.
   *
   * @var string
   */
  private $testData = '[
    {
      "id": 1,
      "name": "Joe Bloggs"
    }
  ]';

  /**
   * Define virtual dir where we'll be creating files in/fetching files from.
   *
   * @var \org\bovigo\vfs\vfsStreamDirectory
   */
  private $baseDir;

  /**
   * Set up test environment.
   */
  public function setUp() {
    $this->baseDir = vfsStream::setup(self::BASE_DIRECTORY);
  }

  /**
   * Test fetching a valid file.
   */
  public function testFetchFile() {
    $file_name = 'file.json';
    $file_path = vfsStream::url(implode(DIRECTORY_SEPARATOR, [
      self::BASE_DIRECTORY,
      $file_name,
    ]));
    $migration_config = $this->specificMigrationConfig + [
      'urls' => [
        $file_path,
      ],
    ];
    $plugin = new File($migration_config, $this->dataFetcherPluginId, $this->pluginDefinition);
    $tree = [
      $file_name => $this->testData,
    ];
    vfsStream::create($tree, $this->baseDir);
    $expected = json_decode($this->testData, TRUE);
    $retrieved = json_decode($plugin
      ->getResponseContent($file_path), TRUE);
    $this
      ->assertEquals($expected, $retrieved);
  }

  /**
   * Test fetching multiple valid files.
   */
  public function testFetchMultipleFiles() {
    $number_of_files = 3;
    $file_paths = [];
    $file_names = [];
    for ($i = 0; $i < $number_of_files; $i++) {
      $file_name = 'file_' . $i . '.json';
      $file_names[] = $file_name;
      $file_paths[] = vfsStream::url(implode(DIRECTORY_SEPARATOR, [
        self::BASE_DIRECTORY,
        $file_name,
      ]));
    }
    $migration_config = $this->specificMigrationConfig + [
      'urls' => $file_paths,
    ];
    $plugin = new File($migration_config, $this->dataFetcherPluginId, $this->pluginDefinition);
    for ($i = 0; $i < $number_of_files; $i++) {
      $file_name = $file_names[$i];
      $file_path = $file_paths[$i];
      $tree = [
        $file_name => $this->testData,
      ];
      vfsStream::create($tree, $this->baseDir);
      $expected = json_decode($this->testData);
      $retrieved = json_decode($plugin
        ->getResponseContent($file_path));
      $this
        ->assertEquals($expected, $retrieved);
    }
  }

  /**
   * Test trying to fetch an unreadable file results in exception.
   */
  public function testFetchUnreadableFile() {
    $file_name = 'file.json';
    $file_path = vfsStream::url(implode(DIRECTORY_SEPARATOR, [
      self::BASE_DIRECTORY,
      $file_name,
    ]));
    $migration_config = $this->specificMigrationConfig + [
      'urls' => [
        $file_path,
      ],
    ];
    $plugin = new File($migration_config, $this->dataFetcherPluginId, $this->pluginDefinition);

    // Create an unreadable file.
    vfsStream::newFile($file_name, 0300)
      ->withContent($this->testData)
      ->at($this->baseDir);

    // Trigger exception trying to read the non-readable file.
    $this
      ->setExpectedException(MigrateException::class, 'file parser plugin: could not retrieve data from vfs://migration_data/file.json');
    $plugin
      ->getResponseContent($file_path);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FileTest::$baseDir private property Define virtual dir where we'll be creating files in/fetching files from.
FileTest::$dataFetcherPluginId private property The data fetcher plugin ID being tested.
FileTest::$pluginDefinition private property The data fetcher plugin definition.
FileTest::$specificMigrationConfig private property Minimal migration configuration data.
FileTest::$testData private property Test data to populate a file with.
FileTest::BASE_DIRECTORY constant Directory where test data will be created.
FileTest::setUp public function Set up test environment. Overrides UnitTestCase::setUp
FileTest::testFetchFile public function Test fetching a valid file.
FileTest::testFetchMultipleFiles public function Test fetching multiple valid files.
FileTest::testFetchUnreadableFile public function Test trying to fetch an unreadable file results in exception.
MigrateTestCase::$idMap protected property The migration ID map.
MigrateTestCase::$migrationConfiguration protected property An array of migration configuration values. 16
MigrateTestCase::$migrationStatus protected property Local store for mocking setStatus()/getStatus().
MigrateTestCase::createSchemaFromRow protected function Generates a table schema from a row.
MigrateTestCase::getDatabase protected function Gets an SQLite database connection object for use in tests.
MigrateTestCase::getMigration protected function Retrieves a mocked migration. 1
MigrateTestCase::getValue protected function Gets the value on a row for a given key. 1
MigrateTestCase::queryResultTest public function Tests a query.
MigrateTestCase::retrievalAssertHelper protected function Asserts tested values during test retrieval.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
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.