Skip to content

Commit

Permalink
Fix unit test for duplicated files
Browse files Browse the repository at this point in the history
  • Loading branch information
ernilambar committed Nov 22, 2024
1 parent df26c5e commit 05721c3
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions tests/phpunit/tests/Checker/Checks/File_Type_Check_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,30 +131,31 @@ public function test_run_with_badly_named_errors() {
public function test_run_with_duplicated_named_errors() {
// Initialize the Check_Context with a plugin path that mimics the directory structure.
$check_context = new Check_Context( UNIT_TESTS_PLUGIN_DIR . 'test-plugin-file-type-badly-named-files-errors/load.php' );

// Create an empty Check_Result instance for this context.
$check_result = new Check_Result( $check_context );

// Initialize the File_Type_Check instance.
$check = new File_Type_Check();

// Use reflection to make check_files accessible.
$reflection = new \ReflectionClass( $check );
$checkFilesMethod = $reflection->getMethod('check_files');
$checkFilesMethod->setAccessible(true);
// Use reflection to make protected method accessible.
$reflection = new ReflectionClass( $check );
$checkFilesMethod = $reflection->getMethod( 'look_for_badly_named_files' );
$checkFilesMethod->setAccessible( true );

// Define the custom file list with duplicate names as they would appear in a plugin directory.
$customFiles = [
UNIT_TESTS_PLUGIN_DIR . 'test-plugin-file-type-badly-named-files-errors/custom-file.php',
UNIT_TESTS_PLUGIN_DIR . 'test-plugin-file-type-badly-named-files-errors/Custom-File.php',
UNIT_TESTS_PLUGIN_DIR . 'test-plugin-file-type-badly-named-files-errors/custom-FILE.php'
];
$customFiles = array(
UNIT_TESTS_PLUGIN_DIR . 'test-plugin-file-type-badly-named-files-errors/custom-file.php',
UNIT_TESTS_PLUGIN_DIR . 'test-plugin-file-type-badly-named-files-errors/Custom-File.php',
UNIT_TESTS_PLUGIN_DIR . 'test-plugin-file-type-badly-named-files-errors/custom-FILE.php',
);

// Invoke check_files with the Check_Result instance and custom file list.
$result = $checkFilesMethod->invoke($check, $check_result, $customFiles);
// Invoke method with the Check_Result instance and custom file list.
$result = $checkFilesMethod->invoke( $check, $check_result, $customFiles );

$errors = $check_result->get_errors();

// Assert that check_files handles the custom file list with duplicates correctly.
$this->assertTrue($result->has_errors(), "The check_files method should detect errors for duplicate file names.");
$this->assertCount( 1, wp_list_filter( $errors['custom-file.php'][0][0], array( 'code' => 'duplicated_files' ) ) );
}

public function test_run_with_library_core_errors() {
Expand Down

0 comments on commit 05721c3

Please sign in to comment.