Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions tests/BaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,15 @@ protected static function streamFromString(string $string)

return $stream;
}

/**
* @return Generator<string>
*/
protected static function iterator(): Generator
{
yield 'one';
yield 'two';
yield 'three';
}

}
36 changes: 28 additions & 8 deletions tests/JsonParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,7 @@ public function testPushItems(): void
$stream = self::streamFromString('');
$parser = new JsonlParser($stream);

function iterator(): Generator
{
yield 'one';
yield 'two';
yield 'three';
}

$parser->pushItems(items:iterator());
$parser->pushItems(items:self::iterator());
$this->assertCount(3, $parser);

$list = iterator_to_array($parser->iterate());
Expand All @@ -106,4 +99,31 @@ function iterator(): Generator
$this->assertCount(3, $list);
$this->assertSame(['three', 'two', 'one'], $list);
}

public function testOpensAnEmptyFile(): void
{
$stream = tmpfile();
$parser = new JsonlParser($stream);
$this->assertNull($parser->pop());
$this->assertCount(0, $parser);

fclose($stream); // this removes the file
}
public function testPushItemsToFile(): void
{
$stream = tmpfile();
$parser = new JsonlParser($stream);
$this->assertCount(0, $parser);

$parser->pushItems(items:self::iterator());
$this->assertCount(3, $parser);

$list = iterator_to_array($parser->iterate());

$this->assertCount(0, $parser);
$this->assertCount(3, $list);
$this->assertSame(['three', 'two', 'one'], $list);

fclose($stream); // this removes the file
}
}