jenkins-bot has submitted this change and it was merged. Change subject: Add tests for existing custom sniffs ......................................................................
Add tests for existing custom sniffs Change-Id: I875b366b5bb273323a5faedcd65f1c2978a33d70 --- A MediaWiki/Tests/MediaWikiStandardTest.php A MediaWiki/Tests/files/NamingConventions/case_global_name_fail.php A MediaWiki/Tests/files/NamingConventions/wg_global_name_fail.php A MediaWiki/Tests/files/generic_pass.php M README.md A TestHelper.php A phpunit.bootstrap.php A phpunit.xml.dist 8 files changed, 322 insertions(+), 1 deletion(-) Approvals: Hashar: Looks good to me, approved jenkins-bot: Verified diff --git a/MediaWiki/Tests/MediaWikiStandardTest.php b/MediaWiki/Tests/MediaWikiStandardTest.php new file mode 100644 index 0000000..e45aadb --- /dev/null +++ b/MediaWiki/Tests/MediaWikiStandardTest.php @@ -0,0 +1,92 @@ +<?php +/** + * This file was copied from CakePhps codesniffer tests before being modified + * https://github.com/cakephp/cakephp-codesniffer/blob/015919e55049e696eef9e518026e2d9abcdba722/CakePHP/tests/CakePHPStandardTest.php + * + * @license MIT + * CakePHP(tm) : The Rapid Development PHP Framework (http://cakephp.org) + * Copyright (c) 2005-2013, Cake Software Foundation, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * @author Adam Shorland + * Modifications + * - Rename appropriatly + * - Adapt $this->helper->runPhpCs call to pass second parameter $standard + */ +class MediaWikiStandardTest extends PHPUnit_Framework_TestCase { + + /** + * @var TestHelper + */ + private $helper; + + public function setUp() { + parent::setUp(); + if (empty($this->helper)) { + $this->helper = new TestHelper(); + } + } + + /** + * testFiles + * + * Run simple syntax checks, if the filename ends with pass.php - expect it to pass + */ + public static function testProvider() { + $tests = array(); + + $standard = dirname(dirname(__FILE__)); + + $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(dirname(__FILE__) . '/files')); + foreach ($iterator as $dir) { + if ($dir->isDir()) { + continue; + } + + $file = $dir->getPathname(); + $expectPass = (substr($file, -8) === 'pass.php'); + $tests[] = array( + $file, + $standard, + $expectPass + ); + } + return $tests; + } + + /** + * _testFile + * + * @dataProvider testProvider + * + * @param string $file + * @param string $standard + * @param boolean $expectPass + */ + public function testFile($file, $standard, $expectPass) { + $outputStr = $this->helper->runPhpCs($file, $standard); + if ($expectPass) { + $this->assertNotRegExp( + "/FOUND \d+ ERROR/", + $outputStr, + basename($file) . ' - expected to pass with no errors, some were reported. ' + ); + } else { + $this->assertRegExp( + "/FOUND \d+ ERROR/", + $outputStr, + basename($file) . ' - expected failures, none reported. ' + ); + } + } + +} \ No newline at end of file diff --git a/MediaWiki/Tests/files/NamingConventions/case_global_name_fail.php b/MediaWiki/Tests/files/NamingConventions/case_global_name_fail.php new file mode 100644 index 0000000..f6488a9 --- /dev/null +++ b/MediaWiki/Tests/files/NamingConventions/case_global_name_fail.php @@ -0,0 +1,6 @@ +<?php + +function fooFoo () { + // The below should have capital after wg + global $wgsomething; +} \ No newline at end of file diff --git a/MediaWiki/Tests/files/NamingConventions/wg_global_name_fail.php b/MediaWiki/Tests/files/NamingConventions/wg_global_name_fail.php new file mode 100644 index 0000000..ba12d41 --- /dev/null +++ b/MediaWiki/Tests/files/NamingConventions/wg_global_name_fail.php @@ -0,0 +1,6 @@ +<?php + +function fooFoo () { + // The below should start with wg... + global $someotherglobal; +} \ No newline at end of file diff --git a/MediaWiki/Tests/files/generic_pass.php b/MediaWiki/Tests/files/generic_pass.php new file mode 100644 index 0000000..e56b7a5 --- /dev/null +++ b/MediaWiki/Tests/files/generic_pass.php @@ -0,0 +1,103 @@ +<?php + +// Many of these code snippets are taken from: +// https://www.mediawiki.org/wiki/Manual:Coding_conventions + +/** + * @param $outputtype + * @param null $ts + * @return null + */ +function wfTimestampOrNull( $outputtype = TS_UNIX, $ts = null ) { + if ( is_null( $ts ) ) { + return null; + } else { + return wfTimestamp( $outputtype, $ts ); + } +} + +$wgAutopromote = array( + 'autoconfirmed' => array( '&', + array( APCOND_EDITCOUNT, &$wgAutoConfirmCount ), + array( APCOND_AGE, &$wgAutoConfirmAge ), + ), +); + +$namespaceNames = array( + NS_MEDIA => 'Media', + NS_SPECIAL => 'Special', + NS_MAIN => '', +); + +class FooBar extends BarBaz implements SomethingSomewhere { + + private $foo = 'halalalalalaa'; + + public $var; + + public function iDoCaseStuff( $word ) { + switch ( $word ) { + case 'lorem': + case 'ipsum': + $bar = 2; + break; + case 'dolor': + $bar = 3; + break; + default: + $bar = 0; + } + return strtolower( $bar ) == 'on' + || strtolower( $bar ) == 'true' + || strtolower( $bar ) == 'yes' + || preg_match( "/^\s*[+-]?0*[1-9]/", $bar ); + } + + public function iDoCaseStuffTwo( $word ) { + switch ( $word ) { + case 'lorem': + case 'ipsum': + $bar = 2; + break; + case 'dolor': + $bar = 3; + break; + default: + $bar = 0; + } + return $bar; + } + + public function fooBarBaz( $par ) { + global $wgBarBarBar, $wgUser; + + if ( $par ) { + return; + } + + $wgBarBarBar->dobar( + Xml::fieldset( wfMessage( 'importinterwiki' )->text() ) . + Xml::openElement( 'form', array( 'method' => 'post', 'action' => $par, + 'id' => 'mw-import-interwiki-form' ) ) . + wfMessage( 'import-interwiki-text' )->parse() . + Xml::hidden( 'action', 'submit' ) . + Xml::hidden( 'source', 'interwiki' ) . + Xml::hidden( 'editToken', $wgUser->editToken() ), + 'secondArgument' + ); + + $foo = $par; + return $foo + $wgBarBarBar + $this->foo; + } + + private function someFunction( FooBar $baz ) { + $foo = array( + $baz, + 'memememememememee', + ); + $cat = array_merge( $foo, array( 'barn', 'door' ) ); + return $cat; + } +} + +// This file has a new line at the end! diff --git a/README.md b/README.md index fef18f2..786d3c9 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,6 @@ * Actually implements the various conventions * Migrate the old code-utils/check-vars.php -* Write tests! [PHP CodeSniffer]: https://pear.php.net/package/PHP_CodeSniffer [MediaWiki conventions]: http://www.mediawiki.org/wiki/Manual:Coding_conventions diff --git a/TestHelper.php b/TestHelper.php new file mode 100644 index 0000000..acefc8a --- /dev/null +++ b/TestHelper.php @@ -0,0 +1,77 @@ +<?php +/** + * This file was copied from CakePhps codesniffer tests before being modified + * https://github.com/cakephp/cakephp-codesniffer/blob/7abb71ed17a52beb16e208d6964f7c9d967e639d/CakePHP/tests/TestHelper.php + * + * @license MIT + * CakePHP(tm) : The Rapid Development PHP Framework (http://cakephp.org) + * Copyright (c) 2005-2013, Cake Software Foundation, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * @author Adam Shorland + * Modifications + * - runPhpCs takes a second parameter $standard to override the default + */ + +class TestHelper { + + protected $_rootDir; + + protected $_dirName; + + protected $_phpcs; + + public function __construct() { + $this->_rootDir = dirname(dirname(__FILE__)); + $this->_dirName = basename($this->_rootDir); + $this->_phpcs = new PHP_CodeSniffer_CLI(); + } + + /** + * Run PHPCS on a file. + * + * @param string $file to run. + * @param string $standard to run against + * @return string The output from phpcs. + */ + public function runPhpCs($file, $standard = '') { + if( empty( $standard ) ){ + $standard = $this->_rootDir . '/ruleset.xml'; + } + $defaults = $this->_phpcs->getDefaults(); + //$standard = $this->_rootDir . '/ruleset.xml'; + if ( + defined('PHP_CodeSniffer::VERSION') && + version_compare(PHP_CodeSniffer::VERSION, '1.5.0') != -1 + ) { + $standard = array($standard); + } + $options = array( + 'encoding' => 'utf-8', + 'files' => array($file), + 'standard' => $standard, + ) + $defaults; + + // New PHPCS has a strange issue where the method arguments + // are not stored on the instance causing weird errors. + $reflection = new ReflectionProperty($this->_phpcs, 'values'); + $reflection->setAccessible(true); + $reflection->setValue($this->_phpcs, $options); + + ob_start(); + $this->_phpcs->process($options); + $result = ob_get_contents(); + ob_end_clean(); + return $result; + } + +} \ No newline at end of file diff --git a/phpunit.bootstrap.php b/phpunit.bootstrap.php new file mode 100644 index 0000000..ed9b63a --- /dev/null +++ b/phpunit.bootstrap.php @@ -0,0 +1,22 @@ +<?php + +// Load CodeSniffer CLI class +call_user_func( function() { + if ( !class_exists( 'PHP_CodeSniffer_CLI' ) ) { + $composer['local'] = dirname( dirname( dirname( __FILE__ ) ) ); + $composer['wmfjenkins'] = '/srv/deployment/integration/phpcs/'; + foreach( $composer as $location ) { + $location = $location . '/vendor/squizlabs/php_codesniffer/CodeSniffer/CLI.php'; + if( file_exists( $location ) ) { + require_once $location; + break; + } + } + if( !class_exists( 'PHP_CodeSniffer_CLI' ) ) { + require_once 'PHP/CodeSniffer/CLI.php'; + } + } +} ); + +// Load Test Helper +require_once dirname( __FILE__ ) . '/TestHelper.php'; diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..d056159 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,16 @@ +<?xml version="1.0" encoding="UTF-8"?> +<phpunit backupGlobals="true" + colors="false" + convertErrorsToExceptions="true" + convertNoticesToExceptions="true" + convertWarningsToExceptions="true" + mapTestClassNameToCoveredClassName="false" + strict="false" + verbose="false" + bootstrap="./phpunit.bootstrap.php"> + <testsuites> + <testsuite name="phpcs/MediaWiki"> + <file>./MediaWiki/Tests/MediaWikiStandardTest.php</file> + </testsuite> + </testsuites> +</phpunit> \ No newline at end of file -- To view, visit https://gerrit.wikimedia.org/r/154276 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I875b366b5bb273323a5faedcd65f1c2978a33d70 Gerrit-PatchSet: 5 Gerrit-Project: mediawiki/tools/codesniffer Gerrit-Branch: master Gerrit-Owner: Addshore <addshorew...@gmail.com> Gerrit-Reviewer: Hashar <has...@free.fr> Gerrit-Reviewer: Hoo man <h...@online.de> Gerrit-Reviewer: Krinkle <krinklem...@gmail.com> Gerrit-Reviewer: Legoktm <legoktm.wikipe...@gmail.com> Gerrit-Reviewer: Siebrand <siebr...@kitano.nl> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits