Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/xPDO/xPDO.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,8 @@ protected function initConfig($data) {
$this->services = $data;
if ($this->services->has('config')) {
$data = $this->services->get('config');
} else {
throw new xPDOException('A ContainerInterface passed to xPDO must provide a \'config\' entry containing the xPDO configuration array.');
}
}
if (!is_array($data)) {
Expand Down
1 change: 1 addition & 0 deletions test/complete.phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<file>./xPDO/Test/Transport/xPDOTransportTest.php</file>
<file>./xPDO/Test/Transport/xPDOVehicleTest.php</file>
<file>./xPDO/Test/PSR4/xPDOTest.php</file>
<file>./xPDO/Test/xPDOPsr11InitTest.php</file>
<file>./xPDO/Test/TearDownTest.php</file>
</testsuite>
<testsuite name="Legacy">
Expand Down
1 change: 1 addition & 0 deletions test/mysql.phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<file>./xPDO/Test/Transport/xPDOTransportTest.php</file>
<file>./xPDO/Test/Transport/xPDOVehicleTest.php</file>
<file>./xPDO/Test/PSR4/xPDOTest.php</file>
<file>./xPDO/Test/xPDOPsr11InitTest.php</file>
<file>./xPDO/Test/TearDownTest.php</file>
</testsuite>
<testsuite name="Legacy">
Expand Down
1 change: 1 addition & 0 deletions test/pgsql.phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<file>./xPDO/Test/Transport/xPDOTransportTest.php</file>
<file>./xPDO/Test/Transport/xPDOVehicleTest.php</file>
<file>./xPDO/Test/PSR4/xPDOTest.php</file>
<file>./xPDO/Test/xPDOPsr11InitTest.php</file>
<file>./xPDO/Test/TearDownTest.php</file>
</testsuite>
</testsuites>
Expand Down
1 change: 1 addition & 0 deletions test/sqlite.phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<file>./xPDO/Test/Transport/xPDOTransportTest.php</file>
<file>./xPDO/Test/Transport/xPDOVehicleTest.php</file>
<file>./xPDO/Test/PSR4/xPDOTest.php</file>
<file>./xPDO/Test/xPDOPsr11InitTest.php</file>
<file>./xPDO/Test/TearDownTest.php</file>
</testsuite>
<testsuite name="Legacy">
Expand Down
68 changes: 68 additions & 0 deletions test/xPDO/Test/xPDOPsr11InitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/**
* This file is part of the xPDO package.
*
* Copyright (c) Jason Coward <jason@opengeek.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace xPDO\Test;

use PHPUnit\Framework\TestCase;
use xPDO\xPDO;
use xPDO\xPDOContainer;
use xPDO\xPDOException;

/**
* Tests for PSR-11 container initialization contract (issue #269).
*
* These tests verify that passing a ContainerInterface to xPDO makes the
* required 'config' entry contract explicit rather than silently falling back
* to default values when it is missing.
*
* @package xPDO\Test
*/
class xPDOPsr11InitTest extends TestCase
{
/**
* Passing a container without a 'config' entry must throw xPDOException.
*
* Before the fix, xPDO would silently fall back to an empty config array
* when the container did not provide a 'config' entry, hiding the broken
* API contract from the caller.
*/
public function testContainerWithoutConfigEntryThrowsException(): void
{
$container = new xPDOContainer();
// Deliberately do NOT add a 'config' entry

$this->expectException(xPDOException::class);
$this->expectExceptionMessage('config');

new xPDO(null, '', '', $container);
}

/**
* Passing a container WITH a valid 'config' entry must initialize normally.
*
* This is the positive-path contract test: a container that provides the
* required 'config' entry must result in a fully initialised xPDO instance
* without throwing.
*/
public function testContainerWithConfigEntryInitializesSuccessfully(): void
{
$properties = include __DIR__ . '/../../properties.inc.php';
$driver = getenv('TEST_DRIVER') ?: 'sqlite';
$config = $properties["{$driver}_array_options"];

$container = new xPDOContainer();
$container->add('config', $config);

$xpdo = new xPDO(null, '', '', $container);

$this->assertInstanceOf(xPDO::class, $xpdo);
$this->assertSame($container, $xpdo->services);
}
}
Loading