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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
vendor/
composer.lock
/.phpunit.result.cache
4 changes: 2 additions & 2 deletions src/Provider/ConfigSection.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function removeItem(string $key): static
#[\Override]
public function offsetExists(mixed $offset): bool
{
return isset($this->_items[$offset]);
return $this->has($offset);
}

#[\Override]
Expand All @@ -99,6 +99,6 @@ public function offsetSet(mixed $offset, mixed $value): void
#[\Override]
public function offsetUnset(mixed $offset): void
{
unset($this->_items[$offset]);
$this->removeItem($offset);
}
}
67 changes: 67 additions & 0 deletions tests/NullKeyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Packaged\Config\Test;

use Closure;
use Packaged\Config\Provider\ConfigProvider;
use Packaged\Config\Provider\ConfigSection;
use Packaged\Config\Provider\Ini\CachedIniConfigProvider;
use Packaged\Config\Provider\Ini\IniConfigProvider;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use TypeError;

/**
* Keys, section names, INI paths and INI strings must be strings; null throws
*/
class NullKeyTest extends TestCase
{
public static function nullCalls(): array
{
$section = static fn() => new ConfigSection('db', ['a' => 1]);
$provider = static fn() => (new ConfigProvider())->addItem('db', 'a', 1);

return [
'section getItem' => [fn() => $section()->getItem(null)],
'section has' => [fn() => $section()->has(null)],
'section addItem' => [fn() => $section()->addItem(null, 1)],
'section removeItem' => [fn() => $section()->removeItem(null)],
'section isset offset' => [fn() => isset($section()[null])],
'section get offset' => [fn() => $section()[null]],
'section append' => [function () use ($section) { $s = $section(); $s[] = 1; }],
'section unset offset' => [function () use ($section) { $s = $section(); unset($s[null]); }],
'section name' => [fn() => new ConfigSection(null)],
'section setName' => [fn() => $section()->setName(null)],
'provider getItem section' => [fn() => $provider()->getItem(null, 'a')],
'provider getItem key' => [fn() => $provider()->getItem('db', null)],
'provider addItem section' => [fn() => $provider()->addItem(null, 'a', 1)],
'provider addItem key' => [fn() => $provider()->addItem('db', null, 1)],
'provider getSection' => [fn() => $provider()->getSection(null)],
'provider sectionExists' => [fn() => $provider()->sectionExists(null)],
'provider has' => [fn() => $provider()->has(null)],
'provider hasItem' => [fn() => $provider()->hasItem(null, 'a')],
'provider removeItem' => [fn() => $provider()->removeItem(null, 'a')],
'provider removeSection' => [fn() => $provider()->removeSectionByName(null)],
'ini loadFile' => [fn() => (new IniConfigProvider())->loadFile(null)],
'ini loadString' => [fn() => (new IniConfigProvider())->loadString(null)],
'cached ini loadCached' => [fn() => (new CachedIniConfigProvider())->loadCached([sys_get_temp_dir()], null)],
];
}

#[DataProvider('nullCalls')]
public function testNullThrows(Closure $call): void
{
$this->expectException(TypeError::class);
$call();
}

public function testIntegerOffsetsStillWork(): void
{
$section = new ConfigSection('db');
$section[0] = 'zero';
$this->assertTrue(isset($section[0]));
$this->assertSame('zero', $section[0]);
unset($section[0]);
$this->assertFalse(isset($section[0]));
}
}
Loading