Skip to content

Commit

Permalink
Coding Standards: Fix the remaining issues in /tests.
Browse files Browse the repository at this point in the history
All PHP files in `/tests` now conform to the PHP coding standards, or have exceptions appropriately marked.

Travis now also runs `phpcs` on the `/tests` directory, any future changes to these files must conform entirely to the WordPress PHP coding standards. 🎉

See #47632.



git-svn-id: https://develop.svn.wordpress.org/trunk@45607 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
pento committed Jul 8, 2019
1 parent 431bc58 commit c6c7849
Show file tree
Hide file tree
Showing 91 changed files with 435 additions and 320 deletions.
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ matrix:
- php: 7.2
env: WP_TRAVISCI=e2e
- php: 7.2
env: WP_TRAVISCI=travis:format
env: WP_TRAVISCI=travis:phpcs
- php: 7.1
env: WP_TRAVISCI=travis:js
- php: 7.4snapshot
Expand Down Expand Up @@ -83,8 +83,8 @@ before_script:
esac
fi
- |
# We only need to run composer install on the code formatting job.
if [[ "$WP_TRAVISCI" == "travis:format" ]]; then
# We only need to run composer install on the PHP coding standards job.
if [[ "$WP_TRAVISCI" == "travis:phpcs" ]]; then
composer --version
travis_retry composer install
fi
Expand Down
23 changes: 22 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -1438,10 +1438,31 @@ module.exports = function(grunt) {
} );
} );

grunt.registerTask( 'lint:php', 'Runs the code linter on changed files.', function() {
var done = this.async();
var flags = this.flags;
var args = changedFiles.php;
if ( flags.travis ) {
args = [ 'tests' ];
}
args.unshift( 'lint' );
grunt.util.spawn( {
cmd: 'composer',
args: args,
opts: { stdio: 'inherit' }
}, function( error ) {
if ( flags.error && error ) {
done( false );
} else {
done( true );
}
} );
} );

// Travis CI tasks.
grunt.registerTask('travis:js', 'Runs Javascript Travis CI tasks.', [ 'jshint:corejs', 'qunit:compiled' ]);
grunt.registerTask('travis:phpunit', 'Runs PHPUnit Travis CI tasks.', [ 'build', 'phpunit' ]);
grunt.registerTask('travis:format', 'Runs Code formatting Travis CI tasks.', [ 'format:php:error' ]);
grunt.registerTask('travis:phpcs', 'Runs PHP Coding Standards Travis CI tasks.', [ 'format:php:error', 'lint:php:travis:error' ]);

// Patch task.
grunt.renameTask('patch_wordpress', 'patch');
Expand Down
34 changes: 33 additions & 1 deletion phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,39 @@
<!-- Whitelist test classes for select sniffs. -->
<rule ref="WordPress.Files.FileName">
<properties>
<property name="custom_test_class_whitelist" type="array" value="WP_UnitTestCase,WP_Ajax_UnitTestCase,WP_Canonical_UnitTestCase,WP_Test_REST_TestCase,WP_Test_REST_Controller_Testcase,WP_Test_REST_Post_Type_Controller_Testcase,WP_XMLRPC_UnitTestCase"/>
<property name="custom_test_class_whitelist" type="array">
<!-- Test case parent classes -->
<element value="WP_UnitTestCase"/>
<element value="WP_Ajax_UnitTestCase"/>
<element value="WP_Canonical_UnitTestCase"/>
<element value="WP_Test_REST_TestCase"/>
<element value="WP_Test_REST_Controller_Testcase"/>
<element value="WP_Test_REST_Post_Type_Controller_Testcase"/>
<element value="WP_XMLRPC_UnitTestCase"/>
<element value="WP_Filesystem_UnitTestCase"/>
<element value="WP_Image_UnitTestCase"/>
<element value="WP_HTTP_UnitTestCase"/>
<element value="WP_Tests_Image_Resize_UnitTestCase"/>
<element value="WP_Import_UnitTestCase"/>
<element value="Tests_Query_Conditionals"/>

<!-- Mock classes -->
<element value="Spy_REST_Server"/>
<element value="WP_REST_Test_Controller"/>
<element value="WP_Image_Editor_Mock"/>
<element value="WP_Filesystem_MockFS"/>
<element value="MockPHPMailer"/>
<element value="MockAction"/>
<element value="WP_Object_Cache"/>

