class CSVTest in Migrate Source CSV 8
Same name and namespace in other branches
- 8.2 tests/src/Kernel/Plugin/migrate/source/CSVTest.php \Drupal\Tests\migrate_source_csv\Unit\Plugin\migrate\source\CSVTest
@coversDefaultClass \Drupal\migrate_source_csv\Plugin\migrate\source\CSV
@group migrate_source_csv
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\migrate_source_csv\Unit\CSVUnitTestCase
- class \Drupal\Tests\migrate_source_csv\Unit\Plugin\migrate\source\CSVTest
- class \Drupal\Tests\migrate_source_csv\Unit\CSVUnitTestCase
Expanded class hierarchy of CSVTest
File
- tests/
src/ Unit/ Plugin/ migrate/ source/ CSVTest.php, line 17 - Code for CSVTest.php.
Namespace
Drupal\Tests\migrate_source_csv\Unit\Plugin\migrate\sourceView source
class CSVTest extends CSVUnitTestCase {
/**
* The plugin id.
*
* @var string
*/
protected $pluginId;
/**
* The plugin definition.
*
* @var array
*/
protected $pluginDefinition;
/**
* The mock migration plugin.
*
* @var \Drupal\migrate\Entity\MigrationInterface
*/
protected $plugin;
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
$this->pluginId = 'test csv migration';
$this->pluginDefinition = [];
$this->plugin = $this
->getMock('\\Drupal\\migrate\\Entity\\MigrationInterface');
}
/**
* Tests the construction of CSV.
*
* @test
*
* @covers ::__construct
*/
public function create() {
$configuration = [
'path' => $this->happyPath,
'keys' => [
'id',
],
'header_row_count' => 1,
];
$csv = new CSV($configuration, $this->pluginId, $this->pluginDefinition, $this->plugin);
$this
->assertInstanceOf('\\Drupal\\migrate_source_csv\\Plugin\\migrate\\source\\CSV', $csv);
}
/**
* Tests that a missing path will throw an exception.
*
* @test
*
* @expectedException \Drupal\migrate\MigrateException
*
* @expectedExceptionMessage You must declare the "path" to the source CSV file in your source settings.
*/
public function migrateExceptionPathMissing() {
new CSV([], $this->pluginId, $this->pluginDefinition, $this->plugin);
}
/**
* Tests that missing keys will throw an exception.
*
* @test
*
* @expectedException \Drupal\migrate\MigrateException
*
* @expectedExceptionMessage You must declare "keys" as a unique array of fields in your source settings.
*/
public function migrateExceptionKeysMissing() {
$configuration = [
'path' => $this->happyPath,
];
new CSV($configuration, $this->pluginId, $this->pluginDefinition, $this->plugin);
}
/**
* Tests that toString functions as expected.
*
* @test
*
* @covers ::__toString
*/
public function toString() {
$configuration = [
'path' => $this->happyPath,
'keys' => [
'id',
],
'header_row_count' => 1,
];
$csv = new CSV($configuration, $this->pluginId, $this->pluginDefinition, $this->plugin);
$this
->assertEquals($configuration['path'], (string) $csv);
}
/**
* Tests initialization of the iterator.
*
* @test
*
* @covers ::initializeIterator
*/
public function initializeIterator() {
$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->plugin);
$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->plugin);
$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->plugin);
$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->plugin);
$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->plugin);
$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.
*
* @test
*
* @covers ::getIds
*/
public function getIds() {
$configuration = [
'path' => $this->happyPath,
'keys' => [
'id',
],
'header_row_count' => 1,
];
$csv = new CSV($configuration, $this->pluginId, $this->pluginDefinition, $this->plugin);
$expected = [
'id' => [
'type' => 'string',
],
];
$this
->assertArrayEquals($expected, $csv
->getIds());
}
/**
* Tests that fields have a machine name and description.
*
* @test
*
* @covers ::fields
*/
public function fields() {
$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, $this->pluginId, $this->pluginDefinition, $this->plugin);
$csv = new CSV($configuration + [
'fields' => $fields,
], $this->pluginId, $this->pluginDefinition, $this->plugin);
$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->plugin);
$this
->assertArrayEquals($fields, $csv
->fields());
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CSVTest:: |
protected | property | The mock migration plugin. | |
CSVTest:: |
protected | property | The plugin definition. | |
CSVTest:: |
protected | property | The plugin id. | |
CSVTest:: |
public | function | Tests the construction of CSV. | |
CSVTest:: |
public | function | Tests that fields have a machine name and description. | |
CSVTest:: |
public | function | Tests that the key is properly identified. | |
CSVTest:: |
public | function | Tests initialization of the iterator. | |
CSVTest:: |
public | function | Tests that missing keys will throw an exception. | |
CSVTest:: |
public | function | Tests that a missing path will throw an exception. | |
CSVTest:: |
public | function |
Overrides CSVUnitTestCase:: |
|
CSVTest:: |
public | function | Tests that toString functions as expected. | |
CSVUnitTestCase:: |
protected | property | The happy path file url. | |
CSVUnitTestCase:: |
protected | property | The un-happy path file url. | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |