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
|
|
|
* @expectedException \chillerlan\Database\DBException |
96
|
|
|
* @expectedExceptionMessage invalid callback |
97
|
|
|
*/ |
98
|
|
|
public function testEachInvalidCallback(){ |
99
|
|
|
$this->DBResult->__each('foo'); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function testMerge(){ |
103
|
|
|
$r1 = new DBResult([['id' => 1]]); |
104
|
|
|
$this->assertSame(1, $r1[0]->id); |
105
|
|
|
|
106
|
|
|
$r2 = new DBResult([['id' => 2]]); |
107
|
|
|
$this->assertSame(2, $r2[0]->id); |
108
|
|
|
|
109
|
|
|
$r1->__merge($r2)->__reverse(); |
110
|
|
|
|
111
|
|
|
$this->assertSame(2, $r1[0]->id); |
112
|
|
|
$this->assertSame(1, $r1[1]->id); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function testToArray(){ |
116
|
|
|
$r = new DBResult([['id' => 1], ['id' => 2]]); |
117
|
|
|
$this->assertSame([['id' => 1], ['id' => 2]], $r->__toArray()); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
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: