diff --git a/migrate_source_csv/tests/src/Unit/CSVFileObjectTest.php b/migrate_source_csv/tests/src/Unit/CSVFileObjectTest.php new file mode 100644 index 0000000..2392aa9 --- /dev/null +++ b/migrate_source_csv/tests/src/Unit/CSVFileObjectTest.php @@ -0,0 +1,136 @@ +csv_file_object = new CSVFileObject($this->happyPath); + } + + /** + * Tests that the construction appropriately creates a CSVFileObject. + * + * @test + * + * @covers ::__construct + */ + public function create() { + $this->assertInstanceOf(CSVFileObject::class, $this->csv_file_object); + $flags = CSVFileObject::READ_CSV | CSVFileObject::READ_AHEAD | CSVFileObject::DROP_NEW_LINE | CSVFileObject::SKIP_EMPTY; + $this->assertSame($flags, $this->csv_file_object->getFlags()); + } + + /** + * Tests that the header row count is correctly set. + * + * @test + * + * @covers ::setHeaderRowCount + */ + public function setHeaderRowCount() { + $expected = 2; + $this->csv_file_object->setHeaderRowCount($expected); + + return $this->csv_file_object->getHeaderRowCount(); + } + + /** + * Tests that the header row count is correctly returned. + * + * @test + * + * @depends setHeaderRowCount + * + * @covers ::getHeaderRowCount + */ + public function getHeaderRowCount($actual) { + $expected = 2; + $this->assertEquals($expected, $actual); + } + + /** + * Tests that line count is correct. + * + * @test + * + * @covers ::count + */ + public function countLines() { + $expected = 15; + $this->csv_file_object->setHeaderRowCount(1); + $actual = $this->csv_file_object->count(); + + $this->assertEquals($expected, $actual); + } + + /** + * Tests that the current row is correctly returned. + * + * @test + * + * @covers ::current + * @covers ::rewind + * @covers ::getColumnNames + * @covers ::setColumnNames + */ + public function current() { + $column_names = array( + array('id' => 'Identifier'), + array('first_name' => 'First Name'), + array('last_name' => 'Last Name'), + array('email' => 'Email'), + array('country' => 'Country'), + array('ip_address' => 'IP Address'), + ); + $columns = array(); + foreach ($column_names as $values) { + $columns[] = key($values); + } + $row = array( + '1', + 'Justin', + 'Dean', + 'jdean0@example.com', + 'Indonesia', + '60.242.130.40', + ); + + $csv_file_object = $this->csv_file_object; + $csv_file_object->rewind(); + $current = $csv_file_object->current(); + $this->assertArrayEquals($columns, $current); + + $csv_file_object->setHeaderRowCount(1); + $csv_file_object->rewind(); + $current = $csv_file_object->current(); + $this->assertArrayEquals($row, $current); + + $csv_file_object->setColumnNames($column_names); + $csv_file_object->rewind(); + $current = $csv_file_object->current(); + $this->assertArrayEquals($columns, array_keys($current)); + $this->assertArrayEquals($row, array_values($current)); + $this->assertArrayEquals($column_names, $csv_file_object->getColumnNames()); + } + +}