|
| 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