Arbit - project tracking

PHP Depend

Browse source code

File: / tests/ PHP/ Depend/ Log/ Summary/ XmlTest.php

Type
text/plain text/plain
Last Author
mapi
Version
1091
Line Rev. Author Source
1 141 mapi <?php
2 mapi /**
3 mapi * This file is part of PHP_Depend.
4 535 mapi *
5 141 mapi * PHP Version 5
6 mapi *
7 1030 mapi * Copyright (c) 2008-2010, Manuel Pichler <mapi@pdepend.org>.
8 141 mapi * All rights reserved.
9 mapi *
10 mapi * Redistribution and use in source and binary forms, with or without
11 mapi * modification, are permitted provided that the following conditions
12 mapi * are met:
13 mapi *
14 mapi * * Redistributions of source code must retain the above copyright
15 mapi * notice, this list of conditions and the following disclaimer.
16 mapi *
17 mapi * * Redistributions in binary form must reproduce the above copyright
18 mapi * notice, this list of conditions and the following disclaimer in
19 mapi * the documentation and/or other materials provided with the
20 mapi * distribution.
21 mapi *
22 mapi * * Neither the name of Manuel Pichler nor the names of his
23 mapi * contributors may be used to endorse or promote products derived
24 mapi * from this software without specific prior written permission.
25 mapi *
26 mapi * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 mapi * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 mapi * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
29 mapi * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
30 mapi * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31 mapi * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
32 mapi * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
33 mapi * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
34 mapi * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 mapi * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
36 mapi * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 mapi * POSSIBILITY OF SUCH DAMAGE.
38 mapi *
39 mapi * @category QualityAssurance
40 mapi * @package PHP_Depend
41 mapi * @subpackage Log
42 269 mapi * @author Manuel Pichler <mapi@pdepend.org>
43 1030 mapi * @copyright 2008-2010 Manuel Pichler. All rights reserved.
44 141 mapi * @license http://www.opensource.org/licenses/bsd-license.php BSD License
45 mapi * @version SVN: $Id: XmlTest.php 1091 2010-02-22 08:17:57Z mapi $
46 824 mapi * @link http://pdepend.org/
47 141 mapi */
48 mapi
49 mapi require_once dirname(__FILE__) . '/../../AbstractTest.php';
50 184 mapi require_once dirname(__FILE__) . '/AnalyzerNodeAwareDummy.php';
51 mapi require_once dirname(__FILE__) . '/AnalyzerProjectAwareDummy.php';
52 1060 mapi require_once dirname(__FILE__) . '/AnalyzerNodeAndProjectAwareDummy.php';
53 141 mapi
54 mapi require_once 'PHP/Depend/Log/Summary/Xml.php';
55 1046 mapi require_once 'PHP/Depend/Metrics/NodeAwareI.php';
56 mapi require_once 'PHP/Depend/Metrics/ProjectAwareI.php';
57 141 mapi
58 mapi /**
59 mapi * Test case for the xml summary log.
60 mapi *
61 mapi * @category QualityAssurance
62 mapi * @package PHP_Depend
63 mapi * @subpackage Log
64 269 mapi * @author Manuel Pichler <mapi@pdepend.org>
65 1030 mapi * @copyright 2008-2010 Manuel Pichler. All rights reserved.
66 141 mapi * @license http://www.opensource.org/licenses/bsd-license.php BSD License
67 mapi * @version Release: @package_version@
68 824 mapi * @link http://pdepend.org/
69 141 mapi */
70 mapi class PHP_Depend_Log_Summary_XmlTest extends PHP_Depend_AbstractTest
71 mapi {
72 mapi /**
73 mapi * Test code structure.
74 mapi *
75 mapi * @var PHP_Depend_Code_NodeIterator $packages
76 mapi */
77 mapi protected $packages = null;
78 535 mapi
79 141 mapi /**
80 143 mapi * The test file name.
81 mapi *
82 mapi * @var string $testFile
83 mapi */
84 mapi protected $testFileName = null;
85 535 mapi
86 143 mapi /**
87 mapi * The temporary file name for the logger result.
88 mapi *
89 mapi * @var string $resultFile
90 mapi */
91 mapi protected $resultFile = null;
92 535 mapi
93 143 mapi /**
94 141 mapi * Creates the package structure from a test source file.
95 mapi *
96 mapi * @return void
97 mapi */
98 mapi protected function setUp()
99 mapi {
100 mapi parent::setUp();
101 535 mapi
102 143 mapi $this->testFileName = dirname(__FILE__) . '/../../_code/mixed_code.php';
103 mapi $this->testFileName = realpath($this->testFileName);
104 535 mapi
105 606 mapi $this->packages = self::parseSource($this->testFileName);
106 616 mapi $this->resultFile = self::createRunResourceURI('log-summary.xml');
107 141 mapi }
108 535 mapi
109 143 mapi /**
110 mapi * Removes the temporary log files.
111 mapi *
112 mapi * @return void
113 mapi */
114 mapi protected function tearDown()
115 141 mapi {
116 143 mapi @unlink($this->resultFile);
117 535 mapi
118 143 mapi parent::tearDown();
119 mapi }
120 352 mapi
121 mapi /**
122 mapi * Tests that the logger returns the expected set of analyzers.
123 mapi *
124 mapi * @return void
125 mapi */
126 mapi public function testReturnsExceptedAnalyzers()
127 mapi {
128 356 mapi $logger = new PHP_Depend_Log_Summary_Xml();
129 352 mapi $actual = $logger->getAcceptedAnalyzers();
130 mapi $exptected = array(
131 mapi 'PHP_Depend_Metrics_NodeAwareI',
132 mapi 'PHP_Depend_Metrics_ProjectAwareI'
133 mapi );
134 535 mapi
135 352 mapi $this->assertEquals($exptected, $actual);
136 mapi }
137 535 mapi
138 143 mapi /**
139 535 mapi * Tests that the logger throws an exception if the log target wasn't
140 356 mapi * configured.
141 mapi *
142 mapi * @return void
143 mapi */
144 mapi public function testThrowsExceptionForInvalidLogTarget()
145 mapi {
146 mapi $this->setExpectedException(
147 mapi 'PHP_Depend_Log_NoLogOutputException',
148 mapi "The log target is not configured for 'PHP_Depend_Log_Summary_Xml'."
149 mapi );
150 535 mapi
151 356 mapi $logger = new PHP_Depend_Log_Summary_Xml();
152 mapi $logger->close();
153 mapi }
154 535 mapi
155 356 mapi /**
156 1046 mapi * testLogMethodReturnsTrueForAnalyzerOfTypeProjectAware
157 mapi *
158 mapi * @return void
159 mapi * @covers PHP_Depend_Log_Summary_Xml
160 mapi * @group pdepend
161 mapi * @group pdepend::logs
162 mapi * @group pdepend::logs::summary
163 mapi * @group unittest
164 mapi */
165 mapi public function testLogMethodReturnsTrueForAnalyzerOfTypeProjectAware()
166 mapi {
167 mapi $logger = new PHP_Depend_Log_Summary_Xml();
168 mapi $actual = $logger->log($this->getMock('PHP_Depend_Metrics_ProjectAwareI'));
169 mapi
170 mapi $this->assertTrue($actual);
171 mapi }
172 mapi
173 mapi /**
174 mapi * testLogMethodReturnsTrueForAnalyzerOfTypeNodeAware
175 mapi *
176 mapi * @return void
177 mapi * @covers PHP_Depend_Log_Summary_Xml
178 mapi * @group pdepend
179 mapi * @group pdepend::logs
180 mapi * @group pdepend::logs::summary
181 mapi * @group unittest
182 mapi */
183 mapi public function testLogMethodReturnsTrueForAnalyzerOfTypeNodeAware()
184 mapi {
185 mapi $logger = new PHP_Depend_Log_Summary_Xml();
186 mapi $actual = $logger->log($this->getMock('PHP_Depend_Metrics_NodeAwareI'));
187 mapi
188 mapi $this->assertTrue($actual);
189 mapi }
190 mapi
191 mapi /**
192 535 mapi * Tests that {@link PHP_Depend_Log_Summary_Xml::write()} generates the
193 mapi * expected document structure for the source, but without any applied
194 143 mapi * metrics.
195 mapi *
196 mapi * @return void
197 mapi */
198 mapi public function testXmlLogWithoutMetrics()
199 mapi {
200 356 mapi $log = new PHP_Depend_Log_Summary_Xml();
201 mapi $log->setLogFile($this->resultFile);
202 166 mapi $log->setCode($this->packages);
203 mapi $log->close();
204 535 mapi
205 143 mapi $fileName = 'xml-log-without-metrics.xml';
206 mapi $this->assertXmlStringEqualsXmlString(
207 mapi $this->getNormalizedPathXml(dirname(__FILE__) . "/_expected/{$fileName}"),
208 1091 mapi $this->getNormalizedPathXml($this->resultFile)
209 143 mapi );
210 mapi }
211 535 mapi
212 143 mapi /**
213 535 mapi * Tests that the xml logger generates the expected xml document for an
214 143 mapi * empty source code structure.
215 mapi *
216 mapi * @return void
217 mapi */
218 184 mapi public function testProjectAwareAnalyzerWithoutCode()
219 143 mapi {
220 141 mapi $metricsOne = array('interfs' => 42, 'cls' => 23);
221 184 mapi $resultOne = new PHP_Depend_Log_Summary_AnalyzerProjectAwareDummy($metricsOne);
222 535 mapi
223 141 mapi $metricsTwo = array('ncloc' => 1742, 'loc' => 4217);
224 184 mapi $resultTwo = new PHP_Depend_Log_Summary_AnalyzerProjectAwareDummy($metricsTwo);
225 535 mapi
226 356 mapi $log = new PHP_Depend_Log_Summary_Xml();
227 mapi $log->setLogFile($this->resultFile);
228 166 mapi $log->setCode(new PHP_Depend_Code_NodeIterator(array()));
229 1046 mapi $log->log($resultOne);
230 mapi $log->log($resultTwo);
231 535 mapi
232 166 mapi $log->close();
233 535 mapi
234 951 mapi $fileName = 'project-aware-result-set-without-code.xml';
235 mapi $this->assertXmlStringEqualsXmlString(
236 mapi $this->getNormalizedPathXml(dirname(__FILE__) . "/_expected/{$fileName}"),
237 1091 mapi $this->getNormalizedPathXml($this->resultFile)
238 951 mapi );
239 141 mapi }
240 535 mapi
241 1060 mapi /**
242 mapi * testAnalyzersThatImplementProjectAndNodeAwareAsExpected
243 mapi *
244 mapi * @return void
245 mapi * @covers PHP_Depend_Log_Summary_Xml
246 mapi * @group pdepend
247 mapi * @group pdepend::log
248 mapi * @group pdepend::log::summary
249 mapi * @group unittest
250 mapi */
251 mapi public function testAnalyzersThatImplementProjectAndNodeAwareAsExpected()
252 mapi {
253 mapi $analyzer = new PHP_Depend_Log_Summary_AnalyzerNodeAndProjectAwareDummy(
254 mapi array('foo' => 42, 'bar' => 23),
255 mapi array('baz' => 23, 'foobar' => 42)
256 mapi );
257 mapi
258 mapi $log = new PHP_Depend_Log_Summary_Xml();
259 mapi $log->setLogFile($this->resultFile);
260 mapi $log->setCode($this->packages);
261 mapi $log->log($analyzer);
262 mapi
263 mapi $log->close();
264 mapi
265 mapi $fileName = 'node-and-project-aware-result-set.xml';
266 mapi $this->assertXmlStringEqualsXmlString(
267 mapi $this->getNormalizedPathXml(dirname(__FILE__) . "/_expected/{$fileName}"),
268 1091 mapi $this->getNormalizedPathXml($this->resultFile)
269 1060 mapi );
270 mapi }
271 mapi
272 184 mapi public function testNodeAwareAnalyzer()
273 143 mapi {
274 mapi $input = array(
275 mapi array('loc' => 42), array('ncloc' => 23),
276 mapi array('loc' => 9), array('ncloc' => 7),
277 mapi array('loc' => 101), array('ncloc' => 99),
278 mapi array('loc' => 90), array('ncloc' => 80),
279 mapi array('loc' => 50), array('ncloc' => 45),
280 mapi array('loc' => 30), array('ncloc' => 22),
281 mapi array('loc' => 9), array('ncloc' => 9),
282 mapi array('loc' => 3), array('ncloc' => 3),
283 mapi array('loc' => 42), array('ncloc' => 23),
284 mapi array('loc' => 33), array('ncloc' => 20),
285 mapi array('loc' => 9), array('ncloc' => 7),
286 mapi );
287 535 mapi
288 143 mapi $metricsOne = array();
289 mapi $metricsTwo = array();
290 mapi foreach ($this->packages as $package) {
291 mapi $metricsOne[$package->getUUID()] = array_shift($input);
292 mapi $metricsTwo[$package->getUUID()] = array_shift($input);
293 mapi foreach ($package->getClasses() as $class) {
294 mapi $metricsOne[$class->getUUID()] = array_shift($input);
295 mapi $metricsTwo[$class->getUUID()] = array_shift($input);
296 mapi foreach ($class->getMethods() as $method) {
297 mapi $metricsOne[$method->getUUID()] = array_shift($input);
298 mapi $metricsTwo[$method->getUUID()] = array_shift($input);
299 mapi }
300 mapi }
301 mapi foreach ($package->getFunctions() as $function) {
302 mapi $metricsOne[$function->getUUID()] = array_shift($input);
303 mapi $metricsTwo[$function->getUUID()] = array_shift($input);
304 mapi }
305 mapi }
306 535 mapi
307 184 mapi $resultOne = new PHP_Depend_Log_Summary_AnalyzerNodeAwareDummy($metricsOne);
308 mapi $resultTwo = new PHP_Depend_Log_Summary_AnalyzerNodeAwareDummy($metricsTwo);
309 535 mapi
310 356 mapi $log = new PHP_Depend_Log_Summary_Xml();
311 mapi $log->setLogFile($this->resultFile);
312 166 mapi $log->setCode($this->packages);
313 1046 mapi $log->log($resultOne);
314 mapi $log->log($resultTwo);
315 535 mapi
316 166 mapi $log->close();
317 535 mapi
318 143 mapi $fileName = 'node-aware-result-set.xml';
319 mapi $this->assertXmlStringEqualsXmlString(
320 mapi $this->getNormalizedPathXml(dirname(__FILE__) . "/_expected/{$fileName}"),
321 1091 mapi $this->getNormalizedPathXml($this->resultFile)
322 143 mapi );
323 mapi }
324 535 mapi
325 143 mapi protected function getNormalizedPathXml($fileName)
326 mapi {
327 951 mapi $dom = new DOMDocument('1.0', 'UTF-8');
328 mapi $dom->preserveWhiteSpace = false;
329 mapi $dom->load($fileName);
330 535 mapi
331 143 mapi // Adjust file path
332 951 mapi foreach ($dom->getElementsByTagName('file') as $file) {
333 143 mapi $file->setAttribute('name', $this->testFileName);
334 mapi }
335 1091 mapi $dom->documentElement->setAttribute('generated', '2010-02-22T08:26:51');
336 535 mapi
337 951 mapi return $dom->saveXML();
338 143 mapi }
339 356 mapi }