PageRenderTime 37ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/dev/tests/static/testsuite/Php/LiveCodeTest.php

https://bitbucket.org/sunil_nextbits/magento2
PHP | 123 lines | 66 code | 8 blank | 49 comment | 6 complexity | 4a0c4a0523385ff8af3ae7f99598dfb0 MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category tests
  22. * @package static
  23. * @copyright Copyright (c) 2012 X.commerce, Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Set of tests for static code analysis, e.g. code style, code complexity, copy paste detecting, etc.
  28. */
  29. class Php_LiveCodeTest extends PHPUnit_Framework_TestCase
  30. {
  31. /**
  32. * @var string
  33. */
  34. protected static $_reportDir = '';
  35. /**
  36. * @var array
  37. */
  38. protected static $_whiteList = array();
  39. /**
  40. * @var array
  41. */
  42. protected static $_blackList = array();
  43. public static function setUpBeforeClass()
  44. {
  45. self::$_reportDir = Utility_Files::init()->getPathToSource() . '/dev/tests/static/report';
  46. if (!is_dir(self::$_reportDir)) {
  47. mkdir(self::$_reportDir, 0777);
  48. }
  49. self::$_whiteList = self::_readLists(__DIR__ . '/_files/whitelist/*.txt');
  50. self::$_blackList = self::_readLists(__DIR__ . '/_files/blacklist/*.txt');
  51. }
  52. public function testCodeStyle()
  53. {
  54. $reportFile = self::$_reportDir . '/phpcs_report.xml';
  55. $wrapper = new CodingStandard_Tool_CodeSniffer_Wrapper();
  56. $codeSniffer = new CodingStandard_Tool_CodeSniffer(realpath(__DIR__ . '/_files/phpcs'), $reportFile, $wrapper);
  57. if (!$codeSniffer->canRun()) {
  58. $this->markTestSkipped('PHP Code Sniffer is not installed.');
  59. }
  60. $result = $codeSniffer->run(self::$_whiteList, self::$_blackList, array('php', 'phtml'));
  61. $this->assertEquals(0, $result,
  62. "PHP Code Sniffer has found $result error(s): See detailed report in $reportFile"
  63. );
  64. }
  65. public function testCodeMess()
  66. {
  67. $reportFile = self::$_reportDir . '/phpmd_report.xml';
  68. $cmd = new Inspection_MessDetector_Command(realpath(__DIR__ . '/_files/phpmd/ruleset.xml'), $reportFile);
  69. if (!$cmd->canRun()) {
  70. $this->markTestSkipped('PHP Mess Detector command line is not available.');
  71. }
  72. $this->assertTrue($cmd->run(self::$_whiteList, self::$_blackList), $cmd->getLastRunMessage());
  73. }
  74. public function testCopyPaste()
  75. {
  76. $reportFile = self::$_reportDir . '/phpcpd_report.xml';
  77. $cmd = new Inspection_CopyPasteDetector_Command($reportFile);
  78. if (!$cmd->canRun()) {
  79. $this->markTestSkipped('PHP Copy/Paste Detector command line is not available.');
  80. }
  81. $this->assertTrue($cmd->run(self::$_whiteList, self::$_blackList), $cmd->getLastRunMessage());
  82. }
  83. /**
  84. * Read all text files by specified glob pattern and combine them into an array of valid files/directories
  85. *
  86. * The Magento root path is prepended to all (non-empty) entries
  87. *
  88. * @param string $globPattern
  89. * @return array
  90. * @throws Exception if any of the patterns don't return any result
  91. */
  92. protected static function _readLists($globPattern)
  93. {
  94. $patterns = array();
  95. foreach (glob($globPattern) as $list) {
  96. $patterns = array_merge($patterns, file($list, FILE_IGNORE_NEW_LINES));
  97. }
  98. $result = array();
  99. foreach ($patterns as $pattern) {
  100. if (0 === strpos($pattern, '#')) {
  101. continue;
  102. }
  103. /**
  104. * Note that glob() for directories will be returned as is,
  105. * but passing directory is supported by the tools (phpcpd, phpmd, phpcs)
  106. */
  107. $files = glob(Utility_Files::init()->getPathToSource() . '/' . $pattern, GLOB_BRACE);
  108. if (empty($files)) {
  109. throw new Exception("The glob() pattern '{$pattern}' didn't return any result.");
  110. }
  111. $result = array_merge($result, $files);
  112. }
  113. return $result;
  114. }
  115. }