summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/tests/phpunit/Unit/Iterators/ChunkedIteratorTest.php
blob: 449adfddb683d3b0c39266eec4674f639aecbb28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php

namespace SMW\Tests\Iterators;

use SMW\Iterators\ChunkedIterator;
use SMW\Tests\PHPUnitCompat;

/**
 * @covers \SMW\Iterators\ChunkedIterator
 * @group semantic-mediawiki
 *
 * @license GNU GPL v2+
 * @since 3.0
 *
 * @author mwjames
 */
class ChunkedIteratorTest extends \PHPUnit_Framework_TestCase {

	use PHPUnitCompat;

	public function testCanConstruct() {

		$this->assertInstanceOf(
			ChunkedIterator::class,
			new ChunkedIterator( [] )
		);
	}

	public function testChunkedOnArray() {

		$result = [
			1, 42, 1001, 9999
		];

		$instance = new ChunkedIterator( $result, 2 );

		foreach ( $instance as $chunk ) {
			$this->assertCount(
				2,
				$chunk
			);
		}

		$chunks = iterator_to_array( $instance, false );

		$this->assertEquals(
			[1, 42],
			$chunks[0]
		);

		$this->assertEquals(
			[1001, 9999],
			$chunks[1]
		);
	}

	public function testInvalidConstructorArgumentThrowsException() {

		$this->setExpectedException( 'RuntimeException' );
		$instance = new ChunkedIterator( 2 );
	}

	public function testInvalidChunkSizeArgumentThrowsException() {

		$this->setExpectedException( 'InvalidArgumentException' );
		$instance = new ChunkedIterator( [], -1 );
	}

}