Skip to content

Commit 084d10e

Browse files
bcordisclaude
andcommitted
fix: provide mock DB and dispatcher to integration table tests
IntegrationTestCase::createTableInstance() now injects a mock DatabaseDriver, QueryInterface, and DispatcherInterface so Table subclasses can call getDatabase(), createQuery(), getQuery(), and dispatch events without a real database connection. Fixes 23 integration test errors when running against real CMS classes. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 33e09de commit 084d10e

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

tests/integration/IntegrationTestCase.php

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,49 @@ protected function createTableInstance(string $fqcn, string $tblKey = 'id'): obj
4141

4242
// Set _tbl_key so delete() and other methods can reference it
4343
$prop = $ref->getProperty('_tbl_key');
44-
$prop->setAccessible(true);
4544
$prop->setValue($instance, $tblKey);
4645

46+
// Provide a mock DatabaseInterface to satisfy non-nullable return types.
47+
// Real CMS Table::getDatabase() throws if no DB is set.
48+
$queryMock = $this->createMock(\Joomla\Database\QueryInterface::class);
49+
$queryMock->method('select')->willReturnSelf();
50+
$queryMock->method('from')->willReturnSelf();
51+
$queryMock->method('where')->willReturnSelf();
52+
$queryMock->method('whereIn')->willReturnSelf();
53+
54+
// Use DatabaseDriver (abstract class) as mock base — it has both
55+
// getQuery() and createQuery() regardless of framework version.
56+
$dbMock = $this->getMockBuilder(\Joomla\Database\DatabaseDriver::class)
57+
->disableOriginalConstructor()
58+
->getMock();
59+
$dbMock->method('getQuery')->willReturn($queryMock);
60+
$dbMock->method('createQuery')->willReturn($queryMock);
61+
$dbMock->method('quoteName')->willReturnCallback(
62+
static fn ($name) => \is_array($name)
63+
? array_map(static fn ($n) => '`' . $n . '`', $name)
64+
: '`' . $name . '`'
65+
);
66+
$dbMock->method('quote')->willReturnCallback(
67+
static fn ($text) => "'" . $text . "'"
68+
);
69+
$dbMock->method('setQuery')->willReturnSelf();
70+
$dbMock->method('loadObject')->willReturn(null);
71+
72+
// Inject DB via the real setter or reflection
73+
if ($ref->hasMethod('setDatabase')) {
74+
$instance->setDatabase($dbMock);
75+
} elseif ($ref->hasProperty('_db')) {
76+
$dbProp = $ref->getProperty('_db');
77+
$dbProp->setValue($instance, $dbMock);
78+
}
79+
80+
// Provide a mock event dispatcher (required by real CMS Table::store/check)
81+
if ($ref->hasMethod('setDispatcher')) {
82+
$dispatcher = $this->createMock(\Joomla\Event\DispatcherInterface::class);
83+
$dispatcher->method('dispatch')->willReturn(new \Joomla\Event\Event('test'));
84+
$instance->setDispatcher($dispatcher);
85+
}
86+
4787
return $instance;
4888
}
4989

0 commit comments

Comments
 (0)