diff --git a/.gitignore b/.gitignore index c3dfaf42..ca74f0da 100644 --- a/.gitignore +++ b/.gitignore @@ -11,4 +11,5 @@ composer.lock phpunit.xml phpcs.xml .phpcs.xml -.phpunit.result.cache +.phpunit.result.cache +.phpunit.cache diff --git a/tests/IterableCodeExtractorTest.php b/tests/IterableCodeExtractorTest.php index 21351218..1d770e7d 100644 --- a/tests/IterableCodeExtractorTest.php +++ b/tests/IterableCodeExtractorTest.php @@ -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 */ @@ -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 ); @@ -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'; @@ -45,7 +47,7 @@ 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 ); @@ -53,7 +55,7 @@ public function test_can_include_empty_array() { } 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 ); @@ -61,7 +63,7 @@ public function test_can_include_wildcard() { } 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 ); @@ -69,7 +71,7 @@ public function test_can_include_subdirectories() { } 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 ); @@ -77,7 +79,7 @@ public function test_can_include_only_php() { } 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 ); @@ -85,7 +87,7 @@ public function test_can_exclude_override_wildcard() { } 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 ); @@ -93,7 +95,7 @@ public function test_can_exclude_override_matching_directory() { } 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 ); @@ -101,13 +103,13 @@ public function test_can_not_exclude_partially_directory() { } 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 ); } @@ -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', @@ -141,7 +143,7 @@ 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 ); } @@ -149,7 +151,7 @@ public function test_can_include_file_in_excluded_folder() { 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 ); } @@ -157,7 +159,7 @@ public function test_can_include_folder_in_excluded_folder() { 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 ); } @@ -165,14 +167,14 @@ public function test_can_include_file_in_excluded_folder_with_leading_slash() { 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', ); @@ -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', ); @@ -192,7 +194,7 @@ 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 ); } @@ -200,7 +202,7 @@ public function test_identical_include_exclude() { 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 ); } @@ -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 ] ); } @@ -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' ], @@ -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 ], diff --git a/tests/includes/TestIterableCodeExtractor.php b/tests/includes/TestIterableCodeExtractor.php new file mode 100644 index 00000000..2476d105 --- /dev/null +++ b/tests/includes/TestIterableCodeExtractor.php @@ -0,0 +1,9 @@ +