使用Dusk测试附加文件 - 通过Tinker而不是Test测试

时间:2017-09-22 10:44:35

标签: php laravel laravel-dusk

我有一个文件上传器,我已经编写了以下测试:

$browser->visit('/product/professional-photo-prints')
        ->attach('photo', __DIR__.'/storage/app/public/testing/test_upload.jpg');

没有上传文件。也没有错误。

然而,当我这样做时:

$browser->visit('/product/professional-photo-prints')
        ->tinker();

并且一旦在Tinker发起的终端中,当我输入完全相同的文件上传命令时我在测试中使用:

$browser->attach('photo', __DIR__.'/storage/app/public/testing/test_upload.jpg');

......它有效。我可以看到我的Chrome实例上传了该文件。

我无法弄清楚这个命令从测试本身或者Tinker运行之间的区别。

非常感谢任何帮助。谢谢。

P.S。我正在使用Laravel 5.5和Dusk 2.0

2 个答案:

答案 0 :(得分:2)

DIR 魔术常量是指正在执行的文件的父文件夹。

    <?php 
    require 'dbh.php';
    $q = $_REQUEST["q"];
    session_start();
    $_SESSION['login_user']=$q;
    $updateQuery = "UPDATE `Guest` SET `Confirmed`='1' WHERE `InviteCode`= '$q' AND `Confirmed` = '0'";
    $selectQuery2 = "SELECT `FirstName`, `LastName`, `Confirmed`, `InviteCode`, `PlusOnes` FROM `Guest` WHERE `InviteCode` = '$q'";
    $stmt = $conn->prepare($updateQuery);
    $stmt->execute();
    $selectStmt = $conn->prepare($selectQuery2);
    $selectStmt->execute();
    if ($stmt->rowCount() > 0) {
        if ($selectStmt->rowCount() > 0) {
            $row = $selectStmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_LAST);
            $hint = "Thank you for your response " . $row[0]. ". We are happy to confirm your attendance to the wedding with " .$row[4] . " guests. Please proceed to the wedding site";
            $_SESSION['login_user'] = $q;
            $selectStmt = null;
        }
    }elseif($selectStmt->rowCount() > 0 && $stmt->rowCount() == 0){
        $row = $selectStmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_LAST);
        $hint = "Thank you " . $row[0]. ". You have already previously responded. We are happy to confirm your attendance to the wedding with ". $row[4] . " guests. If that decision has changed,
           please contact the organiser on the number provided on your invitation card or proceed to the wedding site.";
        $selectStmt = null;
    }else{
        $hint = 'The invitation code you entered does not exist. Please try again.';
    }
    $conn = null;
//  if(session_start()) echo "session started";
    echo $hint;
?>

当然,此路径找不到任何文件。你应该试试这个:

->attach('photo', __DIR__.'/storage/app/public/testing/test_upload.jpg');

答案 1 :(得分:0)

此外,例如,如果您的测试文件位于公共内部,则可以使用base_path;

例如:base_path("public/sample-recording/test.mp3")