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
8 changes: 5 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: 8.3
extensions: dom, curl, libxml, mbstring, zip, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo
coverage: none
extensions: dom, curl, libxml, mbstring, zip, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo, xdebug
coverage: xdebug

- name: Install dependencies
run: composer install --no-interaction

- name: Test
run: composer run test
run: composer run test
- name: TestCoverage
run: composer run coverage
28 changes: 27 additions & 1 deletion Tests/JsonlTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php

use Indykoning\Jsonl\Jsonl;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;

class JsonlTest extends TestCase
Expand All @@ -14,13 +13,28 @@ public function testJsonlEncode()
$this->assertEquals($expected, iterator_to_array(Jsonl::encode($data)));
}

public function testJsonlEncodeSkipsEmptyLines()
{
$data = [[], ['key' => 'value'], null, ['key2' => 'value2']];
$expected = ['{"key":"value"}' . "\n", '{"key2":"value2"}' . "\n"];

$this->assertEquals($expected, iterator_to_array(Jsonl::encode($data)));
}

public function testJsonlDecode()
{
$jsonlString = '{"key":"value"}' . "\n" . '{"key2":"value2"}' . "\n";
$expected = [['key' => 'value'], ['key2' => 'value2']];
$this->assertEquals($expected, iterator_to_array(Jsonl::decode($jsonlString, true)));
}

public function testJsonlDecodeSkipsEmptyLines()
{
$jsonlString = '{"key":"value"}' . "\n" . '{"key2":"value2"}' . "\n\n \n";
$expected = [['key' => 'value'], ['key2' => 'value2']];
$this->assertEquals($expected, iterator_to_array(Jsonl::decode($jsonlString, true)));
}

public function testJsonlEncodeToFile()
{
$data = [['key' => 'value'], ['key2' => 'value2']];
Expand All @@ -42,6 +56,18 @@ public function testJsonlEncodeIncorrectResource()
Jsonl::encodeToResource('', $data);
}

public function testJsonlEncodeToReadOnlyResourceThrowsException()
{
$file = fopen(__DIR__ . '/data/test_decode.jsonl', 'r');

try {
$this->expectException(\RuntimeException::class);
Jsonl::encodeToResource($file, [['key' => 'value']]);
} finally {
fclose($file);
}
}

public function testJsonlDecodeFromFile()
{
$expected = [['key' => 'value'], ['key2' => 'value2']];
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"scripts": {
"analyse": "phpstan --memory-limit=256M",
"test": "phpunit",
"coverage": "XDEBUG_MODE=coverage phpunit --coverage-text --only-summary-for-coverage-text | grep -c \"100.00%\" | grep -q \"^3$\" && exit 0 || exit 1",
"phpcs": "vendor/bin/phpcs --standard=PSR12 --colors --exclude=Generic.Files.LineLength --extensions=php,phtml src",
"phpcbf": "vendor/bin/phpcbf --standard=PSR12 --colors --exclude=Generic.Files.LineLength --extensions=php src || exit 0",
"codestyle": [
Expand Down
9 changes: 8 additions & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<phpunit>
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.4/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true">
<php>
<ini name="error_reporting" value="-1"/>
</php>
Expand All @@ -7,4 +8,10 @@
<directory>./Tests</directory>
</testsuite>
</testsuites>
<coverage/>
<source>
<include>
<directory suffix=".php">./src</directory>
</include>
</source>
</phpunit>
17 changes: 12 additions & 5 deletions src/Jsonl.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ class Jsonl
public static function decode(
Traversable|array|string $jsonl,
?bool $associative = null,
int $depth = 512
int $depth = 512,
int $flags = JSON_THROW_ON_ERROR
): Generator {
if (is_string($jsonl)) {
$jsonl = explode("\n", $jsonl);
Expand All @@ -43,11 +44,16 @@ public static function decode(
continue;
}

$line = trim($line);
if ($line === '') {
continue;
}

yield json_decode(
trim($line),
$line,
$associative,
$depth,
JSON_THROW_ON_ERROR
$flags
);
}
}
Expand All @@ -65,14 +71,15 @@ public static function decode(
public static function decodeFromResource(
$resource,
?bool $associative = null,
int $depth = 512
int $depth = 512,
int $flags = JSON_THROW_ON_ERROR
): Generator {
if (!is_resource($resource)) {
throw new InvalidArgumentException('decodeFromResource expects a resource "' . gettype($resource) . '" was passed');
}

while (($line = fgets($resource)) !== false) {
foreach (static::decode($line, $associative, $depth) as $item) {
foreach (static::decode($line, $associative, $depth, $flags) as $item) {
yield $item;
}
}
Expand Down
Loading