1
|
|
|
<?php declare( strict_types=1 ); |
2
|
|
|
|
3
|
|
|
namespace Coco\SourceWatcher\Tests\Core\Database\Connections; |
4
|
|
|
|
5
|
|
|
use Coco\SourceWatcher\Core\Database\Connections\Connector; |
6
|
|
|
use Coco\SourceWatcher\Core\Database\Connections\MySqlConnector; |
7
|
|
|
use Coco\SourceWatcher\Core\Row; |
8
|
|
|
use Coco\SourceWatcher\Core\SourceWatcherException; |
9
|
|
|
use Coco\SourceWatcher\Tests\Common\ParentTest; |
10
|
|
|
use Coco\SourceWatcher\Utils\Internationalization; |
11
|
|
|
use Doctrine\DBAL\DBALException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class MySqlConnectorTest |
15
|
|
|
* |
16
|
|
|
* @package Coco\SourceWatcher\Tests\Core\Database\Connections |
17
|
|
|
*/ |
18
|
|
|
class MySqlConnectorTest extends ParentTest |
19
|
|
|
{ |
20
|
|
|
public string $user; |
21
|
|
|
public string $password; |
22
|
|
|
public string $host; |
23
|
|
|
public int $port; |
24
|
|
|
public string $dbname; |
25
|
|
|
public string $unix_socket; |
26
|
|
|
public string $charset; |
27
|
|
|
|
28
|
|
|
public string $tableName; |
29
|
|
|
|
30
|
|
|
public MySqlConnector $mysqlConnector; |
31
|
|
|
|
32
|
|
|
public Row $row; |
33
|
|
|
|
34
|
|
|
public function setUp () : void |
35
|
|
|
{ |
36
|
|
|
$this->user = "admin"; |
37
|
|
|
$this->password = "secret"; |
38
|
|
|
$this->host = "localhost"; |
39
|
|
|
$this->port = 3306; |
40
|
|
|
$this->dbname = "people"; |
41
|
|
|
$this->unix_socket = "/var/run/mysqld/mysqld.sock"; |
42
|
|
|
$this->charset = "utf8"; |
43
|
|
|
|
44
|
|
|
$this->tableName = "people"; |
45
|
|
|
|
46
|
|
|
$this->mysqlConnector = new MySqlConnector(); |
47
|
|
|
$this->mysqlConnector->setUser( $this->user ); |
48
|
|
|
$this->mysqlConnector->setPassword( $this->password ); |
49
|
|
|
$this->mysqlConnector->setHost( $this->host ); |
50
|
|
|
$this->mysqlConnector->setPort( $this->port ); |
51
|
|
|
$this->mysqlConnector->setDbName( $this->dbname ); |
52
|
|
|
$this->mysqlConnector->setUnixSocket( $this->unix_socket ); |
53
|
|
|
$this->mysqlConnector->setCharset( $this->charset ); |
54
|
|
|
|
55
|
|
|
$this->row = new Row( [ "name" => "John Doe", "email_address" => "[email protected]" ] ); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testSetGetUnixSocket () : void |
59
|
|
|
{ |
60
|
|
|
$connector = new MySqlConnector(); |
61
|
|
|
|
62
|
|
|
$given = $this->unix_socket; |
63
|
|
|
$expected = $this->unix_socket; |
64
|
|
|
|
65
|
|
|
$connector->setUnixSocket( $given ); |
66
|
|
|
|
67
|
|
|
$this->assertEquals( $expected, $connector->getUnixSocket() ); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testSetGetCharset () : void |
71
|
|
|
{ |
72
|
|
|
$connector = new MySqlConnector(); |
73
|
|
|
|
74
|
|
|
$given = $this->charset; |
75
|
|
|
$expected = $this->charset; |
76
|
|
|
|
77
|
|
|
$connector->setCharset( $given ); |
78
|
|
|
|
79
|
|
|
$this->assertEquals( $expected, $connector->getCharset() ); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @throws DBALException |
84
|
|
|
*/ |
85
|
|
|
public function testGetConnection () : void |
86
|
|
|
{ |
87
|
|
|
$this->assertNotNull( $this->mysqlConnector->getConnection() ); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function testSetGetConnectionParameters () : void |
91
|
|
|
{ |
92
|
|
|
$connectionParameters = $this->mysqlConnector->getConnectionParameters(); |
93
|
|
|
$this->assertNotNull( $connectionParameters ); |
94
|
|
|
|
95
|
|
|
$connectionParametersKeys = [ |
96
|
|
|
"driver", |
97
|
|
|
"user", |
98
|
|
|
"password", |
99
|
|
|
"host", |
100
|
|
|
"port", |
101
|
|
|
"dbname", |
102
|
|
|
"unix_socket", |
103
|
|
|
"charset" |
104
|
|
|
]; |
105
|
|
|
|
106
|
|
|
foreach ( $connectionParametersKeys as $key ) { |
107
|
|
|
$this->assertArrayHasKey( $key, $connectionParameters ); |
108
|
|
|
$this->assertNotNull( $connectionParameters[$key] ); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @throws SourceWatcherException |
114
|
|
|
*/ |
115
|
|
|
public function testInsertWithNoTableSpecified () : void |
116
|
|
|
{ |
117
|
|
|
$this->expectException( SourceWatcherException::class ); |
118
|
|
|
$this->expectExceptionMessage( Internationalization::getInstance()->getText( Connector::class, |
119
|
|
|
"No_Table_Name_Found" ) ); |
120
|
|
|
|
121
|
|
|
$this->mysqlConnector->insert( $this->row ); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @throws SourceWatcherException |
126
|
|
|
*/ |
127
|
|
|
public function testInsertUsingWrongConnectionParameters () : void |
128
|
|
|
{ |
129
|
|
|
$this->expectException( SourceWatcherException::class ); |
130
|
|
|
$this->expectExceptionMessage( Internationalization::getInstance()->getText( Connector::class, |
131
|
|
|
"Connection_Object_Not_Connected_Cannot_Insert" ) ); |
132
|
|
|
|
133
|
|
|
$this->mysqlConnector->setTableName( $this->tableName ); |
134
|
|
|
|
135
|
|
|
$this->mysqlConnector->insert( $this->row ); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @throws SourceWatcherException |
140
|
|
|
*/ |
141
|
|
|
public function testInsertUsingEnvironmentVariables () : void |
142
|
|
|
{ |
143
|
|
|
$connector = new MySqlConnector(); |
144
|
|
|
$connector->setUser( $this->getEnvironmentVariable( "UNIT_TEST_MYSQL_USERNAME", null ) ); |
145
|
|
|
$connector->setPassword( $this->getEnvironmentVariable( "UNIT_TEST_MYSQL_PASSWORD", null ) ); |
146
|
|
|
$connector->setHost( $this->getEnvironmentVariable( "UNIT_TEST_MYSQL_HOST", null ) ); |
147
|
|
|
$connector->setPort( $this->getEnvironmentVariable( "UNIT_TEST_MYSQL_PORT", 5432, "intval" ) ); |
148
|
|
|
$connector->setDbName( $this->getEnvironmentVariable( "UNIT_TEST_MYSQL_DATABASE", null ) ); |
149
|
|
|
|
150
|
|
|
$connector->setTableName( $this->tableName ); |
151
|
|
|
|
152
|
|
|
$this->assertEquals( 1, $connector->insert( $this->row ) ); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|