Passed
Push — master ( d5cfa3...5d1e58 )
by Aimeos
06:02
created

PHPArrayTest::testApply()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 11
rs 10
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2011
6
 * @copyright Aimeos (aimeos.org), 2015-2021
7
 */
8
9
10
namespace Aimeos\MW\Config;
11
12
13
class PHPArrayTest extends \PHPUnit\Framework\TestCase
14
{
15
	private $object;
16
17
18
	protected function setUp() : void
19
	{
20
		$dir = __DIR__ . DIRECTORY_SEPARATOR . 'testfiles';
21
		$dir2 = __DIR__ . DIRECTORY_SEPARATOR . 'testowrite';
22
23
		$conf = array( 'resource' => array( 'db' => array( 'host' => '127.0.0.1' ) ) );
24
		$this->object = new \Aimeos\MW\Config\PHPArray( $conf, array( $dir, $dir2 ) );
25
	}
26
27
28
	public function testApply()
29
	{
30
		$this->object->apply( ['resource' => ['db' => ['database' => 'testdb']]] );
31
		$this->assertEquals( 'testdb', $this->object->get( 'resource/db/database' ) );
32
33
		$this->object->apply( ['resource' => ['foo' => 'testdb']] );
34
		$this->object->set( 'resource/foo', 'testdb' );
35
		$this->assertEquals( 'testdb', $this->object->get( 'resource/foo' ) );
36
37
		$this->object->apply( ['resource' => ['bar' => ['db' => 'testdb']]] );
38
		$this->assertEquals( 'testdb', $this->object->get( 'resource/bar/db' ) );
39
	}
40
41
42
	public function testGet()
43
	{
44
		$this->assertEquals( '127.0.0.1', $this->object->get( 'resource/db/host' ) );
45
46
		$x = $this->object->get( 'config/manager/default/select', 'defvalue1' );
47
		$this->assertEquals( 'select11', $x );
48
49
		$x = $this->object->get( 'config/provider/delivery/sh/select', 'defvalue2' );
50
		$this->assertEquals( 'select2', $x );
51
52
		$x = $this->object->get( 'subconfig/default/subitem/a/aa', 'defvalue3' );
53
		$this->assertEquals( '111', $x );
54
55
		$x = $this->object->get( 'subconfig/subsubconfig/default/subsubitem/aa/aaa', 'defvalue4' );
56
		$this->assertEquals( '111', $x );
57
58
		$x = $this->object->get( 'config/manager/default/select', 'defvalue5' );
59
		$this->assertEquals( 'select11', $x );
60
61
		$x = $this->object->get( 'subconfig/subsubconfig/default/subsubitem/aa/aaa', 'defvalue6' );
62
		$this->assertEquals( '111', $x );
63
64
		$x = $this->object->get( 'subconfig/default/subitem/a/aa', 'defvalue7' );
65
		$this->assertEquals( '111', $x );
66
67
		$x = $this->object->get( 'subconfig/default/subitem/a/bb', 'defvalue8' );
68
		$this->assertEquals( 'defvalue8', $x );
69
70
		$x = $this->object->get( 'nonsubconfig', 'defvalue9' );
71
		$this->assertEquals( 'defvalue9', $x );
72
73
		$x = $this->object->get( 'subconfig', 'defvalue10' );
74
		$this->assertIsArray( $x );
75
	}
76
77
78
	public function testGetArray()
79
	{
80
		$this->assertEquals( array( 'host' => '127.0.0.1' ), $this->object->get( 'resource/db/' ) );
81
82
		$this->assertEquals(
83
			array(
84
				'subitem' => array(
85
					'a' => array(
86
						'aa' => '111',
87
					),
88
				),
89
				'subbla' => array(
90
					'b' => array(
91
						'bb' => '22',
92
					),
93
				),
94
			),
95
			$this->object->get( 'subconfig/default' )
96
		);
97
	}
98
99
100
	public function testGetDefault()
101
	{
102
		$this->assertEquals( 3306, $this->object->get( 'resource/db/port', 3306 ) );
103
	}
104
105
106
	public function testSet()
107
	{
108
		$this->object->set( 'resource/db/database', 'testdb' );
109
		$this->assertEquals( 'testdb', $this->object->get( 'resource/db/database' ) );
110
111
		$this->object->set( 'resource/foo', 'testdb' );
112
		$this->assertEquals( 'testdb', $this->object->get( 'resource/foo' ) );
113
114
		$this->object->set( 'resource/bar/db', 'testdb' );
115
		$this->assertEquals( 'testdb', $this->object->get( 'resource/bar/db' ) );
116
	}
117
118
119
	public function testSetArray()
120
	{
121
		$this->object->set( 'resource/ldap/', array( 'host' => 'localhost', 'port' => 389 ) );
122
		$this->assertEquals( array( 'host' => 'localhost', 'port' => 389 ), $this->object->get( 'resource/ldap' ) );
123
	}
124
}
125