/dev/tests/static/testsuite/Php/LiveCodeTest.php
PHP | 123 lines | 66 code | 8 blank | 49 comment | 6 complexity | 4a0c4a0523385ff8af3ae7f99598dfb0 MD5 | raw file
- <?php
- /**
- * Magento
- *
- * NOTICE OF LICENSE
- *
- * This source file is subject to the Open Software License (OSL 3.0)
- * that is bundled with this package in the file LICENSE.txt.
- * It is also available through the world-wide-web at this URL:
- * http://opensource.org/licenses/osl-3.0.php
- * If you did not receive a copy of the license and are unable to
- * obtain it through the world-wide-web, please send an email
- * to license@magentocommerce.com so we can send you a copy immediately.
- *
- * DISCLAIMER
- *
- * Do not edit or add to this file if you wish to upgrade Magento to newer
- * versions in the future. If you wish to customize Magento for your
- * needs please refer to http://www.magentocommerce.com for more information.
- *
- * @category tests
- * @package static
- * @copyright Copyright (c) 2012 X.commerce, Inc. (http://www.magentocommerce.com)
- * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
- */
- /**
- * Set of tests for static code analysis, e.g. code style, code complexity, copy paste detecting, etc.
- */
- class Php_LiveCodeTest extends PHPUnit_Framework_TestCase
- {
- /**
- * @var string
- */
- protected static $_reportDir = '';
- /**
- * @var array
- */
- protected static $_whiteList = array();
- /**
- * @var array
- */
- protected static $_blackList = array();
- public static function setUpBeforeClass()
- {
- self::$_reportDir = Utility_Files::init()->getPathToSource() . '/dev/tests/static/report';
- if (!is_dir(self::$_reportDir)) {
- mkdir(self::$_reportDir, 0777);
- }
- self::$_whiteList = self::_readLists(__DIR__ . '/_files/whitelist/*.txt');
- self::$_blackList = self::_readLists(__DIR__ . '/_files/blacklist/*.txt');
- }
- public function testCodeStyle()
- {
- $reportFile = self::$_reportDir . '/phpcs_report.xml';
- $wrapper = new CodingStandard_Tool_CodeSniffer_Wrapper();
- $codeSniffer = new CodingStandard_Tool_CodeSniffer(realpath(__DIR__ . '/_files/phpcs'), $reportFile, $wrapper);
- if (!$codeSniffer->canRun()) {
- $this->markTestSkipped('PHP Code Sniffer is not installed.');
- }
- $result = $codeSniffer->run(self::$_whiteList, self::$_blackList, array('php', 'phtml'));
- $this->assertEquals(0, $result,
- "PHP Code Sniffer has found $result error(s): See detailed report in $reportFile"
- );
- }
- public function testCodeMess()
- {
- $reportFile = self::$_reportDir . '/phpmd_report.xml';
- $cmd = new Inspection_MessDetector_Command(realpath(__DIR__ . '/_files/phpmd/ruleset.xml'), $reportFile);
- if (!$cmd->canRun()) {
- $this->markTestSkipped('PHP Mess Detector command line is not available.');
- }
- $this->assertTrue($cmd->run(self::$_whiteList, self::$_blackList), $cmd->getLastRunMessage());
- }
- public function testCopyPaste()
- {
- $reportFile = self::$_reportDir . '/phpcpd_report.xml';
- $cmd = new Inspection_CopyPasteDetector_Command($reportFile);
- if (!$cmd->canRun()) {
- $this->markTestSkipped('PHP Copy/Paste Detector command line is not available.');
- }
- $this->assertTrue($cmd->run(self::$_whiteList, self::$_blackList), $cmd->getLastRunMessage());
- }
- /**
- * Read all text files by specified glob pattern and combine them into an array of valid files/directories
- *
- * The Magento root path is prepended to all (non-empty) entries
- *
- * @param string $globPattern
- * @return array
- * @throws Exception if any of the patterns don't return any result
- */
- protected static function _readLists($globPattern)
- {
- $patterns = array();
- foreach (glob($globPattern) as $list) {
- $patterns = array_merge($patterns, file($list, FILE_IGNORE_NEW_LINES));
- }
- $result = array();
- foreach ($patterns as $pattern) {
- if (0 === strpos($pattern, '#')) {
- continue;
- }
- /**
- * Note that glob() for directories will be returned as is,
- * but passing directory is supported by the tools (phpcpd, phpmd, phpcs)
- */
- $files = glob(Utility_Files::init()->getPathToSource() . '/' . $pattern, GLOB_BRACE);
- if (empty($files)) {
- throw new Exception("The glob() pattern '{$pattern}' didn't return any result.");
- }
- $result = array_merge($result, $files);
- }
- return $result;
- }
- }