|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Class DBResultTest |
|
4
|
|
|
* |
|
5
|
|
|
* @filesource DBResultTest.php |
|
6
|
|
|
* @created 30.05.2017 |
|
7
|
|
|
* @package chillerlan\DatabaseTest |
|
8
|
|
|
* @author Smiley <[email protected]> |
|
9
|
|
|
* @copyright 2017 Smiley |
|
10
|
|
|
* @license MIT |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace chillerlan\DatabaseTest; |
|
14
|
|
|
|
|
15
|
|
|
use chillerlan\Database\DBResult; |
|
16
|
|
|
use Iterator, ArrayAccess, Countable, stdClass; |
|
17
|
|
|
|
|
18
|
|
|
class DBResultTest extends TestAbstract{ |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var \chillerlan\Database\DBResult |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $DBResult; |
|
24
|
|
|
|
|
25
|
|
|
protected function setUp(){ |
|
26
|
|
|
$this->DBResult = new DBResult; |
|
27
|
|
|
|
|
28
|
|
|
foreach(range(0, 9) as $k){ |
|
29
|
|
|
$this->DBResult[] = ['id' => $k, 'hash' => md5($k)]; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
$this->DBResult->rewind(); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function testInstance(){ |
|
36
|
|
|
$this->assertInstanceOf(DBResult::class, $this->DBResult); |
|
37
|
|
|
$this->assertInstanceOf(Iterator::class, $this->DBResult); |
|
38
|
|
|
$this->assertInstanceOf(ArrayAccess::class, $this->DBResult); |
|
39
|
|
|
$this->assertInstanceOf(Countable::class, $this->DBResult); |
|
40
|
|
|
|
|
41
|
|
|
//coverage |
|
42
|
|
|
new DBResult(new DBResult); |
|
43
|
|
|
new DBResult(new stdClass); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @expectedException \chillerlan\Database\DBException |
|
48
|
|
|
* @expectedExceptionMessage invalid data |
|
49
|
|
|
*/ |
|
50
|
|
|
public function testConstructInvalidData(){ |
|
51
|
|
|
new DBResult(''); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function testMagicGetCall(){ |
|
55
|
|
|
|
|
56
|
|
|
$this->assertSame(10, $this->DBResult->length); |
|
57
|
|
|
$this->assertSame(md5(0), $this->DBResult[0]->hash); |
|
58
|
|
|
|
|
59
|
|
|
while($row = $this->DBResult->current()){ |
|
60
|
|
|
$this->assertTrue(isset($row['hash'], $row['id'])); |
|
61
|
|
|
|
|
62
|
|
|
$this->assertSame($row->hash, $row->id(function($v){ |
|
63
|
|
|
return md5($v); |
|
64
|
|
|
})); |
|
65
|
|
|
|
|
66
|
|
|
$this->assertSame(md5($row->id), $row->hash()); |
|
67
|
|
|
$this->assertSame(['id' => $row->id, 'hash' => $row->hash], $row->__toArray()); |
|
68
|
|
|
$this->assertNull($row->foo); |
|
69
|
|
|
$this->assertNull($row->foo()); |
|
70
|
|
|
$this->assertSame($row->id, $this->DBResult->key()); |
|
71
|
|
|
|
|
72
|
|
|
$this->assertTrue($this->DBResult->valid()); |
|
73
|
|
|
unset($this->DBResult[$row->id]); |
|
74
|
|
|
$this->assertFalse($this->DBResult->valid()); |
|
75
|
|
|
|
|
76
|
|
|
$this->DBResult->next(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function testEach(){ |
|
82
|
|
|
$this->DBResult->__each(function($row, $i){ |
|
83
|
|
|
/** @var \chillerlan\Database\DBResultRow $row */ |
|
84
|
|
|
$this->assertSame($row->id, $i); |
|
85
|
|
|
$this->assertSame(md5($row->id), $row->hash()); |
|
86
|
|
|
|
|
87
|
|
|
$row->__each(function($v, $j) use ($row){ |
|
88
|
|
|
$this->assertSame($row->{$j}, $v); |
|
89
|
|
|
$this->assertSame($row[$j], $v); |
|
90
|
|
|
}); |
|
91
|
|
|
}); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
|