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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
<?php
namespace Engelsystem\Test\Unit\Database;
use Engelsystem\Database\Db;
use Illuminate\Database\Capsule\Manager as CapsuleManager;
use Illuminate\Database\Connection as DatabaseConnection;
use PDO;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
class DbTest extends TestCase
{
/**
* @covers \Engelsystem\Database\Db::connection()
* @covers \Engelsystem\Database\Db::setDbManager()
*/
public function testSetDbManager()
{
/** @var Pdo|MockObject $pdo */
$pdo = $this->getMockBuilder(Pdo::class)
->disableOriginalConstructor()
->getMock();
/** @var CapsuleManager|MockObject $dbManager */
$dbManager = $this->getMockBuilder(CapsuleManager::class)
->disableOriginalConstructor()
->getMock();
/** @var DatabaseConnection|MockObject $dbManager */
$databaseConnection = $this->getMockBuilder(DatabaseConnection::class)
->disableOriginalConstructor()
->getMock();
$dbManager
->expects($this->atLeastOnce())
->method('getConnection')
->willReturn($databaseConnection);
$databaseConnection
->expects($this->atLeastOnce())
->method('getPdo')
->willReturn($pdo);
Db::setDbManager($dbManager);
$this->assertEquals($pdo, Db::getPdo());
$this->assertEquals($databaseConnection, Db::connection());
}
/**
* @covers \Engelsystem\Database\Db::select()
*/
public function testSelect()
{
$return = Db::select('SELECT * FROM test_data');
$this->assertTrue(count($return) > 3);
$return = Db::select('SELECT * FROM test_data WHERE id = ?', [2]);
$this->assertCount(1, $return);
}
/**
* @covers \Engelsystem\Database\Db::selectOne()
*/
public function testSelectOne()
{
$return = Db::selectOne('SELECT * FROM test_data');
$this->assertEquals('Foo', $return['data']);
$return = Db::selectOne('SELECT * FROM test_data WHERE id = -1');
$this->assertEmpty($return);
$return = Db::selectOne('SELECT * FROM test_data WHERE id = ?', [3]);
$return = array_pop($return);
$this->assertIsNotArray($return);
}
/**
* @covers \Engelsystem\Database\Db::insert()
*/
public function testInsert()
{
$result = Db::insert("INSERT INTO test_data (id, data) VALUES (5, 'Some random text'), (6, 'another text')");
$this->assertTrue($result);
}
/**
* @covers \Engelsystem\Database\Db::update()
*/
public function testUpdate()
{
$count = Db::update("UPDATE test_data SET data='NOPE' WHERE data LIKE '%Replaceme%'");
$this->assertEquals(3, $count);
$count = Db::update("UPDATE test_data SET data=? WHERE data LIKE '%NOPE%'", ['Some random text!']);
$this->assertEquals(3, $count);
}
/**
* @covers \Engelsystem\Database\Db::delete()
*/
public function testDelete()
{
$count = Db::delete('DELETE FROM test_data WHERE id=1');
$this->assertEquals(1, $count);
$count = Db::delete('DELETE FROM test_data WHERE data LIKE ?', ['%Replaceme%']);
$this->assertEquals(3, $count);
}
/**
* @covers \Engelsystem\Database\Db::getPdo()
*/
public function testGetPdo()
{
$pdo = Db::getPdo();
$this->assertInstanceOf(PDO::class, $pdo);
}
/**
* Setup in memory database
*/
protected function setUp(): void
{
$dbManager = new CapsuleManager();
$dbManager->addConnection(['driver' => 'sqlite', 'database' => ':memory:']);
$dbManager->setAsGlobal();
$dbManager->bootEloquent();
Db::setDbManager($dbManager);
Db::getPdo()->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
Db::connection()->statement(
'
CREATE TABLE test_data(
id INT PRIMARY KEY NOT NULL,
data TEXT NOT NULL
);
'
);
Db::connection()->statement('CREATE UNIQUE INDEX test_data_id_uindex ON test_data (id);');
Db::insert("
INSERT INTO test_data (id, data)
VALUES
(1, 'Foo'),
(2, 'Bar'),
(3, 'Batz'),
(4, 'Lorem ipsum dolor sit'),
(10, 'Replaceme ipsum dolor sit amet'),
(11, 'Lorem Replaceme dolor sit amet'),
(12, 'Lorem ipsum Replaceme sit amet')
;");
}
}
|