You are here

class CsvEncoderTest in CSV Serialization 8

Same name and namespace in other branches
  1. 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

Expanded class hierarchy of CsvEncoderTest

File

tests/src/Unit/CsvEncoderTest.php, line 13

Namespace

Drupal\Tests\csv_serialization\Unit
View 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

Namesort descending Modifiers Type Description Overrides
CsvEncoderTest::$conversionService public property
CsvEncoderTest::provideDecodeData public function Provides data for testing the decoder.
CsvEncoderTest::provideEncodeData public function Provides data for testing the encoder.
CsvEncoderTest::setUp public function Overrides UnitTestCase::setUp
CsvEncoderTest::testDecodeCsv public function Tests the data structure created by decoding a CSV.
CsvEncoderTest::testEncodeCsv public function Tests the CSV output of the encoder.
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.