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

Passed
Pull Request — master (#924)
by Dan
04:13
created

test__construct_invalid_character_set_throws_exception()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 8
rs 10
1
<?php
2
3
namespace SmrTest\lib\DefaultGame;
4
5
use DI\Container;
6
use Error;
7
use MySqlDatabase;
8
use mysqli;
9
use PHPUnit\Framework\TestCase;
10
use RuntimeException;
11
use Smr\Container\DiContainer;
12
use Smr\MySqlProperties;
13
14
class MySqlDatabaseTest extends TestCase {
15
	private Container $container;
16
	private $mysql;
17
18
	protected function setUp(): void {
19
		DiContainer::initializeContainer();
20
		$this->container = DiContainer::getContainer();
21
		$this->mysql = $this->createMock(mysqli::class);
22
		// Replace the factory definition for mysqli object with our mock, so when
23
		// requesting a mysqli instance, it will always return a mock for this test
24
		$this->container->set(mysqli::class, $this->mysql);
25
	}
26
27
	public function test_mysql_factory() {
28
		// Given mysql properties are retrieved from the container
29
		$mysqlProperties = $this->container->get(MySqlProperties::class);
30
		// When using the factory to retrieve a mysqli instance
31
		$mysqlDatabase = MySqlDatabase::mysqliFactory($mysqlProperties);
32
		// Then the connection is successful
33
		self::assertNotNull($mysqlDatabase->server_info);
34
	}
35
36
	public function test__construct_happy_path() {
37
		$this->mysql
38
			->expects(self::once())
39
			->method("character_set_name")
40
			->willReturn("utf8");
41
		$mysqlDatabase = $this->container->get(MySqlDatabase::class);
42
		$this->assertNotNull($mysqlDatabase);
43
	}
44
45
	public function test__construct_invalid_character_set_throws_exception() {
46
		$this->expectException(RuntimeException::class);
47
		$this->mysql
48
			->expects(self::once())
49
			->method("character_set_name")
50
			->willReturn("invalid character set");
51
		// MySqlDatabase::getInstance() is also a valid way to retrieve the managed class instance.
52
		MySqlDatabase::getInstance();
53
	}
54
55
	public function test_performing_operations_on_closed_database_throws_error() {
56
		// Expectations
57
		$this->expectException(Error::class);
58
		$this->expectExceptionMessage('Typed property MySqlDatabase::$dbConn must not be accessed before initialization');
59
		// Given mysqli return valid charset
60
		$this->mysql
61
			->expects(self::once())
62
			->method("character_set_name")
63
			->willReturn("utf8");
64
		// And a mysql database instance
65
		$mysqlDatabase = MySqlDatabase::getInstance();
66
		// And disconnect is called
67
		$mysqlDatabase->close();
68
		// When calling database methods
69
		$mysqlDatabase->query("foo query");
70
	}
71
72
	public function test_getInstance_will_perform_reconnect_after_connection_closed() {
73
		// Given mysqli return valid charset
74
		$this->mysql
75
			->expects(self::once())
76
			->method("character_set_name")
77
			->willReturn("utf8");
78
		// And a mysql database instance
79
		$mysqlDatabase = MySqlDatabase::getInstance();
80
		// And disconnect is called
81
		$mysqlDatabase->close();
82
		// And mysql database is retrieved from the container
83
		$mysqlDatabase = MySqlDatabase::getInstance();
84
		// When performing a query
85
		$mysqlDatabase->query("select 1");
86
		// Then new mysqli instance is not the same as the initial mock
87
		self::assertNotSame($this->mysql, $this->container->get(mysqli::class));
88
	}
89
90
	public function test_getInstance_will_not_perform_reconnect_if_connection_not_closed() {
91
		// Given mysqli return valid charset
92
		$this->mysql
93
			->expects(self::once())
94
			->method("character_set_name")
95
			->willReturn("utf8");
96
		// And a mysql database instance
97
		MySqlDatabase::getInstance();
98
		// And get instance is called again
99
		MySqlDatabase::getInstance();
100
		// Then the two mysqli instances are the same
101
		self::assertSame($this->mysql, $this->container->get(mysqli::class));
102
	}
103
104
	public function test_getInstance_will_return_reconnected_instance_when_called_multiple_times_after_closing_connection() {
105
		// Given mysqli return valid charset
106
		$this->mysql
107
			->expects(self::once())
108
			->method("character_set_name")
109
			->willReturn("utf8");
110
		// And a mysql database instance
111
		$mysqlDatabase = MySqlDatabase::getInstance();
112
		// And disconnect is called
113
		$mysqlDatabase->close();
114
		// And mysql database is retrieved from the container
115
		$mysqlDatabase2 = MySqlDatabase::getInstance();
116
		$mysqlDatabase3 = MySqlDatabase::getInstance();
117
		// Then the two new container retrievals are the same
118
		self::assertSame($mysqlDatabase2, $mysqlDatabase3);
119
		// And the original mysql database object is not the same reference as the others
120
		self::assertNotSame($mysqlDatabase, $mysqlDatabase2);
121
	}
122
}
123