diff --git a/.styleci.yml b/.styleci.yml index 48feb9c49..9a1c54393 100644 --- a/.styleci.yml +++ b/.styleci.yml @@ -6,4 +6,5 @@ finder: not-name: - "*Stub.php" path: - - "src" \ No newline at end of file + - "src" + - "tests" \ No newline at end of file diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 000000000..612d6e93e --- /dev/null +++ b/.travis.yml @@ -0,0 +1,13 @@ +language: php + +php: + - 5.5 + - 5.6 + - 7.0 + - hhvm + +before_script: + - travis_retry composer self-update + - travis_retry composer install --no-interaction --prefer-source + +script: phpunit --configuration phpunit.xml --coverage-text \ No newline at end of file diff --git a/composer.json b/composer.json index 345cb59a0..2f8ef80e6 100644 --- a/composer.json +++ b/composer.json @@ -44,6 +44,9 @@ "zendframework/zend-version": "^2.5", "monolog/monolog": "^1.18" }, + "require-dev": { + "phpunit/phpunit": "^4.8" + }, "autoload": { "psr-4": { "TorrentPier\\": "src/" diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 000000000..9c2f15d79 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,19 @@ + + + + tests + + + + src + + + diff --git a/src/Config.php b/src/Config.php index bd6871edb..091a2a79f 100644 --- a/src/Config.php +++ b/src/Config.php @@ -4,6 +4,10 @@ namespace TorrentPier; use Zend\Config\Config as ZendConfig; +/** + * Class Config + * @package TorrentPier + */ class Config extends ZendConfig { protected $root; @@ -43,7 +47,7 @@ class Config extends ZendConfig $result = $this; foreach ($keys as $key) { $result = $result->get($key); - if ($result === null) { + if (null === $result) { break; } } @@ -55,6 +59,12 @@ class Config extends ZendConfig return $this->prepareValue($result); } + /** + * Parse value + * + * @param mixed $value + * @return mixed + */ protected function prepareValue($value) { if (is_string($value)) { diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 000000000..ec633c733 --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,6 @@ +addPsr4('TorrentPier\\', __DIR__ . '/src'); diff --git a/tests/src/ConfigTest.php b/tests/src/ConfigTest.php new file mode 100644 index 000000000..f6871e58f --- /dev/null +++ b/tests/src/ConfigTest.php @@ -0,0 +1,64 @@ + 'value1', + 'key2' => [ + 'key3' => 'value2', + 'key4' => '{self.key1}', + 'key5' => '{self.key2.key4}', + ] + ]; + + $this->config = new Config($data); + } + + /** + * @covers \TorrentPier\Config::get + */ + public function testGet() + { + $this->assertEquals($this->config->get('key1'), 'value1'); + $this->assertEquals($this->config->get('key2.key3'), 'value2'); + $this->assertEquals($this->config->get('key2.key4'), 'value1'); + $this->assertEquals($this->config->get('key2.key5'), 'value1'); + $this->assertEquals($this->config->get('key2')->get('key3'), 'value2'); + + $this->assertEquals($this->config['key1'], 'value1'); + $this->assertEquals($this->config['key2.key3'], 'value2'); + $this->assertEquals($this->config['key2.key4'], 'value1'); + $this->assertEquals($this->config['key2.key5'], 'value1'); + $this->assertEquals($this->config['key2']['key3'], 'value2'); + + $this->assertEquals($this->config['key2.key6'], null); + } + + /** + * @covers \TorrentPier\Config::toArray + */ + public function testToArray() + { + $this->assertEquals($this->config->toArray(), [ + 'key1' => 'value1', + 'key2' => [ + 'key3' => 'value2', + 'key4' => 'value1', + 'key5' => 'value1', + ] + ]); + } +}