dimanche 17 mars 2019

How can I mock a call to the DB facade testing a Laravel 5 package?

I'm working on a Laravel 5 package, and writing tests I'm trying to mock a call to the DB facade.

My wish is to test this line of code:

$photoTableDatas = DB::table($tableName)->get();

I have created this test with Mockery, at the moment I manage to test the expression:

$value = DB::table('photos');

But I cannot understand how to test the method get.

<?php

namespace MyVendorName\ResponsiveGallery\Tests;

use PHPUnit\Framework\TestCase;
use Illuminate\Support\Facades\DB;

use \Mockery\Adapter\Phpunit\MockeryTestCase;


class DatabaseTest extends MockeryTestCase
{    
    /** @test */
    public function it_gets_photo_data_from_db()
    {

        $returnValue = new DB();
            $returnValue->file_name = "DSC_9470.jpg";
            $returnValue->description = "Photo description";
            $returnValue->alt_text = "Photo alt text";
            $returnValue->video_link = "https://www.youtube.com/fsda234";


        $mock = \Mockery::mock('DB');        
        $mock->shouldReceive('table')
            ->with('photos')
            ->once()
            ->andReturn($returnValue);

        $photos = $mock->table($tableName);
        var_dump($photos);

    }
}

I found this post but it is not so helpful in my situation: how do I mock the DB facade in laravel?

Is it possible to do what I'm trying to do, or I'm missing something?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire