CsvFormatterTest.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. use Grav\Framework\File\Formatter\CsvFormatter;
  3. class CsvFormatterTest extends \Codeception\TestCase\Test
  4. {
  5. public function testEncodeWithAssocColumns()
  6. {
  7. $data = [
  8. ['col1' => 1, 'col2' => 2, 'col3' => 3],
  9. ['col1' => 'aaa', 'col2' => 'bbb', 'col3' => 'ccc'],
  10. ];
  11. $encoded = (new CsvFormatter())->encode($data);
  12. $lines = array_filter(explode(PHP_EOL, $encoded));
  13. self::assertCount(3, $lines);
  14. self::assertEquals('col1,col2,col3', $lines[0]);
  15. }
  16. /**
  17. * TBD - If indexes are all numeric, what's the purpose
  18. * of displaying header
  19. */
  20. public function testEncodeWithIndexColumns()
  21. {
  22. $data = [
  23. [0 => 1, 1 => 2, 2 => 3],
  24. ];
  25. $encoded = (new CsvFormatter())->encode($data);
  26. $lines = array_filter(explode(PHP_EOL, $encoded));
  27. self::assertCount(2, $lines);
  28. self::assertEquals('0,1,2', $lines[0]);
  29. }
  30. public function testEncodeEmptyData()
  31. {
  32. $encoded = (new CsvFormatter())->encode([]);
  33. self::assertEquals('', $encoded);
  34. }
  35. }