Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Failed Conditions
Push — master ( f174b5...646f17 )
by Dan
21s queued 18s
created

DatabaseResultTest::test_hasRecord_no_rows()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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