Passed
Push — master ( ded953...93f7ca )
by Jean Paul
01:51
created

testInsertUsingEnvironmentVariables()   B

Complexity

Conditions 7
Paths 64

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 16
c 1
b 0
f 0
nc 64
nop 0
dl 0
loc 22
rs 8.8333
1
<?php declare( strict_types = 1 );
2
3
namespace Coco\SourceWatcher\Tests\Core\Database\Connections;
4
5
use Coco\SourceWatcher\Core\Database\Connections\PostgreSqlConnector;
6
use Coco\SourceWatcher\Core\Row;
7
use Coco\SourceWatcher\Core\SourceWatcherException;
8
use PHPUnit\Framework\TestCase;
9
10
/**
11
 * Class PostgreSqlConnectorTest
12
 * @package Coco\SourceWatcher\Tests\Core\Database\Connections
13
 */
14
class PostgreSqlConnectorTest extends TestCase
15
{
16
    /**
17
     *
18
     */
19
    public function testSetGetCharset () : void
20
    {
21
        $connector = new PostgreSqlConnector();
22
23
        $givenCharset = "utf8";
24
        $expectedCharset = "utf8";
25
26
        $connector->setCharset( $givenCharset );
27
28
        $this->assertEquals( $expectedCharset, $connector->getCharset() );
29
    }
30
31
    /**
32
     *
33
     */
34
    public function testSetGetDefaultDatabaseName () : void
35
    {
36
        $connector = new PostgreSqlConnector();
37
38
        $givenDefaultDatabaseName = "people";
39
        $expectedDefaultDatabaseName = "people";
40
41
        $connector->setDefaultDatabaseName( $givenDefaultDatabaseName );
42
43
        $this->assertEquals( $expectedDefaultDatabaseName, $connector->getDefaultDatabaseName() );
44
    }
45
46
    /**
47
     *
48
     */
49
    public function testSetGetSslMode () : void
50
    {
51
        $connector = new PostgreSqlConnector();
52
53
        $givenSslMode = "allow";
54
        $expectedSslMode = "allow";
55
56
        $connector->setSslMode( $givenSslMode );
57
58
        $this->assertEquals( $expectedSslMode, $connector->getSslMode() );
59
    }
60
61
    /**
62
     *
63
     */
64
    public function testSetGetSslRootCert () : void
65
    {
66
        $connector = new PostgreSqlConnector();
67
68
        $givenSslRootCert = "~/.postgresql/root.crt";
69
        $expectedSslRootCert = "~/.postgresql/root.crt";
70
71
        $connector->setSslRootCert( $givenSslRootCert );
72
73
        $this->assertEquals( $expectedSslRootCert, $connector->getSslRootCert() );
74
    }
75
76
    /**
77
     *
78
     */
79
    public function testSetGetSslCert () : void
80
    {
81
        $connector = new PostgreSqlConnector();
82
83
        $givenSslCert = "~/.postgresql/postgresql.crt";
84
        $expectedSslCert = "~/.postgresql/postgresql.crt";
85
86
        $connector->setSslCert( $givenSslCert );
87
88
        $this->assertEquals( $expectedSslCert, $connector->getSslCert() );
89
    }
90
91
    /**
92
     *
93
     */
94
    public function testSetGetSslKey () : void
95
    {
96
        $connector = new PostgreSqlConnector();
97
98
        $givenSslKey = "~/.postgresql/postgresql.key";
99
        $expectedSslKey = "~/.postgresql/postgresql.key";
100
101
        $connector->setSslKey( $givenSslKey );
102
103
        $this->assertEquals( $expectedSslKey, $connector->getSslKey() );
104
    }
105
106
    /**
107
     *
108
     */
109
    public function testSetGetSslCrl () : void
110
    {
111
        $connector = new PostgreSqlConnector();
112
113
        $givenSslCrl = "~/.postgresql/root.crl";
114
        $expectedSslCrl = "~/.postgresql/root.crl";
115
116
        $connector->setSslCrl( $givenSslCrl );
117
118
        $this->assertEquals( $expectedSslCrl, $connector->getSslCrl() );
119
    }
120
121
    /**
122
     *
123
     */
124
    public function testSetGetApplicationName () : void
125
    {
126
        $connector = new PostgreSqlConnector();
127
128
        $givenAppName = "App Name for PG Stat Activity";
129
        $expectedAppName = "App Name for PG Stat Activity";
130
131
        $connector->setApplicationName( $givenAppName );
132
133
        $this->assertEquals( $expectedAppName, $connector->getApplicationName() );
134
    }
135
136
    /**
137
     * @throws SourceWatcherException
138
     */
139
    public function testGetConnection () : void
140
    {
141
        $connector = new PostgreSqlConnector();
142
        $connector->setUser( "admin" );
143
        $connector->setPassword( "secret" );
144
        $connector->setHost( "localhost" );
145
        $connector->setPort( 5432 );;
146
        $connector->setDbName( "people" );
147
        $connector->setCharset( "utf8" );
148
        $connector->setDefaultDatabaseName( "people" );
149
        $connector->setSslMode( "allow" );
150
        $connector->setSslRootCert( "~/.postgresql/root.crt" );
151
        $connector->setSslCert( "~/.postgresql/postgresql.crt" );
152
        $connector->setSslKey( "~/.postgresql/postgresql.key" );
153
        $connector->setSslCrl( "~/.postgresql/root.crl" );
154
        $connector->setApplicationName( "App Name for PG Stat Activity" );
155
156
        $this->assertNotNull( $connector->getConnection() );
157
    }
158
159
    /**
160
     * @throws SourceWatcherException
161
     */
162
    public function testInsertUsingEnvironmentVariables () : void
163
    {
164
        $user = array_key_exists( "UNIT_TEST_POSTGRESQL_USER", $_ENV ) ? $_ENV["UNIT_TEST_POSTGRESQL_USER"] : null;
165
        $password = array_key_exists( "UNIT_TEST_POSTGRESQL_PASSWORD", $_ENV ) ? $_ENV["UNIT_TEST_POSTGRESQL_PASSWORD"] : null;
166
        $host = array_key_exists( "UNIT_TEST_POSTGRESQL_HOST", $_ENV ) ? $_ENV["UNIT_TEST_POSTGRESQL_HOST"] : null;
167
        $port = array_key_exists( "UNIT_TEST_POSTGRESQL_PORT", $_ENV ) ? intval( $_ENV["UNIT_TEST_POSTGRESQL_PORT"] ) : 5432;
168
        $dbName = array_key_exists( "UNIT_TEST_POSTGRESQL_DB_NAME", $_ENV ) ? $_ENV["UNIT_TEST_POSTGRESQL_DB_NAME"] : null;
169
        $defaultDatabaseName = array_key_exists( "UNIT_TEST_POSTGRESQL_DEFAULT_DATABASE_NAME", $_ENV ) ? $_ENV["UNIT_TEST_POSTGRESQL_DEFAULT_DATABASE_NAME"] : null;
170
171
        $connector = new PostgreSqlConnector();
172
        $connector->setUser( $user );
0 ignored issues
show
Bug introduced by
It seems like $user 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

172
        $connector->setUser( /** @scrutinizer ignore-type */ $user );
Loading history...
173
        $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

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

174
        $connector->setHost( /** @scrutinizer ignore-type */ $host );
Loading history...
175
        $connector->setPort( $port );;
176
        $connector->setDbName( $dbName );
0 ignored issues
show
Bug introduced by
It seems like $dbName 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

176
        $connector->setDbName( /** @scrutinizer ignore-type */ $dbName );
Loading history...
177
        $connector->setDefaultDatabaseName( $defaultDatabaseName );
0 ignored issues
show
Bug introduced by
It seems like $defaultDatabaseName can also be of type null; however, parameter $defaultDatabaseName of Coco\SourceWatcher\Core\...etDefaultDatabaseName() 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

177
        $connector->setDefaultDatabaseName( /** @scrutinizer ignore-type */ $defaultDatabaseName );
Loading history...
178
179
        $connector->setTableName( "people" );
180
181
        $row = new Row( [ "name" => "John Doe", "email_address" => "[email protected]" ] );
182
183
        $this->assertEquals( 1, $connector->insert( $row ) );
184
    }
185
}
186