2014-03-27 48 views
1

我有在掃描目錄,並創建所有的子目錄的陣列的類的方法。這很簡單,效果很好。但是,我想爲這種方法添加一個單元測試,而且我很難弄清楚如何。PHP單元測試和嘲笑文件系統SCANDIR()

這裏是我的問題:我能創建使用vfsstream一個虛擬文件系統,它工作正常。但是,我無法將其傳遞給我的類以從中創建數組。它需要一個真實的目錄來掃描。我想測試一個受控目錄(顯然,我確切知道每次掃描的結果是什麼,所以我可以測試它)。生產中的掃描目錄可能會頻繁更改。

所以,我唯一能做的就是在我的測試文件夾中創建一個測試專用假目錄,該路徑傳遞給我的掃描儀,然後檢查它,我所知道的是在那個假目錄。這是最佳做法還是我錯過了什麼?

謝謝!

下面是一些代碼: 測試

function testPopulateAuto() 
{ 
    $c = new \Director\Core\Components\Components; 

    // The structure of the file system I am checking against. This is what I want to generate. 
    $check = array( 
     'TestFolder1', 
     'TestFolder2', 
    );  

    $path = dirname(__FILE__) . "/test-file-system/"; // Contains TestFolder1 and TestFolder1 
    $list = $c->generateList($path); // Scans the path and returns an array that should be identical to $check 

    $this->assertEquals($check, $list); 
} 

回答

1

很抱歉,如果我誤解了你的問題,但scandir應該使用自定義工作流。例如:

$structure = array(
     'tmp' => array(
       'music' => array(
         'wawfiles' => array(
           'mp3'      => array(), 
           'hello world.waw'   => 'nice song', 
           'abc.waw'     => 'bad song', 
           'put that cookie down.waw' => 'best song ever', 
           "zed's dead baby.waw"  => 'another cool song' 
         ) 
       ) 
     ) 
); 
$vfs = vfsStream::setup('root'); 
vfsStream::create($structure, $vfs); 

$music = vfsStream::url('root/tmp/music/wawfiles'); 

var_dump(scandir($music)); 

輸出:

array(5) { 
    [0]=> 
    string(7) "abc.waw" 
    [1]=> 
    string(15) "hello world.waw" 
    [2]=> 
    string(3) "mp3" 
    [3]=> 
    string(24) "put that cookie down.waw" 
    [4]=> 
    string(19) "zed's dead baby.waw" 
} 
+1

謝謝,我只是用錯了VFSstream。我沒有發現文檔非常有幫助。再次感謝! – Apollo