Skip to content

Commit

Permalink
- changed method signature for fromObject to `fromObject(Configuratio…
Browse files Browse the repository at this point in the history
…n|string $object)`

- fixed Config::$root fallback assignment
  • Loading branch information
kodeart committed Oct 16, 2023
1 parent 30bd855 commit 331dabc
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
9 changes: 5 additions & 4 deletions Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use function file_get_contents;
use function getcwd;
use function getenv;
use function is_a;
use function is_string;
use function iterator_to_array;
use function join;
Expand Down Expand Up @@ -80,7 +81,7 @@ public function __construct(
Data $defaults = null)
{
parent::__construct($defaults?->toArray() ?? []);
$this->root ??= getcwd();
$this->root = $root ?: getcwd();
}

/**
Expand Down Expand Up @@ -112,12 +113,12 @@ public function withParameters(array $parameters): Configuration
return $this->import($parameters);
}

public function fromObject(object|string $object): Configuration
public function fromObject(Configuration|string $object): Configuration
{
if (is_string($object) && class_exists($object)) {
if (is_string($object) && class_exists($object) && is_a($object, Configuration::class, true)) {
$object = new $object;
}
$this->root ??= $object->root;
$this->root = $object->root;
return $this->import(iterator_to_array($object));
}

Expand Down
5 changes: 3 additions & 2 deletions Interfaces.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,11 +318,12 @@ public function fromIniFile(string $filename): Configuration;
/**
* Loads the configuration options from other Config instance.
*
* @param object|string $object A FQN of the configuration object, or an instance of it
* @param Configuration|string $object A FQN of the configuration object,
* or an instance of it
*
* @return Configuration
*/
public function fromObject(object|string $object): Configuration;
public function fromObject(Configuration|string $object): Configuration;

/**
* Yell if something bad has happened, or pass quietly.
Expand Down
32 changes: 28 additions & 4 deletions tests/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ConfigTest extends TestCase
{
public function test_should_load_defaults_from_other_instance()
{
$config = new Config('', new MockOtherConfigInstance);
$config = new Config('', new TestOtherConfInstanceClass);
$this->assertSame([1, 2, 3], $config->list);
}

Expand Down Expand Up @@ -52,7 +52,7 @@ public function test_should_load_options_from_object_instance()
$config = new Config;
$this->assertNull($config->foo);

$config->fromObject(new MockOtherConfigInstance);
$config->fromObject(new TestOtherConfInstanceClass);
$this->assertSame('bar', $config->foo);
}

Expand All @@ -61,7 +61,7 @@ public function test_should_load_options_from_object_fqn()
$config = new Config;
$this->assertNull($config->foo);

$config->fromObject(MockOtherConfigInstance::class);
$config->fromObject(TestOtherConfInstanceClass::class);
$this->assertSame('bar', $config->foo);
}

Expand Down Expand Up @@ -241,17 +241,41 @@ public function test_ini_sections_parsing()
$this->assertSame(include_once __DIR__ . '/fixtures/config-sections.php', $config->toArray());
}

public function test_should_set_root_on_empty_constructor_argument()
{
$config = new Config;
$this->assertNotEmpty($config->root);
}

public function test_shoud_set_root_from_empty_object_root()
{
$initial = new TestRootFromThisClass;
$config = new Config(__DIR__);
$config->fromObject($initial);

$this->assertSame($config->root, $initial->root);
$this->assertSame('/tmp', $config->root);
}

protected function tearDown(): void
{
env('', null, []);
}
}

class MockOtherConfigInstance extends Config
class TestOtherConfInstanceClass extends Config
{
public function __construct()
{
parent::__construct();
$this->fromPhpFile(__DIR__ . '/fixtures/nested-array.php');
}
}

class TestRootFromThisClass extends Config
{
public function __construct()
{
parent::__construct('/tmp');
}
}

0 comments on commit 331dabc

Please sign in to comment.