<?php
namespace nur\sery\file\base;

use nur\sery\file\FileReader;
use PHPUnit\Framework\TestCase;

class FileReaderTest extends TestCase {
  function testIgnoreBom() {
    # la lecture avec et sans BOM doit être identique
    ## sans BOM
    $reader = new FileReader(__DIR__ . '/impl/sans_bom.txt');
    self::assertSame("0123456789", $reader->fread(10));
    self::assertSame(10, $reader->ftell());
    $reader->seek(30);
    self::assertSame("abcdefghij", $reader->fread(10));
    self::assertSame(40, $reader->ftell());
    $reader->seek(10);
    self::assertSame("ABCDEFGHIJ", $reader->fread(10));
    self::assertSame(20, $reader->ftell());
    $reader->seek(40);
    self::assertSame("0123456789\n", $reader->getContents());
    $reader->close();
    ## avec BOM
    $reader = new FileReader(__DIR__ . '/impl/avec_bom.txt');
    self::assertSame("0123456789", $reader->fread(10));
    self::assertSame(10, $reader->ftell());
    $reader->seek(30);
    self::assertSame("abcdefghij", $reader->fread(10));
    self::assertSame(40, $reader->ftell());
    $reader->seek(10);
    self::assertSame("ABCDEFGHIJ", $reader->fread(10));
    self::assertSame(20, $reader->ftell());
    $reader->seek(40);
    self::assertSame("0123456789\n", $reader->getContents());
    $reader->close();
  }

  function testCsvAutoParams() {
    $reader = new FileReader(__DIR__ . '/impl/msexcel.csv');
    self::assertSame(["nom", "prenom", "age"], $reader->fgetcsv());
    self::assertSame(["clain", "jephte", "50"], $reader->fgetcsv());
    self::assertNull($reader->fgetcsv());
    $reader->close();

    $reader = new FileReader(__DIR__ . '/impl/ooffice.csv');
    self::assertSame(["nom", "prenom", "age"], $reader->fgetcsv());
    self::assertSame(["clain", "jephte", "50"], $reader->fgetcsv());
    self::assertNull($reader->fgetcsv());
    $reader->close();

    $reader = new FileReader(__DIR__ . '/impl/weird.tsv');
    self::assertSame(["nom", "prenom", "age"], $reader->fgetcsv());
    self::assertSame(["clain", "jephte", "50"], $reader->fgetcsv());
    self::assertNull($reader->fgetcsv());
    $reader->close();

    $reader = new FileReader(__DIR__ . '/impl/avec_bom.csv');
    self::assertSame(["nom", "prenom", "age"], $reader->fgetcsv());
    self::assertSame(["clain", "jephte", "50"], $reader->fgetcsv());
    self::assertNull($reader->fgetcsv());
    $reader->close();
  }
}
