Passed
Push — master ( 6021d7...3aed48 )
by Jean Paul
06:27
created

SqliteConnectorTest::testSetGetPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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