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