2015-01-09 3 views
0

업로드 방법에 대한 BDD 테스트를 수행하려고합니다. symfony2 프로젝트에서 Behat with Mink를 사용하고 있습니다. Guzzle 클라이언트로 파일을 업로드하는 방법은 무엇입니까?

는 지금이 클라이언트에 간단한 요청을 할 수 있어요 :

$this->client = $this->mink->getSession('goutte')->getDriver()->getClient(); 

하고 문제없이

$this->client->request("POST", $url, array('content-type' => 'application/json'), array(), array(), $fields); 

.

파일 요청은 어떻게하나요? 나는이 시도 :

$file = new \Symfony\Component\HttpFoundation\File\UploadedFile($path, "video"); 
$fields = json_encode($table->getColumnsHash()[0]); 
$this->client->request("POST", $url, array('content-type' => 'multipart/form-data'), array($file), array(), $fields); 

을 그리고 나타나는 오류는 다음과 같습니다

PHP Fatal error: Call to undefined method GuzzleHttp\Stream\Stream::addFile()

실수는 무엇입니까? 감사합니다.

답변

1

좋아 마침내 나는 답을 발견했다. 희망은 누군가를 돕습니다.

$fields = $table->getColumnsHash()[0]; //array('name' => 'test', 'surname' => 'test'); 
$fields["file"] = fopen($path, 'rb'); 
$this->client->request("POST", $url, array('Content-Type => multipart/form-data'), array(), array(), $fields); 

트릭 당신이 Goutte 요청의 네 번째 매개 변수를 사용하지해야한다는 것입니다,하지만 당신은 몸 원시 데이터 등의 모든 필드를 통과해야 :

파일을 업로드하려면 올바른 방법입니다.

+0

$ table 변수는 무엇입니까? – jedema

+0

Behat \ Gherkin \ Node \ TableNode 개체입니다. 이 표기법 (http://docs.behat.org/en/v3.0/guides/2.definitions.html#transforming-tables)을 사용할 때와 같은 객체가 있습니다. – stuzzo

0

나는 Guzzle 업로드에 대해 모르지만 간단한 업로드는 다음과 같이 작동합니다. 불필요한 비트는 제거 할 수 있습니다.

참고 : 동일한 프로젝트에서 많은 개발자가 작업하는 경우 정확히 동일한 폴더 구조를 사용하므로 이미지에 액세스 할 수 있으므로 더미 이미지 파일을 프로젝트 폴더에 보관하는 것이 좋습니다. 필자는 사람마다 사람마다 다른 이미지를 데스크탑에서 선택하여 테스트가 실패하는 것을 보았습니다.

아래는 프로젝트 디렉토리를 가리켜 야하며, 예를 들어 다음과 같이 존재해야합니다. /var/www/html/myproject/test/build/dummy/

behat.yml

default: 
    context: 
     class: Site\FrontendBundle\Features\Context\FeatureContext 
     parameters: 
      output_path: %behat.paths.base%/build/behat/output/ 
      screen_shot_path: %behat.paths.base%/build/behat/screenshot/ 
    extensions: 
     Behat\Symfony2Extension\Extension: 
      mink_driver: true 
      kernel: 
       env: test 
       debug: true 
     Behat\MinkExtension\Extension: 
      base_url: 'http://localhost/local/symfony/web/app_test.php/' 
      files_path: %behat.paths.base%/build/dummy/ 
      javascript_session: selenium2 
      browser_name: firefox 
      goutte: ~ 
      selenium2: ~ 
    paths: 
     features: %behat.paths.base%/src 
     bootstrap: %behat.paths.features%/Context 

하면 아래와 같이 jpg.jpg/var/www/html/myproject/test/build/dummy/에서 폴더가 있다고 가정. 업로드

예 기능 :

Feature: Create League 
    In order to upload a file 
    As a user 
    I should be able to select and upload a file 

    @javascript 
    Scenario: I can create league 
    Given I am on "/" 

    When I attach the file "jpg.jpg" to "league_flag" 
    And I press "Submit" 
    Then I should see "Succeeded." 
+0

안녕하세요, 귀하의 답변에 감사드립니다. 나는 뭔가를 놓치고있다. 실행을 시도하고 호스트에 연결할 수 없다고 말합니다. The Selenium Driver를 설치했는데, Selenium 서버도 설치해야합니까? 도와 줘서 고마워! – stuzzo

+0

[내 대답을 참조하십시오.] (http://stackoverflow.com/questions/27542896/configure-behat-with-symfony2/27618212#27618212) 셀렌을 실행하는 방법을 설명했습니다. 프로젝트로 다운로드 한 다음 명령을 실행하면됩니다. – BentCoder

+0

도움을 주셔서 감사합니다.하지만 마침내 원하는대로 요청을하는 방법을 찾았습니다. – stuzzo