2024-04-23 05:40:56 +04:00
|
|
|
<?php
|
2024-04-25 17:46:14 +04:00
|
|
|
namespace nur\sery\file\base;
|
2024-04-23 05:40:56 +04:00
|
|
|
|
2024-04-25 17:46:14 +04:00
|
|
|
use nur\sery\file\base\FileReader;
|
2024-04-23 05:40:56 +04:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
|
|
|
class FileReaderTest extends TestCase {
|
|
|
|
function testIgnoreBom() {
|
|
|
|
# la lecture avec et sans BOM doit être identique
|
|
|
|
## sans BOM
|
2024-04-25 17:46:14 +04:00
|
|
|
$reader = new FileReader(__DIR__ . '/impl/sans_bom.txt');
|
2024-04-23 05:40:56 +04:00
|
|
|
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
|
2024-04-25 17:46:14 +04:00
|
|
|
$reader = new FileReader(__DIR__ . '/impl/avec_bom.txt');
|
2024-04-23 05:40:56 +04:00
|
|
|
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() {
|
2024-04-25 17:46:14 +04:00
|
|
|
$reader = new FileReader(__DIR__ . '/impl/msexcel.csv');
|
2024-04-23 05:40:56 +04:00
|
|
|
self::assertSame(["nom", "prenom", "age"], $reader->fgetcsv());
|
|
|
|
self::assertSame(["clain", "jephte", "50"], $reader->fgetcsv());
|
|
|
|
self::assertNull($reader->fgetcsv());
|
|
|
|
$reader->close();
|
|
|
|
|
2024-04-25 17:46:14 +04:00
|
|
|
$reader = new FileReader(__DIR__ . '/impl/ooffice.csv');
|
2024-04-23 05:40:56 +04:00
|
|
|
self::assertSame(["nom", "prenom", "age"], $reader->fgetcsv());
|
|
|
|
self::assertSame(["clain", "jephte", "50"], $reader->fgetcsv());
|
|
|
|
self::assertNull($reader->fgetcsv());
|
|
|
|
$reader->close();
|
|
|
|
|
2024-04-25 17:46:14 +04:00
|
|
|
$reader = new FileReader(__DIR__ . '/impl/weird.tsv');
|
2024-04-23 05:40:56 +04:00
|
|
|
self::assertSame(["nom", "prenom", "age"], $reader->fgetcsv());
|
|
|
|
self::assertSame(["clain", "jephte", "50"], $reader->fgetcsv());
|
|
|
|
self::assertNull($reader->fgetcsv());
|
|
|
|
$reader->close();
|
|
|
|
|
2024-04-25 17:46:14 +04:00
|
|
|
$reader = new FileReader(__DIR__ . '/impl/avec_bom.csv');
|
2024-04-23 05:40:56 +04:00
|
|
|
self::assertSame(["nom", "prenom", "age"], $reader->fgetcsv());
|
|
|
|
self::assertSame(["clain", "jephte", "50"], $reader->fgetcsv());
|
|
|
|
self::assertNull($reader->fgetcsv());
|
|
|
|
$reader->close();
|
|
|
|
}
|
|
|
|
}
|