Legoktm has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/401823 )

Change subject: Add sniff to use namespaced PHPUnit\Framework\TestCase
......................................................................

Add sniff to use namespaced PHPUnit\Framework\TestCase

PHPUnit 6 introduced namespaces, but those changes were backported to
PHPUnit 4 to make the migration easier.

In preparation for the PHPUnit version upgrade, move to the namespaced
version. This requires a minimum of PHPUnit 4.8.35/5.4.3, which
MediaWiki has been using for a while now.

Bug: T184137
Change-Id: I1021dba1a74a4afcf2aa4fe33232e06fdedf271f
---
A MediaWiki/Sniffs/Usage/PHPUnitClassUsageSniff.php
M MediaWiki/Tests/MediaWikiStandardTest.php
A MediaWiki/Tests/files/Usage/phpunit_usage.php
A MediaWiki/Tests/files/Usage/phpunit_usage.php.expect
A MediaWiki/Tests/files/Usage/phpunit_usage.php.fixed
A MediaWiki/Tests/files/Usage/phpunit_usage2.php
A MediaWiki/Tests/files/Usage/phpunit_usage2.php.expect
A MediaWiki/Tests/files/Usage/phpunit_usage2.php.fixed
A MediaWiki/Tests/files/Usage/phpunit_usage3.php
A MediaWiki/Tests/files/Usage/phpunit_usage3.php.expect
A MediaWiki/Tests/files/Usage/phpunit_usage3.php.fixed
M composer.json
12 files changed, 161 insertions(+), 2 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/tools/codesniffer 
refs/changes/23/401823/1

diff --git a/MediaWiki/Sniffs/Usage/PHPUnitClassUsageSniff.php 
b/MediaWiki/Sniffs/Usage/PHPUnitClassUsageSniff.php
new file mode 100644
index 0000000..4c646be
--- /dev/null
+++ b/MediaWiki/Sniffs/Usage/PHPUnitClassUsageSniff.php
@@ -0,0 +1,78 @@
+<?php
+/**
+ * Copyright (C) 2018 Kunal Mehta <lego...@member.fsf.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+
+namespace MediaWiki\Sniffs\Usage;
+
+use PHP_CodeSniffer\Files\File;
+use PHP_CodeSniffer\Sniffs\Sniff;
+
+/**
+ * Converts PHPUnit_Framework_TestCase to the new
+ * PHPUnit 6 namespaced PHPUnit\Framework\Testcase
+ *
+ * The namespaced classes were backported in 4.8.35,
+ * so this is compatible with 4.8.35+ and 5.4.3+
+ */
+class PHPUnitClassUsageSniff implements Sniff {
+       /**
+        * Only look for classes that extend something
+        *
+        * @return array|int[]
+        */
+       public function register() {
+               return [ T_EXTENDS ];
+       }
+
+       /**
+        * @param File $phpcsFile File object
+        * @param int $stackPtr Position of extends token
+        */
+       public function process( File $phpcsFile, $stackPtr ) {
+               $tokens = $phpcsFile->getTokens();
+               $classPtr = $phpcsFile->findPrevious( T_CLASS, $stackPtr );
+               if ( $classPtr === false ) {
+                       // interface Foo extends .. which we don't care about
+                       return;
+               }
+               $phpunitPtr = $phpcsFile->findNext( T_STRING, $stackPtr );
+               $phpunitToken = $tokens[$phpunitPtr];
+               if ( $phpunitToken['content'] !== 'PHPUnit_Framework_TestCase' 
) {
+                       return;
+               }
+
+               $fix = $phpcsFile->addFixableWarning(
+                       'Namespaced PHPUnit TestCase class should be used 
instead',
+                       $phpunitPtr,
+                       'NotNamespaced'
+               );
+               if ( $fix ) {
+                       $new = 'PHPUnit\\Framework\\TestCase';
+                       // If this file is namespaced, we need a leading \
+                       $inANamespace = $phpcsFile->findPrevious( T_NAMESPACE, 
$classPtr ) !== false;
+                       $classNameWithSlash = 
$phpcsFile->findExtendedClassName( $classPtr );
+                       // But make sure it doesn't already have a slash...
+                       $hashLeadingSlash = $classNameWithSlash[0] === '\\';
+                       if ( $inANamespace && !$hashLeadingSlash ) {
+                               $new = '\\' . $new;
+                       }
+                       $phpcsFile->fixer->replaceToken( $phpunitPtr, $new );
+               }
+       }
+}
diff --git a/MediaWiki/Tests/MediaWikiStandardTest.php 
b/MediaWiki/Tests/MediaWikiStandardTest.php
index 8a28bb3..b1295af 100644
--- a/MediaWiki/Tests/MediaWikiStandardTest.php
+++ b/MediaWiki/Tests/MediaWikiStandardTest.php
@@ -20,7 +20,7 @@
 
 namespace MediaWiki\Sniffs\Tests;
 
