Passed
Push — master ( 93f7ca...abf277 )
by Jean Paul
01:45
created

testInsertUsingWrongConnectionParameters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 13
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 19
rs 9.8333
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 );
0 ignored issues
show
Bug introduced by
It seems like $username can also be of type null; however, parameter $user of Coco\SourceWatcher\Core\...ns\Connector::setUser() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

149
        $connector->setUser( /** @scrutinizer ignore-type */ $username );
Loading history...
150
        $connector->setPassword( $password );
0 ignored issues
show
Bug introduced by
It seems like $password can also be of type null; however, parameter $password of Coco\SourceWatcher\Core\...onnector::setPassword() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

150
        $connector->setPassword( /** @scrutinizer ignore-type */ $password );
Loading history...
151
        $connector->setHost( $host );
0 ignored issues
show
Bug introduced by
It seems like $host can also be of type null; however, parameter $host of Coco\SourceWatcher\Core\...aseConnector::setHost() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

151
        $connector->setHost( /** @scrutinizer ignore-type */ $host );
Loading history...
152
        $connector->setPort( $port );
153
        $connector->setDbName( $database );
0 ignored issues
show
Bug introduced by
It seems like $database can also be of type null; however, parameter $dbName of Coco\SourceWatcher\Core\...eConnector::setDbName() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

153
        $connector->setDbName( /** @scrutinizer ignore-type */ $database );
Loading history...
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