Skip to content

Instantly share code, notes, and snippets.

@recca0120
Created November 25, 2016 10:22
Show Gist options
  • Save recca0120/ab9d7f418fcbf6de4fda21351a939260 to your computer and use it in GitHub Desktop.
Save recca0120/ab9d7f418fcbf6de4fda21351a939260 to your computer and use it in GitHub Desktop.
<?php
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class GetFileName
{
private $keyWord;
public function __construct($keyWord)
{
$this->keyWord = $keyWord;
}
/**
* 依副檔名取得檔案名稱...
* @param string $subName
* @return array
*/
public function get($subName = 'txt')
{
$fileName = [];
// 改為絕對路徑
foreach (glob(fake_public_path()."*" .$this->keyWord. "*".$subName) as $name) {
// 去除路徑只留檔名
$fileName[] = basename($name);
}
return $fileName;
}
}
class GetFileNameTest extends TestCase
{
/**
* @test
*/
public function test_get_filename()
{
/** arrange */
//建立個假文件
$fileName = 'test_AAA-123_2099-12-31.txt';
$file = fake_public_path('test_AAA-123_2099-12-31.txt');
$txtFile = touch($file);
/** act */
$obj = \App::make(GetFileName::class,['keyWord'=>'AAA-123']);
$actual = $obj->get();
/** assert */
$this->assertEquals($fileName, $actual[0]);
}
}
// 模擬 public_path
function fake_public_path($path = null) {
return __DIR__.'/'.$path;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment