Php 如何测试使用存储外观的类?
在我制作的Laravel5包中,有一个类FileSelector,它在某个方法中使用存储外观Php 如何测试使用存储外观的类?,php,unit-testing,laravel,phpunit,orchestra,Php,Unit Testing,Laravel,Phpunit,Orchestra,在我制作的Laravel5包中,有一个类FileSelector,它在某个方法中使用存储外观 public function filterFilesOnDate($files, DateTime $date) { return array_filter($files, function($file) use($date){ return Storage::lastModified($file) < $date->getTimeStamp(); });
public function filterFilesOnDate($files, DateTime $date)
{
return array_filter($files, function($file) use($date){
return Storage::lastModified($file) < $date->getTimeStamp();
});
}
失败的测试是:
public function test_if_files_are_filtered_on_date()
{
$files = Storage::allFiles('tests/_data/backups');
$filteredFiles = $this->fileSelector->filterFilesOnDate($files, $this->date);
}
存储::allFiles'tests/_data/backups'完全不返回任何文件。
路径是正确的,因为使用文件外观会返回所需的文件,但这与filterFilesOnDate方法不兼容,因为它使用存储
使用文件facade会生成以下错误:
League\Flysystem\FileNotFoundException: File not found at tests/_data/backups/ElvisPresley.zip
我在测试中是否使用了错误的存储方法,或者我是否偶然发现了Orchestra/Testbench的局限性?好的,事实证明我没有完全理解存储和磁盘是如何工作的 使用诸如Storage::lastModified之类的方法调用文件系统配置中指定的默认文件系统 因为这是一个测试,所以没有配置 Storage::disk所做的是使用文件系统对象创建FilesystemAdapter的实例,因此需要“重新创建”存储对象 因此: 变成:
$this->disk = new Illuminate\Filesystem\FilesystemAdapter(
new Filesystem(new Local($this->root))
);
$this->fileSelector = new FileSelector($this->disk, $this->path);
$this->path是我用于测试的文件存储的路径
还有人向我指出,我应该在每次运行测试时手动设置lastModified时间戳,以避免不同的测试结果
foreach (scandir($this->testFilesPath) as $file)
{
touch($this->testFilesPath . '/' . $file, time() - (60 * 60 * 24 * 5));
}
使用触摸屏,您可以创建文件或设置文件的时间戳。在本例中,它们被设置为5天。好的,我没有完全理解存储和磁盘是如何工作的 使用诸如Storage::lastModified之类的方法调用文件系统配置中指定的默认文件系统 因为这是一个测试,所以没有配置 Storage::disk所做的是使用文件系统对象创建FilesystemAdapter的实例,因此需要“重新创建”存储对象 因此: 变成:
$this->disk = new Illuminate\Filesystem\FilesystemAdapter(
new Filesystem(new Local($this->root))
);
$this->fileSelector = new FileSelector($this->disk, $this->path);
$this->path是我用于测试的文件存储的路径
还有人向我指出,我应该在每次运行测试时手动设置lastModified时间戳,以避免不同的测试结果
foreach (scandir($this->testFilesPath) as $file)
{
touch($this->testFilesPath . '/' . $file, time() - (60 * 60 * 24 * 5));
}
使用触摸屏,您可以创建文件或设置文件的时间戳。在这种情况下,它们被设置为5天