Skip to content

Commit c13d1d1

Browse files
committed
Adding composer.json + PHPUnit tests
1 parent eb99b26 commit c13d1d1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1312
-0
lines changed

composer.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "refactorstudio/php-array-to-xml",
3+
"description": "Convert an array to XML with PHP",
4+
"keywords": [
5+
"array",
6+
"xml",
7+
"converter"
8+
],
9+
"homepage": "https://github.com/refactorstudio/php-array-to-xml",
10+
"license": "MIT",
11+
"authors": [
12+
{
13+
"name": "Roel Boonzaijer",
14+
"email": "mail@refactorstudio.com",
15+
"homepage": "https://www.refactorstudio.com",
16+
"role": "Developer"
17+
}
18+
],
19+
"require": {
20+
"php" : "^5.6|^7.0"
21+
},
22+
"require-dev": {
23+
"phpunit/phpunit" : "^6.3",
24+
"mockery/mockery": "^1.0"
25+
},
26+
"autoload": {
27+
"psr-4": {
28+
"RefactorStudio\\PhpArrayToXml\\": "src"
29+
}
30+
},
31+
"autoload-dev": {
32+
"psr-4": {
33+
"RefactorStudio\\PhpArrayToXml\\Test\\": "tests"
34+
}
35+
}
36+
}

