You are here

public function XlsTest::testEncode in Excel Serialization 8

@covers ::encode

File

tests/src/Unit/Encoder/XlsTest.php, line 31

Class

XlsTest
Tests the XLS encoder.

Namespace

Drupal\Tests\xls_serialization\Unit\Encoder

Code

public function testEncode() {
  $data = [
    [
      'foo' => 'bar',
      'biz' => 'baz',
    ],
    [
      'foo' => 'bar1',
      'biz' => 'baz1',
    ],
    [
      'foo' => 'bar2',
      'biz' => 'baz2',
    ],
  ];
  $encoder = new Xls();
  $encoded = $encoder
    ->encode($data, 'xlsx');

  // Load the file and verify the data.
  $file = $this
    ->loadXlsFile($encoded);
  $sheet = $file
    ->getSheet(0);

  // Verify headers.
  $this
    ->assertEquals('foo', $sheet
    ->getCellByColumnAndRow(1, 1)
    ->getValue());
  $this
    ->assertEquals('biz', $sheet
    ->getCellByColumnAndRow(2, 1)
    ->getValue());

  // Verify some of the data.
  $this
    ->assertEquals('bar1', $sheet
    ->getCellByColumnAndRow(1, 3)
    ->getValue());
  $this
    ->assertEquals('baz2', $sheet
    ->getCellByColumnAndRow(2, 4)
    ->getValue());
}