title: Testing file upload without form in phpunit/Symfony 3.4
body: <p>I am testing avatar photo upload functionality in a Symfony 3.4 app. The Symfony documentation assumes that this will be via a form. The page in question does not use form input however so I am having difficulty working out how to write the test.</p>
  
  <p>Here is the html:</p>
  
  <pre><code>    &lt;div class="col-md-4 docs-buttons"&gt;
          &lt;label class="btn btn-info btn-block btn-upload" for="inputImage" title="Upload image file" style="margin-bottom:15px;"&gt;
              &lt;input class="sr-only" id="inputImage" name="file" type="file" accept="image/*"&gt;
              &lt;span class="docs-tooltip" data-toggle="tooltip" title="Import image with Blob URLs"&gt;
                  &lt;span class="fa fa-plus"&gt;&lt;/span&gt; Upload New Photo
              &lt;/span&gt;
          &lt;/label&gt;
  
          &lt;div class="btn-group" style="margin-right:10px;"&gt;
              &lt;button class="btn btn-info" data-method="zoom" data-option="-0.1" type="button" title="Zoom Out"&gt;
                  &lt;span class="docs-tooltip" data-toggle="tooltip" title="$().cropper(&amp;quot;zoom&amp;quot;, -0.1)"&gt;
                      &lt;span class="fa fa-search-minus"&gt;&lt;/span&gt;
                  &lt;/span&gt;
              &lt;/button&gt;
              &lt;button class="btn btn-info" data-method="zoom" data-option="0.1" type="button" title="Zoom In"&gt;
                  &lt;span class="docs-tooltip" data-toggle="tooltip" title="$().cropper(&amp;quot;zoom&amp;quot;, 0.1)"&gt;
                      &lt;span class="fa fa-search-plus"&gt;&lt;/span&gt;
                  &lt;/span&gt;
              &lt;/button&gt;
          &lt;/div&gt;
  
          &lt;div class="btn-group"&gt;
              &lt;button class="btn btn-info" data-method="rotate" data-option="-22.5" type="button" title="Rotate Left"&gt;
                  &lt;span class="docs-tooltip" data-toggle="tooltip" title="$().cropper(&amp;quot;rotate&amp;quot;, -22.5)"&gt;
                      &lt;span class="fa fa-rotate-left"&gt;&lt;/span&gt;
                  &lt;/span&gt;
              &lt;/button&gt;
              &lt;button class="btn btn-info" data-method="rotate" data-option="22.5" type="button" title="Rotate Right"&gt;
                  &lt;span class="docs-tooltip" data-toggle="tooltip" title="$().cropper(&amp;quot;rotate&amp;quot;, 22.5)"&gt;
                      &lt;span class="fa fa-rotate-right"&gt;&lt;/span&gt;
                  &lt;/span&gt;
              &lt;/button&gt;
          &lt;/div&gt;
      &lt;/div&gt;
      &lt;input type="hidden" id="userId" value="6001777"&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p></p>
  
  <pre><code>&lt;div class="modal-footer"&gt;
  &lt;div class="row"&gt;
      &lt;div class="col-xs-6 text-left"&gt;
          &lt;a class="btn btn-default" data-dismiss="modal"&gt;Cancel&lt;/a&gt;
      &lt;/div&gt;
      &lt;div class="col-xs-6"&gt;
          &lt;button id="save" class="btn btn-info"&gt;Save&lt;/button&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>
  </p>
  
  <p>This is a phpunit test written for a similar case, but where the input is a form. Is there any way to refactor this to test the page above?</p>
  
  <pre><code>  public function testUpload()
   {
    # Open the page
    ...
  
   # Select the file from the filesystem
   $image = new UploadedFile(
      # Path to the file to send
      dirname(__FILE__).'/../uploads/photo.jpg',
      # Name of the sent file
      'photo.jpg',
      # MIME type
      'image/jpeg',
      # Size of the file
      11832
   );
  
   # Select the form (adapt it for your needs)
   $form = $crawler-&gt;filter('input[type=submit]...')-&gt;form();
  
   # Put the file in the upload field
   $form['... name of your field ....']-&gt;upload($image);
  
    # Send it
   $crawler = $this-&gt;client-&gt;submit($form);
  
   # Check that the file has been successfully sent
   #  (in my case the filename is displayed in a &lt;a&gt; link so I check
   #  that it appears on the page)
   $this-&gt;assertEquals(
       1,
       $crawler-&gt;filter('a:contains("filename.jpg")')-&gt;count()
   );
  }
  </code></pre>