Passed
Push — master ( 7d22f9...1460f7 )
by Jean Paul
01:51
created

SqliteConnectorTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 9
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\Connector;
6
use Coco\SourceWatcher\Core\Database\Connections\SqliteConnector;
7
use Coco\SourceWatcher\Core\Row;
8
use Coco\SourceWatcher\Core\SourceWatcherException;
9
use Coco\SourceWatcher\Utils\Internationalization;
10
use Doctrine\DBAL\DBALException;
11
use PHPUnit\Framework\TestCase;
12
13
/**
14
 * Class SqliteConnectorTest
15
 *
16
 * @package Coco\SourceWatcher\Tests\Core\Database\Connections
17
 */
18
class SqliteConnectorTest extends TestCase
19
{
20
    private string $sqliteDbLocation;
21
22
    private string $nameIndex;
23
    private string $emailAddressIndex;
24
25
    private string $johnDoeName;
26
    private string $johnDoeEmailAddress;
27
28
    public function setUp () : void
29
    {
30
        $this->sqliteDbLocation = __DIR__ . "/../../../../samples/data/sqlite/people-db.sqlite";
31
32
        $this->nameIndex = "name";
33
        $this->emailAddressIndex = "email_address";
34
35
        $this->johnDoeName = "John Doe";
36
        $this->johnDoeEmailAddress = "[email protected]";
37
    }
38
39
    public function testSetGetPath () : void
40
    {
41
        $connector = new SqliteConnector();
42
43
        $givenPath = $this->sqliteDbLocation;
44
        $expectedPath = $this->sqliteDbLocation;
45
46
        $connector->setPath( $givenPath );
47
48
        $this->assertEquals( $expectedPath, $connector->getPath() );
49
    }
50
51
    public function testSetIsMemory () : void
52
    {
53
        $connector = new SqliteConnector();
54
55
        $givenMemoryValue = false;
56
        $expectedMemoryValue = false;
57
58
        $connector->setMemory( $givenMemoryValue );
59
60
        $this->assertEquals( $expectedMemoryValue, $connector->isMemory() );
61
    }
62
63
    /**
64
     * @throws DBALException
65
     */
66
    public function testGetConnection () : void
67
    {
68
        $connector = new SqliteConnector();
69
        $connector->setPath( $this->sqliteDbLocation );
70
        $connector->setMemory( false );
71
72
        $this->assertNotNull( $connector->getConnection() );
73
    }
74
75
    /**
76
     * @throws SourceWatcherException
77
     */
78
    public function testInsertWithNoTableSpecified () : void
79
    {
80
        $this->expectException( SourceWatcherException::class );
81
        $this->expectExceptionMessage( Internationalization::getInstance()->getText( Connector::class,
82
            "No_Table_Name_Found" ) );
83
84
        $connector = new SqliteConnector();
85
        $connector->setPath( $this->sqliteDbLocation );
86
        $connector->setMemory( false );
87
88
        $row = new Row( [
89
            $this->nameIndex => $this->johnDoeName,
90
            $this->emailAddressIndex => $this->johnDoeEmailAddress
91
        ] );
92
93
        $connector->insert( $row );
94
    }
95
96
    /**
97
     * @throws SourceWatcherException
98
     */
99
    public function testInsertUsingWrongConnectionParameters () : void
100
    {
101
        $this->expectException( SourceWatcherException::class );
102
        $this->expectExceptionMessage( Internationalization::getInstance()->getText( Connector::class,
103
            "Unexpected_Error" ) );
104
105
        $connector = new SqliteConnector();
106
        $connector->setPath( $this->sqliteDbLocation );
107
        $connector->setMemory( false );
108
109
        $connector->setTableName( "some_non_existing_table" );
110
111
        $row = new Row( [
112
            $this->nameIndex => $this->johnDoeName,
113
            $this->emailAddressIndex => $this->johnDoeEmailAddress
114
        ] );
115
116
        $connector->insert( $row );
117
    }
118
119
    /**
120
     * @throws SourceWatcherException
121
     */
122
    public function testInsertUsingCorrectConnectionParameters () : void
123
    {
124
        $connector = new SqliteConnector();
125
        $connector->setPath( $this->sqliteDbLocation );
126
        $connector->setMemory( false );
127
128
        $connector->setTableName( "people" );
129
130
        $row = new Row( [
131
            $this->nameIndex => $this->johnDoeName,
132
            $this->emailAddressIndex => $this->johnDoeEmailAddress
133
        ] );
134
135
        $this->assertEquals( 1, $connector->insert( $row ) );
136
    }
137
}
138