PostgreSqlConnectorTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 11
rs 10
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 Coco\SourceWatcher\Tests\Common\ParentTest;
9
use Doctrine\DBAL\DBALException;
0 ignored issues
show
Bug introduced by
The type Doctrine\DBAL\DBALException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Exception;
11
12
/**
13
 * Class PostgreSqlConnectorTest
14
 *
15
 * @package Coco\SourceWatcher\Tests\Core\Database\Connections
16
 */
17
class PostgreSqlConnectorTest extends ParentTest
18
{
19
    private string $databaseName;
20
21
    private string $sslAllowMode;
22
    private string $sslRootCert;
23
    private string $sslCert;
24
    private string $sslKey;
25
    private string $sslCrl;
26
27
    private string $appName;
28
29
    public function setUp () : void
30
    {
31
        $this->databaseName = "people";
32
33
        $this->sslAllowMode = "allow";
34
        $this->sslRootCert = "~/.postgresql/root.crt";
35
        $this->sslCert = "~/.postgresql/postgresql.crt";
36
        $this->sslKey = "~/.postgresql/postgresql.key";
37
        $this->sslCrl = "~/.postgresql/root.crl";
38
39
        $this->appName = "App Name for PG Stat Activity";
40
    }
41
42
    public function testSetGetCharset () : void
43
    {
44
        $connector = new PostgreSqlConnector();
45
46
        $givenCharset = "utf8";
47
        $expectedCharset = "utf8";
48
49
        $connector->setCharset( $givenCharset );
50
51
        $this->assertEquals( $expectedCharset, $connector->getCharset() );
52
    }
53
54
    public function testSetGetDefaultDatabaseName () : void
55
    {
56
        $connector = new PostgreSqlConnector();
57
58
        $givenDefaultDatabaseName = $this->databaseName;
59
        $expectedDefaultDatabaseName = $this->databaseName;
60
61
        $connector->setDefaultDatabaseName( $givenDefaultDatabaseName );
62
63
        $this->assertEquals( $expectedDefaultDatabaseName, $connector->getDefaultDatabaseName() );
64
    }
65
66
    public function testSetGetSslMode () : void
67
    {
68
        $connector = new PostgreSqlConnector();
69
70
        $givenSslMode = $this->sslAllowMode;
71
        $expectedSslMode = $this->sslAllowMode;
72
73
        $connector->setSslMode( $givenSslMode );
74
75
        $this->assertEquals( $expectedSslMode, $connector->getSslMode() );
76
    }
77
78
    public function testSetGetSslRootCert () : void
79
    {
80
        $connector = new PostgreSqlConnector();
81
82
        $givenSslRootCert = $this->sslRootCert;
83
        $expectedSslRootCert = $this->sslRootCert;
84
85
        $connector->setSslRootCert( $givenSslRootCert );
86
87
        $this->assertEquals( $expectedSslRootCert, $connector->getSslRootCert() );
88
    }
89
90
    public function testSetGetSslCert () : void
91
    {
92
        $connector = new PostgreSqlConnector();
93
94
        $givenSslCert = $this->sslCert;
95
        $expectedSslCert = $this->sslCert;
96
97
        $connector->setSslCert( $givenSslCert );
98
99
        $this->assertEquals( $expectedSslCert, $connector->getSslCert() );
100
    }
101
102
    public function testSetGetSslKey () : void
103
    {
104
        $connector = new PostgreSqlConnector();
105
106
        $givenSslKey = $this->sslKey;
107
        $expectedSslKey = $this->sslKey;
108
109
        $connector->setSslKey( $givenSslKey );
110
111
        $this->assertEquals( $expectedSslKey, $connector->getSslKey() );
112
    }
113
114
    public function testSetGetSslCrl () : void
115
    {
116
        $connector = new PostgreSqlConnector();
117
118
        $givenSslCrl = $this->sslCrl;
119
        $expectedSslCrl = $this->sslCrl;
120
121
        $connector->setSslCrl( $givenSslCrl );
122
123
        $this->assertEquals( $expectedSslCrl, $connector->getSslCrl() );
124
    }
125
126
    public function testSetGetApplicationName () : void
127
    {
128
        $connector = new PostgreSqlConnector();
129
130
        $givenAppName = $this->appName;
131
        $expectedAppName = $this->appName;
132
133
        $connector->setApplicationName( $givenAppName );
134
135
        $this->assertEquals( $expectedAppName, $connector->getApplicationName() );
136
    }
137
138
    /**
139
     * @throws Exception
140
     */
141
    public function testGetConnection () : void
142
    {
143
        $connector = new PostgreSqlConnector();
144
        $connector->setUser( "admin" );
145
        $connector->setPassword( "secret" );
146
        $connector->setHost( "localhost" );
147
        $connector->setPort( 5432 );
148
        $connector->setDbName( $this->databaseName );
149
        $connector->setCharset( "utf8" );
150
        $connector->setDefaultDatabaseName( $this->databaseName );
151
        $connector->setSslMode( $this->sslAllowMode );
152
        $connector->setSslRootCert( $this->sslRootCert );
153
        $connector->setSslCert( $this->sslCert );
154
        $connector->setSslKey( $this->sslKey );
155
        $connector->setSslCrl( $this->sslCrl );
156
        $connector->setApplicationName( $this->appName );
157
158
        $this->assertNotNull( $connector->getNewConnection() );
159
    }
160
161
    /**
162
     * @throws SourceWatcherException
163
     */
164
    public function testInsertUsingEnvironmentVariables () : void
165
    {
166
        $user = $this->getEnvironmentVariable( "UNIT_TEST_POSTGRESQL_USER", null );
167
        $password = $this->getEnvironmentVariable( "UNIT_TEST_POSTGRESQL_PASSWORD", null );
168
        $host = $this->getEnvironmentVariable( "UNIT_TEST_POSTGRESQL_HOST", null );
169
        $port = $this->getEnvironmentVariable( "UNIT_TEST_POSTGRESQL_PORT", 5432, "intval" );
170
        $dbName = $this->getEnvironmentVariable( "UNIT_TEST_POSTGRESQL_DB_NAME", null );
171
        $defaultDatabaseName = $this->getEnvironmentVariable( "UNIT_TEST_POSTGRESQL_DEFAULT_DATABASE_NAME", null );
172
173
        $connector = new PostgreSqlConnector();
174
        $connector->setUser( $user );
175
        $connector->setPassword( $password );
176
        $connector->setHost( $host );
177
        $connector->setPort( $port );
178
        $connector->setDbName( $dbName );
179
        $connector->setDefaultDatabaseName( $defaultDatabaseName );
180
181
        $connector->setTableName( "people" );
182
183
        $row = new Row( [ "name" => "John Doe", "email_address" => "[email protected]" ] );
184
185
        $this->assertEquals( 1, $connector->insert( $row ) );
186
    }
187
}
188