You are here

class CSVUnitTest in Migrate Source CSV 8.2

Same name and namespace in other branches
  1. 8.3 tests/src/Unit/Plugin/migrate/source/CSVUnitTest.php \Drupal\Tests\migrate_source_csv\Unit\Plugin\migrate\source\CSVUnitTest

@coversDefaultClass \Drupal\migrate_source_csv\Plugin\migrate\source\CSV

@group migrate_source_csv

Hierarchy

Expanded class hierarchy of CSVUnitTest

File

tests/src/Unit/Plugin/migrate/source/CSVUnitTest.php, line 17

Namespace

Drupal\Tests\migrate_source_csv\Unit\Plugin\migrate\source
View source
class CSVUnitTest extends CSVUnitBase {

  /**
   * The plugin id.
   *
   * @var string
   */
  protected $pluginId;

  /**
   * The plugin definition.
   *
   * @var array
   */
  protected $pluginDefinition;

  /**
   * The migration plugin.
   *
   * @var \Drupal\migrate\Plugin\MigrationInterface
   */
  protected $migration;

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();
    $this->pluginId = 'test csv migration';
    $this->pluginDefinition = [];
    $migration = $this
      ->prophesize(MigrationInterface::class);
    $migration
      ->getIdMap()
      ->willReturn(NULL);
    $this->migration = $migration
      ->reveal();
  }

  /**
   * Tests the construction of CSV.
   *
   * @covers ::__construct
   */
  public function testCreate() {
    $configuration = [
      'path' => $this->happyPath,
      'keys' => [
        'id',
      ],
      'header_row_count' => 1,
    ];
    $csv = new CSV($configuration, $this->pluginId, $this->pluginDefinition, $this->migration);
    $this
      ->assertInstanceOf(CSV::class, $csv);
  }

  /**
   * Tests that a missing path will throw an exception.
   */
  public function testMigrateExceptionPathMissing() {
    $this
      ->setExpectedException(MigrateException::class, 'You must declare the "path" to the source CSV file in your source settings.');
    new CSV([], $this->pluginId, $this->pluginDefinition, $this->migration);
  }

  /**
   * Tests that missing keys will throw an exception.
   */
  public function testMigrateExceptionKeysMissing() {
    $configuration = [
      'path' => $this->happyPath,
    ];
    $this
      ->setExpectedException(MigrateException::class, 'You must declare "keys" as a unique array of fields in your source settings.');
    new CSV($configuration, $this->pluginId, $this->pluginDefinition, $this->migration);
  }

  /**
   * Tests that toString functions as expected.
   *
   * @covers ::__toString
   */
  public function testToString() {
    $configuration = [
      'path' => $this->happyPath,
      'keys' => [
        'id',
      ],
      'header_row_count' => 1,
    ];
    $csv = new CSV($configuration, $this->pluginId, $this->pluginDefinition, $this->migration);
    $this
      ->assertEquals($configuration['path'], (string) $csv);
  }

  /**
   * Tests initialization of the iterator.
   *
   * @covers ::initializeIterator
   */
  public function testInitializeIterator() {
    $configuration = [
      'path' => $this->happyPath,
      'keys' => [
        'id',
      ],
      'header_row_count' => 1,
    ];
    $config_common = [
      'path' => $this->sad,
      'keys' => [
        'id',
      ],
    ];
    $config_delimiter = [
      'delimiter' => '|',
    ];
    $config_enclosure = [
      'enclosure' => '%',
    ];
    $config_escape = [
      'escape' => '`',
    ];
    $csv = new CSV($config_common + $config_delimiter, $this->pluginId, $this->pluginDefinition, $this->migration);
    $this
      ->assertEquals(current($config_delimiter), $csv
      ->initializeIterator()
      ->getCsvControl()[0]);
    $this
      ->assertEquals('"', $csv
      ->initializeIterator()
      ->getCsvControl()[1]);
    $csv = new CSV($config_common + $config_enclosure, $this->pluginId, $this->pluginDefinition, $this->migration);
    $this
      ->assertEquals(',', $csv
      ->initializeIterator()
      ->getCsvControl()[0]);
    $this
      ->assertEquals(current($config_enclosure), $csv
      ->initializeIterator()
      ->getCsvControl()[1]);
    $csv = new CSV($config_common + $config_delimiter + $config_enclosure + $config_escape, $this->pluginId, $this->pluginDefinition, $this->migration);
    $csv_file_object = $csv
      ->initializeIterator();
    $row = [
      '1',
      'Justin',
      'Dean',
      'jdean0@example.com',
      'Indonesia',
      '60.242.130.40',
    ];
    $csv_file_object
      ->rewind();
    $current = $csv_file_object
      ->current();
    $this
      ->assertArrayEquals($row, $current);
    $csv = new CSV($configuration, $this->pluginId, $this->pluginDefinition, $this->migration);
    $csv_file_object = $csv
      ->initializeIterator();
    $row = [
      'id' => '1',
      'first_name' => 'Justin',
      'last_name' => 'Dean',
      'email' => 'jdean0@example.com',
      'country' => 'Indonesia',
      'ip_address' => '60.242.130.40',
    ];
    $second_row = [
      'id' => '2',
      'first_name' => 'Joan',
      'last_name' => 'Jordan',
      'email' => 'jjordan1@example.com',
      'country' => 'Thailand',
      'ip_address' => '137.230.209.171',
    ];
    $csv_file_object
      ->rewind();
    $current = $csv_file_object
      ->current();
    $this
      ->assertArrayEquals($row, $current);
    $csv_file_object
      ->next();
    $next = $csv_file_object
      ->current();
    $this
      ->assertArrayEquals($second_row, $next);
    $column_names = [
      'column_names' => [
        0 => [
          'id' => 'identifier',
        ],
        2 => [
          'last_name' => 'User last name',
        ],
      ],
    ];
    $csv = new CSV($configuration + $column_names, $this->pluginId, $this->pluginDefinition, $this->migration);
    $csv_file_object = $csv
      ->initializeIterator();
    $row = [
      'id' => '1',
      'last_name' => 'Dean',
    ];
    $second_row = [
      'id' => '2',
      'last_name' => 'Jordan',
    ];
    $csv_file_object
      ->rewind();
    $current = $csv_file_object
      ->current();
    $this
      ->assertArrayEquals($row, $current);
    $csv_file_object
      ->next();
    $next = $csv_file_object
      ->current();
    $this
      ->assertArrayEquals($second_row, $next);
  }

  /**
   * Tests that the key is properly identified.
   *
   * @covers ::getIds
   */
  public function testGetIds() {
    $configuration = [
      'path' => $this->happyPath,
      'keys' => [
        'id',
      ],
      'header_row_count' => 1,
    ];
    $csv = new CSV($configuration, $this->pluginId, $this->pluginDefinition, $this->migration);
    $expected = [
      'id' => [
        'type' => 'string',
      ],
    ];
    $this
      ->assertArrayEquals($expected, $csv
      ->getIds());
  }

  /**
   * Tests that the key is properly identified.
   *
   * @covers ::getIds
   */
  public function testGetIdsComplex() {

    // @codingStandardsIgnoreStart
    $configuration = [
      'path' => $this->happyPath,
      'keys' => [
        'id',
        'paragraph' => [
          'type' => 'text',
          'size' => 'big',
        ],
      ],
      'header_row_count' => 1,
    ];

    // @codingStandardsIgnoreEnd
    $csv = new CSV($configuration, $this->pluginId, $this->pluginDefinition, $this->migration);
    $expected = [
      'id' => [
        'type' => 'string',
      ],
      'paragraph' => [
        'type' => 'text',
        'size' => 'big',
      ],
    ];
    $this
      ->assertArrayEquals($expected, $csv
      ->getIds());
  }

  /**
   * Test the getFile return a valid file instance.
   *
   * @covers ::getFile
   */
  public function testGetFile() {
    $configuration = [
      'path' => $this->happyPath,
      'keys' => [
        'id',
      ],
      'header_row_count' => 1,
    ];
    $csv = new CSV($configuration, $this->pluginId, $this->pluginDefinition, $this->migration);
    $csv
      ->initializeIterator();
    $file = $csv
      ->getFile();
    $this
      ->assertInstanceOf(\SplFileObject::class, $file);
  }

  /**
   * Tests that fields have a machine name and description.
   *
   * @covers ::fields
   */
  public function testFields() {
    $configuration = [
      'path' => $this->happyPath,
      'keys' => [
        'id',
      ],
      'header_row_count' => 1,
    ];
    $fields = [
      'id' => 'identifier',
      'first_name' => 'User first name',
    ];
    $expected = $fields + [
      'last_name' => 'last_name',
      'email' => 'email',
      'country' => 'country',
      'ip_address' => 'ip_address',
    ];
    $csv = new CSV($configuration + [
      'fields' => $fields,
    ], $this->pluginId, $this->pluginDefinition, $this->migration);
    $this
      ->assertArrayEquals($expected, $csv
      ->fields());
    $column_names = [
      0 => [
        'id' => 'identifier',
      ],
      2 => [
        'first_name' => 'User first name',
      ],
    ];
    $csv = new CSV($configuration + [
      'fields' => $fields,
      'column_names' => $column_names,
    ], $this->pluginId, $this->pluginDefinition, $this->migration);
    $this
      ->assertArrayEquals($fields, $csv
      ->fields());
  }

  /**
   * Tests configurable CSV file object.
   *
   * @covers ::__construct
   */
  public function testConfigurableCsvFileObject() {
    $configuration = [
      'path' => $this->happyPath,
      'keys' => [
        'id',
      ],
      'header_row_count' => 1,
      'file_class' => FooCSVFileObject::class,
    ];
    $csv = new CSV($configuration, $this->pluginId, $this->pluginDefinition, $this->migration);
    $csv
      ->initializeIterator();
    $fileObject = $this
      ->readAttribute($csv, 'file');
    $this
      ->assertInstanceOf(FooCSVFileObject::class, $fileObject);
  }

  /**
   * Tests configurable CSV file object.
   *
   * @covers ::current
   * @covers ::rewind
   */
  public function testConfigurableCsvFileObjectFlags() {
    $configuration = [
      'path' => $this->multiLine,
      'keys' => [
        'id',
      ],
      'header_row_count' => 1,
      'file_flags' => \SplFileObject::READ_CSV | \SplFileObject::READ_AHEAD | \SplFileObject::SKIP_EMPTY,
    ];
    $csv = new CSV($configuration, $this->pluginId, $this->pluginDefinition, $this->migration);
    $csv_file_object = $csv
      ->initializeIterator();
    $row = [
      'id' => '1',
      'title' => 'Title 1',
      'description' => "Description 1 Line 1\nDescription 1 Line 2\nDescription 1 Line 3",
    ];
    $csv_file_object
      ->rewind();
    $current = $csv_file_object
      ->current();
    $this
      ->assertArrayEquals($row, $current);
  }

  /**
   * Tests malformed CSV file path.
   *
   * @covers ::initializeIterator
   */
  public function testMalformedFilePath() {
    $configuration = [
      'path' => 'non-existent-path',
      'keys' => [
        'id',
      ],
    ];
    $csv = new CSV($configuration, $this->pluginId, $this->pluginDefinition, $this->migration);
    $this
      ->setExpectedException(InvalidPluginDefinitionException::class, 'File path (non-existent-path) does not exist.');
    $csv
      ->initializeIterator();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CSVUnitBase::$happyPath protected property The happy path file url.
CSVUnitBase::$multiLine protected property The multi line file url.
CSVUnitBase::$sad protected property The un-happy path file url.
CSVUnitTest::$migration protected property The migration plugin.
CSVUnitTest::$pluginDefinition protected property The plugin definition.
CSVUnitTest::$pluginId protected property The plugin id.
CSVUnitTest::setUp public function Overrides CSVUnitBase::setUp
CSVUnitTest::testConfigurableCsvFileObject public function Tests configurable CSV file object.
CSVUnitTest::testConfigurableCsvFileObjectFlags public function Tests configurable CSV file object.
CSVUnitTest::testCreate public function Tests the construction of CSV.
CSVUnitTest::testFields public function Tests that fields have a machine name and description.
CSVUnitTest::testGetFile public function Test the getFile return a valid file instance.
CSVUnitTest::testGetIds public function Tests that the key is properly identified.
CSVUnitTest::testGetIdsComplex public function Tests that the key is properly identified.
CSVUnitTest::testInitializeIterator public function Tests initialization of the iterator.
CSVUnitTest::testMalformedFilePath public function Tests malformed CSV file path.
CSVUnitTest::testMigrateExceptionKeysMissing public function Tests that missing keys will throw an exception.
CSVUnitTest::testMigrateExceptionPathMissing public function Tests that a missing path will throw an exception.
CSVUnitTest::testToString public function Tests that toString functions as expected.
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.