summaryrefslogtreecommitdiff
path: root/vendor/chillerlan/php-settings-container/tests/ContainerTest.php
blob: 8fca7c3e8780e440cbd6e650566a31ae022a6f74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
/**
 * Class ContainerTraitTest
 *
 * @created      28.08.2018
 * @author       Smiley <[email protected]>
 * @copyright    2018 Smiley
 * @license      MIT
 */

namespace chillerlan\SettingsTest;

use PHPUnit\Framework\TestCase;
use JsonException, TypeError;
use function sha1;

class ContainerTest extends TestCase{

	public function testConstruct(){
		$container = new TestContainer([
			'test1' => 'test1',
			'test2' => true,
			'test3' => 'test3',
			'test4' => 'test4',
		]);

		$this::assertSame('test1', $container->test1);
		$this::assertSame(true, $container->test2);
		$this::assertNull($container->test3);
		$this::assertSame('test4', $container->test4);

		$this::assertSame('success', $container->testConstruct);
	}

	public function testGet(){
		$container = new TestContainer;

		$this::assertSame('foo', $container->test1);
		$this::assertNull($container->test2);
		$this::assertNull($container->test3);
		$this::assertNull($container->test4);
		$this::assertNull($container->foo);

		// isset test
		$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));

		// custom getter
		$container->test6 = 'foo';
		$this::assertSame(sha1('foo'), $container->test6);
		// nullable/isset test
		$container->test6 = null;
		$this::assertFalse(isset($container->test6));
		$this::assertSame('null', $container->test6);
	}

	public function testSet(){
		$container = new TestContainer;
		$container->test1 = 'bar';
		$container->test2 = false;
		$container->test3 = 'nope';

		$this::assertSame('bar', $container->test1);
		$this::assertSame(false, $container->test2);
		$this::assertNull($container->test3);

		// unset
		unset($container->test1);
		$this::assertFalse(isset($container->test1));

		// custom setter
		$container->test5 = 'bar';
		$this::assertSame('bar_test5', $container->test5);
	}

	public function testToArray(){
		$container = new TestContainer([
			'test1'         => 'no',
			'test2'         => true,
			'testConstruct' => 'success',
		]);

		$this::assertSame([
			'test1'         => 'no',
			'test2'         => true,
			'testConstruct' => 'success',
			'test4'         => null,
			'test5'         => null,
			'test6'         => null
		], $container->toArray());
	}

	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}';

		$this::assertSame($expected, $container->toJSON());
		$this::assertSame($expected, (string)$container);
	}

	public function testFromJsonException(){
		$this->expectException(JsonException::class);
		(new TestContainer)->fromJSON('-');

	}
	public function testFromJsonTypeError(){
		$this->expectException(TypeError::class);
		(new TestContainer)->fromJSON('2');
	}

}