Skip to content

Commit 9799582

Browse files
authored
Merge pull request #668 from magento/new-codecoverage
MQE-2042: Port MFTF Coverage to Jenkins
2 parents cbd7c85 + fbcbe1a commit 9799582

File tree

3 files changed

+87
-1
lines changed

3 files changed

+87
-1
lines changed

etc/codecoverage/index.php

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
/**
3+
* Application entry point
4+
*
5+
* Example - run a particular store or website:
6+
* --------------------------------------------
7+
* require __DIR__ . '/app/bootstrap.php';
8+
* $params = $_SERVER;
9+
* $params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = 'website2';
10+
* $params[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] = 'website';
11+
* $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $params);
12+
* \/** @var \Magento\Framework\App\Http $app *\/
13+
* $app = $bootstrap->createApplication(\Magento\Framework\App\Http::class);
14+
* $bootstrap->run($app);
15+
* --------------------------------------------
16+
*
17+
* Copyright © Magento, Inc. All rights reserved.
18+
* See COPYING.txt for license details.
19+
*/
20+
21+
try {
22+
require __DIR__ . '/app/bootstrap.php';
23+
} catch (\Exception $e) {
24+
echo <<<HTML
25+
<div style="font:12px/1.35em arial, helvetica, sans-serif;">
26+
<div style="margin:0 0 25px 0; border-bottom:1px solid #ccc;">
27+
<h3 style="margin:0;font-size:1.7em;font-weight:normal;text-transform:none;text-align:left;color:#2f2f2f;">
28+
Autoload error</h3>
29+
</div>
30+
<p>{$e->getMessage()}</p>
31+
</div>
32+
HTML;
33+
exit(1);
34+
}
35+
36+
//Patch start
37+
$driver = new pcov\Clobber\Driver\PHPUnit6();
38+
$coverage = new \SebastianBergmann\CodeCoverage\CodeCoverage($driver);
39+
$coverage->filter()->addDirectoryToWhitelist("app/code/Magento/*");
40+
$coverage->filter()->removeDirectoryFromWhitelist("app/code/Magento/*/Test");
41+
$testName = "NO_TEST_NAME";
42+
if (file_exists(__DIR__ . '/CURRENT_TEST')) {
43+
$testName = file_get_contents(__DIR__ . '/CURRENT_TEST');
44+
}
45+
$id = !empty($testName) ? $testName : "NO_TEST_NAME";
46+
$coverage->start($id);
47+
//Patch end
48+
49+
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
50+
/** @var \Magento\Framework\App\Http $app */
51+
$app = $bootstrap->createApplication(\Magento\Framework\App\Http::class);
52+
$bootstrap->run($app);
53+
54+
// Patch start
55+
$coverage->stop();
56+
$writer = new \SebastianBergmann\CodeCoverage\Report\PHP();
57+
$writer->process($coverage, '/var/www/html/coverage/reports/' . $id . "_" . md5(mt_rand()) . '.cov');
58+
// Patch end

etc/codecoverage/test.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
8+
$test = $_GET['test'] ?? "NO_TEST_SPECIFIED";
9+
file_put_contents('CURRENT_TEST', $test);
10+
echo 'SET CURRENT TEST TO ' . $test;

src/Magento/FunctionalTestingFramework/Extension/TestContextExtension.php

+19-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ class TestContextExtension extends BaseExtension
3232
*/
3333
public static $events;
3434

35+
/**
36+
* The name of the currently running test
37+
* @var string
38+
*/
39+
public $currentTest;
40+
3541
/**
3642
* Initialize local vars
3743
*
@@ -55,8 +61,20 @@ public function _initialize()
5561
* @throws \Exception
5662
* @return void
5763
*/
58-
public function testStart()
64+
public function testStart(\Codeception\Event\TestEvent $e)
5965
{
66+
if (getenv('ENABLE_CODE_COVERAGE') === 'true') {
67+
// Curl against test.php and pass in the test name. Used when gathering code coverage.
68+
$this->currentTest = $e->getTest()->getMetadata()->getName();
69+
$cURLConnection = curl_init();
70+
curl_setopt_array($cURLConnection, [
71+
CURLOPT_RETURNTRANSFER => 1,
72+
CURLOPT_URL => getenv('MAGENTO_BASE_URL') . "/test.php?test=" . $this->currentTest,
73+
]);
74+
curl_exec($cURLConnection);
75+
curl_close($cURLConnection);
76+
}
77+
6078
PersistedObjectHandler::getInstance()->clearHookObjects();
6179
PersistedObjectHandler::getInstance()->clearTestObjects();
6280
}

0 commit comments

Comments
 (0)