1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace SmrTest\lib\DefaultGame; |
4
|
|
|
|
5
|
|
|
use Smr\Database; |
6
|
|
|
use Smr\DatabaseResult; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @covers \Smr\DatabaseResult |
10
|
|
|
*/ |
11
|
|
|
class DatabaseResultTest extends \PHPUnit\Framework\TestCase { |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Create and run a trivial query that returns $num rows |
15
|
|
|
*/ |
16
|
|
|
private function runQuery(int $num) : DatabaseResult { |
17
|
|
|
$db = Database::getInstance(); |
18
|
|
|
// This query will look like (for increasing $num): |
19
|
|
|
// SELECT 1 LIMIT 0 |
20
|
|
|
// SELECT 1 LIMIT 1 |
21
|
|
|
// SELECT 1 UNION SELECT 2 LIMIT 2 |
22
|
|
|
// SELECT 1 UNION SELECT 2 UNION SELECT 3 LIMIT 3 |
23
|
|
|
// We always include at least "SELECT 1" so that we have a valid |
24
|
|
|
// query in the $num=0 case, and then add "LIMIT $num" to ensure |
25
|
|
|
// that we get the requested number of rows. |
26
|
|
|
$query = 'SELECT 1'; |
27
|
|
|
for ($i = 2; $i <= $num; $i++) { |
28
|
|
|
$query .= ' UNION SELECT ' . $i; |
29
|
|
|
} |
30
|
|
|
$query .= ' LIMIT ' . $num; |
31
|
|
|
return $db->read($query); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function test_record_one_row() { |
35
|
|
|
self::assertSame([1 => '1'], $this->runQuery(1)->record()->getRow()); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function test_record_too_many_rows() { |
39
|
|
|
$result = $this->runQuery(2); |
40
|
|
|
$this->expectException(\RuntimeException::class); |
41
|
|
|
$this->expectExceptionMessage('One record required, but found 2'); |
42
|
|
|
$result->record(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function test_record_too_few_rows() { |
46
|
|
|
$result = $this->runQuery(0); |
47
|
|
|
$this->expectException(\RuntimeException::class); |
48
|
|
|
$this->expectExceptionMessage('One record required, but found 0'); |
49
|
|
|
$result->record(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function test_hasRecord_no_rows() { |
53
|
|
|
$result = $this->runQuery(0); |
54
|
|
|
self::assertFalse($result->hasRecord()); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function test_hasRecord_with_rows() { |
58
|
|
|
$result = $this->runQuery(1); |
59
|
|
|
self::assertTrue($result->hasRecord()); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function test_getNumRecords() { |
63
|
|
|
foreach ([0, 1, 2] as $numRecords) { |
64
|
|
|
$result = $this->runQuery($numRecords); |
65
|
|
|
self::assertSame($numRecords, $result->getNumRecords()); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function test_records() { |
70
|
|
|
$numRecords = 0; |
71
|
|
|
$result = $this->runQuery(3); |
72
|
|
|
foreach ($result->records() as $index => $record) { |
73
|
|
|
$row = $index + 1; |
74
|
|
|
self::assertSame([1 => (string)$row], $record->getRow()); |
75
|
|
|
$numRecords++; |
76
|
|
|
} |
77
|
|
|
self::assertSame(3, $numRecords); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function test_records_no_rows() { |
81
|
|
|
$result = $this->runQuery(0); |
82
|
|
|
self::assertSame([], iterator_to_array($result->records())); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
} |
86
|
|
|
|