Passed
Push — master ( 3c2107...846d8b )
by Jean Paul
01:45
created

MySqlConnectorTest::testGetConnection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 0
dl 0
loc 12
rs 9.9666
c 0
b 0
f 0
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 );
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

123
        $connector->setUser( /** @scrutinizer ignore-type */ $username );
Loading history...
124
        $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

124
        $connector->setPassword( /** @scrutinizer ignore-type */ $password );
Loading history...
125
        $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

125
        $connector->setHost( /** @scrutinizer ignore-type */ $host );
Loading history...
126
        $connector->setPort( $port );
127
        $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

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