Skip to content

Commit

Permalink
add some tests for config
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Nov 9, 2021
1 parent 87ecf7e commit 489d511
Show file tree
Hide file tree
Showing 14 changed files with 131 additions and 54 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ composer.lock
*.log
*.pid
*.patch
*.cache
.DS_Store
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,15 @@ composer require phppkg/config

## Usage

**Config**

```php
use PhpPkg\Config\ConfigBox;

$config = ConfigBox::new();

$config->getString('name'); // 'inhere'
```

## License

Expand Down
32 changes: 11 additions & 21 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="test/boot.php"
colors="false"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnFailure="false"
>
<testsuites>
<testsuite name="Php Library Test Suite">
<directory>test/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory suffix=".php">src</directory>
</whitelist>
</filter>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false" bootstrap="test/bootstrap.php" colors="false" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Php Library Test Suite">
<directory>test/</directory>
</testsuite>
</testsuites>
</phpunit>
29 changes: 18 additions & 11 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,18 @@
*/
class Collection extends \Toolkit\Stdlib\Std\Collection
{

/**
* @var int
*/
public int $mergeDepth = 3;

/**
* Property separator.
* The key path separator.
*
* @var string
*/
protected string $keyPathSep = '.';
public string $keyPathSep = '.';

/**
* set config value by key/path
Expand Down Expand Up @@ -121,6 +127,7 @@ public function setKeyPathSep(string $keyPathSep): void
public function load(array|Traversable $data): self
{
$this->bindData($this->data, $data);

return $this;
}

Expand All @@ -132,26 +139,28 @@ public function load(array|Traversable $data): self
public function loadData(array|Traversable $data): self
{
$this->bindData($this->data, $data);

return $this;
}

/**
* @param array $parent
* @param array|Traversable $data
* @param int $depth
*/
protected function bindData(array &$parent, array|Traversable $data): void
protected function bindData(array &$parent, array|Traversable $data, int $depth = 1): void
{
foreach ($data as $key => $value) {
if ($value === null) {
continue;
}

if (is_array($value)) {
if (!isset($parent[$key])) {
$parent[$key] = [];
if (is_array($value) && isset($parent[$key]) && is_array($parent[$key])) {
if ($depth > $this->mergeDepth) {
$parent[$key] = $value;
} else {
$this->bindData($parent[$key], $value, ++$depth);
}

$this->bindData($parent[$key], $value);
} else {
$parent[$key] = $value;
}
Expand All @@ -177,9 +186,7 @@ public function getIterator(): Traversable
/**
* Unset an offset in the iterator.
*
* @param mixed $offset The array offset.
*
* @return void
* @param mixed $offset
*/
public function offsetUnset($offset): void
{
Expand Down
26 changes: 26 additions & 0 deletions src/ConfigBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,32 @@ public static function newFromFile(string $filepath, string $format = ''): self
return (new self())->loadFromFile($filepath, $format);
}

/**
* @param string[] $filePaths
* @param string $format
*
* @return static
*/
public static function newFromFiles(array $filePaths, string $format = ''): self
{
return (new self())->loadFromFiles($filePaths, $format);
}

/**
* @param string[] $filePaths
* @param string $format
*
* @return $this
*/
public function loadFromFiles(array $filePaths, string $format = ''): self
{
foreach ($filePaths as $filePath) {
$this->loadFromFile($filePath, $format);
}

return $this;
}

/**
* @param string $filepath
* @param string $format
Expand Down
27 changes: 27 additions & 0 deletions test/ConfigBoxTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php declare(strict_types=1);

namespace PhpPkg\ConfigTest;

use PhpPkg\Config\ConfigBox;
use PHPUnit\Framework\TestCase;
use function vdump;

/**
* class ConfigBoxTest
*
* @author inhere
*/
class ConfigBoxTest extends TestCase
{
public function testNewFromFiles(): void
{
$c = ConfigBox::newFromFiles([
__DIR__ . '/testdata/config.ini',
__DIR__ . '/testdata/config.neon',
__DIR__ . '/testdata/config.yml',
]);

vdump($c);
$this->assertEquals('inher', $c->get('name'));
}
}
3 changes: 1 addition & 2 deletions test/LanguageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function testTranslate(): void
$l = new Language([
'lang' => 'en',
'allowed' => ['en', 'zh-CN'],
'basePath' => __DIR__ . '/testdata',
'basePath' => __DIR__ . '/testdata/language',
'langFiles' => [
'response.php'
],
Expand All @@ -35,7 +35,6 @@ public function testTranslate(): void
'k' => 'c',
];


$msg = $l->tl('response.key');
$this->assertEquals('message', $msg);
$this->assertTrue($l->has('response.key'));
Expand Down
20 changes: 0 additions & 20 deletions test/boot.php

This file was deleted.

29 changes: 29 additions & 0 deletions test/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php declare(strict_types=1);
/**
* phpunit --bootstrap tests/boot.php tests
*/

error_reporting(E_ALL | E_STRICT);
date_default_timezone_set('Asia/Shanghai');

$libDir = dirname(__DIR__);
$npMap = [
'PhpPkg\\ConfigTest\\' => $libDir . '/test/',
'PhpPkg\\Config\\' => $libDir . '/src/',
];

spl_autoload_register(static function ($class) use ($npMap) {
foreach ($npMap as $np => $dir) {
$file = $dir . str_replace('\\', '/', substr($class, strlen($np))) . '.php';

if (file_exists($file)) {
include $file;
}
}
});

if (is_file(dirname(__DIR__, 3) . '/autoload.php')) {
require dirname(__DIR__, 3) . '/autoload.php';
} elseif (is_file(dirname(__DIR__) . '/vendor/autoload.php')) {
require dirname(__DIR__) . '/vendor/autoload.php';
}
3 changes: 3 additions & 0 deletions test/testdata/config.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name=inhere
age=89
atIni=value0
3 changes: 3 additions & 0 deletions test/testdata/config.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: inhere
age: 89
atNeon: value1
4 changes: 4 additions & 0 deletions test/testdata/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
name: inhere
age: 89
atYaml: value2

File renamed without changes.
File renamed without changes.

0 comments on commit 489d511

Please sign in to comment.