Completed
Push — master ( 38a554...54855b )
by Zoltán
02:36
created

StandardXMLDefinitionParser   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
c 2
b 0
f 0
lcom 1
cbo 2
dl 0
loc 71
ccs 25
cts 25
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setTestGroup() 0 3 1
A getXPathQuery() 0 7 2
A createTestArrayFromXmlElement() 0 18 3
A parse() 0 9 2
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 4
    private function getXPathQuery() {
60 4
        if($this->groupName === NULL) {
61 1
            return '/testGroups/testGroup[1]';
62
        }
63
64 3
        return '/testGroups/testGroup[@name="' . $this->groupName . '"]';
65
    }
66
67
    /**
68
     * Takes previously find test group and collect all data set from it
69
     * parses and returns an array
70
     *
71
     * @param \SimpleXMLElement $element
72
     *
73
     * @return array
74
     */
75 3
    private function createTestArrayFromXmlElement(SimpleXMLElement $element) {
76 3
        $result = [];
77 3
        $dataSets = $element->xpath('*');
78
79 3
        foreach($dataSets as $dataSet) {
80 3
            $values = [];
81 3
            $properties = $dataSet->xpath('*');
82
83 3
            foreach($properties as $property) {
84 3
                $typedGetter = new SimpleXMLNodeTypedAttributeGetter($property);
85 3
                $values[$typedGetter->getName()] = $typedGetter->getValue();
86 3
            }
87
88 3
            $result[] = $values;
89 3
        }
90
91 3
        return $result;
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97 4
    public function parse(SimpleXMLElement $root) {
98 4
        $result = $root->xpath($this->getXPathQuery());
99
        
100 4
        if(empty($result)) {
101 1
            throw XMLDataSetParsingException::noResult($this->getXPathQuery());
102
        }
103
104 3
        return $this->createTestArrayFromXmlElement($result[0]);
105
    }
106
    
107
}
108