Skip to content

Commit

Permalink
Manager::getItems() now returns array of items indexed by item's id
Browse files Browse the repository at this point in the history
  • Loading branch information
klimov-paul committed May 30, 2023
1 parent 5b174b8 commit c9bf44d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Yii1 Application Runtime Configuration extension
================================================

1.0.0 Under Development
-----------------------

- Enh: `Manager::getItems()` now returns array of items indexed by item's id (klimov-paul)


1.0.0, May 29, 2023
-------------------

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ class ConfigController extends CController
/* @var $configManager \yii1tech\config\Manager */
$configManager = Yii::app()->get('configManager');

$configManager->restoreValues();
$configManager->restore();

$models = $configManager->getItems();

Expand Down
7 changes: 4 additions & 3 deletions src/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class Manager extends CApplicationComponent
public function setStorage($storage): self
{
if (!is_array($storage) && !is_object($storage)) {
throw new InvalidArgumentException('"' . get_class($this) . '::storage" should be instance of "' . Storage::class . '" or its array configuration. "' . gettype($storage) . '" given.');
throw new InvalidArgumentException('"' . get_class($this) . '::$storage" should be instance of "' . Storage::class . '" or its array configuration. "' . gettype($storage) . '" given.');
}
$this->_storage = $storage;

Expand Down Expand Up @@ -153,9 +153,10 @@ public function setItems($items)
public function getItems(): array
{
$this->normalizeItems();

$items = [];
foreach ($this->_items as $id => $item) {
$items[] = $this->getItem($id);
$items[$id] = $this->getItem($id);
}

return $items;
Expand Down Expand Up @@ -216,7 +217,7 @@ protected function normalizeItems()
throw new LogicException('File "' . $this->_items . '" does not exist.');
}
} else {
throw new LogicException('"' . get_class($this) . '::items" should be array or file name containing it.');
throw new LogicException('"' . get_class($this) . '::$items" should be array or file name containing it.');
}
}
}
Expand Down
31 changes: 23 additions & 8 deletions tests/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,24 +61,39 @@ protected function createTestManager(): Manager

// Tests :

public function testSetGet(): void
public function testSetupStorage(): void
{
$manager = new Manager();

$storage = new StorageArray();
$manager->setStorage($storage);
$this->assertEquals($storage, $manager->getStorage(), 'Unable to setup storage!');
}

public function testSetupItems(): void
{
$manager = new Manager();

$items = [
new Item(),
new Item(),
'item1' => new Item(),
'item2' => new Item(),
];
$manager->setItems($items);
$this->assertEquals($items, $manager->getItems(), 'Unable to setup items!');

$storage = new StorageArray();
$manager->setStorage($storage);
$this->assertEquals($storage, $manager->getStorage(), 'Unable to setup storage!');
$manager->setItems([
'test_id' => [
'path' => 'test/path',
],
]);
$normalizedItems = $manager->getItems();

$this->assertFalse(empty($normalizedItems['test_id']));
$this->assertSame('test_id', $normalizedItems['test_id']->id);
}

/**
* @depends testSetGet
* @depends testSetupStorage
*/
public function testGetDefaultStorage(): void
{
Expand All @@ -88,7 +103,7 @@ public function testGetDefaultStorage(): void
}

/**
* @depends testSetGet
* @depends testSetupItems
*/
public function testGetItemById(): void
{
Expand Down

0 comments on commit c9bf44d

Please sign in to comment.