Skip to content

Commit

Permalink
Add CLI command PHPUnit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JDGrimes authored and JDGrimes committed Nov 12, 2013
1 parent 5e964e8 commit 3a657a4
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 0 deletions.
22 changes: 22 additions & 0 deletions tests/data/no-config/no-config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/**
* Plugin Name: No config
*/

function do_some_things() {

global $wpdb;

return $wpdb->get_results( 'SELECT * FROM ' . $wpdb->posts );
}

function do_some_other_stuff() {

echo 'Hello world';
}

function display_message() {

_e( 'Message', 'textdomain' );
}
24 changes: 24 additions & 0 deletions tests/data/with-config/with-config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/**
* Plugin Name: With config
*/

function do_some_things() {

global $wpdb;

return $wpdb->get_results( 'SELECT * FROM ' . $wpdb->posts );
}

function do_some_other_stuff() {

echo 'Hello world';
echo 'Dlrow olleh';
}

function display_message() {

_e( 'Message', 'textdomain' );
func_to_ignore( 'yes' );
}
10 changes: 10 additions & 0 deletions tests/data/with-config/wp-l10n-validator.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"textdomain":"textdomain",
"config":"wordpress",
"ignored-functions": {
"func_to_ignore":true
},
"ignored-strings": [
"Dlrow olleh"
]
}
108 changes: 108 additions & 0 deletions tests/tests/cli.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

/**
* Test case for bash commands.
*
* @package WP_L10n_Validator
* @since 0.1.1
*/

/**
* Test that the bash commands function properly.
*
* @since 0.1.1
* @group dev
*/
class WP_L10n_Validator_CLI_Test extends PHPUnit_Framework_TestCase {

/**
* Test default CLI usage.
*
* @since 0.1.1
*/
public function test_default_usage() {

$output = $this->run_command( 'wp-l10n-validator', '/no-config' );
$this->assertEquals( 0, strpos( $output, 'Usage:' ) );

$output = $this->run_command( 'wp-l10n-validator textdomain', '/no-config' );
$this->assertEquals( "/no-config.php#16: Non gettexted string 'Hello world'", $output );

$output = $this->run_command( 'wp-l10n-validator textdomain default', '/no-config' );
$this->assertEquals(
"/no-config.php#11 \$wpdb->get_results( 1 ): Non gettexted string 'SELECT * FROM'"
. "\n/no-config.php#16: Non gettexted string 'Hello world'"
. "\n/no-config.php#21 _e( 1 ): Non gettexted string 'Message'"
. "\n/no-config.php#21 _e( 2 ): Non gettexted string 'textdomain'"
, $output
);
}

/**
* Test usage with JSON config.
*
* @since 0.1.1
*/
public function test_with_json_config() {

$output = $this->run_command( 'wp-l10n-validator', '/with-config' );
$this->assertEquals( "/with-config.php#16: Non gettexted string 'Hello world'", $output );
}

/**
* Text ignores cache generation.
*
* @since 0.1.1
*/
public function test_ignores_cache_generation() {

$output = $this->run_command( 'wp-l10n-validator -c', '/with-config' );
$this->assertEmpty( $output );

$ignores_cache = dirname( __DIR__ ) . '/data/with-config/wp-l10n-validator-ignores.cache';

if ( ! ($content = @file_get_contents( $ignores_cache )) )
$this->fail( 'The ignores cache file was not generated, or could not be read.' );

unlink( $ignores_cache );

$this->assertEquals( array( '/with-config.php' => array( 'Hello world' => array( 16 => false ) ) ), json_decode( $content, true ) );
}

/**
* Run a command and return the output.
*
* @since 0.1.1
*
* @param string $command The command to run.
* @param string $working_dir The current working directory.
*
* @return string The result of the command.
*/
protected function run_command( $command, $working_dir ) {

$command = dirname( dirname( __DIR__ ) ) . '/bin/' . $command;
$working_dir = dirname( __DIR__ ) . '/data' . $working_dir;

$process = proc_open( $command, array( 2 => array( 'pipe', 'w' ) ), $pipes, $working_dir );

if ( is_resource( $process ) ) {

while ( ($proc_status = proc_get_status( $process )) && $proc_status['running'] ) {

usleep( 10000 );
}

$output = stream_get_contents( $pipes[2] );

fclose( $pipes[2] );
proc_close( $process );

@unlink( $working_dir . '/wp-l10n-validator.cache' );

return trim( $output );
}

$this->markTestSkipped( 'Unable to open the process with proc_open()' );
}
}

0 comments on commit 3a657a4

Please sign in to comment.