2021-02-26 16:16:17 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Class ContainerTraitTest
|
|
|
|
*
|
|
|
|
* @created 28.08.2018
|
|
|
|
* @author Smiley <smiley@chillerlan.net>
|
|
|
|
* @copyright 2018 Smiley
|
|
|
|
* @license MIT
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace chillerlan\SettingsTest;
|
|
|
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
2022-07-02 14:01:51 +00:00
|
|
|
use JsonException, TypeError;
|
|
|
|
use function sha1;
|
2021-02-26 16:16:17 +00:00
|
|
|
|
2022-07-02 14:01:51 +00:00
|
|
|
class ContainerTest extends TestCase{
|
2021-02-26 16:16:17 +00:00
|
|
|
|
|
|
|
public function testConstruct(){
|
|
|
|
$container = new TestContainer([
|
|
|
|
'test1' => 'test1',
|
2022-07-02 14:01:51 +00:00
|
|
|
'test2' => true,
|
2021-02-26 16:16:17 +00:00
|
|
|
'test3' => 'test3',
|
|
|
|
'test4' => 'test4',
|
|
|
|
]);
|
|
|
|
|
2022-07-02 14:01:51 +00:00
|
|
|
$this::assertSame('test1', $container->test1);
|
|
|
|
$this::assertSame(true, $container->test2);
|
|
|
|
$this::assertNull($container->test3);
|
|
|
|
$this::assertSame('test4', $container->test4);
|
2021-02-26 16:16:17 +00:00
|
|
|
|
2022-07-02 14:01:51 +00:00
|
|
|
$this::assertSame('success', $container->testConstruct);
|
2021-02-26 16:16:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGet(){
|
|
|
|
$container = new TestContainer;
|
|
|
|
|
2022-07-02 14:01:51 +00:00
|
|
|
$this::assertSame('foo', $container->test1);
|
|
|
|
$this::assertNull($container->test2);
|
|
|
|
$this::assertNull($container->test3);
|
|
|
|
$this::assertNull($container->test4);
|
|
|
|
$this::assertNull($container->foo);
|
2021-02-26 16:16:17 +00:00
|
|
|
|
|
|
|
// isset test
|
2022-07-02 14:01:51 +00:00
|
|
|
$this::assertTrue(isset($container->test1));
|
|
|
|
$this::assertFalse(isset($container->test2));
|
|
|
|
$this::assertFalse(isset($container->test3));
|
|
|
|
$this::assertFalse(isset($container->test4));
|
|
|
|
$this::assertFalse(isset($container->foo));
|
2021-02-26 16:16:17 +00:00
|
|
|
|
|
|
|
// custom getter
|
|
|
|
$container->test6 = 'foo';
|
2022-07-02 14:01:51 +00:00
|
|
|
$this::assertSame(sha1('foo'), $container->test6);
|
2021-02-26 16:16:17 +00:00
|
|
|
// nullable/isset test
|
|
|
|
$container->test6 = null;
|
2022-07-02 14:01:51 +00:00
|
|
|
$this::assertFalse(isset($container->test6));
|
|
|
|
$this::assertSame('null', $container->test6);
|
2021-02-26 16:16:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testSet(){
|
|
|
|
$container = new TestContainer;
|
|
|
|
$container->test1 = 'bar';
|
2022-07-02 14:01:51 +00:00
|
|
|
$container->test2 = false;
|
2021-02-26 16:16:17 +00:00
|
|
|
$container->test3 = 'nope';
|
|
|
|
|
2022-07-02 14:01:51 +00:00
|
|
|
$this::assertSame('bar', $container->test1);
|
|
|
|
$this::assertSame(false, $container->test2);
|
|
|
|
$this::assertNull($container->test3);
|
2021-02-26 16:16:17 +00:00
|
|
|
|
|
|
|
// unset
|
|
|
|
unset($container->test1);
|
2022-07-02 14:01:51 +00:00
|
|
|
$this::assertFalse(isset($container->test1));
|
2021-02-26 16:16:17 +00:00
|
|
|
|
|
|
|
// custom setter
|
|
|
|
$container->test5 = 'bar';
|
2022-07-02 14:01:51 +00:00
|
|
|
$this::assertSame('bar_test5', $container->test5);
|
2021-02-26 16:16:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testToArray(){
|
2022-07-02 14:01:51 +00:00
|
|
|
$container = new TestContainer([
|
|
|
|
'test1' => 'no',
|
|
|
|
'test2' => true,
|
|
|
|
'testConstruct' => 'success',
|
|
|
|
]);
|
2021-02-26 16:16:17 +00:00
|
|
|
|
2022-07-02 14:01:51 +00:00
|
|
|
$this::assertSame([
|
|
|
|
'test1' => 'no',
|
|
|
|
'test2' => true,
|
|
|
|
'testConstruct' => 'success',
|
|
|
|
'test4' => null,
|
|
|
|
'test5' => null,
|
|
|
|
'test6' => null
|
|
|
|
], $container->toArray());
|
2021-02-26 16:16:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testToJSON(){
|
|
|
|
$container = (new TestContainer)->fromJSON('{"test1":"no","test2":true,"testConstruct":"success"}');
|
|
|
|
|
|
|
|
$expected = '{"test1":"no","test2":true,"testConstruct":"success","test4":null,"test5":null,"test6":null}';
|
|
|
|
|
2022-07-02 14:01:51 +00:00
|
|
|
$this::assertSame($expected, $container->toJSON());
|
|
|
|
$this::assertSame($expected, (string)$container);
|
2021-02-26 16:16:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testFromJsonException(){
|
2022-07-02 14:01:51 +00:00
|
|
|
$this->expectException(JsonException::class);
|
2021-02-26 16:16:17 +00:00
|
|
|
(new TestContainer)->fromJSON('-');
|
|
|
|
|
|
|
|
}
|
|
|
|
public function testFromJsonTypeError(){
|
|
|
|
$this->expectException(TypeError::class);
|
|
|
|
(new TestContainer)->fromJSON('2');
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|