1
|
|
|
<?php declare( strict_types = 1 ); |
2
|
|
|
|
3
|
|
|
namespace Coco\SourceWatcher\Tests\Core\Database\Connections; |
4
|
|
|
|
5
|
|
|
use Coco\SourceWatcher\Core\Database\Connections\MySqlConnector; |
6
|
|
|
use Coco\SourceWatcher\Core\Row; |
7
|
|
|
use Coco\SourceWatcher\Core\SourceWatcherException; |
8
|
|
|
use Coco\SourceWatcher\Utils\i18n; |
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class MySqlConnectorTest |
13
|
|
|
* @package Coco\SourceWatcher\Tests\Core\Database\Connections |
14
|
|
|
*/ |
15
|
|
|
class MySqlConnectorTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* |
19
|
|
|
*/ |
20
|
|
|
public function testSetGetUnixSocket () : void |
21
|
|
|
{ |
22
|
|
|
$connector = new MySqlConnector(); |
23
|
|
|
|
24
|
|
|
$given = "/var/run/mysqld/mysqld.sock"; |
25
|
|
|
$expected = "/var/run/mysqld/mysqld.sock"; |
26
|
|
|
|
27
|
|
|
$connector->setUnixSocket( $given ); |
28
|
|
|
|
29
|
|
|
$this->assertEquals( $expected, $connector->getUnixSocket() ); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* |
34
|
|
|
*/ |
35
|
|
|
public function testSetGetCharset () : void |
36
|
|
|
{ |
37
|
|
|
$connector = new MySqlConnector(); |
38
|
|
|
|
39
|
|
|
$given = "utf8"; |
40
|
|
|
$expected = "utf8"; |
41
|
|
|
|
42
|
|
|
$connector->setCharset( $given ); |
43
|
|
|
|
44
|
|
|
$this->assertEquals( $expected, $connector->getCharset() ); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @throws SourceWatcherException |
49
|
|
|
*/ |
50
|
|
|
public function testGetConnection () : void |
51
|
|
|
{ |
52
|
|
|
$connector = new MySqlConnector(); |
53
|
|
|
$connector->setUser( "admin" ); |
54
|
|
|
$connector->setPassword( "secret" ); |
55
|
|
|
$connector->setHost( "localhost" ); |
56
|
|
|
$connector->setPort( 3306 );; |
57
|
|
|
$connector->setDbName( "people" ); |
58
|
|
|
$connector->setUnixSocket( "/var/run/mysqld/mysqld.sock" ); |
59
|
|
|
$connector->setCharset( "utf-8" ); |
60
|
|
|
|
61
|
|
|
$this->assertNotNull( $connector->connect() ); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @throws SourceWatcherException |
66
|
|
|
*/ |
67
|
|
|
public function testInsertUsingWrongConnectionParameters () : void |
68
|
|
|
{ |
69
|
|
|
$this->expectException( SourceWatcherException::class ); |
70
|
|
|
$this->expectExceptionMessage( i18n::getInstance()->getText( MySqlConnector::class, "No_Table_Name_Found" ) ); |
71
|
|
|
|
72
|
|
|
$connector = new MySqlConnector(); |
73
|
|
|
$connector->setUser( "admin" ); |
74
|
|
|
$connector->setPassword( "secret" ); |
75
|
|
|
$connector->setHost( "localhost" ); |
76
|
|
|
$connector->setPort( 3306 );; |
77
|
|
|
$connector->setDbName( "people" ); |
78
|
|
|
$connector->setUnixSocket( "/var/run/mysqld/mysqld.sock" ); |
79
|
|
|
$connector->setCharset( "utf-8" ); |
80
|
|
|
|
81
|
|
|
$row = new Row( [ "id" => "1", "name" => "John Doe", "email_address" => "[email protected]" ] ); |
82
|
|
|
|
83
|
|
|
$connector->insert( $row ); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|