2010-05-08 21:59:14 +00:00
|
|
|
<?php
|
2010-05-08 22:12:20 +00:00
|
|
|
/**
|
|
|
|
* Test case for HtmlCoverageReport
|
|
|
|
*
|
|
|
|
* PHP5
|
|
|
|
*
|
2017-06-10 21:33:55 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
|
2017-06-10 22:10:52 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2010-05-08 22:12:20 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2010-05-08 22:12:20 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2017-06-10 22:10:52 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2017-06-10 21:33:55 +00:00
|
|
|
* @link https://cakephp.org CakePHP(tm) Project
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.TestSuite
|
2010-05-08 22:12:20 +00:00
|
|
|
* @since CakePHP(tm) v 2.0
|
2017-06-10 22:23:14 +00:00
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License
|
2010-05-08 22:12:20 +00:00
|
|
|
*/
|
2010-05-08 21:59:14 +00:00
|
|
|
|
2010-12-11 05:47:55 +00:00
|
|
|
App::uses('HtmlCoverageReport', 'TestSuite/Coverage');
|
2011-05-13 03:59:31 +00:00
|
|
|
App::uses('CakeBaseReporter', 'TestSuite/Reporter');
|
2010-05-08 21:59:14 +00:00
|
|
|
|
2013-05-30 22:11:14 +00:00
|
|
|
/**
|
2016-08-10 10:22:09 +00:00
|
|
|
* HtmlCoverageReportTest
|
2013-05-30 22:11:14 +00:00
|
|
|
*
|
|
|
|
* @package Cake.Test.Case.TestSuite
|
|
|
|
*/
|
2010-05-08 21:59:14 +00:00
|
|
|
class HtmlCoverageReportTest extends CakeTestCase {
|
2012-03-18 21:31:53 +00:00
|
|
|
|
2010-05-08 21:59:14 +00:00
|
|
|
/**
|
2011-12-04 21:27:51 +00:00
|
|
|
* setUp
|
2010-05-08 21:59:14 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setUp() {
|
2011-09-03 00:14:51 +00:00
|
|
|
parent::setUp();
|
2011-05-09 03:55:32 +00:00
|
|
|
App::build(array(
|
2012-02-18 12:31:29 +00:00
|
|
|
'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
|
2011-09-03 00:14:51 +00:00
|
|
|
), App::RESET);
|
2012-02-17 03:59:24 +00:00
|
|
|
CakePlugin::load(array('TestPlugin'));
|
2010-05-08 21:59:14 +00:00
|
|
|
$reporter = new CakeBaseReporter();
|
|
|
|
$reporter->params = array('app' => false, 'plugin' => false, 'group' => false);
|
|
|
|
$coverage = array();
|
|
|
|
$this->Coverage = new HtmlCoverageReport($coverage, $reporter);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test getting the path filters.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testGetPathFilter() {
|
2010-05-08 21:59:14 +00:00
|
|
|
$this->Coverage->appTest = false;
|
|
|
|
$result = $this->Coverage->getPathFilter();
|
2011-04-17 10:35:21 +00:00
|
|
|
$this->assertEquals(CAKE, $result);
|
2010-05-08 21:59:14 +00:00
|
|
|
|
|
|
|
$this->Coverage->appTest = true;
|
|
|
|
$result = $this->Coverage->getPathFilter();
|
|
|
|
$this->assertEquals(ROOT . DS . APP_DIR . DS, $result);
|
|
|
|
|
|
|
|
$this->Coverage->appTest = false;
|
2011-05-09 03:55:32 +00:00
|
|
|
$this->Coverage->pluginTest = 'TestPlugin';
|
2010-05-08 21:59:14 +00:00
|
|
|
$result = $this->Coverage->getPathFilter();
|
2011-05-09 03:55:32 +00:00
|
|
|
$this->assertEquals(CakePlugin::path('TestPlugin'), $result);
|
2010-05-08 21:59:14 +00:00
|
|
|
}
|
|
|
|
|
2010-05-08 22:08:02 +00:00
|
|
|
/**
|
|
|
|
* test filtering coverage data.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testFilterCoverageDataByPathRemovingElements() {
|
2010-05-08 22:08:02 +00:00
|
|
|
$data = array(
|
2011-04-17 10:35:21 +00:00
|
|
|
CAKE . 'dispatcher.php' => array(
|
2010-09-29 05:44:14 +00:00
|
|
|
10 => -1,
|
|
|
|
12 => 1
|
|
|
|
),
|
|
|
|
APP . 'app_model.php' => array(
|
|
|
|
50 => 1,
|
|
|
|
52 => -1
|
2010-05-08 22:08:02 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->Coverage->setCoverage($data);
|
2011-04-17 10:35:21 +00:00
|
|
|
$result = $this->Coverage->filterCoverageDataByPath(CAKE);
|
|
|
|
$this->assertTrue(isset($result[CAKE . 'dispatcher.php']));
|
2010-05-08 22:08:02 +00:00
|
|
|
$this->assertFalse(isset($result[APP . 'app_model.php']));
|
|
|
|
}
|
|
|
|
|
2010-05-09 04:04:03 +00:00
|
|
|
/**
|
|
|
|
* test generating HTML reports from file arrays.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testGenerateDiff() {
|
2010-05-09 04:04:03 +00:00
|
|
|
$file = array(
|
|
|
|
'line 1',
|
|
|
|
'line 2',
|
|
|
|
'line 3',
|
|
|
|
'line 4',
|
|
|
|
'line 5',
|
|
|
|
'line 6',
|
|
|
|
'line 7',
|
|
|
|
'line 8',
|
|
|
|
'line 9',
|
|
|
|
'line 10',
|
|
|
|
);
|
|
|
|
$coverage = array(
|
2010-09-29 05:44:14 +00:00
|
|
|
1 => array(array('id' => 'HtmlCoverageReportTest::testGenerateDiff')),
|
|
|
|
2 => -2,
|
|
|
|
3 => array(array('id' => 'HtmlCoverageReportTest::testGenerateDiff')),
|
|
|
|
4 => array(array('id' => 'HtmlCoverageReportTest::testGenerateDiff')),
|
|
|
|
5 => -1,
|
|
|
|
6 => array(array('id' => 'HtmlCoverageReportTest::testGenerateDiff')),
|
|
|
|
7 => array(array('id' => 'HtmlCoverageReportTest::testGenerateDiff')),
|
|
|
|
8 => array(array('id' => 'HtmlCoverageReportTest::testGenerateDiff')),
|
|
|
|
9 => -1,
|
|
|
|
10 => array(array('id' => 'HtmlCoverageReportTest::testGenerateDiff'))
|
2010-05-09 04:04:03 +00:00
|
|
|
);
|
|
|
|
$result = $this->Coverage->generateDiff('myfile.php', $file, $coverage);
|
2011-11-12 03:13:20 +00:00
|
|
|
$this->assertRegExp('/myfile\.php Code coverage\: \d+\.?\d*\%/', $result);
|
2014-12-23 20:29:08 +00:00
|
|
|
$this->assertRegExp('/<div class="code-coverage-results" id\="coverage\-myfile\.php-' . md5('myfile.php') . '"/', $result);
|
2011-11-12 03:13:20 +00:00
|
|
|
$this->assertRegExp('/<pre>/', $result);
|
|
|
|
foreach ($file as $i => $line) {
|
|
|
|
$this->assertTrue(strpos($line, $result) !== 0, 'Content is missing ' . $i);
|
|
|
|
$class = 'covered';
|
|
|
|
if (in_array($i + 1, array(5, 9, 2))) {
|
|
|
|
$class = 'uncovered';
|
|
|
|
}
|
2014-04-29 12:19:33 +00:00
|
|
|
if ($i + 1 === 2) {
|
2011-11-12 03:13:20 +00:00
|
|
|
$class .= ' dead';
|
|
|
|
}
|
|
|
|
$this->assertTrue(strpos($class, $result) !== 0, 'Class name is wrong ' . $i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that coverage works with phpunit 3.6 as the data formats from coverage are totally different.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testPhpunit36Compatibility() {
|
|
|
|
$file = array(
|
|
|
|
'line 1',
|
|
|
|
'line 2',
|
|
|
|
'line 3',
|
|
|
|
'line 4',
|
|
|
|
'line 5',
|
|
|
|
'line 6',
|
|
|
|
'line 7',
|
|
|
|
'line 8',
|
|
|
|
'line 9',
|
|
|
|
'line 10',
|
|
|
|
);
|
|
|
|
$coverage = array(
|
|
|
|
1 => array('HtmlCoverageReportTest::testGenerateDiff'),
|
|
|
|
2 => null,
|
|
|
|
3 => array('HtmlCoverageReportTest::testGenerateDiff'),
|
|
|
|
4 => array('HtmlCoverageReportTest::testGenerateDiff'),
|
|
|
|
5 => array(),
|
|
|
|
6 => array('HtmlCoverageReportTest::testGenerateDiff'),
|
|
|
|
7 => array('HtmlCoverageReportTest::testGenerateDiff'),
|
|
|
|
8 => array('HtmlCoverageReportTest::testGenerateDiff'),
|
|
|
|
9 => array(),
|
|
|
|
10 => array('HtmlCoverageReportTest::testSomething', 'HtmlCoverageReportTest::testGenerateDiff')
|
|
|
|
);
|
|
|
|
|
|
|
|
$result = $this->Coverage->generateDiff('myfile.php', $file, $coverage);
|
2010-05-10 02:15:20 +00:00
|
|
|
$this->assertRegExp('/myfile\.php Code coverage\: \d+\.?\d*\%/', $result);
|
2014-12-23 20:29:08 +00:00
|
|
|
$this->assertRegExp('/<div class="code-coverage-results" id\="coverage\-myfile\.php-' . md5('myfile.php') . '"/', $result);
|
2010-05-09 04:04:03 +00:00
|
|
|
$this->assertRegExp('/<pre>/', $result);
|
|
|
|
foreach ($file as $i => $line) {
|
|
|
|
$this->assertTrue(strpos($line, $result) !== 0, 'Content is missing ' . $i);
|
2010-05-09 16:30:55 +00:00
|
|
|
$class = 'covered';
|
|
|
|
if (in_array($i + 1, array(5, 9, 2))) {
|
|
|
|
$class = 'uncovered';
|
|
|
|
}
|
2014-04-29 12:19:33 +00:00
|
|
|
if ($i + 1 === 2) {
|
2010-05-09 16:30:55 +00:00
|
|
|
$class .= ' dead';
|
2010-05-09 04:04:03 +00:00
|
|
|
}
|
|
|
|
$this->assertTrue(strpos($class, $result) !== 0, 'Class name is wrong ' . $i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-13 02:43:44 +00:00
|
|
|
/**
|
|
|
|
* test that covering methods show up as title attributes for lines.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testCoveredLinesTitleAttributes() {
|
2010-05-13 02:43:44 +00:00
|
|
|
$file = array(
|
|
|
|
'line 1',
|
|
|
|
'line 2',
|
|
|
|
'line 3',
|
|
|
|
'line 4',
|
|
|
|
'line 5',
|
|
|
|
);
|
|
|
|
|
|
|
|
$coverage = array(
|
2010-09-29 05:44:14 +00:00
|
|
|
1 => array(array('id' => 'HtmlCoverageReportTest::testAwesomeness')),
|
|
|
|
2 => -2,
|
|
|
|
3 => array(array('id' => 'HtmlCoverageReportTest::testCakeIsSuperior')),
|
|
|
|
4 => array(array('id' => 'HtmlCoverageReportTest::testOther')),
|
|
|
|
5 => -1
|
2010-05-13 02:43:44 +00:00
|
|
|
);
|
2010-09-29 05:44:14 +00:00
|
|
|
|
2010-05-13 02:43:44 +00:00
|
|
|
$result = $this->Coverage->generateDiff('myfile.php', $file, $coverage);
|
|
|
|
|
|
|
|
$this->assertTrue(
|
2010-09-29 05:44:14 +00:00
|
|
|
strpos($result, "title=\"Covered by:\nHtmlCoverageReportTest::testAwesomeness\n\"><span class=\"line-num\">1") !== false,
|
2010-05-13 02:43:44 +00:00
|
|
|
'Missing method coverage for line 1'
|
|
|
|
);
|
|
|
|
$this->assertTrue(
|
2010-09-29 05:44:14 +00:00
|
|
|
strpos($result, "title=\"Covered by:\nHtmlCoverageReportTest::testCakeIsSuperior\n\"><span class=\"line-num\">3") !== false,
|
2010-05-13 02:43:44 +00:00
|
|
|
'Missing method coverage for line 3'
|
|
|
|
);
|
|
|
|
$this->assertTrue(
|
2010-09-29 05:44:14 +00:00
|
|
|
strpos($result, "title=\"Covered by:\nHtmlCoverageReportTest::testOther\n\"><span class=\"line-num\">4") !== false,
|
2010-05-13 02:43:44 +00:00
|
|
|
'Missing method coverage for line 4'
|
|
|
|
);
|
|
|
|
$this->assertTrue(
|
|
|
|
strpos($result, "title=\"\"><span class=\"line-num\">5") !== false,
|
|
|
|
'Coverage report is wrong for line 5'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2010-05-08 21:59:14 +00:00
|
|
|
/**
|
2011-12-04 21:27:51 +00:00
|
|
|
* tearDown
|
2010-05-08 21:59:14 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function tearDown() {
|
2011-05-09 03:55:32 +00:00
|
|
|
CakePlugin::unload();
|
2010-05-08 21:59:14 +00:00
|
|
|
unset($this->Coverage);
|
2011-12-04 21:27:51 +00:00
|
|
|
parent::tearDown();
|
2010-05-08 21:59:14 +00:00
|
|
|
}
|
2011-04-17 10:35:21 +00:00
|
|
|
}
|