class CsvEncoderTest in CSV Serialization 8
Same name and namespace in other branches
- 8.2 tests/src/Unit/CsvEncoderTest.php \Drupal\Tests\csv_serialization\Unit\CsvEncoderTest
Tests the encoding and decoding functionality of CsvEncoder.
@group test_example
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\csv_serialization\Unit\CsvEncoderTest
Expanded class hierarchy of CsvEncoderTest
File
- tests/
src/ Unit/ CsvEncoderTest.php, line 13
Namespace
Drupal\Tests\csv_serialization\UnitView source
class CsvEncoderTest extends UnitTestCase {
/**
* @var \Drupal\csv_serialization\Encoder\CsvEncoder
*/
public $conversionService;
/**
*
*/
public function setUp() {
$this->encoder = new CsvEncoder();
}
/**
* Provides data for testing the encoder.
*
* @return array
* Am array of multi-dimensional arrays, to be converted to CSVs.
*/
public function provideEncodeData() {
$csv1_data = [
// Row 1.
[
'title' => 'This is title 1',
'body' => 'This is, body 1',
'images' => [
'img1.jpg',
],
'alias' => '',
'status' => 1,
],
// Row 2.
[
'title' => 'This is title 2',
'body' => '<p>This is, body 2</p>',
'images' => [
'img1.jpg',
'img2.jpg',
],
'alias' => '',
'status' => 0,
],
// Row 3.
[
'title' => 'This is title 3',
'body' => [
'<p>This is, body 3</p>',
],
'images' => [
[
'src' => 'img1.jpg',
'alt' => 'Image 1',
],
[
'src' => 'img2.jpg',
'alt' => 'Image, 2',
],
],
'alias' => '',
'status' => 0,
],
];
$csv1_encoded = trim(file_get_contents(__DIR__ . '/CsvEncoderTest.csv'));
return [
[
$csv1_data,
$csv1_encoded,
],
];
}
/**
* Provides data for testing the decoder.
*/
public function provideDecodeData() {
$csv1_encoded = file_get_contents(__DIR__ . '/CsvEncoderTest.csv');
$csv1_data = [
// Row 1.
[
'title' => 'This is title 1',
'body' => 'This is, body 1',
'images' => 'img1.jpg',
'alias' => '',
'status' => 1,
],
// Row 2.
[
'title' => 'This is title 2',
'body' => 'This is, body 2',
'images' => [
'img1.jpg',
'img2.jpg',
],
'alias' => '',
'status' => 0,
],
// Row 3.
[
'title' => 'This is title 3',
'body' => 'This is, body 3',
// Note that due to the flattening of multi-dimensional arrays
// during encoding, this does not match Row 3 in provideEncodeData().
'images' => [
'img1.jpg',
'Image 1',
'img2.jpg',
'Image, 2',
],
'alias' => '',
'status' => 0,
],
];
return [
[
$csv1_encoded,
$csv1_data,
],
];
}
/**
* Tests the CSV output of the encoder.
*
* @dataProvider provideEncodeData
*/
public function testEncodeCsv($csv_data, $csv_encoded) {
// @todo Test passing in arguments to the constructor. E.g., $separator, $enclosure, strip_tags, etc.
// Note that what we encode does not exactly represent the hierarchy of
// the data passed in. This is because cells containing multi-dimensional
// arrays are flattened. Thus, encode($input) != decode($output).
$this
->assertEquals($csv_encoded, $this->encoder
->encode($csv_data, 'csv'));
}
/**
* Tests the data structure created by decoding a CSV.
*
* @dataProvider provideDecodeData
*/
public function testDecodeCsv($csv_encoded, $csv_data) {
$this
->assertEquals($csv_data, $this->encoder
->decode($csv_encoded, 'csv'));
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CsvEncoderTest:: |
public | property | ||
CsvEncoderTest:: |
public | function | Provides data for testing the decoder. | |
CsvEncoderTest:: |
public | function | Provides data for testing the encoder. | |
CsvEncoderTest:: |
public | function |
Overrides UnitTestCase:: |
|
CsvEncoderTest:: |
public | function | Tests the data structure created by decoding a CSV. | |
CsvEncoderTest:: |
public | function | Tests the CSV output of the encoder. | |
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. |