JillzTom's avatar

Image uploading testing phpunit

I'm trying to test the image uploading as per @JeffreyWay 's Flyyer series. I added both time() and sha1() functions outside the Test Class declaration and still I'm getting this error:


1) App\UploadTest::it_uploads_an_image_on_post
Mockery\Exception\NoMatchingExpectationException: No matching handler found for Mockery_0_Symfony_Component_HttpFoundation_File_UploadedFile::move("posts/photos", "/nowfoo.jpg"). Either the method was unexpected or its argumen
ts matched no expected argument list for this method

and my Test file is:

<?php

namespace App;

use Illuminate\Foundation\Testing\DatabaseTransactions;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Mockery as m;
use App\BBImage;

class UploadTest extends \TestCase
{

    /**
     * @test
     */

    public function it_uploads_an_image_on_post()
    {
        $file = m::mock(UploadedFile::class, [
            'getClientOriginalName'      => 'foo',
            'getClientOriginalExtension' => 'jpg'
        ]);


        $file->shouldReceive('move')
            ->once()
            ->with('posts/photos', 'nowfoo.jpg');


        $photo = new BBImage($file);


        $this->assertEquals('posts/photos/nowfoo.jpg', $photo->makePhoto());


    }

}

function time() { return 'now'; }

function sha1($path) {return $path;}

and my BBImage.php class

<?php

namespace App;

use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\File\UploadedFile;

class BBImage
{

    protected $file;

    public function __construct(UploadedFile $file)
    {
        $this->file = $file;
    }


    public function makePhoto()
    {

        $path = 'posts/photos';

        $name = $this->makeFileName();

        $this->file->move($path, $name);

        return ('/'.$path.$name);
    }



    protected function makeFileName()
    {

        $name = sha1(
            time() . $this->file->getClientOriginalName()
        );

        $extension = $this->file->getClientOriginalExtension();

        return "/$name.$extension";

    }


}

@thomaskim , @talentia any idea?

0 likes
1 reply
JillzTom's avatar

The error seems to be in the move method. time() and sha1() are working perfectly. Also, in phpstorm, for the shouldReceive, I'm getting a warning like

method call uses 1 parameter but signature uses 0 parameter

Please or to participate in this conversation.