-
Notifications
You must be signed in to change notification settings - Fork 0
/
XmlAdapter.phpt
103 lines (87 loc) · 4.23 KB
/
XmlAdapter.phpt
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
<?php
/**
* Test: Sallyx\Nette\DI\Config\Adapters\XmlAdapter
*/
use Nette\DI\Config;
use Nette\DI\Statement;
use Tester\Assert;
require __DIR__ . '/../bootstrap.php';
define('TEMP_FILE', TEMP_DIR . '/cfg.xml');
$config = new Config\Loader;
$config->addAdapter('xml','Sallyx\Nette\DI\Config\Adapters\XmlAdapter');
$data = $config->load('files/xmlAdapter.xml', 'production');
Assert::same(array(
'webname' => 'the example',
'database' => array(
'adapter' => 'pdo_mysql',
'params' => array(
'host' => 'db.example.com',
'username' => 'dbuser',
'password' => 'secret ',
'dbname' => 'dbname',
),
),
), $data);
$data = $config->load('files/xmlAdapter.xml', 'development');
Assert::same(array(
'webname' => 'the example',
'database' => array(
'adapter' => 'pdo_mysql',
'params' => array(
'host' => 'dev.example.com',
'username' => 'devuser',
'password' => 'devsecret',
'dbname' => 'dbname',
),
),
'timeout' => 10,
'display_errors' => TRUE,
'html_errors' => FALSE,
'items' => array(10, 20),
'php' => array(
'zlib.output_compression' => TRUE,
'date.timezone' => 'Europe/Prague',
),
), $data);
$config->save($data, TEMP_FILE);
$actual = file_get_contents(TEMP_FILE);
$actual = preg_replace('/\<([^\s\/>]+)(\s*[^\/>]*)\/\s*\>/i', '<$1$2></$1>', $actual);
Assert::match(<<<EOD
<?xml version="1.0"?>
<config xmlns:nc="http://www.sallyx.org/xmlns/nette/config/1.0" xmlns="http://www.sallyx.org/xmlns/nette/config/1.0"><webname>the example</webname><database><adapter>pdo_mysql</adapter><params><host>dev.example.com</host><username>devuser</username><password>devsecret</password><dbname>dbname</dbname></params></database><timeout number="10"></timeout><display_errors bool="1"></display_errors><html_errors bool="0"></html_errors><items array="numeric"><item number="10"></item><item number="20"></item></items><php><zlib.output_compression bool="1"></zlib.output_compression><date.timezone>Europe/Prague</date.timezone></php></config>
EOD
, $actual);
$data = $config->load('files/xmlAdapter.xml');
$config->save($data, TEMP_FILE);
$actual = file_get_contents(TEMP_FILE);
$actual = preg_replace('/\<([^\s\/>]+)(\s*[^\/>]*)\/\s*\>/i', '<$1$2></$1>', $actual);
Assert::match(<<<EOD
<?xml version="1.0"?>
<config xmlns:nc="http://www.sallyx.org/xmlns/nette/config/1.0" xmlns="http://www.sallyx.org/xmlns/nette/config/1.0"><production><webname>the example</webname><database><adapter>pdo_mysql</adapter><params><host>db.example.com</host><username>dbuser</username><password>secret </password><dbname>dbname</dbname></params></database></production><development extends="production"><database><params><host>dev.example.com</host><username>devuser</username><password>devsecret</password></params></database><timeout number="10"></timeout><display_errors bool="1"></display_errors><html_errors bool="0"></html_errors><items array="numeric"><item number="10"></item><item number="20"></item></items><php><zlib.output_compression bool="1"></zlib.output_compression><date.timezone>Europe/Prague</date.timezone></php></development><nothing></nothing></config>
EOD
, $actual);
$data = $config->load('files/xmlAdapter.entity.xml');
Assert::equal(array(
new Statement('ent', array(1)),
new Statement(array(
new Statement('ent', array(2)),
'inner',
),
array('3', '4')
),
new Statement(array(
new Statement('ent', array('3')),
'inner',
),
array('5','6')
),
), $data);
$data = $config->load('files/xmlAdapter.entity.xml');
$config->save($data, TEMP_FILE);
$actual = file_get_contents(TEMP_FILE);
$actual = preg_replace('/\<([^\s\/>]+)(\s*[^\/>]*)\/\s*\>/i', '<$1$2></$1>', $actual);
Assert::match(<<<EOD
<?xml version="1.0"?>
<config xmlns:nc="http://www.sallyx.org/xmlns/nette/config/1.0" xmlns="http://www.sallyx.org/xmlns/nette/config/1.0" array="numeric"><item statement="statement"><s><ent>ent</ent><args array="numeric"><item number="1"></item></args></s></item><item statement="statement"><s><ent>ent</ent><args array="numeric"><item number="2"></item></args></s><s><ent>inner</ent><args array="numeric"><item>3</item><item>4</item></args></s></item><item statement="statement"><s><ent>ent</ent><args array="numeric"><item>3</item></args></s><s><ent>inner</ent><args array="numeric"><item>5</item><item>6</item></args></s></item></config>
EOD
, $actual);