我正在使用 Laravel Dusk 測驗頁面上的所有鏈接,這些鏈接是 PDF 檔案,它們使用 laravel mix 從資源/檔案復制到公共/檔案。
問題是嘗試使用 get(uri) 方法訪問檔案的 URL 時會出現 404,這是我 Dusk 的簡化版本:
protected function refreshApplicationWithLocale($locale)
{
self::tearDown();
putenv(LaravelLocalization::ENV_ROUTE_KEY . '=' . $locale);
self::setUp();
}
protected function setUp(): void
{
parent::setUp();
if(!File::copyDirectory( resource_path('docs'), public_path() . '/docs'))
throw new \Exception("couldn't copy the folder :(");
// Storage::assertExists(public_path() . '/docs/clothing/existing_file.pdf'); // this assertion fails, what's weird is the path in the exception message does actually exist
$this->artisan('storage:link');
}
protected function tearDown(): void
{
parent::tearDown();
foreach (static::$browsers as $browser) {
$browser->driver->manage()->deleteAllCookies();
}
}
public function testClothing()
{
$this->refreshApplicationWithLocale('fr');
$this->get('/test')->assertStatus(200); // passes
$this->get('/fr/vetements')->assertStatus(200); // passes
//usually I'd get the page urls dynamically with Dusk, but for simplicity sake I'm just testing with this link that I know it exists and can curl it from withing Homestead
$this->get('/docs/clothing/Hoodies.pdf')->assertStatus(200); //failes (404)
}
我錯過了什么?
謝謝你。
PS:我正在使用 laravel 8 和 Homestead
編輯:
@steven7mwesigwa 回答后,這是我的最終代碼:
protected function refreshApplicationWithLocale($locale)
{
self::tearDown();
putenv(LaravelLocalization::ENV_ROUTE_KEY . '=' . $locale);
self::setUp();
}
protected function setUp(): void
{
parent::setUp();
if(!File::copyDirectory( resource_path('docs'), public_path() . '/docs'))
throw new \Exception("couldn't copy the documents folder :(");
}
protected function tearDown(): void
{
parent::tearDown();
foreach (static::$browsers as $browser) {
$browser->driver->manage()->deleteAllCookies();
}
}
public function testClothing()
{
$this->refreshApplicationWithLocale('fr');
$elements = [];
$this->browse(function (Browser $browser) use(&$elements){
$elements = $browser->visit('myroute')
->elements('.tmp_products_list a', 'href');
});
$this->assertGreaterThan(1, count($elements));
foreach($elements as $element) {
$response = Http::get($element->getAttribute('href'));
self::assertEquals($response->status(), 200);
}
}
uj5u.com熱心網友回復:
問題一:
// Storage::assertExists(public_path() . '/docs/clothing/existing_file.pdf');
// this assertion fails, what's weird is the path in the exception message does actually exist
Storage外觀使用組態檔 (config/filesystems.php)中活動的存盤。請注意,這默認為local. 為每個存盤選項設定根路徑。在本地的情況下,它指向storage/app我的腦海。- @bobbybouwmann
另外,查找storage路徑的正確方法是首先指定檔案系統“磁盤”。即,對于位于storage/app/public/demo.js中的檔案:
Storage::disk("public")->assertExists("demo.js");
順便說一句,由于您所需的測驗檔案位于 pubic 檔案夾中,因此可以使用以下方法進行測驗:
$this->assertFileExists(public_path() . '/docs/clothing/existing_file.pdf');
問題 2:
//usually I'd get the page urls dynamically with Dusk, but for simplicity sake I'm just testing with this link that I know it exists and can curl it from withing Homestead
$this->get('/docs/clothing/Hoodies.pdf')->assertStatus(200); //failes (404)
上面的代碼片段不起作用,因為$this->get(...)本質上是在應用程式的路由檔案中搜索與未找到的路徑相對應的明確定義的路由。
您必須使用瀏覽器測驗框架(即Laravel Dusk)來測驗 Web 服務器上的靜態檔案。
更多內容可以在這里找到:如何使用 phpunit 在 symfony 中測驗位于 web 檔案夾中的靜態檔案?
盡管如此,由于您已經在使用Laravel Dusk,這可以通過以下方式實作:
$this->browse(function (Browser $browser) {
$browser->visit('/docs/clothing/Hoodies.pdf')
->assertDontSee("404")
->assertDontSee("NOT FOUND");
});
如果 URL 有效且 PDF 檔案存在且已加載,則不會加載 HTML 視圖。因此測驗應該通過。
如果 URL 無效,404.blade.php將加載 Laravel 的默認視圖。此視圖包括以下文本字串:“404”和“NOT FOUND”。因此,在這種情況下測驗將失敗。
assertDontSee(....)如果您已經有自定義 404錯誤頁面,請隨意更改方法引數中傳遞的文本字串。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/411809.html
標籤:
