Security update for permissions_by_term
[yaffs-website] / vendor / behat / behat / src / Behat / Testwork / Output / Printer / JUnitOutputPrinter.php
1 <?php
2
3 /*
4  * This file is part of the Behat Testwork.
5  * (c) Konstantin Kudryashov <ever.zet@gmail.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 namespace Behat\Testwork\Output\Printer;
12
13 use Behat\Testwork\Output\Printer\Factory\FilesystemOutputFactory;
14 use Symfony\Component\Console\Output\OutputInterface;
15
16 /**
17  * A convenient wrapper around the ConsoleOutputPrinter to write valid JUnit
18  * reports.
19  *
20  * @author Wouter J <wouter@wouterj.nl>
21  * @author James Watson <james@sitepulse.org>
22  */
23 final class JUnitOutputPrinter extends StreamOutputPrinter
24 {
25     const XML_VERSION  = '1.0';
26     const XML_ENCODING = 'UTF-8';
27
28     /**
29      * @var \DOMDocument
30      */
31     private $domDocument;
32     /**
33      * @var \DOMElement
34      */
35     private $currentTestsuite;
36     /**
37      * @var \DOMElement
38      */
39     private $currentTestcase;
40     /**
41      * @var \DOMElement
42      */
43     private $testSuites;
44
45     public function __construct(FilesystemOutputFactory $outputFactory)
46     {
47         parent::__construct($outputFactory);
48     }
49
50     /**
51      * Creates a new JUnit file.
52      *
53      * The file will be initialized with an XML definition and the root element.
54      *
55      * @param string $name                 The filename (without extension) and default value of the name attribute
56      * @param array  $testsuitesAttributes Attributes for the root element
57      */
58     public function createNewFile($name, array $testsuitesAttributes = array())
59     {
60         $this->setFileName(strtolower(trim(preg_replace('/[^[:alnum:]_]+/', '_', $name), '_')));
61
62         $this->domDocument = new \DOMDocument(self::XML_VERSION, self::XML_ENCODING);
63         $this->domDocument->formatOutput = true;
64
65         $this->testSuites = $this->domDocument->createElement('testsuites');
66         $this->domDocument->appendChild($this->testSuites);
67         $this->addAttributesToNode($this->testSuites, array_merge(array('name' => $name), $testsuitesAttributes));
68         $this->flush();
69     }
70
71     /**
72      * Adds a new <testsuite> node.
73      *
74      * @param array $testsuiteAttributes
75      */
76     public function addTestsuite(array $testsuiteAttributes = array())
77     {
78         $this->currentTestsuite = $this->domDocument->createElement('testsuite');
79         $this->testSuites->appendChild($this->currentTestsuite);
80         $this->addAttributesToNode($this->currentTestsuite, $testsuiteAttributes);
81     }
82
83
84     /**
85      * Adds a new <testcase> node.
86      *
87      * @param array $testcaseAttributes
88      */
89     public function addTestcase(array $testcaseAttributes = array())
90     {
91         $this->currentTestcase = $this->domDocument->createElement('testcase');
92         $this->currentTestsuite->appendChild($this->currentTestcase);
93         $this->addAttributesToNode($this->currentTestcase, $testcaseAttributes);
94     }
95
96     /**
97      * Add a testcase child element.
98      *
99      * @param string $nodeName
100      * @param array  $nodeAttributes
101      * @param string $nodeValue
102      */
103     public function addTestcaseChild($nodeName, array $nodeAttributes = array(), $nodeValue = null)
104     {
105         $childNode = $this->domDocument->createElement($nodeName, $nodeValue);
106         $this->currentTestcase->appendChild($childNode);
107         $this->addAttributesToNode($childNode, $nodeAttributes);
108     }
109
110     private function addAttributesToNode(\DOMElement $node, array $attributes)
111     {
112         foreach ($attributes as $name => $value){
113             $node->setAttribute($name, $value);
114         }
115     }
116
117     /**
118      * Sets file name.
119      *
120      * @param string $fileName
121      * @param string $extension The file extension, defaults to "xml"
122      */
123     public function setFileName($fileName, $extension = 'xml')
124     {
125         if ('.'.$extension !== substr($fileName, strlen($extension) + 1)) {
126             $fileName .= '.'.$extension;
127         }
128
129         $this->getOutputFactory()->setFileName($fileName);
130         $this->flush();
131     }
132
133     /**
134      * Generate XML from the DOMDocument and parse to the the writing stream
135      */
136     public function flush()
137     {
138         if($this->domDocument instanceof \DOMDocument){
139             $this->getWritingStream()->write(
140                 $this->domDocument->saveXML(null, LIBXML_NOEMPTYTAG),
141                 false,
142                 OutputInterface::OUTPUT_RAW
143             );
144         }
145
146         parent::flush();
147     }
148 }