-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabaseManagerTest.php
More file actions
139 lines (107 loc) · 4.35 KB
/
DatabaseManagerTest.php
File metadata and controls
139 lines (107 loc) · 4.35 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
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
<?php
namespace NeuronTests\Database;
use Neuron\Database\DatabaseDriver;
use Neuron\Database\DatabaseManager;
use Neuron\Database\Drivers\MysqlDatabaseDriver;
use Neuron\Extensibility\ExtensionStore;
use Neuron\Extensibility\InstantiatorInterface;
use NeuronTests\Database\Drivers\MockDatabaseDriver;
use PHPUnit\Framework\TestCase;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Log\LoggerInterface;
class DatabaseManagerTest extends TestCase
{
private LoggerInterface $logger;
private InstantiatorInterface $instantiator;
private EventDispatcherInterface $eventDispatcher;
private ExtensionStore $extensions;
private DatabaseManager $database;
protected function setUp(): void
{
$this->logger = $this->createMock(LoggerInterface::class);
$this->instantiator = $this->createMock(InstantiatorInterface::class);
$this->eventDispatcher = $this->createMock(EventDispatcherInterface::class);
$this->extensions = new ExtensionStore(
$this->instantiator,
$this->logger,
$this->eventDispatcher
);
$this->database = new DatabaseManager(
$this->logger,
$this->extensions,
$this->eventDispatcher
);
$this->extensions
->getRegistry()
->register(DatabaseDriver::class, MockDatabaseDriver::class);
}
public function testCanRegisterMysqlDriver(): void
{
$this->database->connect('_default_', MockDatabaseDriver::class, [
'database' => 'test_db',
'username' => 'user',
'password' => 'pass'
]);
$this->assertTrue($this->database->getConnectionStore()->has('_default_'));
}
public function testWithReturnsDriverInstance(): void
{
$mockDriver = $this->createMock(MysqlDatabaseDriver::class);
$this->instantiator->expects($this->once())
->method('instantiate')
->willReturn($mockDriver);
$this->database->connect('_default_', MockDatabaseDriver::class, [
'database' => 'test_db',
'username' => 'user',
'password' => 'pass'
]);
$driver = $this->database->with('_default_');
$this->assertSame($mockDriver, $driver);
}
public function testFetchAllReturnsArray(): void
{
$mockDriver = $this->createMock(MockDatabaseDriver::class);
$mockDriver->method('fetchAll')
->willReturn([['id' => 1, 'name' => 'John Doe']]);
$this->instantiator->method('instantiate')
->willReturn($mockDriver);
$this->database->connect('_default_', MockDatabaseDriver::class);
$result = $this->database->fetchAll('SELECT * FROM users');
$this->assertIsArray($result);
$this->assertCount(1, $result);
$this->assertEquals('John Doe', $result[0]['name']);
}
public function testExecuteReturnsLastInsertId(): void
{
$mockDriver = $this->createMock(MysqlDatabaseDriver::class);
$mockDriver->method('execute')
->willReturn(123);
$this->instantiator->method('instantiate')
->willReturn($mockDriver);
$this->database->connect('_default_', MockDatabaseDriver::class);
$result = $this->database->execute('INSERT INTO users (name) VALUES (?)', ['Jane Doe']);
$this->assertEquals(123, $result);
}
public function testTransactionRollsBackOnException(): void
{
$mockDriver = $this->createMock(MockDatabaseDriver::class);
$mockDriver->method('transaction')
->will($this->throwException(new \Exception('Transaction failed')));
$this->instantiator->method('instantiate')
->willReturn($mockDriver);
$this->database->connect('_default_', MockDatabaseDriver::class);
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Transaction failed');
$this->database->transaction(function () {
throw new \Exception('Transaction failed');
});
}
public function testProfilingTogglesCorrectly(): void
{
$this->assertFalse($this->database->isProfiling());
$this->database->enableProfiling();
$this->assertTrue($this->database->isProfiling());
$this->database->disableProfiling();
$this->assertFalse($this->database->isProfiling());
}
}