Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added test showing section override #116

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

intco
Copy link

@intco intco commented Dec 20, 2018

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
Copy link
Collaborator

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

@intco
Copy link
Author

intco commented Dec 27, 2018

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
Copy link
Collaborator

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
Copy link
Author

intco commented Jan 2, 2019

consider the SimpleConfig class used in the test

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

its getDefaults() is:

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

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

public function testDefaultOptionsSetOnInstantiationDoNotOverwriteSections()
{
$config = new SimpleConfig(
[
'application' => ['secret' => 'verysecret']
]
);
$this->assertEquals('configuration', $config->get('application.name'));
$this->assertEquals('verysecret', $config->get('application.secret'));
}

@DavidePastore
Copy link
Collaborator

DavidePastore commented Jan 3, 2019

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:

$this->data = array_merge($this->getDefaults(), $data);

What do you think about it?

@intco
Copy link
Author

intco commented Jan 3, 2019

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
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants