1

I'm working on a Laravel 5 package, and writing tests I'm trying to test a function that gets datas from DB.

public function getPhotoDatasFromDb()
{
    $ret = GalleryImage::get()->keyBy('file_name');
    return $ret;
}

The returned values should be in this format:

Collection {#416 ▼
  #items: array:2 [▼
    "IMG_1979.jpg" => GalleryImage {#423 ▼}
        "alt_text" => "example alt text"
        "description" => "lorem ipsum"
    "IMG_1980.jpg" => GalleryImage {#424 ▶}
  ]
}

I had already experiences with testing database testing in other Laravel applications.

My question is: since I'm writing a package, and in the dev environment I don't have an instance of the DB I'm wondering what is the best approach to test it?

If can help to have a wider picture, the database table gets created in the application trough this migration:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateGalleryImagesTable extends Migration
{
    public function up()
    {
        Schema::create('gallery_images', function (Blueprint $table) {
            $table->increments('id');
            $table->string('file_name')->unique();
            $table->text('description')->nullable();
            $table->string('alt')->nullable();
            $table->string('video_link')->nullable();
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::dropIfExists('gallery_images');
    }
}

And this is the model associated

<?php

namespace DavideCasiraghi\ResponsiveGallery;

use Illuminate\Database\Eloquent\Model;

class GalleryImage extends Model
{
    protected $fillable = [
        'file_name', 'description', 'alt', 'video_link',
    ];
}
2
  • Are you actually trying to test that the get() method works as expected, or are you trying to test that your code saves the expected data to the database? Commented Mar 17, 2019 at 13:58
  • Thank you for your question, it helped me to get some clarity. I need to test that the get() method works as aspected. Commented Mar 20, 2019 at 6:29

1 Answer 1

1

I found the solution myself. I post it in case it can be helpful for somebody else.

This is my test:

    /** @test */
    public function it_gets_photos_from_db()
    {
        $gallery = new ResponsiveGalleryFactory();
        $dbImageDatas = $gallery->getPhotoDatasFromDb();
        $this->assertStringContainsString($dbImageDatas['DSC_9470.jpg']->description, 'Photo description');
    }

To make it work I had to configure the DB in the beginning of the testing class:

    /**
     * Create the tables this model needs for testing.
     */
    public static function setUpBeforeClass() : void
    {
        $capsule = new Capsule;

        $capsule->addConnection([
            'driver' => 'sqlite',
            'database' => ':memory:',
            'prefix' => '',
        ]);

        $capsule->setAsGlobal();
        $capsule->bootEloquent();

        Capsule::schema()->create('gallery_images', function (Blueprint $table) {
            $table->increments('id');
            $table->string('file_name')->unique();
            $table->text('description')->nullable();
            $table->string('alt')->nullable();
            $table->string('video_link')->nullable();
            $table->timestamps();
        });

        Model::unguard();

        GalleryImage::create([
            'file_name' => 'DSC_9470.jpg',
            'description' => 'Photo description',
            'alt_text' => 'Photo alt text',
            'video_link' => 'https://www.youtube.com/fsda234',
        ]);
    } 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.