Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Static Public Member Functions | Static Protected Attributes
LiveCodeTest Class Reference
Inheritance diagram for LiveCodeTest:

Public Member Functions

 testCodeStyle ()
 
 testCodeMess ()
 
 testCopyPaste ()
 
 testStrictTypes ()
 

Static Public Member Functions

static setUpBeforeClass ()
 
static getWhitelist ( $fileTypes=['php'], $changedFilesBaseDir='', $baseFilesFolder='', $whitelistFile='/_files/whitelist/common.txt')
 

Static Protected Attributes

static $reportDir = ''
 
static $pathToSource = ''
 

Detailed Description

Set of tests for static code analysis, e.g. code style, code complexity, copy paste detecting, etc.

Definition at line 20 of file LiveCodeTest.php.

Member Function Documentation

◆ getWhitelist()

static getWhitelist (   $fileTypes = ['php'],
  $changedFilesBaseDir = '',
  $baseFilesFolder = '',
  $whitelistFile = '/_files/whitelist/common.txt' 
)
static

Returns whitelist based on blacklist and git changed files

Parameters
array$fileTypes
string$changedFilesBaseDir
string$baseFilesFolder
string$whitelistFile
Returns
array

Definition at line 75 of file LiveCodeTest.php.

80  {
81  $changedFiles = self::getChangedFilesList($changedFilesBaseDir);
82  if (empty($changedFiles)) {
83  return [];
84  }
85 
86  $globPatternsFolder = ('' !== $baseFilesFolder) ? $baseFilesFolder : self::getBaseFilesFolder();
87  try {
88  $directoriesToCheck = Files::init()->readLists($globPatternsFolder . $whitelistFile);
89  } catch (\Exception $e) {
90  // no directories matched white list
91  return [];
92  }
93  $targetFiles = self::filterFiles($changedFiles, $fileTypes, $directoriesToCheck);
94  return $targetFiles;
95  }

◆ setUpBeforeClass()

static setUpBeforeClass ( )
static

Setup basics for all tests

Returns
void

Definition at line 37 of file LiveCodeTest.php.

38  {
39  self::$pathToSource = BP;
40  self::$reportDir = self::$pathToSource . '/dev/tests/static/report';
41  if (!is_dir(self::$reportDir)) {
42  mkdir(self::$reportDir);
43  }
44  }
const BP
Definition: autoload.php:14
mkdir($pathname, $mode=0777, $recursive=false, $context=null)
Definition: ioMock.php:25

◆ testCodeMess()

testCodeMess ( )

Test code quality using phpmd

Definition at line 282 of file LiveCodeTest.php.

283  {
284  $reportFile = self::$reportDir . '/phpmd_report.txt';
285  $codeMessDetector = new CodeMessDetector(realpath(__DIR__ . '/_files/phpmd/ruleset.xml'), $reportFile);
286 
287  if (!$codeMessDetector->canRun()) {
288  $this->markTestSkipped('PHP Mess Detector is not available.');
289  }
290 
291  $result = $codeMessDetector->run(self::getWhitelist(['php']));
292 
293  $output = "";
294  if (file_exists($reportFile)) {
295  $output = file_get_contents($reportFile);
296  }
297 
298  $this->assertEquals(
299  Command::EXIT_SUCCESS,
300  $result,
301  "PHP Code Mess has found error(s):" . PHP_EOL . $output
302  );
303 
304  // delete empty reports
305  if (file_exists($reportFile)) {
306  unlink($reportFile);
307  }
308  }
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60

◆ testCodeStyle()

testCodeStyle ( )

Test code quality using phpcs

Definition at line 262 of file LiveCodeTest.php.

263  {
264  $isFullScan = defined('TESTCODESTYLE_IS_FULL_SCAN') && TESTCODESTYLE_IS_FULL_SCAN === '1';
265  $reportFile = self::$reportDir . '/phpcs_report.txt';
266  if (!file_exists($reportFile)) {
267  touch($reportFile);
268  }
269  $codeSniffer = new CodeSniffer('Magento', $reportFile, new Wrapper());
270  $result = $codeSniffer->run($isFullScan ? $this->getFullWhitelist() : self::getWhitelist(['php', 'phtml']));
271  $report = file_get_contents($reportFile);
272  $this->assertEquals(
273  0,
274  $result,
275  "PHP Code Sniffer detected {$result} violation(s): " . PHP_EOL . $report
276  );
277  }

◆ testCopyPaste()

testCopyPaste ( )

Test code quality using phpcpd

Definition at line 313 of file LiveCodeTest.php.

314  {
315  $reportFile = self::$reportDir . '/phpcpd_report.xml';
316  $copyPasteDetector = new CopyPasteDetector($reportFile);
317 
318  if (!$copyPasteDetector->canRun()) {
319  $this->markTestSkipped('PHP Copy/Paste Detector is not available.');
320  }
321 
322  $blackList = [];
323  foreach (glob(__DIR__ . '/_files/phpcpd/blacklist/*.txt') as $list) {
324  $blackList = array_merge($blackList, file($list, FILE_IGNORE_NEW_LINES));
325  }
326 
327  $copyPasteDetector->setBlackList($blackList);
328 
329  $result = $copyPasteDetector->run([BP]);
330 
331  $output = "";
332  if (file_exists($reportFile)) {
333  $output = file_get_contents($reportFile);
334  }
335 
336  $this->assertTrue(
337  $result,
338  "PHP Copy/Paste Detector has found error(s):" . PHP_EOL . $output
339  );
340  }
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60
const BP
Definition: autoload.php:14

◆ testStrictTypes()

testStrictTypes ( )

Tests whitelisted files for strict type declarations.

Definition at line 345 of file LiveCodeTest.php.

346  {
347  $changedFiles = self::getAddedFilesList('');
348 
349  try {
350  $blackList = Files::init()->readLists(
351  self::getBaseFilesFolder() . '/_files/blacklist/strict_type.txt'
352  );
353  } catch (\Exception $e) {
354  // nothing matched black list
355  $blackList = [];
356  }
357 
358  $toBeTestedFiles = array_diff(
359  self::filterFiles($changedFiles, ['php'], []),
360  $blackList
361  );
362 
363  $filesMissingStrictTyping = [];
364  foreach ($toBeTestedFiles as $fileName) {
365  $file = file_get_contents($fileName);
366  if (strstr($file, 'strict_types=1') === false) {
367  $filesMissingStrictTyping[] = $fileName;
368  }
369  }
370 
371  $this->assertEquals(
372  0,
373  count($filesMissingStrictTyping),
374  "Following files are missing strict type declaration:"
375  . PHP_EOL
376  . implode(PHP_EOL, $filesMissingStrictTyping)
377  );
378  }
$fileName
Definition: translate.phtml:15

Field Documentation

◆ $pathToSource

$pathToSource = ''
staticprotected

Definition at line 30 of file LiveCodeTest.php.

◆ $reportDir

$reportDir = ''
staticprotected

Definition at line 25 of file LiveCodeTest.php.


The documentation for this class was generated from the following file: