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 (#951)
by
unknown
04:36
created

MySqlDatabaseIntegrationTest::tearDown()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 6
rs 10
1
<?php
2
3
namespace SmrTest\lib\DefaultGame;
4
5
use DI\Container;
6
use Error;
7
use Exception;
8
use MySqlDatabase;
9
use mysqli;
10
use PHPUnit\Framework\TestCase;
11
use Smr\Container\DiContainer;
12
13
/**
14
 * Class MySqlDatabaseIntegrationTest
15
 * This is an integration test, but does not need to extend BaseIntegrationTest since we are not writing any data.
16
 * @covers MySqlDatabase
17
 * @package SmrTest\lib\DefaultGame
18
 */
19
class MySqlDatabaseIntegrationTest extends TestCase {
20
	private Container $container;
21
22
	protected function setUp(): void {
23
		DiContainer::initializeContainer();
24
		$this->container = DiContainer::getContainer();
25
	}
26
27
	protected function tearDown(): void {
28
		$mysqli = $this->container->get(mysqli::class);
29
		try {
30
			$mysqli->close();
31
		} catch (Exception $e) {
32
			print "tearDown() - mysqli connection already closed. $e\n";
33
		}
34
	}
35
36
	public function test_performing_operations_on_closed_database_throws_error() {
37
		// Expectations
38
		$this->expectException(Error::class);
39
		$this->expectExceptionMessage('Typed property MySqlDatabase::$dbConn must not be accessed before initialization');
40
		// Given a mysql database instance
41
		$mysqlDatabase = MySqlDatabase::getInstance();
42
		// And disconnect is called
43
		$mysqlDatabase->close();
44
		// When calling database methods
45
		$mysqlDatabase->query("foo query");
46
	}
47
48
	public function test_getInstance_will_perform_reconnect_after_connection_closed() {
49
		// Given an original mysql connection
50
		$originalMysql = $this->container->get(mysqli::class);
51
		// And a mysql database instance
52
		$mysqlDatabase = MySqlDatabase::getInstance();
53
		// And disconnect is called
54
		$mysqlDatabase->close();
55
		// And mysql database is retrieved from the container
56
		$mysqlDatabase = MySqlDatabase::getInstance();
57
		// When performing a query
58
		$mysqlDatabase->query("select 1");
59
		// Then new mysqli instance is not the same as the initial mock
60
		self::assertNotSame($originalMysql, $this->container->get(mysqli::class));
61
	}
62
63
	public function test_getInstance_will_not_perform_reconnect_if_connection_not_closed() {
64
		// Given an original mysql connection
65
		$originalMysql = $this->container->get(mysqli::class);
66
		// And a mysql database instance
67
		MySqlDatabase::getInstance();
68
		// And get instance is called again
69
		MySqlDatabase::getInstance();
70
		// Then the two mysqli instances are the same
71
		self::assertSame($originalMysql, $this->container->get(mysqli::class));
72
	}
73
}
74