hassankhan / config

Config is a lightweight configuration file loader that supports PHP, INI, XML, JSON, and YAML files
MIT License
971 stars 136 forks source link

added test showing section override #116

Open intco opened 5 years ago

intco commented 5 years ago

currently you can not overwrite a single key (e.g. 'secret' in the test method) without overwriting the whole section (e.g. 'application' in the test method).

DavidePastore commented 5 years ago

Hi @intco . Pay attention because $this->config is different from $config.

intco commented 5 years ago

I know :-) I created specifically a new instance to demonstrate that default options from getDefaults will be overridden entirely. The application.name is lost even if i set only the application.secret.

DavidePastore commented 5 years ago

Please, take a look to the source code of getDefaults to better understand it. It should be overridden to work as you want. Tell me in details if I'm missing something and if I'm missing your point.

intco commented 5 years ago

consider the SimpleConfig class used in the test

https://github.com/hassankhan/config/blob/develop/tests/Fixture/SimpleConfig.php

its getDefaults() is:

https://github.com/hassankhan/config/blob/a2b993191d7a104c01b56293bea3eec4dff8963e/tests/Fixture/SimpleConfig.php#L11-L23

If you instantiate the class providing an array like this:

      $config = new SimpleConfig(
            [
                'application' => ['secret' => 'verysecret']
            ]
        );

then this will fail

$this->assertEquals('configuration', $config->get('application.name')); 

because $this->data is now

     [
            'host' => 'localhost',
            'port'    => 80,
            'servers' => [
                'host1',
                'host2',
                'host3'
            ],
            'application' => [
                'secret' => 's3cr3t'
            ]
     ];

just add these lines to your local copy of AbstractConfigTest and try yourself

https://github.com/hassankhan/config/blob/c72f0f0da5a76a7bb25536441b695e86d64b2347/tests/AbstractConfigTest.php#L506-L515

DavidePastore commented 5 years ago

Thanks for the detailed explanation @intco , now it's clear. 😄 It's an issue and you are right about it.

A possible solution could be using array_merge_recursive instead of array_merge here:

https://github.com/hassankhan/config/blob/a2b993191d7a104c01b56293bea3eec4dff8963e/src/AbstractConfig.php#L40

What do you think about it?

intco commented 5 years ago

I've tried with array_merge_recursive but a lot of tests failed, see below

> .\vendor\bin\phpunit
PHPUnit 7.5.1 by Sebastian Bergmann and contributors.

Runtime:       PHP 7.1.17 with Xdebug 2.6.1
Configuration: (project-dir)phpunit.xml.dist

FF.F...F.........FFFF.........F..F............................... 65 / 72 ( 90%)
.......                                                           72 / 72 (100%)

Time: 2.35 seconds, Memory: 8.00MB

There were 10 failures:

1) Noodlehaus\AbstractConfigTest::testDefaultOptionsSetOnInstantiation
Array (...) does not match expected type "string".

(project-dir)tests\AbstractConfigTest.php:62

2) Noodlehaus\AbstractConfigTest::testGet
Array (...) does not match expected type "string".

(project-dir)tests\AbstractConfigTest.php:71

3) Noodlehaus\AbstractConfigTest::testGetNestedKey
Array (...) does not match expected type "string".

(project-dir)tests\AbstractConfigTest.php:87

4) Noodlehaus\AbstractConfigTest::testGetReturnsArray
Array (...) does not match expected type "string".

(project-dir)tests\AbstractConfigTest.php:120

5) Noodlehaus\AbstractConfigTest::testAll
Failed asserting that two arrays are equal.
--- Expected
+++ Actual
@@ @@
 Array (
-    'host' => 'localhost'
-    'port' => 80
+    'host' => Array (...)
+    'port' => Array (...)
     'servers' => Array (
         0 => 'host1'
         1 => 'host2'
         2 => 'host3'
+        3 => 'host1'
+        4 => 'host2'
+        5 => 'host3'
     )
     'application' => Array (
-        'name' => 'configuration'
-        'secret' => 's3cr3t'
+        'name' => Array (...)
+        'secret' => Array (...)
         'runtime' => null
     )
     'user' => null
 )

(project-dir)tests\AbstractConfigTest.php:288

6) Noodlehaus\AbstractConfigTest::testMerge
Array (...) does not match expected type "string".

(project-dir)tests\AbstractConfigTest.php:304

7) Noodlehaus\AbstractConfigTest::testOffsetGet
Array (...) does not match expected type "string".

(project-dir)tests\AbstractConfigTest.php:312

8) Noodlehaus\AbstractConfigTest::testOffsetGetNestedKey
Array (...) does not match expected type "string".

(project-dir)tests\AbstractConfigTest.php:320

9) Noodlehaus\AbstractConfigTest::testIterator
Array (...) does not match expected type "string".

(project-dir)tests\AbstractConfigTest.php:485

10) Noodlehaus\AbstractConfigTest::testDefaultOptionsSetOnInstantiationDoNotOverwriteSections
Array (...) does not match expected type "string".

(project-dir)tests\AbstractConfigTest.php:514

FAILURES!
Tests: 72, Assertions: 142, Failures: 10.
hassankhan commented 5 years ago

Hi @intco, thanks for the detailed response and for posting your findings. I haven't had the chance to properly look, but I think those failing test cases are mainly due to test fixtures that weren't set up for recursive merging of properties.