PdoIteratorTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 52
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setup() 0 16 2
A tearDown() 0 4 1
A testBasicFunctionality() 0 13 2
A testFetchAssoc() 0 9 2
1
<?php
2
3
namespace itertools;
4
5
use PDO;
6
use PHPUnit_Framework_TestCase;
7
use stdClass;
8
9
10
class PdoIteratorTest 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 testBasicFunctionality()
38
	{
39
		$it = new PdoIterator($this->pdo, 'SELECT * FROM entries');
40
		$rows = iterator_to_array($it);
41
		$this->assertEquals(10, count($rows));
42
		$this->assertTrue($rows[0] instanceof stdClass);
43
		foreach($rows as $i => $row) {
44
			$this->assertEquals($i, $row->name);
45
		}
46
47
		$it = new PdoIterator($this->pdo, 'SELECT * FROM entries WHERE name > :minName', array(':minName' => 5));
48
		$this->assertEquals(4, count(iterator_to_array($it)));
49
	}
50
51
	/** @test */
52
	public function testFetchAssoc()
53
	{
54
		$it = new PdoIterator($this->pdo, 'SELECT * FROM entries', array(), PDO::FETCH_ASSOC);
55
		$rows = iterator_to_array($it);
56
		$this->assertEquals(10, count($rows));
57
		foreach($rows as $i => $row) {
58
			$this->assertEquals($i, $row['name']);
59
		}
60
	}
61
}
62
63