<!-- PHPUnit helpers -->
<element value="TracTickets"/>
<element value="WP_PHPUnit_Util_Getopt"/>
<element value="PHPUnit_Util_Test"/>
<element value="WPProfiler"/>
<element value="SpeedTrapListener"/>
<element value="PHPUnit_Framework_Exception"/>
</property>
</properties>
</rule>

Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
<listener class="SpeedTrapListener" file="tests/phpunit/includes/listener-loader.php">
<arguments>
<array>
<element key="slowThreshold">
<element key="slow_threshold">
<integer>150</integer>
</element>
</array>
Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/includes/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
$_SERVER['PHP_SELF'] = '/index.php';

// Should we run in multisite mode?
$multisite = '1' == getenv( 'WP_MULTISITE' );
$multisite = ( '1' === getenv( 'WP_MULTISITE' ) );
$multisite = $multisite || ( defined( 'WP_TESTS_MULTISITE' ) && WP_TESTS_MULTISITE );
$multisite = $multisite || ( defined( 'MULTISITE' ) && MULTISITE );

Expand Down Expand Up @@ -191,7 +191,7 @@ function __construct( $argv ) {
}

foreach ( $skipped_groups as $group_name => $skipped ) {
if ( in_array( $group_name, $groups ) ) {
if ( in_array( $group_name, $groups, true ) ) {
$skipped_groups[ $group_name ] = false;
}
}
Expand Down
2 changes: 2 additions & 0 deletions tests/phpunit/includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,15 @@ function _delete_all_data() {
$wpdb->term_relationships,
$wpdb->termmeta,
) as $table ) {
//phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$wpdb->query( "DELETE FROM {$table}" );
}

foreach ( array(
$wpdb->terms,
$wpdb->term_taxonomy,
) as $table ) {
//phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$wpdb->query( "DELETE FROM {$table} WHERE term_id != 1" );
}

Expand Down
2 changes: 2 additions & 0 deletions tests/phpunit/includes/install.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,12 @@

$wpdb->query( 'SET foreign_key_checks = 0' );
foreach ( $wpdb->tables() as $table => $prefixed_table ) {
//phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$wpdb->query( "DROP TABLE IF EXISTS $prefixed_table" );
}

foreach ( $wpdb->tables( 'ms_global' ) as $table => $prefixed_table ) {
//phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$wpdb->query( "DROP TABLE IF EXISTS $prefixed_table" );

// We need to create references to ms global tables.
Expand Down
18 changes: 9 additions & 9 deletions tests/phpunit/includes/mock-fs.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ function setfs( $paths ) {

foreach ( $paths as $path ) {
// Allow for comments
if ( '#' == $path[0] ) {
if ( '#' === $path[0] ) {
continue;
}

// Directories
if ( '/' == $path[ strlen( $path ) - 1 ] ) {
if ( '/' === $path[ strlen( $path ) - 1 ] ) {
$this->mkdir( $path );
} else { // Files (with dummy content for now)
$this->put_contents( $path, 'This is a test file' );
Expand Down Expand Up @@ -161,7 +161,7 @@ function is_dir( $path ) {

function dirlist( $path = '.', $include_hidden = true, $recursive = false ) {

if ( empty( $path ) || '.' == $path ) {
if ( empty( $path ) || '.' === $path ) {
$path = $this->cwd();
}

Expand All @@ -177,23 +177,23 @@ function dirlist( $path = '.', $include_hidden = true, $recursive = false ) {

$ret = array();
foreach ( $this->fs_map[ $path ]->children as $entry ) {
if ( '.' == $entry->name || '..' == $entry->name ) {
if ( '.' === $entry->name || '..' === $entry->name ) {
continue;
}

if ( ! $include_hidden && '.' == $entry->name ) {
if ( ! $include_hidden && '.' === $entry->name ) {
continue;
}

if ( $limit_file && $entry->name != $limit_file ) {
if ( $limit_file && $entry->name !== $limit_file ) {
continue;
}

$struc = array();
$struc['name'] = $entry->name;
$struc['type'] = $entry->type;

if ( 'd' == $struc['type'] ) {
if ( 'd' === $struc['type'] ) {
if ( $recursive ) {
$struc['files'] = $this->dirlist( trailingslashit( $path ) . trailingslashit( $struc['name'] ), $include_hidden, $recursive );
} else {
Expand All @@ -219,11 +219,11 @@ function __construct( $path ) {
}

function is_file() {
return $this->type == 'f';
return 'f' === $this->type;
}

function is_dir() {
return $this->type == 'd';
return 'd' === $this->type;
}
}

Expand Down
Loading

0 comments on commit c6c7849

Please sign in to comment.