Skip to content

Commit

Permalink
Merge pull request #402 from wp-cli/fix/401
Browse files Browse the repository at this point in the history
  • Loading branch information
swissspidy authored May 21, 2024
2 parents 6d58aa5 + 0ed6262 commit c7ddfda
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 26 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ composer.lock
phpunit.xml
phpcs.xml
.phpcs.xml
.phpunit.result.cache
.phpunit.result.cache
.phpunit.cache
52 changes: 27 additions & 25 deletions tests/IterableCodeExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use WP_CLI\Tests\TestCase;
use WP_CLI\Utils;

require_once __DIR__ . '/includes/TestIterableCodeExtractor.php';

class IterableCodeExtractorTest extends TestCase {

/** @var string A path files are located */
Expand All @@ -19,7 +21,7 @@ public function set_up() {
*/
self::$base = Utils\normalize_path( __DIR__ ) . '/data/';

$property = new \ReflectionProperty( 'WP_CLI\I18n\IterableCodeExtractor', 'dir' );
$property = new \ReflectionProperty( TestIterableCodeExtractor::class, 'dir' );
$property->setAccessible( true );
$property->setValue( null, self::$base );
$property->setAccessible( false );
Expand All @@ -35,7 +37,7 @@ public function tear_down() {

public function test_can_include_files() {
$includes = [ 'foo-plugin', 'bar', 'baz/inc*.js' ];
$result = IterableCodeExtractor::getFilesFromDirectory( self::$base, $includes, [], [ 'php', 'js' ] );
$result = TestIterableCodeExtractor::getFilesFromDirectory( self::$base, $includes, [], [ 'php', 'js' ] );
$expected = static::$base . 'foo-plugin/foo-plugin.php';
$this->assertContains( $expected, $result );
$expected = static::$base . 'baz/includes/should_be_included.js';
Expand All @@ -45,69 +47,69 @@ public function test_can_include_files() {
}

public function test_can_include_empty_array() {
$result = IterableCodeExtractor::getFilesFromDirectory( self::$base, [], [], [ 'php', 'js' ] );
$result = TestIterableCodeExtractor::getFilesFromDirectory( self::$base, [], [], [ 'php', 'js' ] );
$expected_1 = static::$base . 'foo-plugin/foo-plugin.php';
$expected_2 = static::$base . 'baz/includes/should_be_included.js';
$this->assertContains( $expected_1, $result );
$this->assertContains( $expected_2, $result );
}

public function test_can_include_wildcard() {
$result = IterableCodeExtractor::getFilesFromDirectory( self::$base, [ '*' ], [], [ 'php', 'js' ] );
$result = TestIterableCodeExtractor::getFilesFromDirectory( self::$base, [ '*' ], [], [ 'php', 'js' ] );
$expected_1 = static::$base . 'foo-plugin/foo-plugin.php';
$expected_2 = static::$base . 'baz/includes/should_be_included.js';
$this->assertContains( $expected_1, $result );
$this->assertContains( $expected_2, $result );
}

public function test_can_include_subdirectories() {
$result = IterableCodeExtractor::getFilesFromDirectory( self::$base, [ 'foo/bar/*' ], [], [ 'php', 'js' ] );
$result = TestIterableCodeExtractor::getFilesFromDirectory( self::$base, [ 'foo/bar/*' ], [], [ 'php', 'js' ] );
$expected_1 = static::$base . 'foo/bar/foo/bar/foo/bar/deep_directory_also_included.php';
$expected_2 = static::$base . 'foo/bar/foofoo/included.js';
$this->assertContains( $expected_1, $result );
$this->assertContains( $expected_2, $result );
}

public function test_can_include_only_php() {
$result = IterableCodeExtractor::getFilesFromDirectory( self::$base, [ 'foo/bar/*' ], [], [ 'php' ] );
$result = TestIterableCodeExtractor::getFilesFromDirectory( self::$base, [ 'foo/bar/*' ], [], [ 'php' ] );
$expected_1 = static::$base . 'foo/bar/foo/bar/foo/bar/deep_directory_also_included.php';
$expected_2 = static::$base . 'foo/bar/foofoo/ignored.js';
$this->assertContains( $expected_1, $result );
$this->assertNotContains( $expected_2, $result );
}

public function test_can_exclude_override_wildcard() {
$result = IterableCodeExtractor::getFilesFromDirectory( self::$base, [ 'foo/bar/*' ], [ 'foo/bar/excluded/*' ], [ 'php' ] );
$result = TestIterableCodeExtractor::getFilesFromDirectory( self::$base, [ 'foo/bar/*' ], [ 'foo/bar/excluded/*' ], [ 'php' ] );
$expected_1 = static::$base . 'foo/bar/foo/bar/foo/bar/deep_directory_also_included.php';
$expected_2 = static::$base . 'foo/bar/excluded/excluded.js';
$this->assertContains( $expected_1, $result );
$this->assertNotContains( $expected_2, $result );
}

public function test_can_exclude_override_matching_directory() {
$result = IterableCodeExtractor::getFilesFromDirectory( self::$base, [ 'foo/bar/*' ], [ 'foo/bar/excluded/*' ], [ 'php' ] );
$result = TestIterableCodeExtractor::getFilesFromDirectory( self::$base, [ 'foo/bar/*' ], [ 'foo/bar/excluded/*' ], [ 'php' ] );
$expected_1 = static::$base . 'foo/bar/foo/bar/foo/bar/deep_directory_also_included.php';
$expected_2 = static::$base . 'foo/bar/excluded/excluded.js';
$this->assertContains( $expected_1, $result );
$this->assertNotContains( $expected_2, $result );
}

public function test_can_not_exclude_partially_directory() {
$result = IterableCodeExtractor::getFilesFromDirectory( self::$base, [ 'foo/bar/*' ], [ 'exc' ], [ 'js' ] );
$result = TestIterableCodeExtractor::getFilesFromDirectory( self::$base, [ 'foo/bar/*' ], [ 'exc' ], [ 'js' ] );
$expected_1 = static::$base . 'foo/bar/foo/bar/foo/bar/deep_directory_also_included.php';
$expected_2 = static::$base . 'foo/bar/excluded/ignored.js';
$this->assertNotContains( $expected_1, $result );
$this->assertContains( $expected_2, $result );
}

public function test_can_exclude_by_wildcard() {
$result = IterableCodeExtractor::getFilesFromDirectory( self::$base, [], [ '*' ], [ 'php', 'js' ] );
$result = TestIterableCodeExtractor::getFilesFromDirectory( self::$base, [], [ '*' ], [ 'php', 'js' ] );
$this->assertEmpty( $result );
}

public function test_can_exclude_files() {
$excludes = [ 'hoge' ];
$result = IterableCodeExtractor::getFilesFromDirectory( self::$base, [], $excludes, [ 'php', 'js' ] );
$result = TestIterableCodeExtractor::getFilesFromDirectory( self::$base, [], $excludes, [ 'php', 'js' ] );
$expected = static::$base . 'hoge/should_NOT_be_included.js';
$this->assertNotContains( $expected, $result );
}
Expand All @@ -116,13 +118,13 @@ public function test_can_override_exclude_by_include() {
// Overrides include option
$includes = [ 'excluded/ignored.js' ];
$excludes = [ 'excluded/*.js' ];
$result = IterableCodeExtractor::getFilesFromDirectory( self::$base, $includes, $excludes, [ 'php', 'js' ] );
$result = TestIterableCodeExtractor::getFilesFromDirectory( self::$base, $includes, $excludes, [ 'php', 'js' ] );
$expected = static::$base . 'foo/bar/excluded/ignored.js';
$this->assertContains( $expected, $result );
}

public function test_can_return_all_directory_files_sorted() {
$result = IterableCodeExtractor::getFilesFromDirectory( self::$base, [ '*' ], [], [ 'php', 'blade.php', 'js' ] );
$result = TestIterableCodeExtractor::getFilesFromDirectory( self::$base, [ '*' ], [], [ 'php', 'blade.php', 'js' ] );
$expected = array(
static::$base . 'baz/includes/should_be_included.js',
static::$base . 'foo-plugin/foo-plugin.php',
Expand All @@ -141,38 +143,38 @@ public function test_can_return_all_directory_files_sorted() {
public function test_can_include_file_in_excluded_folder() {
$includes = [ 'vendor/vendor-file.php' ];
$excludes = [ 'vendor' ];
$result = IterableCodeExtractor::getFilesFromDirectory( self::$base, $includes, $excludes, [ 'php', 'js' ] );
$result = TestIterableCodeExtractor::getFilesFromDirectory( self::$base, $includes, $excludes, [ 'php', 'js' ] );
$expected = static::$base . 'vendor/vendor-file.php';
$this->assertContains( $expected, $result );
}

public function test_can_include_folder_in_excluded_folder() {
$includes = [ 'vendor/vendor1' ];
$excludes = [ 'vendor' ];
$result = IterableCodeExtractor::getFilesFromDirectory( self::$base, $includes, $excludes, [ 'php', 'js' ] );
$result = TestIterableCodeExtractor::getFilesFromDirectory( self::$base, $includes, $excludes, [ 'php', 'js' ] );
$expected = static::$base . 'vendor/vendor1/vendor1-file.php';
$this->assertContains( $expected, $result );
}

public function test_can_include_file_in_excluded_folder_with_leading_slash() {
$includes = [ '/vendor/vendor-file.php' ];
$excludes = [ 'vendor' ];
$result = IterableCodeExtractor::getFilesFromDirectory( self::$base, $includes, $excludes, [ 'php', 'js' ] );
$result = TestIterableCodeExtractor::getFilesFromDirectory( self::$base, $includes, $excludes, [ 'php', 'js' ] );
$expected = static::$base . 'vendor/vendor-file.php';
$this->assertContains( $expected, $result );
}

public function test_can_include_file_in_excluded_folder_by_wildcard() {
$includes = [ 'vendor/**' ];
$excludes = [ 'vendor' ];
$result = IterableCodeExtractor::getFilesFromDirectory( self::$base, $includes, $excludes, [ 'php', 'js' ] );
$result = TestIterableCodeExtractor::getFilesFromDirectory( self::$base, $includes, $excludes, [ 'php', 'js' ] );
$expected = static::$base . 'vendor/vendor-file.php';
$this->assertContains( $expected, $result );
}

public function test_exclude_not_included_files() {
$includes = [ 'foo/bar/foo/bar/foo/bar/deep_directory_also_included.php' ];
$result = IterableCodeExtractor::getFilesFromDirectory( self::$base, $includes, [], [ 'php', 'js' ] );
$result = TestIterableCodeExtractor::getFilesFromDirectory( self::$base, $includes, [], [ 'php', 'js' ] );
$expected = array(
static::$base . 'foo/bar/foo/bar/foo/bar/deep_directory_also_included.php',
);
Expand All @@ -182,7 +184,7 @@ public function test_exclude_not_included_files() {
public function test_wildcard_exclude() {
$includes = [ 'foofoo/*' ];
$excludes = [ '*.min.js' ];
$result = IterableCodeExtractor::getFilesFromDirectory( self::$base, $includes, $excludes, [ 'php', 'js' ] );
$result = TestIterableCodeExtractor::getFilesFromDirectory( self::$base, $includes, $excludes, [ 'php', 'js' ] );
$expected = array(
static::$base . 'foo/bar/foofoo/included.js',
);
Expand All @@ -192,15 +194,15 @@ public function test_wildcard_exclude() {
public function test_identical_include_exclude() {
$includes = [ '*.min.js' ];
$excludes = [ '*.min.js' ];
$result = IterableCodeExtractor::getFilesFromDirectory( self::$base, $includes, $excludes, [ 'php', 'js' ] );
$result = TestIterableCodeExtractor::getFilesFromDirectory( self::$base, $includes, $excludes, [ 'php', 'js' ] );
$expected = array();
$this->assertEquals( $expected, $result );
}

public function test_can_include_file_in_symlinked_folder() {
symlink( self::$base . '/baz', self::$base . '/symlinked' );
$includes = [ 'symlinked/includes/should_be_included.js' ];
$result = IterableCodeExtractor::getFilesFromDirectory( self::$base, $includes, [], [ 'php', 'js' ] );
$result = TestIterableCodeExtractor::getFilesFromDirectory( self::$base, $includes, [], [ 'php', 'js' ] );
$expected = static::$base . 'symlinked/includes/should_be_included.js';
$this->assertContains( $expected, $result );
}
Expand All @@ -214,12 +216,12 @@ protected static function get_method_as_public( $class_name, $method_name ) {
}

protected static function file_get_extension_multi_invoke( $file ) {
$file_get_extension_multi_method = static::get_method_as_public( 'WP_CLI\I18n\IterableCodeExtractor', 'file_get_extension_multi' );
$file_get_extension_multi_method = static::get_method_as_public( TestIterableCodeExtractor::class, 'file_get_extension_multi' );
return $file_get_extension_multi_method->invokeArgs( null, [ $file ] );
}

protected static function file_has_file_extension_invoke( $file, $extensions ) {
$file_get_extension_multi_method = static::get_method_as_public( 'WP_CLI\I18n\IterableCodeExtractor', 'file_has_file_extension' );
$file_get_extension_multi_method = static::get_method_as_public( TestIterableCodeExtractor::class, 'file_has_file_extension' );
return $file_get_extension_multi_method->invokeArgs( null, [ $file, $extensions ] );
}

Expand All @@ -230,7 +232,7 @@ public function test_gets_file_extension_correctly( $rel_input_file, $expected_e
$this->assertEquals( static::file_get_extension_multi_invoke( new \SplFileObject( self::$base . $rel_input_file ) ), $expected_extension );
}

public function file_extension_extract_provider() {
public static function file_extension_extract_provider() {
return [
[ 'foo/bar/foofoo/included.js', 'js' ],
[ 'foo-plugin/foo-plugin.php', 'php' ],
Expand All @@ -245,7 +247,7 @@ public function test_matches_file_extensions_correctly( $rel_input_file, $matchi
$this->assertEquals( static::file_has_file_extension_invoke( new \SplFileObject( self::$base . $rel_input_file ), $matching_extensions ), $expected_result );
}

public function file_extensions_matches_provider() {
public static function file_extensions_matches_provider() {
return [
[ 'foo/bar/foofoo/included.js', [ 'js' ], true ],
[ 'foo/bar/foofoo/included.js', [ 'js', 'php', 'blade.php' ], true ],
Expand Down
9 changes: 9 additions & 0 deletions tests/includes/TestIterableCodeExtractor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace WP_CLI\I18n\Tests;

use WP_CLI\I18n\IterableCodeExtractor;

class TestIterableCodeExtractor {
use IterableCodeExtractor;
}

0 comments on commit c7ddfda

Please sign in to comment.