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
|
|
|
|