Skip to content

Commit

Permalink
Merge pull request #2 from elecena/test/using-tmpfile
Browse files Browse the repository at this point in the history
Test the JsonlParser using the tmpfile()
  • Loading branch information
macbre authored Nov 27, 2023
2 parents 2560900 + cc315c3 commit b6b85ce
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
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
}
}

0 comments on commit b6b85ce

Please sign in to comment.