1
0
mirror of https://github.com/halaxa/json-machine.git synced 2025-02-23 23:52:37 +01:00

84 lines
3.2 KiB
PHP
Raw Normal View History

2015-10-06 18:02:03 +02:00
<?php
2018-11-29 11:41:26 +01:00
namespace JsonMachineTest;
2015-10-06 18:02:03 +02:00
2021-12-21 16:13:39 +01:00
use JsonMachine\Items;
use JsonMachine\JsonDecoder\PassThruDecoder;
2015-10-06 18:02:03 +02:00
2021-12-21 16:13:39 +01:00
class ItemsTest extends \PHPUnit_Framework_TestCase
2015-10-06 18:02:03 +02:00
{
/**
* @dataProvider dataFactories
*/
2020-04-16 15:22:53 +02:00
public function testFactories($expected, $methodName, ...$args)
2015-10-06 18:02:03 +02:00
{
$iterator = call_user_func_array(Items::class."::$methodName", [
$args[0],
[
'pointer' => $args[1],
'decoder' => $args[2],
'debug' => $args[3],
],
]);
2020-04-16 15:22:53 +02:00
$this->assertSame($expected, iterator_to_array($iterator));
2015-10-06 18:02:03 +02:00
}
2021-12-23 13:02:31 +01:00
public function testItemsYieldsObjectItemsByDefault()
{
$iterator = Items::fromString('{"path": {"key":"value"}}');
foreach ($iterator as $item) {
$this->assertEquals((object) ['key' => 'value'], $item);
2021-12-23 13:02:31 +01:00
}
}
2015-10-06 18:02:03 +02:00
public function dataFactories()
{
2020-04-16 15:22:53 +02:00
$extJsonResult = ['key' => 'value'];
$passThruResult = ['key' => '"value"'];
$ptDecoder = new PassThruDecoder();
2021-12-16 16:41:29 +01:00
foreach ([true, false] as $debug) {
foreach ([
[$extJsonResult, 'fromStream', fopen('data://text/plain,{"path": {"key":"value"}}', 'r'), '/path', null, $debug],
[$extJsonResult, 'fromString', '{"path": {"key":"value"}}', '/path', null, $debug],
[$extJsonResult, 'fromFile', __DIR__.'/ItemsTest.json', '/path', null, $debug],
2021-12-16 16:41:29 +01:00
[$extJsonResult, 'fromIterable', ['{"path": {"key', '":"value"}}'], '/path', null, $debug],
[$extJsonResult, 'fromIterable', new \ArrayIterator(['{"path": {"key', '":"value"}}']), '/path', null, $debug],
2020-04-16 15:22:53 +02:00
2021-12-16 16:41:29 +01:00
[$passThruResult, 'fromStream', fopen('data://text/plain,{"path": {"key":"value"}}', 'r'), '/path', $ptDecoder, $debug],
[$passThruResult, 'fromString', '{"path": {"key":"value"}}', '/path', $ptDecoder, $debug],
[$passThruResult, 'fromFile', __DIR__.'/ItemsTest.json', '/path', $ptDecoder, $debug],
2021-12-16 16:41:29 +01:00
[$passThruResult, 'fromIterable', ['{"path": {"key', '":"value"}}'], '/path', $ptDecoder, $debug],
[$passThruResult, 'fromIterable', new \ArrayIterator(['{"path": {"key', '":"value"}}']), '/path', $ptDecoder, $debug],
] as $case) {
yield $case;
}
}
2015-10-06 18:02:03 +02:00
}
2020-12-01 20:34:17 +01:00
2021-12-16 16:41:29 +01:00
public function testGetPositionDebugEnabled()
2020-12-01 20:34:17 +01:00
{
$expectedPosition = ['key1' => 10, 'key2' => 20];
$items = Items::fromString('{"key1":1, "key2":2} ', ['debug' => true]);
2020-12-01 20:34:17 +01:00
foreach ($items as $key => $val) {
$this->assertSame($expectedPosition[$key], $items->getPosition());
}
}
public function testIterationWithoutForeach()
{
$iterator =
2021-12-21 16:13:39 +01:00
Items::fromString('{"key1":1, "key2":2}')
->getIterator();
$iterator->rewind();
$this->assertTrue($iterator->valid());
$this->assertSame(['key1', 1], [$iterator->key(), $iterator->current()]);
$iterator->next();
$this->assertTrue($iterator->valid());
$this->assertSame(['key2', 2], [$iterator->key(), $iterator->current()]);
$iterator->next();
$this->assertFalse($iterator->valid());
}
2015-10-06 18:02:03 +02:00
}