Skip to content

Commit 331dabc

Browse files
committed
- changed method signature for fromObject to fromObject(Configuration|string $object)
- fixed Config::$root fallback assignment
1 parent 30bd855 commit 331dabc

File tree

3 files changed

+36
-10
lines changed

3 files changed

+36
-10
lines changed

Config.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use function file_get_contents;
2121
use function getcwd;
2222
use function getenv;
23+
use function is_a;
2324
use function is_string;
2425
use function iterator_to_array;
2526
use function join;
@@ -80,7 +81,7 @@ public function __construct(
8081
Data $defaults = null)
8182
{
8283
parent::__construct($defaults?->toArray() ?? []);
83-
$this->root ??= getcwd();
84+
$this->root = $root ?: getcwd();
8485
}
8586

8687
/**
@@ -112,12 +113,12 @@ public function withParameters(array $parameters): Configuration
112113
return $this->import($parameters);
113114
}
114115

115-
public function fromObject(object|string $object): Configuration
116+
public function fromObject(Configuration|string $object): Configuration
116117
{
117-
if (is_string($object) && class_exists($object)) {
118+
if (is_string($object) && class_exists($object) && is_a($object, Configuration::class, true)) {
118119
$object = new $object;
119120
}
120-
$this->root ??= $object->root;
121+
$this->root = $object->root;
121122
return $this->import(iterator_to_array($object));
122123
}
123124

Interfaces.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,11 +318,12 @@ public function fromIniFile(string $filename): Configuration;
318318
/**
319319
* Loads the configuration options from other Config instance.
320320
*
321-
* @param object|string $object A FQN of the configuration object, or an instance of it
321+
* @param Configuration|string $object A FQN of the configuration object,
322+
* or an instance of it
322323
*
323324
* @return Configuration
324325
*/
325-
public function fromObject(object|string $object): Configuration;
326+
public function fromObject(Configuration|string $object): Configuration;
326327

327328
/**
328329
* Yell if something bad has happened, or pass quietly.

tests/ConfigTest.php

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class ConfigTest extends TestCase
1111
{
1212
public function test_should_load_defaults_from_other_instance()
1313
{
14-
$config = new Config('', new MockOtherConfigInstance);
14+
$config = new Config('', new TestOtherConfInstanceClass);
1515
$this->assertSame([1, 2, 3], $config->list);
1616
}
1717

@@ -52,7 +52,7 @@ public function test_should_load_options_from_object_instance()
5252
$config = new Config;
5353
$this->assertNull($config->foo);
5454

55-
$config->fromObject(new MockOtherConfigInstance);
55+
$config->fromObject(new TestOtherConfInstanceClass);
5656
$this->assertSame('bar', $config->foo);
5757
}
5858

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

64-
$config->fromObject(MockOtherConfigInstance::class);
64+
$config->fromObject(TestOtherConfInstanceClass::class);
6565
$this->assertSame('bar', $config->foo);
6666
}
6767

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

244+
public function test_should_set_root_on_empty_constructor_argument()
245+
{
246+
$config = new Config;
247+
$this->assertNotEmpty($config->root);
248+
}
249+
250+
public function test_shoud_set_root_from_empty_object_root()
251+
{
252+
$initial = new TestRootFromThisClass;
253+
$config = new Config(__DIR__);
254+
$config->fromObject($initial);
255+
256+
$this->assertSame($config->root, $initial->root);
257+
$this->assertSame('/tmp', $config->root);
258+
}
259+
244260
protected function tearDown(): void
245261
{
246262
env('', null, []);
247263
}
248264
}
249265

250-
class MockOtherConfigInstance extends Config
266+
class TestOtherConfInstanceClass extends Config
251267
{
252268
public function __construct()
253269
{
254270
parent::__construct();
255271
$this->fromPhpFile(__DIR__ . '/fixtures/nested-array.php');
256272
}
257273
}
274+
275+
class TestRootFromThisClass extends Config
276+
{
277+
public function __construct()
278+
{
279+
parent::__construct('/tmp');
280+
}
281+
}

0 commit comments

Comments
 (0)