Completed
Push — master ( 13f1f9...a95f40 )
by Zoltán
02:30
created

createTestArrayFromXmlElement()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 4

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 23
ccs 17
cts 17
cp 1
rs 8.7972
cc 4
eloc 15
nc 5
nop 1
crap 4
1
<?php namespace BuildR\TestTools\DataSetLoader\XML\Parser;
2
3
use BuildR\TestTools\DataSetLoader\XML\Helper\SimpleXMLNodeTypedAttributeGetter;
4
use BuildR\TestTools\Exception\XMLDataSetParsingException;
5
use SimpleXMLElement;
6
7
/**
8
 * Parser for standard XML definition
9
 *
10
 * ```xml
11
 * <testGroups>
12
 *     <testGroup name="testGroup">
13
 *         <dataSet>
14
 *             <dataSetProperty name="input" value="inputValue"/>
15
 *             <dataSetProperty name="expected" value="INPUTVALUE"/>
16
 *
17
 *             ...
18
 *         </dataSet>
19
 *
20
 *         ...
21
 *     </testGroup>
22
 *
23
 *     ...
24
 * </testGroups>
25
 * ```
26
 *
27
 * BuildR PHP Framework
28
 *
29
 * @author Zoltán Borsos <[email protected]>
30
 * @package TestTools
31
 * @subpackage DataSetLoader\XML\Parser
32
 *
33
 * @copyright    Copyright 2016, Zoltán Borsos.
34
 * @license      https://github.com/BuildrPHP/Test-Tools/blob/master/LICENSE.md
35
 * @link         https://github.com/BuildrPHP/Test-Tools
36
 */
37
class StandardXMLDefinitionParser implements XMLDefinitionParserInterface {
38
39
    /**
40
     * @type string
41
     */
42
    private $groupName;
43
44
    /**
45
     * Set the parsed test group name. If no name set the first defined
46
     * group will be used.
47
     *
48
     * @param string $groupName
49
     */
50 3
    public function setTestGroup($groupName) {
51 3
        $this->groupName = $groupName;
52 3
    }
53
54
    /**
55
     * Creates the root XPath query to find the correct test group
56
     *
57
     * @return string
58
     */
59
    //@codingStandardIgnoreStart
60 4
    private function getXPathQuery() {
61
    //@codingStandardIgnoreEnd
62 4
        if($this->groupName === NULL) {
63 1
            return '/testGroups/testGroup[1]';
64
        }
65
66 3
        return '/testGroups/testGroup[@name="' . $this->groupName . '"]';
67
    }
68
69
    /**
70
     * Takes previously find test group and collect all data set from it
71
     * parses and returns an array
72
     *
73
     * @param \SimpleXMLElement $element
74
     *
75
     * @return array
76
     */
77 3
    private function createTestArrayFromXmlElement(SimpleXMLElement $element) {
78 3
        $result = [];
79 3
        $dataSets = $element->xpath('*');
80 3
        $setCount = 0;
81
82 3
        foreach($dataSets as $dataSet) {
83 3
            $values = [];
84
85 3
            $properties = $dataSet->xpath('*');
86 3
            $setName = (string) $dataSet->attributes()->name;
87 3
            $setName = (empty($setName)) ? 'Index #' . $setCount : $setName;
88
89 3
            foreach($properties as $property) {
90 3
                $typedGetter = new SimpleXMLNodeTypedAttributeGetter($property);
91 3
                $values[$typedGetter->getName()] = $typedGetter->getValue();
92 3
            }
93
94 3
            $result[$setName] = $values;
95 3
            $setCount++;
96 3
        }
97
98 3
        return $result;
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104 4
    public function parse(SimpleXMLElement $root) {
105 4
        $result = $root->xpath($this->getXPathQuery());
106
        
107 4
        if(empty($result)) {
108 1
            throw XMLDataSetParsingException::noResult($this->getXPathQuery());
109
        }
110
111 3
        return $this->createTestArrayFromXmlElement($result[0]);
112
    }
113
    
114
}
115