Commit 7164045c authored by claudiu.cristea's avatar claudiu.cristea Committed by claudiu.cristea

Issue #2933932 by claudiu.cristea: Coding standards

parent bf4042a2
......@@ -2,19 +2,28 @@
## Overview
The module provides a migrate source plugin for importing data from spreadsheet files. This source plugin uses the [PhpOffice/PhpSpreadsheet](https://github.com/PHPOffice/PhpSpreadsheet) library to read from the spreadsheet files.
The module provides a migrate source plugin for importing data from spreadsheet
files. This source plugin uses the
[PhpOffice/PhpSpreadsheet](https://github.com/PHPOffice/PhpSpreadsheet) library
to read from the spreadsheet files.
[The supported source files](https://github.com/PHPOffice/PhpSpreadsheet#file-formats-supported) includes .ods, .xls, .xlsx, .csv.
[The supported source
files](https://github.com/PHPOffice/PhpSpreadsheet#file-formats-supported)
includes .ods, .xls, .xlsx, .csv.
## Installing PHPSpreadsheet
### Requirements
Migrate Spreadsheet module requires PHP >= 5.6 due to its dependency on [PhpOffice/PhpSpreadsheet](https://github.com/PHPOffice/PhpSpreadsheet) library, which requires at least PHP 5.6.
Migrate Spreadsheet module requires PHP >= 5.6 due to its dependency on
[PhpOffice/PhpSpreadsheet](https://github.com/PHPOffice/PhpSpreadsheet) library,
which requires at least PHP 5.6.
### Get the code
Usually we use [Composer](https://getcomposer.org/) to install dependencies in Drupal 8. All you need to do is to add this module as dependency in your project top `composer.json` file:
Usually we use [Composer](https://getcomposer.org/) to install dependencies in
Drupal 8. All you need to do is to add this module as dependency in your project
top `composer.json` file:
```json
{
......@@ -28,7 +37,9 @@ Usually we use [Composer](https://getcomposer.org/) to install dependencies in D
}
```
Then a `$ composer update` should be issued and Composer will install the [PhpOffice/PhpSpreadsheet](https://github.com/PHPOffice/PhpSpreadsheet) dependency.
Then a `$ composer update` should be issued and Composer will install the
[PhpOffice/PhpSpreadsheet](https://github.com/PHPOffice/PhpSpreadsheet)
dependency.
### Enable the module
......
......@@ -40,7 +40,7 @@ class Spreadsheet extends SourcePluginBase implements ConfigurablePluginInterfac
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\migrate\Plugin\MigrationInterface
* @param \Drupal\migrate\Plugin\MigrationInterface $migration
* The current migration.
* @param \Drupal\Core\File\FileSystemInterface $file_system
* The file system service.
......
......@@ -157,8 +157,8 @@ class SpreadsheetIterator implements SpreadsheetIteratorInterface {
if (!isset($this->cache['origin'])) {
$config = $this->getConfiguration();
if (empty($config['origin'])) {
// Defaulting to a table where the first row contains the header and data
// starts on the second row, column A.
// Defaulting to a table where the first row contains the header and
// data starts on the second row, column A.
return 'A2';
}
if ($coordinates = Coordinate::coordinateFromString($config['origin'])) {
......@@ -305,6 +305,7 @@ class SpreadsheetIterator implements SpreadsheetIteratorInterface {
* Gets the absolute row index.
*
* @return int
* The absolute row index.
*/
protected function getAbsoluteRowIndex() {
if (!isset($this->absoluteRow)) {
......
......@@ -5,7 +5,7 @@ namespace Drupal\migrate_spreadsheet;
/**
* Provides an interface for spreadsheet iterators.
*/
interface SpreadsheetIteratorInterface extends \Iterator{
interface SpreadsheetIteratorInterface extends \Iterator {
/**
* Sets the iterator configuration.
......@@ -14,7 +14,8 @@ interface SpreadsheetIteratorInterface extends \Iterator{
*
* @param array $configuration
* An associative array with the next keys:
* - worksheet (\PhpOffice\PhpSpreadsheet\Worksheet\Worksheet): The worksheet object.
* - worksheet (\PhpOffice\PhpSpreadsheet\Worksheet\Worksheet): The
* worksheet object.
* - columns (string[]): An indexed array of columns.
* - keys (string[]): A list of columns that are giving the primary key.
* - header_row (int): The index of the first row from where the table
......@@ -37,6 +38,7 @@ interface SpreadsheetIteratorInterface extends \Iterator{
* Gets the iterator configuration.
*
* @return array
* The iterator configuration as an associative array.
*/
public function getConfiguration();
......@@ -44,6 +46,7 @@ interface SpreadsheetIteratorInterface extends \Iterator{
* Gets the worksheet.
*
* @return \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet
* The PHP Office worksheet object.
*
* @throws \InvalidArgumentException
* If an empty or invalid 'worksheet' has been passed.
......@@ -54,6 +57,7 @@ interface SpreadsheetIteratorInterface extends \Iterator{
* Retrieves the top-left origin of data area.
*
* @return string
* The top-left cell of data area (such as A2 or B5).
*/
public function getOrigin();
......@@ -85,6 +89,7 @@ interface SpreadsheetIteratorInterface extends \Iterator{
* Gets the header row index.
*
* @return int
* The header row index.
*/
public function getHeaderRow();
......@@ -92,6 +97,7 @@ interface SpreadsheetIteratorInterface extends \Iterator{
* Gets the name of the row index column.
*
* @return string
* A name to be given to the column returning the numeric index of the row.
*/
public function getRowIndexColumn();
......@@ -112,6 +118,7 @@ interface SpreadsheetIteratorInterface extends \Iterator{
* Gets the total number of rows in the worksheet.
*
* @return int
* The total number of rows in the worksheet.
*/
public function getRowsCount();
......@@ -119,11 +126,12 @@ interface SpreadsheetIteratorInterface extends \Iterator{
* Gets the total number of columns in the worksheet.
*
* @return int
* The total number of columns in the worksheet.
*/
public function getColumnsCount();
/**
* Clears the internal cache.
* Clears the iterator internal, static cache.
*/
public function clearCache();
......
......@@ -13,7 +13,7 @@ use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
*
* @group migrate_spreadsheet
*/
class SpreadsheetIteratorTest extends UnitTestCase {
class SpreadsheetIteratorTest extends UnitTestCase {
/**
* A worksheet.
......@@ -75,6 +75,7 @@ class SpreadsheetIteratorTest extends UnitTestCase {
* Provides test cases for ::testGetOrigin()
*
* @return array[]
* A list of test parameters to be passed to ::testGetOrigin().
*/
public function providerTestGetException() {
return [
......@@ -97,14 +98,24 @@ class SpreadsheetIteratorTest extends UnitTestCase {
* @covers ::getHeaders
*/
public function testGetHeaders() {
$cols = ['column b' => 'B', 'column c' => 'C', 'column d' => 'D', 'column e' => 'E'];
$cols = [
'column b' => 'B',
'column c' => 'C',
'column d' => 'D',
'column e' => 'E',
];
$this->assertSame($cols, $this->iterator->getHeaders());
// Check headers when there's no header row.
$config = $this->iterator->getConfiguration();
unset($config['header_row']);
$this->iterator->setConfiguration($config);
$this->assertSame(['B' => 'B', 'C' => 'C', 'D' => 'D', 'E' => 'E'], $this->iterator->getHeaders());
$this->assertSame([
'B' => 'B',
'C' => 'C',
'D' => 'D',
'E' => 'E',
], $this->iterator->getHeaders());
// Check duplicate headers.
$this->getWorksheet()->setCellValue('C2', 'column b');
......@@ -124,19 +135,34 @@ class SpreadsheetIteratorTest extends UnitTestCase {
$this->assertTrue($this->iterator->valid());
$this->assertSame([3], $this->iterator->key());
$this->assertSame(['row' => 3, 'column b' => 'cell b0', 'column d' => 'cell d0', 'column e' => 'cell e0'], $this->iterator->current());
$this->assertSame([
'row' => 3,
'column b' => 'cell b0',
'column d' => 'cell d0',
'column e' => 'cell e0',
], $this->iterator->current());
// Move the cursor.
$this->iterator->next();
$this->assertTrue($this->iterator->valid());
$this->assertSame([4], $this->iterator->key());
$this->assertSame(['row' => 4, 'column b' => 'cell b1', 'column d' => 'cell d1', 'column e' => 'cell e1'], $this->iterator->current());
$this->assertSame([
'row' => 4,
'column b' => 'cell b1',
'column d' => 'cell d1',
'column e' => 'cell e1',
], $this->iterator->current());
// Move the cursor.
$this->iterator->next();
$this->assertTrue($this->iterator->valid());
$this->assertSame([5], $this->iterator->key());
$this->assertSame(['row' => 5, 'column b' => 'cell b2', 'column d' => 'cell d2', 'column e' => 'cell e2'], $this->iterator->current());
$this->assertSame([
'row' => 5,
'column b' => 'cell b2',
'column d' => 'cell d2',
'column e' => 'cell e2',
], $this->iterator->current());
// Move the cursor. Should run out of set.
$this->iterator->next();
......@@ -146,14 +172,25 @@ class SpreadsheetIteratorTest extends UnitTestCase {
$this->iterator->rewind();
$this->assertTrue($this->iterator->valid());
$this->assertSame([3], $this->iterator->key());
$this->assertSame(['row' => 3, 'column b' => 'cell b0', 'column d' => 'cell d0', 'column e' => 'cell e0'], $this->iterator->current());
$this->assertSame([
'row' => 3,
'column b' => 'cell b0',
'column d' => 'cell d0',
'column e' => 'cell e0',
], $this->iterator->current());
// Try to return all columns.
$config['columns'] = [];
$this->iterator->setConfiguration($config);
$this->assertTrue($this->iterator->valid());
$this->assertSame([3], $this->iterator->key());
$this->assertSame(['row' => 3, 'column b' => 'cell b0', 'column c' => 'cell c0', 'column d' => 'cell d0', 'column e' => 'cell e0'], $this->iterator->current());
$this->assertSame([
'row' => 3,
'column b' => 'cell b0',
'column c' => 'cell c0',
'column d' => 'cell d0',
'column e' => 'cell e0',
], $this->iterator->current());
// Use different primary keys.
$config['columns'] = ['column b', 'column e'];
......@@ -162,7 +199,12 @@ class SpreadsheetIteratorTest extends UnitTestCase {
$this->iterator->setConfiguration($config);
$this->assertTrue($this->iterator->valid());
$this->assertSame(['cell c0', 'cell d0'], $this->iterator->key());
$this->assertSame(['column b' => 'cell b0', 'column c' => 'cell c0', 'column d' => 'cell d0', 'column e' => 'cell e0'], $this->iterator->current());
$this->assertSame([
'column b' => 'cell b0',
'column c' => 'cell c0',
'column d' => 'cell d0',
'column e' => 'cell e0',
], $this->iterator->current());
// Test with no header_row.
unset($config['header_row']);
......@@ -171,19 +213,25 @@ class SpreadsheetIteratorTest extends UnitTestCase {
$this->iterator->setConfiguration($config);
$this->assertTrue($this->iterator->valid());
$this->assertSame(['cell c0', 'cell d0'], $this->iterator->key());
$this->assertSame(['B' => 'cell b0', 'C' => 'cell c0', 'D' => 'cell d0', 'E' => 'cell e0'], $this->iterator->current());
$this->assertSame([
'B' => 'cell b0',
'C' => 'cell c0',
'D' => 'cell d0',
'E' => 'cell e0',
], $this->iterator->current());
}
/**
* Populates a testing worksheet.
*
* @return \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet
* A PhpSpreadsheet worksheet object.
*/
protected function getWorksheet() {
if (!isset($this->worksheet)) {
// Test case:
// - origin: B3
// - header_row: 2
// - origin: B3;
// - header_row: 2.
$this->worksheet = (new Worksheet())
// The header row starts on the 2nd line.
->setCellValue('B2', 'column b')
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment