PDOStamentIteratorTest::tearDown()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace itertools;
4
5
use itertools\PDOStatementIterator;
6
use PDO;
7
use PHPUnit_Framework_TestCase;
8
9
10
class PDOStamentIteratorTest extends PHPUnit_Framework_TestCase
11
{
12
	protected $pdo;
13
14
	public function setup()
15
	{
16
		$pdo = new PDO('sqlite::memory:');
17
		$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
18
19
		$pdo->exec('
20
			CREATE TABLE "entries"(
21
			  "id" INTEGER PRIMARY KEY NOT NULL,
22
			  "name" int
23
			);
24
		');
25
		foreach(range(0, 9) as $i) {
26
			$pdo->exec('INSERT INTO entries ("name") VALUES (' . $i . ')');
27
		}
28
		$this->pdo = $pdo;
29
	}
30
31
	public function tearDown()
32
	{
33
		$this->pdo = null;
34
	}
35
36
	/** @test */
37
	public function testNormalStatementIteration()
38
	{
39
		$it = new PDOStatementIterator($this->pdo->query('select name from entries limit 0, 2'), PDO::FETCH_NUM);
40
		$this->assertEquals(array(array('0'), array('1')), iterator_to_array($it));
41
		$this->assertEquals(array(), iterator_to_array($it));
42
	}
43
44
	/** @test */
45
	public function testFactoryStatementIteration()
46
	{
47
		$pdo = $this->pdo;
48
		$factory = function () use ($pdo) { return $pdo->query('select name from entries limit 0, 2'); };
49
		$it = new PDOStatementIterator($factory, PDO::FETCH_NUM);
50
		$this->assertEquals(array(array('0'), array('1')), iterator_to_array($it));
51
		$this->assertEquals(array(array('0'), array('1')), iterator_to_array($it));
52
	}
53
}
54
55