增加单元测试

This commit is contained in:
yansongda 2017-08-22 21:56:51 +08:00
parent 007ded7a1f
commit 74c07048c6
3 changed files with 59 additions and 2 deletions

View File

@ -72,7 +72,7 @@ class Config implements ArrayAccess
*/
public function set(string $key, $value)
{
if (is_null($key) || $key == '') {
if ($key == '') {
throw new InvalidArgumentException('Invalid config key.');
}
@ -86,7 +86,7 @@ class Config implements ArrayAccess
$this->config[$keys[0]][$keys[1]] = $value;
break;
case '3':
$this->config[$keys[0]][$keys[1]][$keys[3]] = $value;
$this->config[$keys[0]][$keys[1]][$keys[2]] = $value;
break;
default:

View File

@ -1,3 +1,52 @@
<?php
namespace Yansongda\Pay\Tests\Support;
use Yansongda\Pay\Tests\TestCase;
use Yansongda\Pay\Support\Config;
use Yansongda\Pay\Exceptions\InvalidArgumentException;
class ConfigTest extends TestCase
{
public function testGetConfig()
{
$array = [
'foo' => 'bar',
'bar' => [
'id' => '18',
'key' => [
'public' => 'qwer',
'private' => 'asdf',
],
],
];
$config = new Config($array);
$this->assertTrue(isset($config['foo']));
$this->assertSame('bar', $config['foo']);
$this->assertSame('bar', $config->get('foo'));
$this->assertSame($array, $config->get());
$this->assertSame('qwer', $config->get('bar.key.public'));
$this->assertNull($config->get('bar.key.public.foo'));
$this->assertNull($config->get('bar.foo.foo.foo'));
}
public function testSetConfig()
{
$config = new Config([]);
$this->assertArrayHasKey('foo', $config->set('foo', 'bar'));
$this->assertSame('bar', $config->get('foo'));
$this->assertArrayHasKey('bar', $config->set('bar.key.public', 'qwer'));
$this->assertSame('qwer', $config->get('bar.key.public'));
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid config key.');
$config->set('', '');
$this->assertArrayHasKey('error', $config->set('error.foo.foo.foo.foo', 'foo'));
}
}

View File

@ -2,3 +2,11 @@
namespace Yansongda\Pay\Tests\Traits;
use Yansongda\Pay\Tests\TestCase;
use Yansongda\Pay\Exceptions\InvalidArgumentException;
class HasHttpRequestTest extends TestCase
{
}