Skip to content

Commit 8883ed1

Browse files
authored
Throw xPDOException when ContainerInterface lacks required 'config' entry (#279)
Previously, passing a PSR-11 container without a 'config' entry to xPDO caused initConfig() to silently fall back to a minimal default config array, hiding the broken API contract from the caller. Now an explicit xPDOException is thrown, making the required contract clear. Closes #269.
1 parent f7cf7b8 commit 8883ed1

6 files changed

Lines changed: 74 additions & 0 deletions

File tree

src/xPDO/xPDO.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,8 @@ protected function initConfig($data) {
369369
$this->services = $data;
370370
if ($this->services->has('config')) {
371371
$data = $this->services->get('config');
372+
} else {
373+
throw new xPDOException('A ContainerInterface passed to xPDO must provide a \'config\' entry containing the xPDO configuration array.');
372374
}
373375
}
374376
if (!is_array($data)) {

test/complete.phpunit.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
<file>./xPDO/Test/Transport/xPDOTransportTest.php</file>
4040
<file>./xPDO/Test/Transport/xPDOVehicleTest.php</file>
4141
<file>./xPDO/Test/PSR4/xPDOTest.php</file>
42+
<file>./xPDO/Test/xPDOPsr11InitTest.php</file>
4243
<file>./xPDO/Test/TearDownTest.php</file>
4344
</testsuite>
4445
<testsuite name="Legacy">

test/mysql.phpunit.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
<file>./xPDO/Test/Transport/xPDOTransportTest.php</file>
4040
<file>./xPDO/Test/Transport/xPDOVehicleTest.php</file>
4141
<file>./xPDO/Test/PSR4/xPDOTest.php</file>
42+
<file>./xPDO/Test/xPDOPsr11InitTest.php</file>
4243
<file>./xPDO/Test/TearDownTest.php</file>
4344
</testsuite>
4445
<testsuite name="Legacy">

test/pgsql.phpunit.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
<file>./xPDO/Test/Transport/xPDOTransportTest.php</file>
4040
<file>./xPDO/Test/Transport/xPDOVehicleTest.php</file>
4141
<file>./xPDO/Test/PSR4/xPDOTest.php</file>
42+
<file>./xPDO/Test/xPDOPsr11InitTest.php</file>
4243
<file>./xPDO/Test/TearDownTest.php</file>
4344
</testsuite>
4445
</testsuites>

test/sqlite.phpunit.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
<file>./xPDO/Test/Transport/xPDOTransportTest.php</file>
4242
<file>./xPDO/Test/Transport/xPDOVehicleTest.php</file>
4343
<file>./xPDO/Test/PSR4/xPDOTest.php</file>
44+
<file>./xPDO/Test/xPDOPsr11InitTest.php</file>
4445
<file>./xPDO/Test/TearDownTest.php</file>
4546
</testsuite>
4647
<testsuite name="Legacy">
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/**
3+
* This file is part of the xPDO package.
4+
*
5+
* Copyright (c) Jason Coward <jason@opengeek.com>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace xPDO\Test;
12+
13+
use PHPUnit\Framework\TestCase;
14+
use xPDO\xPDO;
15+
use xPDO\xPDOContainer;
16+
use xPDO\xPDOException;
17+
18+
/**
19+
* Tests for PSR-11 container initialization contract (issue #269).
20+
*
21+
* These tests verify that passing a ContainerInterface to xPDO makes the
22+
* required 'config' entry contract explicit rather than silently falling back
23+
* to default values when it is missing.
24+
*
25+
* @package xPDO\Test
26+
*/
27+
class xPDOPsr11InitTest extends TestCase
28+
{
29+
/**
30+
* Passing a container without a 'config' entry must throw xPDOException.
31+
*
32+
* Before the fix, xPDO would silently fall back to an empty config array
33+
* when the container did not provide a 'config' entry, hiding the broken
34+
* API contract from the caller.
35+
*/
36+
public function testContainerWithoutConfigEntryThrowsException(): void
37+
{
38+
$container = new xPDOContainer();
39+
// Deliberately do NOT add a 'config' entry
40+
41+
$this->expectException(xPDOException::class);
42+
$this->expectExceptionMessage('config');
43+
44+
new xPDO(null, '', '', $container);
45+
}
46+
47+
/**
48+
* Passing a container WITH a valid 'config' entry must initialize normally.
49+
*
50+
* This is the positive-path contract test: a container that provides the
51+
* required 'config' entry must result in a fully initialised xPDO instance
52+
* without throwing.
53+
*/
54+
public function testContainerWithConfigEntryInitializesSuccessfully(): void
55+
{
56+
$properties = include __DIR__ . '/../../properties.inc.php';
57+
$driver = getenv('TEST_DRIVER') ?: 'sqlite';
58+
$config = $properties["{$driver}_array_options"];
59+
60+
$container = new xPDOContainer();
61+
$container->add('config', $config);
62+
63+
$xpdo = new xPDO(null, '', '', $container);
64+
65+
$this->assertInstanceOf(xPDO::class, $xpdo);
66+
$this->assertSame($container, $xpdo->services);
67+
}
68+
}

0 commit comments

Comments
 (0)