-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathSqliteModelTest.php
More file actions
93 lines (79 loc) · 3.32 KB
/
SqliteModelTest.php
File metadata and controls
93 lines (79 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\SkippedTestSuiteError;
class SqliteModelTest extends TestCase
{
public static function setUpBeforeClass(): void
{
if (!in_array('sqlite', \PDO::getAvailableDrivers(), true)) {
throw new SkippedTestSuiteError('The pdo_sqlite extension is required to run SQLite-specific tests.');
}
App\Model\SqliteCategory::connectDb('sqlite::memory:', '', '');
App\Model\SqliteCategory::execute(
'CREATE TABLE categories (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NULL,
updated_at TEXT NULL,
created_at TEXT NULL
)'
);
}
protected function setUp(): void
{
App\Model\SqliteCategory::execute('DELETE FROM `categories`');
$this->resetSqliteSequenceIfPresent();
}
private function resetSqliteSequenceIfPresent(): void
{
try {
App\Model\SqliteCategory::execute('DELETE FROM sqlite_sequence WHERE name = ?', ['categories']);
} catch (\PDOException $e) {
if (strpos($e->getMessage(), 'no such table: sqlite_sequence') === false) {
throw $e;
}
}
}
public function testSqliteInsertWithDirtyFields(): void
{
/** @var App\Model\SqliteCategory $category */
$category = new App\Model\SqliteCategory([
'name' => 'SQLite Fiction',
]);
$this->assertTrue($category->save());
$this->assertSame('SQLite Fiction', $category->name);
$this->assertSame('1', (string) $category->id);
$this->assertNotEmpty($category->created_at);
$this->assertNotEmpty($category->updated_at);
/** @var App\Model\SqliteCategory|null $reloaded */
$reloaded = App\Model\SqliteCategory::getById((int) $category->id);
$this->assertNotNull($reloaded);
$this->assertSame('SQLite Fiction', $reloaded->name);
}
public function testPreparedStatementsStayBoundToTheirOwnConnection(): void
{
App\Model\IsolatedConnectionCategoryA::connectDb('sqlite::memory:', '', '');
App\Model\IsolatedConnectionCategoryB::connectDb('sqlite::memory:', '', '');
App\Model\IsolatedConnectionCategoryA::execute(
'CREATE TABLE items (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NULL
)'
);
App\Model\IsolatedConnectionCategoryB::execute(
'CREATE TABLE items (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NULL
)'
);
App\Model\IsolatedConnectionCategoryA::execute('INSERT INTO `items` (`name`) VALUES (?)', ['from-a']);
App\Model\IsolatedConnectionCategoryB::execute('INSERT INTO `items` (`name`) VALUES (?)', ['from-b']);
/** @var App\Model\IsolatedConnectionCategoryA|null $fromA */
$fromA = App\Model\IsolatedConnectionCategoryA::fetchOneWhere('`id` = ?', [1]);
/** @var App\Model\IsolatedConnectionCategoryB|null $fromB */
$fromB = App\Model\IsolatedConnectionCategoryB::fetchOneWhere('`id` = ?', [1]);
$this->assertNotNull($fromA);
$this->assertNotNull($fromB);
$this->assertSame('from-a', $fromA->name);
$this->assertSame('from-b', $fromB->name);
}
}