|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +require_once __DIR__ . '/vendor/autoload.php'; |
| 8 | + |
| 9 | +/** |
| 10 | + * The main entrypoint. Called at the bottom of this file. |
| 11 | + */ |
| 12 | +function main($filepath, $runID, $testType) { |
| 13 | + readCoverageFromFolder($filepath, $runID, $testType); |
| 14 | +} |
| 15 | + |
| 16 | +/** |
| 17 | + * Reads each .cov file in a directory and inserts records into the db define din db.class.php |
| 18 | + * |
| 19 | + * @param string $filepath |
| 20 | + * @param string $runID |
| 21 | + * @return void |
| 22 | + */ |
| 23 | +function readCoverageFromFolder($filepath, $runID, $testType) { |
| 24 | + require_once __DIR__ . DIRECTORY_SEPARATOR . 'db.class.php'; |
| 25 | + $start_time = microtime(TRUE); |
| 26 | + //INPUT RUN IF NOT PRESENT |
| 27 | + $run = DB::query("SELECT * FROM CC_RUN WHERE runid=%s", $runID); |
| 28 | + if (empty($run)) { |
| 29 | + DB::insert("CC_RUN", [ |
| 30 | + "runid" => $runID |
| 31 | + ]); |
| 32 | + $cachedCCRUNID = DB::insertId(); |
| 33 | + } else { |
| 34 | + $cachedCCRUNID = $run[0]['ccrunid']; |
| 35 | + } |
| 36 | + unset($run); |
| 37 | + |
| 38 | + //CACHE LOCAL IDS FOR EASY USE |
| 39 | + $cachedTests = []; |
| 40 | + $tests = DB::query("SELECT * FROM CC_TESTS WHERE ccrunid=%s AND testtype=%s", $cachedCCRUNID, $testType); |
| 41 | + foreach ($tests as $test) { |
| 42 | + $cachedTests[$test['testname']] = $test['testid']; |
| 43 | + } |
| 44 | + unset($tests); |
| 45 | + $cachedFiles = []; |
| 46 | + $files = DB::query("SELECT * FROM CC_FILES WHERE ccrunid=%s", $cachedCCRUNID); |
| 47 | + foreach ($files as $file) { |
| 48 | + $cachedFiles[$file['filepath']] = $file['fileid']; |
| 49 | + } |
| 50 | + unset($files); |
| 51 | + |
| 52 | + //ITERATE THROUGH FILES |
| 53 | + printf("Reading {$filepath}\r"); |
| 54 | + $fileCoverage = readCoverage($filepath); |
| 55 | + |
| 56 | + //TEST NAME INSERT |
| 57 | + foreach ($fileCoverage->getTests() as $testname => $content) { |
| 58 | + //found in initial cache |
| 59 | + if (isset($cachedTests[$testname])) { |
| 60 | + continue; |
| 61 | + } |
| 62 | + $existingTest = DB::query( |
| 63 | + "SELECT * FROM CC_TESTS WHERE ccrunid=%s AND testtype=%s AND testname=%s", |
| 64 | + $cachedCCRUNID, $testType, $testname |
| 65 | + ); |
| 66 | + //not found in initial cache but found in DB |
| 67 | + if (!empty($existingTest)) { |
| 68 | + $cachedTests[$existingTest[0]['testname']] = $existingTest[0]['testid']; |
| 69 | + continue; |
| 70 | + } |
| 71 | + //actual new record |
| 72 | + DB::insert("CC_TESTS", [ |
| 73 | + "testtype" => $testType, |
| 74 | + "testname" => $testname, |
| 75 | + "ccrunid" => $cachedCCRUNID |
| 76 | + ]); |
| 77 | + $cachedTests[$testname] = DB::insertId(); |
| 78 | + } |
| 79 | + |
| 80 | + //DO FILE + LINE INSERTS |
| 81 | + foreach ($fileCoverage->getData(true)->lineCoverage() as $testFile => $content) { |
| 82 | + // not found in initial cache |
| 83 | + if (!isset($cachedFiles[$testFile])) { |
| 84 | + $existingFile = DB::query( |
| 85 | + "SELECT * FROM CC_FILES WHERE ccrunid=%s AND filepath=%s", |
| 86 | + $cachedCCRUNID, $testFile |
| 87 | + ); |
| 88 | + // found in DB |
| 89 | + if (!empty($existingFile)) { |
| 90 | + $cachedFiles[$existingFile[0]['filepath']] = $existingFile[0]['fileid']; |
| 91 | + } else { |
| 92 | + // actual new record |
| 93 | + DB::insert("CC_FILES", [ |
| 94 | + "filepath" => $testFile, |
| 95 | + "ccrunid" => $cachedCCRUNID |
| 96 | + ]); |
| 97 | + $cachedFiles[$testFile] = DB::insertId(); |
| 98 | + } |
| 99 | + } |
| 100 | + foreach ($content as $lineNumber => $tests) { |
| 101 | + foreach ($tests as $test) { |
| 102 | + $existingLine = DB::query( |
| 103 | + "SELECT * FROM CC_LINES WHERE linenumber=%s AND testid=%s AND fileid=%s AND ccrunid=%s", |
| 104 | + $lineNumber, $cachedTests[$test], $cachedFiles[$testFile], $cachedCCRUNID |
| 105 | + ); |
| 106 | + if (empty($existingLine)) { |
| 107 | + DB::insert("CC_LINES", [ |
| 108 | + "linenumber" => $lineNumber, |
| 109 | + "testid" => $cachedTests[$test], |
| 110 | + "fileid" => $cachedFiles[$testFile], |
| 111 | + "ccrunid" => $cachedCCRUNID |
| 112 | + ]); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + printf("Inserted from file {$filepath}\n"); |
| 118 | + |
| 119 | + $endtime = microtime(TRUE); |
| 120 | + $runtime = $endtime-$start_time; |
| 121 | + printf("Execution took {$runtime} seconds.\n"); |
| 122 | +} |
| 123 | + |
| 124 | +/** |
| 125 | + * Reads a single .cov file into memory. |
| 126 | + */ |
| 127 | +function readCoverage($coveragePath) { |
| 128 | + if (!is_file($coveragePath)) { |
| 129 | + return null; |
| 130 | + } |
| 131 | + $file = include($coveragePath); |
| 132 | + return $file; |
| 133 | +} |
| 134 | + |
| 135 | +if (!array_key_exists(3, $argv)) { |
| 136 | + printf("This script requires 3 parameters to run:\nFilePath RunID TestType"); |
| 137 | +} else { |
| 138 | + main(filter_var($argv[1], FILTER_SANITIZE_STRING),filter_var($argv[2], FILTER_SANITIZE_STRING),filter_var($argv[3], FILTER_SANITIZE_STRING)); |
| 139 | +} |
0 commit comments