Skip to content

Commit be79c9f

Browse files
committed
Add hasItem
1 parent ded9d44 commit be79c9f

File tree

5 files changed

+45
-14
lines changed

5 files changed

+45
-14
lines changed

phpunit.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
processIsolation="false"
1111
stopOnFailure="false"
1212
syntaxCheck="false"
13-
>
13+
>
1414
<testsuites>
1515
<testsuite>
1616
<directory>tests</directory>

src/ConfigProviderInterface.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ public function has($name);
7171
*/
7272
public function getItem($section, $key, $default = null);
7373

74+
/**
75+
* @param string $section Section Name
76+
* @param string $key Config Item Key
77+
*
78+
* @return bool
79+
*/
80+
public function hasItem($section, $key);
81+
7482
/**
7583
* Add an item to the configuration
7684
*

src/Provider/AbstractConfigProvider.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace Packaged\Config\Provider;
33

4+
use Exception;
45
use Packaged\Config\ConfigProviderInterface;
56
use Packaged\Config\ConfigSectionInterface;
67

@@ -74,6 +75,24 @@ public function getItem($section, $key, $default = null)
7475
return $this->getSection($section)->getItem($key, $default);
7576
}
7677

78+
/**
79+
* @param string $section Section Name
80+
* @param string $key Config Item Key
81+
*
82+
* @return bool
83+
*/
84+
public function hasItem($section, $key)
85+
{
86+
try
87+
{
88+
return $this->getSection($section)->has($key);
89+
}
90+
catch(Exception $e)
91+
{
92+
return false;
93+
}
94+
}
95+
7796
/**
7897
* Retrieve all configuration sections
7998
*

tests/ConfigProviderBaseTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,19 @@ public function testSectionExists()
212212
$this->assertFalse($provider->sectionExists("database"));
213213
}
214214

215+
/**
216+
* @depends testValidProvider
217+
*/
218+
public function testHasItem()
219+
{
220+
$provider = $this->getConfigProvider();
221+
$this->assertFalse($provider->hasItem('invalidsection', 'invaliditem'));
222+
$provider->addItem("newsection", "newitem", "newvalue");
223+
$this->assertFalse($provider->hasItem('newsection', 'invaliditem'));
224+
$this->assertFalse($provider->hasItem('invalidsection', 'newitem'));
225+
$this->assertTrue($provider->hasItem('newsection', 'newitem'));
226+
}
227+
215228
/**
216229
* @depends testValidProvider
217230
*/

tests/IniCachedConfigProviderTest.php

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -68,29 +68,20 @@ public function testCachedLoad()
6868
$contents = file_get_contents(dirname(__DIR__) . '/testData/cached.ini');
6969
file_put_contents($tempFile, $contents);
7070

71-
$provider = new \Packaged\Config\Provider\Ini\CachedIniConfigProvider(
72-
[$dir], $filename, false, 3
73-
);
71+
$provider = new \Packaged\Config\Provider\Ini\CachedIniConfigProvider([$dir], $filename, false, 3);
7472
$this->assertEquals("value2", $provider->getItem("main", "item2"));
7573

7674
// replace the value of item2 in the temp file
77-
file_put_contents(
78-
$tempFile,
79-
str_replace('value2', 'new_value_2', $contents)
80-
);
75+
file_put_contents($tempFile, str_replace('value2', 'new_value_2', $contents));
8176

8277
// 3 seconds should not have passed yet so the value should be returned from the cache
83-
$provider = new \Packaged\Config\Provider\Ini\CachedIniConfigProvider(
84-
[$dir], $filename, false, 3
85-
);
78+
$provider = new \Packaged\Config\Provider\Ini\CachedIniConfigProvider([$dir], $filename, false, 3);
8679
$this->assertEquals("value2", $provider->getItem("main", "item2"));
8780

8881
sleep(3);
8982

9083
// TTL expired, should load from the file again
91-
$provider = new \Packaged\Config\Provider\Ini\CachedIniConfigProvider(
92-
[$dir], $filename, false, 3
93-
);
84+
$provider = new \Packaged\Config\Provider\Ini\CachedIniConfigProvider([$dir], $filename, false, 3);
9485
$this->assertEquals("new_value_2", $provider->getItem("main", "item2"));
9586

9687
unlink($tempFile);

0 commit comments

Comments
 (0)