function ScanDirectoryTest::testOptionKey

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/File/ScanDirectoryTest.php \Drupal\KernelTests\Core\File\ScanDirectoryTest::testOptionKey()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/File/ScanDirectoryTest.php \Drupal\KernelTests\Core\File\ScanDirectoryTest::testOptionKey()
  3. 10 core/tests/Drupal/KernelTests/Core/File/ScanDirectoryTest.php \Drupal\KernelTests\Core\File\ScanDirectoryTest::testOptionKey()

Check that key parameter sets the return value's key.

@covers ::scanDirectory

File

core/tests/Drupal/KernelTests/Core/File/ScanDirectoryTest.php, line 118

Class

ScanDirectoryTest
Tests <a href="/api/drupal/core%21lib%21Drupal%21Core%21File%21FileSystem.php/class/FileSystem/11.x" title="Provides helpers to operate on files and stream wrappers." class="local">\Drupal\Core\File\FileSystem</a>::scanDirectory.

Namespace

Drupal\KernelTests\Core\File

Code

public function testOptionKey() : void {
    // "filename", for the path starting with $dir.
    $expected = [
        $this->path . '/javascript-1.txt',
        $this->path . '/javascript-2.script',
    ];
    $actual = array_keys($this->fileSystem
        ->scanDirectory($this->path, '/^javascript-/', [
        'key' => 'filepath',
    ]));
    sort($actual);
    $this->assertEquals($expected, $actual, 'Returned the correct values for the filename key.');
    // "basename", for the basename of the file.
    $expected = [
        'javascript-1.txt',
        'javascript-2.script',
    ];
    $actual = array_keys($this->fileSystem
        ->scanDirectory($this->path, '/^javascript-/', [
        'key' => 'filename',
    ]));
    sort($actual);
    $this->assertEquals($expected, $actual, 'Returned the correct values for the basename key.');
    // "name" for the name of the file without an extension.
    $expected = [
        'javascript-1',
        'javascript-2',
    ];
    $actual = array_keys($this->fileSystem
        ->scanDirectory($this->path, '/^javascript-/', [
        'key' => 'name',
    ]));
    sort($actual);
    $this->assertEquals($expected, $actual, 'Returned the correct values for the name key.');
    // Invalid option that should default back to "filename".
    $expected = [
        $this->path . '/javascript-1.txt',
        $this->path . '/javascript-2.script',
    ];
    $actual = array_keys($this->fileSystem
        ->scanDirectory($this->path, '/^javascript-/', [
        'key' => 'INVALID',
    ]));
    sort($actual);
    $this->assertEquals($expected, $actual, 'An invalid key defaulted back to the default.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.