-class MediaWikiStandardTest extends \PHPUnit_Framework_TestCase {
+class MediaWikiStandardTest extends \PHPUnit\Framework\TestCase {
 
        /**
         * @var Helper
diff --git a/MediaWiki/Tests/files/Usage/phpunit_usage.php 
b/MediaWiki/Tests/files/Usage/phpunit_usage.php
new file mode 100644
index 0000000..9f5d77f
--- /dev/null
+++ b/MediaWiki/Tests/files/Usage/phpunit_usage.php
@@ -0,0 +1,13 @@
+<?php
+
+class Test1 extends PHPUnit_Framework_TestCase {
+}
+
+class Test2 extends PHPUnit\Framework\TestCase {
+}
+
+class Test3 extends \PHPUnit_Framework_TestCase {
+}
+
+class Test4 extends \PHPUnit\Framework\TestCase {
+}
diff --git a/MediaWiki/Tests/files/Usage/phpunit_usage.php.expect 
b/MediaWiki/Tests/files/Usage/phpunit_usage.php.expect
new file mode 100644
index 0000000..f2adea7
--- /dev/null
+++ b/MediaWiki/Tests/files/Usage/phpunit_usage.php.expect
@@ -0,0 +1,13 @@
+  3 | WARNING | [x] Namespaced PHPUnit TestCase class should be used
+    |         |     instead
+    |         |     (MediaWiki.Usage.PHPUnitClassUsage.NotNamespaced)
+  6 | ERROR   | [ ] Only one object structure is allowed in a file
+    |         |     (Generic.Files.OneObjectStructurePerFile.MultipleFound)
+  9 | ERROR   | [ ] Only one object structure is allowed in a file
+    |         |     (Generic.Files.OneObjectStructurePerFile.MultipleFound)
+  9 | WARNING | [x] Namespaced PHPUnit TestCase class should be used
+    |         |     instead
+    |         |     (MediaWiki.Usage.PHPUnitClassUsage.NotNamespaced)
+ 12 | ERROR   | [ ] Only one object structure is allowed in a file
+    |         |     (Generic.Files.OneObjectStructurePerFile.MultipleFound)
+PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY
\ No newline at end of file
diff --git a/MediaWiki/Tests/files/Usage/phpunit_usage.php.fixed 
b/MediaWiki/Tests/files/Usage/phpunit_usage.php.fixed
new file mode 100644
index 0000000..b3a6a93
--- /dev/null
+++ b/MediaWiki/Tests/files/Usage/phpunit_usage.php.fixed
@@ -0,0 +1,13 @@
+<?php
+
+class Test1 extends PHPUnit\Framework\TestCase {
+}
+
+class Test2 extends PHPUnit\Framework\TestCase {
+}
+
+class Test3 extends \PHPUnit\Framework\TestCase {
+}
+
+class Test4 extends \PHPUnit\Framework\TestCase {
+}
diff --git a/MediaWiki/Tests/files/Usage/phpunit_usage2.php 
b/MediaWiki/Tests/files/Usage/phpunit_usage2.php
new file mode 100644
index 0000000..308fc98
--- /dev/null
+++ b/MediaWiki/Tests/files/Usage/phpunit_usage2.php
@@ -0,0 +1,9 @@
+<?php
+
+namespace Something\Test;
+
+class Test1 extends \PHPUnit_Framework_TestCase {
+}
+
+class Test2 extends \PHPUnit\Framework\TestCase {
+}
diff --git a/MediaWiki/Tests/files/Usage/phpunit_usage2.php.expect 
b/MediaWiki/Tests/files/Usage/phpunit_usage2.php.expect
new file mode 100644
index 0000000..269c09e
--- /dev/null
+++ b/MediaWiki/Tests/files/Usage/phpunit_usage2.php.expect
@@ -0,0 +1,6 @@
+ 5 | WARNING | [x] Namespaced PHPUnit TestCase class should be used
+   |         |     instead
+   |         |     (MediaWiki.Usage.PHPUnitClassUsage.NotNamespaced)
+ 8 | ERROR   | [ ] Only one object structure is allowed in a file
+   |         |     (Generic.Files.OneObjectStructurePerFile.MultipleFound)
+PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
\ No newline at end of file
diff --git a/MediaWiki/Tests/files/Usage/phpunit_usage2.php.fixed 
b/MediaWiki/Tests/files/Usage/phpunit_usage2.php.fixed
new file mode 100644
index 0000000..3ff9cdc
--- /dev/null
+++ b/MediaWiki/Tests/files/Usage/phpunit_usage2.php.fixed
@@ -0,0 +1,9 @@
+<?php
+
+namespace Something\Test;
+
+class Test1 extends \PHPUnit\Framework\TestCase {
+}
+
+class Test2 extends \PHPUnit\Framework\TestCase {
+}
diff --git a/MediaWiki/Tests/files/Usage/phpunit_usage3.php 
b/MediaWiki/Tests/files/Usage/phpunit_usage3.php
new file mode 100644
index 0000000..534f609
--- /dev/null
+++ b/MediaWiki/Tests/files/Usage/phpunit_usage3.php
@@ -0,0 +1,8 @@
+<?php
+
+namespace Something;
+
+use PHPUnit_Framework_TestCase;
+
+class Test1 extends PHPUnit_Framework_TestCase {
+}
diff --git a/MediaWiki/Tests/files/Usage/phpunit_usage3.php.expect 
b/MediaWiki/Tests/files/Usage/phpunit_usage3.php.expect
new file mode 100644
index 0000000..8e92c36
--- /dev/null
+++ b/MediaWiki/Tests/files/Usage/phpunit_usage3.php.expect
@@ -0,0 +1,4 @@
+ 7 | WARNING | [x] Namespaced PHPUnit TestCase class should be used
+   |         |     instead
+   |         |     (MediaWiki.Usage.PHPUnitClassUsage.NotNamespaced)
+PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
\ No newline at end of file
diff --git a/MediaWiki/Tests/files/Usage/phpunit_usage3.php.fixed 
b/MediaWiki/Tests/files/Usage/phpunit_usage3.php.fixed
new file mode 100644
index 0000000..e2ccfd1
--- /dev/null
+++ b/MediaWiki/Tests/files/Usage/phpunit_usage3.php.fixed
@@ -0,0 +1,6 @@
+<?php
+
+namespace Something;
+
+class Test1 extends \PHPUnit\Framework\TestCase {
+}
diff --git a/composer.json b/composer.json
index 417d4a7..a22de9a 100644
--- a/composer.json
+++ b/composer.json
@@ -11,7 +11,7 @@
        "require-dev": {
                "jakub-onderka/php-parallel-lint": "0.9.2",
                "jakub-onderka/php-console-highlighter": "0.3.2",
-               "phpunit/phpunit": "~4.8"
+               "phpunit/phpunit": "4.8.36"
        },
        "scripts": {
                "test": [

-- 
To view, visit https://gerrit.wikimedia.org/r/401823
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I1021dba1a74a4afcf2aa4fe33232e06fdedf271f
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/tools/codesniffer
Gerrit-Branch: master
Gerrit-Owner: Legoktm <lego...@member.fsf.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to