phpunit.xml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false"
11+
syntaxCheck="false">
12+
13+
<filter>
14+
<whitelist>
15+
<directory>./src</directory>
16+
<exclude>
17+
<directory>./vendor</directory>
18+
<directory>./tests</directory>
19+
</exclude>
20+
</whitelist>
21+
</filter>
22+
23+
<testsuites>
24+
<testsuite name="PhpArrayToXml Test Suite">
25+
<directory>./tests/</directory>
26+
</testsuite>
27+
</testsuites>
28+
29+
</phpunit>
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
<?php
2+
3+
use PHPUnit\Framework\TestCase;
4+
use RefactorStudio\PhpArrayToXml\PhpArrayToXml;
5+
6+
class PhpArrayToXmlGettersSettersTest extends TestCase
7+
{
8+
/** @covers \RefactorStudio\PhpArrayToXml\PhpArrayToXml::getDefaultRootName */
9+
public function test_it_should_get_the_default_root_name()
10+
{
11+
$class = new PhpArrayToXml();
12+
13+
// Default
14+
$this->assertEquals('root', $class->getDefaultRootName());
15+
}
16+
17+
/** @covers \RefactorStudio\PhpArrayToXml\PhpArrayToXml::getDefaultNodeName */
18+
public function test_it_should_get_the_default_node_name()
19+
{
20+
$class = new PhpArrayToXml();
21+
22+
// Default
23+
$this->assertEquals('node', $class->getDefaultNodeName());
24+
}
25+
26+
/**
27+
* @covers \RefactorStudio\PhpArrayToXml\PhpArrayToXml::setVersion
28+
* @covers \RefactorStudio\PhpArrayToXml\PhpArrayToXml::getVersion
29+
*/
30+
public function test_it_should_get_and_set_the_version()
31+
{
32+
$class = new PhpArrayToXml();
33+
34+
// Default
35+
$this->assertEquals('1.0', $class->getVersion());
36+
37+
// Custom
38+
$class->setVersion('1.1');
39+
$this->assertEquals('1.1', $class->getVersion());
40+
}
41+
42+
/**
43+
* @covers \RefactorStudio\PhpArrayToXml\PhpArrayToXml::setEncoding
44+
* @covers \RefactorStudio\PhpArrayToXml\PhpArrayToXml::getEncoding
45+
*/
46+
public function test_it_should_get_and_set_the_encoding()
47+
{
48+
$class = new PhpArrayToXml();
49+
50+
// Default
51+
$this->assertEquals('UTF-8', $class->getEncoding());
52+
53+
// Custom
54+
$class->setEncoding('ISO-8859-1');
55+
$this->assertEquals('ISO-8859-1', $class->getEncoding());
56+
}
57+
58+
/**
59+
* @covers \RefactorStudio\PhpArrayToXml\PhpArrayToXml::setFormatOutput
60+
* @covers \RefactorStudio\PhpArrayToXml\PhpArrayToXml::getFormatOutput
61+
*/
62+
public function test_it_should_get_and_set_the_format_output()
63+
{
64+
$class = new PhpArrayToXml();
65+
66+
// Default
67+
$this->assertEquals(false, $class->getFormatOutput());
68+
69+
// Custom
70+
$class->setFormatOutput(true);
71+
$this->assertEquals(true, $class->getFormatOutput());
72+
}
73+
74+
/**
75+
* @covers \RefactorStudio\PhpArrayToXml\PhpArrayToXml::setCustomRootName
76+
* @covers \RefactorStudio\PhpArrayToXml\PhpArrayToXml::getCustomRootName
77+
*/
78+
public function test_it_should_get_and_set_the_custom_root_name()
79+
{
80+
$class = new PhpArrayToXml();
81+
82+
// Default
83+
$this->assertEquals(null, $class->getCustomRootName());
84+
85+
// Custom
86+
$class->setCustomRootName('MyCustomRootName');
87+
$this->assertEquals('MyCustomRootName', $class->getCustomRootName());
88+
}
89+
90+
/**
91+
* @covers \RefactorStudio\PhpArrayToXml\PhpArrayToXml::setCustomNodeName
92+
* @covers \RefactorStudio\PhpArrayToXml\PhpArrayToXml::getCustomNodeName
93+
*/
94+
public function test_it_should_get_and_set_the_custom_node_name()
95+
{
96+
$class = new PhpArrayToXml();
97+
98+
// Default
99+
$this->assertEquals(null, $class->getCustomNodeName());
100+
101+
// Custom
102+
$class->setCustomNodeName('MyCustomNodeName');
103+
$this->assertEquals('MyCustomNodeName', $class->getCustomNodeName());
104+
}
105+
106+
/**
107+
* @covers \RefactorStudio\PhpArrayToXml\PhpArrayToXml::setCustomNodeName
108+
*/
109+
public function test_it_should_give_an_exception_when_setting_an_invalid_custom_node_name()
110+
{
111+
$this->expectException(Exception::class);
112+
113+
$class = new PhpArrayToXml();
114+
$class->setCustomNodeName(123);
115+
}
116+
117+
/**
118+
* @covers \RefactorStudio\PhpArrayToXml\PhpArrayToXml::setCustomRootName
119+
*/
120+
public function test_it_should_give_an_exception_when_setting_an_invalid_custom_root_name()
121+
{
122+
$this->expectException(Exception::class);
123+
124+
$class = new PhpArrayToXml();
125+
$class->setCustomRootName(123);
126+
}
127+
128+
/**
129+
* @covers \RefactorStudio\PhpArrayToXml\PhpArrayToXml::setSeparator
130+
* @covers \RefactorStudio\PhpArrayToXml\PhpArrayToXml::getSeparator
131+
*/
132+
public function test_it_should_get_and_set_the_separator()
133+
{
134+
$class = new PhpArrayToXml();
135+
136+
// Default
137+
$this->assertEquals('_', $class->getSeparator());
138+
139+
// Custom
140+
$class->setSeparator('-');
141+
$this->assertEquals('-', $class->getSeparator());
142+
}
143+
144+
/**
145+
* @covers \RefactorStudio\PhpArrayToXml\PhpArrayToXml::setMethodTransformKeys
146+
* @covers \RefactorStudio\PhpArrayToXml\PhpArrayToXml::getMethodTransformKeys
147+
*/
148+
public function test_it_should_get_and_set_the_key_transform_method()
149+
{
150+
$class = new PhpArrayToXml();
151+
152+
// Default
153+
$this->assertEquals(null, $class->getMethodTransformKeys());
154+
155+
// Custom
156+
$class->setMethodTransformKeys(PhpArrayToXml::LOWERCASE);
157+
$this->assertEquals(PhpArrayToXml::LOWERCASE, $class->getMethodTransformKeys());
158+
159+
$class->setMethodTransformKeys(PhpArrayToXml::UPPERCASE);
160+
$this->assertEquals(PhpArrayToXml::UPPERCASE, $class->getMethodTransformKeys());
161+
162+
$class->setMethodTransformKeys(null);
163+
$this->assertEquals(null, $class->getMethodTransformKeys());
164+
}
165+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?php
2+
3+
use PHPUnit\Framework\TestCase;
4+
use RefactorStudio\PhpArrayToXml\PhpArrayToXml;
5+
6+
class PhpArrayToXmlNodeValidationTest extends TestCase
7+
{
8+
protected $valid_node_names = [
9+
'node',
10+
'NoDe',
11+
'NODE',
12+
'no.de',
13+
'no-de',
14+
'no_de',
15+
'no:de',
16+
'no..de',
17+
'no--de',
18+
'no__de',
19+
'no::de',
20+
'n_o-d:e',
21+
'no5de',
22+
'_node',
23+
'node-',
24+
'node_',
25+
'node:',
26+
'_node',
27+
'_node_',
28+
'_node-',
29+
':node',
30+
];
31+
32+
protected $invalid_node_names = [
33+
'no!de',
34+
'no@de',
35+
'no#de',
36+
'no$de',
37+
'no%de',
38+
'no^de',
39+
'no&de',
40+
'no*de',
41+
'no(de',
42+
'no)de',
43+
'no{de',
44+
'no}de',
45+
'no[de',
46+
'no]de',
47+
'no|de',
48+
' node ',
49+
'node ',
50+
'no de',
51+
];
52+
53+
protected $node_names_with_invalid_starting_characters = [
54+
-1,
55+
0,
56+
123,
57+
'-1',
58+
'123',
59+
'123node',
60+
'-node',
61+
'!node',
62+
'@node',
63+
'#node',
64+
'$node',
65+
'%node',
66+
'^node',
67+
'&node',
68+
'*node',
69+
'(node',
70+
')node',
71+
'{node',
72+
'}node',
73+
'[node',
74+
']node',
75+
'|node',
76+
"'node'",
77+
'"node"',
78+
'`node`',
79+
' node',
80+
];
81+
82+
public function test_valid_node_starting_characters()
83+
{
84+
foreach($this->valid_node_names as $node) {
85+
$this->assertTrue(PhpArrayToXml::hasValidNodeStart($node), 'Not a valid starting character in: ' . $node);
86+
}
87+
}
88+
89+
public function test_valid_node_name()
90+
{
91+
foreach($this->valid_node_names as $node) {
92+
$this->assertTrue(PhpArrayToXml::isValidNodeName($node), 'This is not a valid node name: ' . $node);
93+
}
94+
}
95+
96+
public function test_valid_node_character_except_starting_char()
97+
{
98+
$chars = ['a', '_', ':'];
99+
foreach($chars as $char) {
100+
$this->assertTrue(PhpArrayToXml::isValidNodeNameChar($char));
101+
}
102+
}
103+
104+
public function test_invalid_node_starting_characters()
105+
{
106+
foreach($this->node_names_with_invalid_starting_characters as $node) {
107+
$this->assertFalse(PhpArrayToXml::hasValidNodeStart($node), 'Valid starting character found in: ' . $node);
108+
}
109+
}
110+
111+
public function test_invalid_node_name()
112+
{
113+
$nodes = array_merge($this->node_names_with_invalid_starting_characters, $this->invalid_node_names);
114+
115+
foreach($nodes as $node) {
116+
$this->assertFalse(PhpArrayToXml::isValidNodeName($node), 'This is a valid node name: ' . $node);
117+
}
118+
}
119+
120+
public function test_invalid_node_character_except_starting_char()
121+
{
122+
$chars = [' ', '<', '>', '&', '|', '@'];
123+
foreach($chars as $char) {
124+
$this->assertFalse(PhpArrayToXml::isValidNodeNameChar($char));
125+
}
126+
}
127+
}

0 commit comments

Comments
 (0)