Skip to content

Commit

Permalink
This maybe fixes and closes #3
Browse files Browse the repository at this point in the history
  • Loading branch information
overclokk committed Dec 26, 2019
1 parent 2f788b3 commit d1cb702
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 26 deletions.
49 changes: 29 additions & 20 deletions src/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ class Config extends ArrayObject implements ConfigInterface {
private $storage = [];

/**
* @var
* @var array
*/
private $temp;
private $temp = [];

/**
* @var
* @var mixed
*/
private $default;

Expand Down Expand Up @@ -68,7 +68,7 @@ public function get( string $key, $default = null ) {
$this->default = $default;

if ( ! $this->has( $key ) ) {
return $this->default;
return $default;
}

// The class::temp variable is always setted by the class::has() method
Expand All @@ -80,6 +80,7 @@ public function get( string $key, $default = null ) {
*/
public function has( string $key ) : bool {
$this->temp = $this->search( $this->storage, $key, $this->default );
$this->default = null;
return isset( $this->temp );
}

Expand Down Expand Up @@ -148,29 +149,37 @@ public function toJson(): string {
return \json_encode( $this->toArray() );
}

/**
*
*/
public function __clone() {
$this->storage = [];
parent::exchangeArray( $this->storage );
}

/**
* @link https://www.php.net/manual/en/class.arrayobject.php#107079
*
* @param $func
* @param $argv
* @return mixed
*/
public function __call( $func, $argv ) {

if ( \array_key_exists( $func, $this->storage ) && \is_callable( $this->storage[ $func ] ) ) {
codecept_debug( $argv );
$new_func = function ( ...$argv ) use ( $func ) {
return $func( $argv );
};
return \call_user_func_array( $new_func, $argv );
}

if ( ! \is_callable( $func ) || \substr( $func, 0, 6 ) !== 'array_' ) {
throw new BadMethodCallException(__CLASS__ . '->' . $func );
}

return \call_user_func_array( $func, \array_merge( [ $this->getArrayCopy() ], $argv ) );
}
// public function __call( $func, $argv ) {
//
// if ( \array_key_exists( $func, $this->storage ) && \is_callable( $this->storage[ $func ] ) ) {
// codecept_debug( $argv );
// $new_func = function ( ...$argv ) use ( $func ) {
// return $func( $argv );
// };
// return \call_user_func_array( $new_func, $argv );
// }
//
// if ( ! \is_callable( $func ) || \substr( $func, 0, 6 ) !== 'array_' ) {
// throw new BadMethodCallException(__CLASS__ . '->' . $func );
// }
//
// return \call_user_func_array( $func, \array_merge( [ $this->getArrayCopy() ], $argv ) );
// }

/**
* @todo In future move this method to its own class
Expand Down
64 changes: 58 additions & 6 deletions tests/unit/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,39 @@ public function it_should_have_and_get_key()
$this->assertArrayHasKey( 'subKey', $config->get( 'recursive' ) );
}

/**
* @test
* it should have key
*/
public function it_should_reset_default_member_on_every_call_and_only_return_value_if_exist()
{
$config = new Config( [ 'key' => 'value' ] );
$this->assertFalse( $config->has( 'some-key' ) );
$this->assertFalse( $config->has( 'some-key' ) );
$this->assertStringContainsString(
\strval( $config->get( 'some-key', 'default value' ) ),
'default value',
''
);
$this->assertFalse( $config->has( 'some-key' ) );
$this->assertStringContainsString(
\strval( $config->get( 'some-key', 'other default value' ) ),
'other default value',
''
);

$this->assertFalse( $config->has( 'some-key' ) );
$this->assertEmpty( $config->get( 'some-key' ), '' );

$this->assertStringContainsString(
\strval( $config->get( 'some-key', 'other default value' ) ),
'other default value',
''
);
$this->assertEmpty( $config->get( 'some-key' ), '' );
$this->assertFalse( $config->has( 'some-key' ) );
}

/**
* @test
* it should get_key
Expand Down Expand Up @@ -416,12 +449,12 @@ public function it_shoud_return_valid_json() {
/**
* @test
*/
public function it_shoud_call_builtin_array_functions() {
$keys = \array_keys( $this->config_arr );

$config = new Config( $this->config_arr );
$this->assertEquals( $keys, $config->array_keys() );
}
// public function it_shoud_call_builtin_array_functions() {
// $keys = \array_keys( $this->config_arr );
//
// $config = new Config( $this->config_arr );
// $this->assertEquals( $keys, $config->array_keys() );
// }

/**
* @test
Expand Down Expand Up @@ -449,6 +482,25 @@ public function it_shoud_search_subkeys() {
$this->assertEquals( 'subSubValue', $config->get( 'key.subSubKey.subSubKeyKey' ), '' );
}

/**
* @test
*/
public function it_should_clone_have_empty_value() {
$arr = [ 'key' => 'value' ];
$config = new Config( $arr );

$this->assertStringContainsString( \strval( $config->get( 'key' ) ), $arr['key'], '' );
$this->assertTrue( $config->has( 'key' ), '' );
$this->assertNotEmpty( $config->get( 'key' ), '' );

$clone = clone $config;

$this->assertFalse( $clone->has( 'key' ), '' );
$this->assertEmpty( $clone->get( 'key' ), '' );

$this->assertNotSame( $config, $clone, '' );
}

/**
* @test
*/
Expand Down

1 comment on commit d1cb702

@overclokk
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also closes #1

Please sign in to comment.