Passed
Push — master ( 079aca...05e9ba )
by Jeroen De
04:40
created

testGivenValidJson_currentReturnsDecodedValues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace Wikibase\JsonDumpReader\Tests\Unit\Iterator;
6
7
use PHPUnit\Framework\TestCase;
8
use Wikibase\JsonDumpReader\JsonDumpFactory;
9
use Wikibase\JsonDumpReader\Reader\FakeDumpReader;
10
11
/**
12
 * @covers \Wikibase\JsonDumpReader\Iterator\ObjectDumpIterator
13
 * @covers \Wikibase\JsonDumpReader\Reader\FakeDumpReader
14
 * @covers \Wikibase\JsonDumpReader\JsonDumpFactory
15
 *
16
 * @licence GNU GPL v2+
17
 * @author Jeroen De Dauw < [email protected] >
18
 */
19
class ObjectDumpIteratorTest extends TestCase {
20
21
	private function getIteratorWithReaderReturning( $lines ) {
22
		return ( new JsonDumpFactory() )->newObjectDumpIterator( new FakeDumpReader( $lines ) );
23
	}
24
25
	public function testGivenValidJson_allElementsAreDecodedInIteration() {
26
		$lines = [ '{"id": "Q1"}', '{"id": "Q2"}', '{"id": "Q3"}' ];
27
28
		$objectIterator = $this->getIteratorWithReaderReturning( $lines );
29
30
		$this->assertSame(
31
			[
32
				[ 'id' => 'Q1' ],
33
				[ 'id' => 'Q2' ],
34
				[ 'id' => 'Q3' ],
35
			],
36
			iterator_to_array( $objectIterator )
37
		);
38
	}
39
40
	public function testGivenValidJson_currentReturnsDecodedValues() {
41
		$lines = [ '{"id": "Q1"}', '{"id": "Q2"}', '{"id": "Q3"}' ];
42
43
		$objectIterator = $this->getIteratorWithReaderReturning( $lines );
44
45
		$this->assertNull( $objectIterator->current() );
46
47
		$objectIterator->rewind();
48
		$this->assertSame( [ 'id' => 'Q1' ], $objectIterator->current() );
49
50
		$objectIterator->next();
51
		$this->assertSame( [ 'id' => 'Q2' ], $objectIterator->current() );
52
53
		$objectIterator->next();
54
		$this->assertSame( [ 'id' => 'Q3' ], $objectIterator->current() );
55
	}
56
57
	public function testGivenValidJson_keyReturnsZeroBasedIntegers() {
58
		$lines = [ '{"id": "Q1"}', '{"id": "Q2"}', '{"id": "Q3"}' ];
59
60
		$objectIterator = $this->getIteratorWithReaderReturning( $lines );
61
62
		$this->assertSame( 0, $objectIterator->key() );
63
64
		$objectIterator->rewind();
65
		$this->assertSame( 0, $objectIterator->key() );
66
67
		$objectIterator->next();
68
		$this->assertSame( 1, $objectIterator->key() );
69
70
		$objectIterator->next();
71
		$this->assertSame( 2, $objectIterator->key() );
72
	}
73
74
	public function testGivenInvalidJson_invalidElementIsSkippedAndErrorRecorded() {
75
		$errors = [];
76
77
		$objectIterator = ( new JsonDumpFactory() )->newObjectDumpIterator(
78
			new FakeDumpReader( [
0 ignored issues
show
Documentation introduced by
array('{"id": "Q1"}', '{...fair}', '{"id": "Q3"}') is of type array<integer,string,{"0..."string","2":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
79
				'{"id": "Q1"}',
80
				'{why discriminate against non-JSON? the world is unfair}',
81
				'{"id": "Q3"}'
82
			] ),
83
			function( $errorMessage ) use ( &$errors ) {
84
				$errors[] = $errorMessage;
85
			}
86
		);
87
88
		$objectIterator->rewind();
89
		$this->assertCount( 0, $errors );
0 ignored issues
show
Documentation introduced by
$errors is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
90
		$this->assertSame( [ 'id' => 'Q1' ], $objectIterator->current() );
91
92
		$objectIterator->next();
93
		$this->assertCount( 1, $errors );
0 ignored issues
show
Documentation introduced by
$errors is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
94
		$this->assertContainsOnly( 'string', $errors );
95
		$this->assertSame( [ 'id' => 'Q3' ], $objectIterator->current() );
96
	}
97
98
}