Skip to content

Commit 04fbe60

Browse files
committed
Full coverage for EncoderOptions.php
1 parent fdda815 commit 04fbe60

File tree

3 files changed

+268
-18
lines changed

3 files changed

+268
-18
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace PE\Exceptions;
4+
5+
use RuntimeException;
6+
7+
class EncoderOptionsException extends RuntimeException implements PEExceptionInterface
8+
{
9+
}

src/PE/options/EncoderOptions.php

Lines changed: 74 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,19 @@
22

33
namespace PE\Options;
44

5+
use PE\Exceptions\EncoderOptionsException;
56
use PE\Nodes\EncoderNode;
67

78
class EncoderOptions {
89

10+
/**
11+
* @var array
12+
*/
913
private $options;
1014

15+
/**
16+
* @var array
17+
*/
1118
private $cached;
1219

1320
const ROOT = "%%root%%";
@@ -19,10 +26,35 @@ function __construct($options) {
1926
$this->cached = array();
2027
}
2128

29+
/**
30+
* @return array Raw options array
31+
*/
2232
public function getRawOptions() {
2333
return $this->options;
2434
}
2535

36+
/**
37+
* @param array $options
38+
* @param null|string $nodeName
39+
*/
40+
public function setOptions($options, $nodeName = null) {
41+
if ($nodeName === null) {
42+
$this->options = array_merge($this->options, $options);
43+
$this->resetCache();
44+
}
45+
else {
46+
$newOptions = (isset($this->options[$nodeName]) ? array_merge($this->options[$nodeName], $options) : $options);
47+
$this->options[$nodeName] = $newOptions;
48+
$this->resetCache($nodeName);
49+
}
50+
}
51+
52+
53+
/**
54+
* @param string $optionName The name of the option
55+
* @param null|string|EncoderNode $node Can be used to get an option from a specific node.
56+
* @return string Should return the value of the requested option if found
57+
*/
2658
public function option($optionName, $node = null) {
2759
$options = null;
2860
if ($node === null || is_string($node)) {
@@ -33,32 +65,68 @@ public function option($optionName, $node = null) {
3365
}
3466
return $this->_option($options, $optionName);
3567
}
68+
/**
69+
* @param string $optionName The name of the option
70+
* @param null|string|EncoderNode $nodeName Can be used to check an option from a specific node
71+
* @return bool
72+
*/
73+
public function hasOption($optionName, $nodeName = null) {
74+
return $this->option($optionName, $nodeName) !== null;
75+
}
3676

77+
/**
78+
* @param array $options Array with the option
79+
* @param string $optionName Name of the option you want to grab
80+
* @return null|mixed Returns the value of the option if it exists. Returns null if not found
81+
*/
3782
protected function _option($options, $optionName) {
3883
if (isset($options[$optionName])) {
3984
return $options[$optionName];
4085
}
4186
return null;
4287
}
4388

89+
/**
90+
* @return array Returns the root options (without a node)
91+
*/
4492
public function getRootOptions() {
4593
return $this->_processOptions(self::ROOT);
4694
}
4795

96+
/**
97+
* Check if a node exists. This also checks if the node exists as a node in EncoderNode::nodeExists()
98+
*
99+
* @param string $nodeName The node name you want to check
100+
* @return bool Returns true if the node exists
101+
*
102+
* @see rawNodeExists()
103+
* @see EncoderNode::nodeExists()
104+
*/
48105
public function nodeExists($nodeName) {
49106
return $this->rawNodeExists($nodeName) && EncoderNode::nodeExists($nodeName);
50107
}
108+
109+
/**
110+
* Simpler version in comparison to EncoderOptions::nodeExists(). It just checks if the node has any options
111+
*
112+
* @param string $nodeName The node name you want to check
113+
* @return bool Returns true if the node exists in the options
114+
*/
51115
public function rawNodeExists($nodeName) {
52116
return isset($this->options[$nodeName]) && is_array($this->options[$nodeName]);
53117
}
118+
119+
/**
120+
* @param string $nodeName
121+
* @return array Returns the raw node array
122+
*/
54123
public function getRawNode($nodeName) {
124+
if (!isset($this->options[$nodeName])) {
125+
throw new EncoderOptionsException(sprintf('Cannot get raw node "%s" because it doesn\'t exist', $nodeName));
126+
}
55127
return $this->options[$nodeName];
56128
}
57129

58-
public function hasOption($optionName, $nodeName = null) {
59-
return $this->option($optionName, $nodeName) !== null;
60-
}
61-
62130
protected function _decodeNodeName($nodeName) {
63131
$node = array();
64132

@@ -78,20 +146,8 @@ protected function _decodeNodeName($nodeName) {
78146
return $node;
79147
}
80148

81-
public function setOptions($options, $nodeName = null) {
82-
if ($nodeName === null) {
83-
$this->options = array_merge($this->options, $options);
84-
$this->resetCache();
85-
}
86-
else {
87-
$newOptions = (isset($this->options[$nodeName]) ? array_merge($this->options[$nodeName], $options) : $options);
88-
$this->options[$nodeName] = $newOptions;
89-
$this->resetCache($nodeName);
90-
}
91-
}
92-
93149
public function processOptionsFromNode(EncoderNode $node) {
94-
return array_merge($this->_processOptions($node->getNodeName(), $this->_processOptions($node->getNodeNameSingle())));
150+
return array_merge($this->_processOptions($node->getNodeNameSingle()), $this->_processOptions($node->getNodeName()));
95151
}
96152

97153
protected function _processOptions($nodeName = null) {
@@ -149,4 +205,4 @@ protected function resetCache($nodeName = null) {
149205
$this->cached = array();
150206
}
151207
}
152-
}
208+
}
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
<?php
2+
3+
namespace PE\Tests\Options;
4+
use PE\Options\EncoderOptions;
5+
use PE\Tests\Samples;
6+
7+
class EncoderOptionsTest extends Samples
8+
{
9+
10+
protected function setUp() {
11+
parent::setUp();
12+
$this->_peApp = $this->encoderOptions(array());
13+
}
14+
15+
/**
16+
* @param $options
17+
* @return EncoderOptions
18+
*/
19+
protected function encoderOptions($options) {
20+
return new EncoderOptions($options);
21+
}
22+
23+
public function testConstructor() {
24+
$options = new EncoderOptions(array());
25+
$this->assertNotNull($options);
26+
$this->assertTrue($options instanceof EncoderOptions);
27+
$this->assertEquals(array(), $options->getRawOptions());
28+
}
29+
30+
public function testGetRawOptions() {
31+
$options = $this->encoderOptions(array(
32+
'hello' => 'test'
33+
));
34+
$this->assertEquals(array(
35+
'hello' => 'test'
36+
), $options->getRawOptions());
37+
}
38+
39+
public function testSetOptions() {
40+
$options = $this->encoderOptions(array(
41+
'init' => 'hello'
42+
));
43+
$this->assertEquals('hello', $options->option('init'));
44+
$options->setOptions(array(
45+
'init' => 'world'
46+
));
47+
$this->assertEquals('world', $options->option('init'));
48+
49+
$options->setOptions(array(
50+
'nodeInit' => 'hello world'
51+
), 'node');
52+
$this->assertEquals(array(
53+
'init' => 'world',
54+
'node' => array(
55+
'nodeInit' => 'hello world'
56+
)
57+
), $options->getRawOptions());
58+
$this->assertEquals('hello world', $options->option('nodeInit', 'node'));
59+
60+
// edit node and retrieve data
61+
$options->setOptions(array(
62+
'nodeInit' => 'hello universe'
63+
), 'node');
64+
$this->assertEquals(array(
65+
'init' => 'world',
66+
'node' => array(
67+
'nodeInit' => 'hello universe'
68+
)
69+
), $options->getRawOptions());
70+
$this->assertEquals('hello universe', $options->option('nodeInit', 'node'));
71+
}
72+
73+
public function testOption() {
74+
$options = $this->encoderOptions(array(
75+
'option' => 'test',
76+
'rootOption' => 'root',
77+
'node' => array(
78+
'option' => 'nodeOption'
79+
),
80+
'node[1]' => array(
81+
'option' => 'nodeOption1'
82+
),
83+
'things' => array(
84+
'option' => 'thingOption'
85+
)
86+
));
87+
$this->assertEquals('test', $options->option('option'));
88+
$this->assertNull($options->option('unknown'));
89+
$this->assertEquals('nodeOption', $options->option('option', 'node'));
90+
$this->assertEquals('test', $options->option('option', 'unknown'));
91+
92+
// inheritance
93+
$this->assertEquals('root', $options->option('rootOption'));
94+
$this->assertEquals('root', $options->option('rootOption', 'node'));
95+
96+
// iterated
97+
$this->assertEquals('nodeOption1', $options->option('option', 'node[1]'));
98+
99+
$node = $this->addThingNode();
100+
$this->assertEquals('thingOption', $options->option('option', $node));
101+
}
102+
103+
public function testHasOption() {
104+
$options = $this->encoderOptions(array(
105+
'option' => 'test',
106+
'node' => array(
107+
'option' => 'option',
108+
'nodeOption' => 'nodeOption'
109+
)
110+
));
111+
$this->assertTrue($options->hasOption('option'));
112+
$this->assertTrue($options->hasOption('option', 'node'));
113+
$this->assertTrue($options->hasOption('nodeOption', 'node'));
114+
$this->assertTrue($options->hasOption('option', 'unknown'));
115+
116+
$this->assertFalse($options->hasOption('unknown'));
117+
}
118+
119+
public function testGetRootOptions() {
120+
$options = $this->encoderOptions(array(
121+
'option' => 'test',
122+
'node' => array(
123+
'option' => 'nodeOption'
124+
)
125+
));
126+
$this->assertEquals(array(
127+
'option' => 'test'
128+
), $options->getRootOptions());
129+
}
130+
131+
public function testNodeExists() {
132+
$this->addThingsNode();
133+
$options = $this->encoderOptions(array(
134+
'thingsContainer' => array(),
135+
'things' => array()
136+
));
137+
$this->assertTrue($options->nodeExists('thingsContainer'));
138+
$this->assertFalse($options->nodeExists('things'));
139+
$this->assertFalse($options->nodeExists('unknown'));
140+
}
141+
public function testRawNodeExists() {
142+
$options = $this->encoderOptions(array(
143+
'node' => array()
144+
));
145+
$this->assertTrue($options->rawNodeExists('node'));
146+
$this->assertFalse($options->rawNodeExists('unknown'));
147+
}
148+
public function testGetRawNode() {
149+
$options = $this->encoderOptions(array(
150+
'node' => array(
151+
'option' => 'hello world'
152+
)
153+
));
154+
$this->assertEquals(array(
155+
'option' => 'hello world'
156+
), $options->getRawNode('node'));
157+
}
158+
159+
public function testGetRawNodeWithUnknownNodeName() {
160+
$this->setExpectedException('\\PE\\Exceptions\\EncoderOptionsException', 'Cannot get raw node "unknown" because it doesn\'t exist');
161+
$options = $this->encoderOptions(array());
162+
$options->getRawNode('unknown');
163+
}
164+
165+
public function testProcessOptionsFromNode() {
166+
$thingsNode = $this->addThingsNode();
167+
$options = $this->encoderOptions(array(
168+
'root' => 'rootOption',
169+
'thingsContainer' => array(
170+
'option' => 'hello',
171+
'thingsContainerOption' => 'options'
172+
),
173+
'thingContainer' => array(
174+
'option' => 'world',
175+
'thingContainerOption' => 'optionSingle'
176+
)
177+
));
178+
$this->assertEquals(array(
179+
'root' => 'rootOption',
180+
'option' => 'hello',
181+
'thingContainerOption' => 'optionSingle',
182+
'thingsContainerOption' => 'options'
183+
), $options->processOptionsFromNode($thingsNode));
184+
}
185+
}

0 commit comments

Comments
 (0)