PageRenderTime 374ms CodeModel.GetById 208ms app.highlight 11ms RepoModel.GetById 151ms app.codeStats 1ms

/lib/Cake/Test/Case/TestSuite/HtmlCoverageReportTest.php

http://github.com/yandod/candycane
PHP | 237 lines | 164 code | 15 blank | 58 comment | 4 complexity | ebce1794b015376b13e3a998041681dc MD5 | raw file
  1<?php
  2/**
  3 * Test case for HtmlCoverageReport
  4 *
  5 * PHP5
  6 *
  7 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9 *
 10 * Licensed under The MIT License
 11 * For full copyright and license information, please see the LICENSE.txt
 12 * Redistributions of files must retain the above copyright notice.
 13 *
 14 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 15 * @link          http://cakephp.org CakePHP(tm) Project
 16 * @package       Cake.Test.Case.TestSuite
 17 * @since         CakePHP(tm) v 2.0
 18 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 19 */
 20
 21App::uses('HtmlCoverageReport', 'TestSuite/Coverage');
 22App::uses('CakeBaseReporter', 'TestSuite/Reporter');
 23
 24/**
 25 * Class HtmlCoverageReportTest
 26 *
 27 * @package       Cake.Test.Case.TestSuite
 28 */
 29class HtmlCoverageReportTest extends CakeTestCase {
 30
 31/**
 32 * setUp
 33 *
 34 * @return void
 35 */
 36	public function setUp() {
 37		parent::setUp();
 38		App::build(array(
 39			'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
 40		), App::RESET);
 41		CakePlugin::load(array('TestPlugin'));
 42		$reporter = new CakeBaseReporter();
 43		$reporter->params = array('app' => false, 'plugin' => false, 'group' => false);
 44		$coverage = array();
 45		$this->Coverage = new HtmlCoverageReport($coverage, $reporter);
 46	}
 47
 48/**
 49 * test getting the path filters.
 50 *
 51 * @return void
 52 */
 53	public function testGetPathFilter() {
 54		$this->Coverage->appTest = false;
 55		$result = $this->Coverage->getPathFilter();
 56		$this->assertEquals(CAKE, $result);
 57
 58		$this->Coverage->appTest = true;
 59		$result = $this->Coverage->getPathFilter();
 60		$this->assertEquals(ROOT . DS . APP_DIR . DS, $result);
 61
 62		$this->Coverage->appTest = false;
 63		$this->Coverage->pluginTest = 'TestPlugin';
 64		$result = $this->Coverage->getPathFilter();
 65		$this->assertEquals(CakePlugin::path('TestPlugin'), $result);
 66	}
 67
 68/**
 69 * test filtering coverage data.
 70 *
 71 * @return void
 72 */
 73	public function testFilterCoverageDataByPathRemovingElements() {
 74		$data = array(
 75			CAKE . 'dispatcher.php' => array(
 76				10 => -1,
 77				12 => 1
 78			),
 79			APP . 'app_model.php' => array(
 80				50 => 1,
 81				52 => -1
 82			)
 83		);
 84		$this->Coverage->setCoverage($data);
 85		$result = $this->Coverage->filterCoverageDataByPath(CAKE);
 86		$this->assertTrue(isset($result[CAKE . 'dispatcher.php']));
 87		$this->assertFalse(isset($result[APP . 'app_model.php']));
 88	}
 89
 90/**
 91 * test generating HTML reports from file arrays.
 92 *
 93 * @return void
 94 */
 95	public function testGenerateDiff() {
 96		$file = array(
 97			'line 1',
 98			'line 2',
 99			'line 3',
100			'line 4',
101			'line 5',
102			'line 6',
103			'line 7',
104			'line 8',
105			'line 9',
106			'line 10',
107		);
108		$coverage = array(
109			1 => array(array('id' => 'HtmlCoverageReportTest::testGenerateDiff')),
110			2 => -2,
111			3 => array(array('id' => 'HtmlCoverageReportTest::testGenerateDiff')),
112			4 => array(array('id' => 'HtmlCoverageReportTest::testGenerateDiff')),
113			5 => -1,
114			6 => array(array('id' => 'HtmlCoverageReportTest::testGenerateDiff')),
115			7 => array(array('id' => 'HtmlCoverageReportTest::testGenerateDiff')),
116			8 => array(array('id' => 'HtmlCoverageReportTest::testGenerateDiff')),
117			9 => -1,
118			10 => array(array('id' => 'HtmlCoverageReportTest::testGenerateDiff'))
119		);
120		$result = $this->Coverage->generateDiff('myfile.php', $file, $coverage);
121		$this->assertRegExp('/myfile\.php Code coverage\: \d+\.?\d*\%/', $result);
122		$this->assertRegExp('/<div class="code-coverage-results" id\="coverage\-myfile\.php-' . md5('myfile.php') . '"/', $result);
123		$this->assertRegExp('/<pre>/', $result);
124		foreach ($file as $i => $line) {
125			$this->assertTrue(strpos($line, $result) !== 0, 'Content is missing ' . $i);
126			$class = 'covered';
127			if (in_array($i + 1, array(5, 9, 2))) {
128				$class = 'uncovered';
129			}
130			if ($i + 1 === 2) {
131				$class .= ' dead';
132			}
133			$this->assertTrue(strpos($class, $result) !== 0, 'Class name is wrong ' . $i);
134		}
135	}
136
137/**
138 * Test that coverage works with phpunit 3.6 as the data formats from coverage are totally different.
139 *
140 * @return void
141 */
142	public function testPhpunit36Compatibility() {
143		$file = array(
144			'line 1',
145			'line 2',
146			'line 3',
147			'line 4',
148			'line 5',
149			'line 6',
150			'line 7',
151			'line 8',
152			'line 9',
153			'line 10',
154		);
155		$coverage = array(
156			1 => array('HtmlCoverageReportTest::testGenerateDiff'),
157			2 => null,
158			3 => array('HtmlCoverageReportTest::testGenerateDiff'),
159			4 => array('HtmlCoverageReportTest::testGenerateDiff'),
160			5 => array(),
161			6 => array('HtmlCoverageReportTest::testGenerateDiff'),
162			7 => array('HtmlCoverageReportTest::testGenerateDiff'),
163			8 => array('HtmlCoverageReportTest::testGenerateDiff'),
164			9 => array(),
165			10 => array('HtmlCoverageReportTest::testSomething', 'HtmlCoverageReportTest::testGenerateDiff')
166		);
167
168		$result = $this->Coverage->generateDiff('myfile.php', $file, $coverage);
169		$this->assertRegExp('/myfile\.php Code coverage\: \d+\.?\d*\%/', $result);
170		$this->assertRegExp('/<div class="code-coverage-results" id\="coverage\-myfile\.php-' . md5('myfile.php') . '"/', $result);
171		$this->assertRegExp('/<pre>/', $result);
172		foreach ($file as $i => $line) {
173			$this->assertTrue(strpos($line, $result) !== 0, 'Content is missing ' . $i);
174			$class = 'covered';
175			if (in_array($i + 1, array(5, 9, 2))) {
176				$class = 'uncovered';
177			}
178			if ($i + 1 === 2) {
179				$class .= ' dead';
180			}
181			$this->assertTrue(strpos($class, $result) !== 0, 'Class name is wrong ' . $i);
182		}
183	}
184
185/**
186 * test that covering methods show up as title attributes for lines.
187 *
188 * @return void
189 */
190	public function testCoveredLinesTitleAttributes() {
191		$file = array(
192			'line 1',
193			'line 2',
194			'line 3',
195			'line 4',
196			'line 5',
197		);
198
199		$coverage = array(
200			1 => array(array('id' => 'HtmlCoverageReportTest::testAwesomeness')),
201			2 => -2,
202			3 => array(array('id' => 'HtmlCoverageReportTest::testCakeIsSuperior')),
203			4 => array(array('id' => 'HtmlCoverageReportTest::testOther')),
204			5 => -1
205		);
206
207		$result = $this->Coverage->generateDiff('myfile.php', $file, $coverage);
208
209		$this->assertTrue(
210			strpos($result, "title=\"Covered by:\nHtmlCoverageReportTest::testAwesomeness\n\"><span class=\"line-num\">1") !== false,
211			'Missing method coverage for line 1'
212		);
213		$this->assertTrue(
214			strpos($result, "title=\"Covered by:\nHtmlCoverageReportTest::testCakeIsSuperior\n\"><span class=\"line-num\">3") !== false,
215			'Missing method coverage for line 3'
216		);
217		$this->assertTrue(
218			strpos($result, "title=\"Covered by:\nHtmlCoverageReportTest::testOther\n\"><span class=\"line-num\">4") !== false,
219			'Missing method coverage for line 4'
220		);
221		$this->assertTrue(
222			strpos($result, "title=\"\"><span class=\"line-num\">5") !== false,
223			'Coverage report is wrong for line 5'
224		);
225	}
226
227/**
228 * tearDown
229 *
230 * @return void
231 */
232	public function tearDown() {
233		CakePlugin::unload();
234		unset($this->Coverage);
235		parent::tearDown();
236	}
237}