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