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
|
|
|
|