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',
];
}
get()method works as expected, or are you trying to test that your code saves the expected data to the database?