HummingBirdAnimeClient/tests/Ion/ConfigTest.php

146 lines
3.2 KiB
PHP
Raw Normal View History

<?php declare(strict_types=1);
/**
2020-03-12 11:45:11 -04:00
* Hummingbird Anime List Client
*
2020-03-12 11:45:11 -04:00
* An API client for Kitsu to manage anime and manga watch lists
*
2021-02-04 11:57:01 -05:00
* PHP version 8
*
2022-03-04 15:50:35 -05:00
* @copyright 2015 - 2022 Timothy J. Warren <tim@timshome.page>
* @license http://www.opensource.org/licenses/mit-license.html MIT License
2020-12-10 17:06:50 -05:00
* @version 5.2
2022-03-04 15:50:35 -05:00
* @link https://git.timshome.page/timw4mail/HummingBirdAnimeClient
*/
namespace Aviat\Ion\Tests;
use Aviat\Ion\Config;
2023-05-09 12:46:52 -04:00
use PHPUnit\Framework\Attributes\DataProvider;
2022-03-04 12:19:47 -05:00
/**
* @internal
*/
final class ConfigTest extends IonTestCase
{
2022-01-07 12:33:01 -05:00
protected Config $config;
2022-03-04 12:19:47 -05:00
protected function setUp(): void
{
$this->config = new Config([
'foo' => 'bar',
'asset_path' => '/assets',
'bar' => 'baz',
'a' => [
'b' => [
'c' => TRUE,
],
],
]);
}
public function testConfigHas(): void
{
$this->assertTrue($this->config->has('foo'));
$this->assertTrue($this->config->has(['a', 'b', 'c']));
$this->assertFalse($this->config->has('baz'));
$this->assertFalse($this->config->has(['c', 'b', 'a']));
}
public function testConfigGet(): void
{
2022-03-04 12:19:47 -05:00
$this->assertSame('bar', $this->config->get('foo'));
$this->assertSame('baz', $this->config->get('bar'));
$this->assertNull($this->config->get('baz'));
$this->assertNull($this->config->get(['apple', 'sauce', 'is']));
}
public function testConfigSet(): void
{
$ret = $this->config->set('foo', 'foobar');
$this->assertInstanceOf(Config::class, $ret);
2022-03-04 12:19:47 -05:00
$this->assertSame('foobar', $this->config->get('foo'));
$this->config->set(['apple', 'sauce', 'is'], 'great');
$apple = $this->config->get('apple');
2022-03-04 12:19:47 -05:00
$this->assertSame('great', $apple['sauce']['is'], 'Config value not set correctly');
2022-03-04 12:19:47 -05:00
$this->assertSame('great', $this->config->get(['apple', 'sauce', 'is']), 'Array argument get for config failed.');
}
2023-05-09 12:46:52 -04:00
public static function dataConfigDelete(): array
{
return [
'top level delete' => [
'key' => 'apple',
'assertKeys' => [
[
'path' => ['apple', 'sauce', 'is'],
2022-03-04 12:19:47 -05:00
'expected' => NULL,
],
[
'path' => ['apple', 'sauce'],
2022-03-04 12:19:47 -05:00
'expected' => NULL,
],
[
'path' => 'apple',
2022-03-04 12:19:47 -05:00
'expected' => NULL,
],
],
],
'mid level delete' => [
'key' => ['apple', 'sauce'],
'assertKeys' => [
[
'path' => ['apple', 'sauce', 'is'],
2022-03-04 12:19:47 -05:00
'expected' => NULL,
],
[
'path' => ['apple', 'sauce'],
2022-03-04 12:19:47 -05:00
'expected' => NULL,
],
[
'path' => 'apple',
'expected' => [
2022-03-04 12:19:47 -05:00
'sauce' => NULL,
],
],
],
],
'deep delete' => [
'key' => ['apple', 'sauce', 'is'],
'assertKeys' => [
[
'path' => ['apple', 'sauce', 'is'],
2022-03-04 12:19:47 -05:00
'expected' => NULL,
],
[
'path' => ['apple', 'sauce'],
'expected' => [
2022-03-04 12:19:47 -05:00
'is' => NULL,
],
],
],
],
];
}
2023-05-09 12:46:52 -04:00
#[DataProvider('dataConfigDelete')]
public function testConfigDelete(string|array $key, array $assertKeys): void
{
$config = new Config([]);
$config->set(['apple', 'sauce', 'is'], 'great');
$config->delete($key);
2022-03-04 12:19:47 -05:00
foreach ($assertKeys as $pair)
{
2022-03-04 12:19:47 -05:00
$this->assertSame($pair['expected'], $config->get($pair['path']));
}
}
public function testGetNonExistentConfigItem(): void
{
$this->assertNull($this->config->get('foobar'));
}
2022-03-04 12:19:47 -05:00
}