Skip to content
This repository has been archived by the owner on Dec 19, 2019. It is now read-only.

API functional tests coverage

Tomash Khamlai edited this page Oct 3, 2019 · 20 revisions

Pay attention:

  • xdebug must be installed

Create directories for reports and for the rendered output

Navigate to the folder where magento2 was installed and execute next command:

cd /var/www/html/graphql-ce;
mkdir -p var/coverage/{reports,html}

Optionally give permissions for that folder. Assuming you are using Apache2 on Ubuntu you can do it next way:

source /etc/apache2/envvars \
&& chown ${USER}:${APACHE_RUN_USER} var/coverage/{reports,html} \
&& chmod 777 var/coverage/{reports,html}

Modify index.php

Apache2

On Apache2 you should replace the content of your /var/www/html/graphql-ce/index.php with next code snippet:

<?php
/**
 * Public alias for the application entry point
 *
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

use Magento\Framework\App\Bootstrap;
use Magento\Framework\App\Filesystem\DirectoryList;

try {
    require __DIR__ . '/app/bootstrap.php';
} catch (\Exception $e) {
    echo <<<HTML
<div style="font:12px/1.35em arial, helvetica, sans-serif;">
    <div style="margin:0 0 25px 0; border-bottom:1px solid #ccc;">
        <h3 style="margin:0;font-size:1.7em;font-weight:normal;text-transform:none;text-align:left;color:#2f2f2f;">
        Autoload error</h3>
    </div>
    <p>{$e->getMessage()}</p>
</div>
HTML;
    exit(1);
}

// Patch start
$coverage = new \SebastianBergmann\CodeCoverage\CodeCoverage;
$coverage->filter()->addDirectoryToWhitelist('app/code/Magento/*GraphQl');
$id = md5(mt_rand());
$coverage->start($id);
// Patch end

$params = $_SERVER;
$params[Bootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS] = array_replace_recursive(
    $params[Bootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS] ?? [],
    [
        DirectoryList::PUB => [DirectoryList::URL_PATH => 'pub'],
        DirectoryList::MEDIA => [DirectoryList::URL_PATH => 'pub/media'],
        DirectoryList::STATIC_VIEW => [DirectoryList::URL_PATH => 'pub/static'],
        DirectoryList::UPLOAD => [DirectoryList::URL_PATH => 'pub/media/upload'],
    ]
);
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $params);
/** @var \Magento\Framework\App\Http $app */
$app = $bootstrap->createApplication(\Magento\Framework\App\Http::class);
$bootstrap->run($app);

// Patch start
$coverage->stop();
$writer = new \SebastianBergmann\CodeCoverage\Report\PHP();
$writer->process($coverage, 'var/coverage/reports/' . $id . '.cov');
// Patch end

Nginx

you should replace the content of your /var/www/html/graphql-ce/pub/index.php (or index.php according to nginx configuration) with next code snippet:

<?php
/**
 * Public alias for the application entry point
 *
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

use Magento\Framework\App\Bootstrap;
use Magento\Framework\App\Filesystem\DirectoryList;

try {
    require __DIR__ . '/../app/bootstrap.php';
} catch (\Exception $e) {
    echo <<<HTML
<div style="font:12px/1.35em arial, helvetica, sans-serif;">
    <div style="margin:0 0 25px 0; border-bottom:1px solid #ccc;">
        <h3 style="margin:0;font-size:1.7em;font-weight:normal;text-transform:none;text-align:left;color:#2f2f2f;">
        Autoload error</h3>
    </div>
    <p>{$e->getMessage()}</p>
</div>
HTML;
    exit(1);
}

// Patch start
$coverage = new \SebastianBergmann\CodeCoverage\CodeCoverage;
$coverage->filter()->addDirectoryToWhitelist('../app/code/Magento/*GraphQl');
$id = md5(mt_rand());
$coverage->start($id);
// Patch end

$params = $_SERVER;
$params[Bootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS] = array_replace_recursive(
    $params[Bootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS] ?? [],
    [
        DirectoryList::PUB => [DirectoryList::URL_PATH => ''],
        DirectoryList::MEDIA => [DirectoryList::URL_PATH => 'media'],
        DirectoryList::STATIC_VIEW => [DirectoryList::URL_PATH => 'static'],
        DirectoryList::UPLOAD => [DirectoryList::URL_PATH => 'media/upload'],
    ]
);
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $params);
/** @var \Magento\Framework\App\Http $app */
$app = $bootstrap->createApplication(\Magento\Framework\App\Http::class);
$bootstrap->run($app);

// Patch start
$coverage->stop();
$writer = new \SebastianBergmann\CodeCoverage\Report\PHP();
$writer->process($coverage, '../var/coverage/reports/' . $id . '.cov');
// Patch end

Install Magento and configure your instance to run api-functional tests

Payment Gateways

Copy mocks for Payment Gateways and upgrade your instance:

cp dev/tests/api-functional/_files/Magento/{TestModuleAuthorizenetAcceptjs,TestModuleBraintree,TestModuleFedex,TestModuleUsps} app/code/Magento/ -rv;

rm -rf var/* generated/* -v;
php bin/magento setup:upgrade;
php bin/magento cache:flush

Run tests on a local machine.

Should be run only tests from dev/tests/api-functional/testsuite/Magento/GraphQl/*. As result will be generated coverage reports in '../var/coverage/reports/*' for each request from tests.

Example:

php `pwd`/vendor/phpunit/phpunit/phpunit \
--configuration `pwd`/dev/tests/api-functional/phpunit_graphql.xml \
`pwd`/dev/tests/api-functional/testsuite/Magento/GraphQl/DownloadableProduct

Merge all of reports in one in HTML format.

$ cd var/coverage
$ wget https://phar.phpunit.de/phpcov.phar
$ php phpcov.phar merge ./reports --html=./html

Look at results

firefox html/index.html &