mardi 19 janvier 2016

Image uploading testing using phpunit and mockery

I'm trying to test the image uploading using Laravel and PhpUnit. Outside the test class, it's working fine. But when I run the test, I'm getting the following 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";

    }


}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire