Skip to content

Commit

Permalink
Add tests to cover morphTo with hybrid MongoDB and SQL
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Nov 8, 2023
1 parent 899a235 commit 8c353c1
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 13 deletions.
66 changes: 66 additions & 0 deletions tests/HybridRelationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
use Illuminate\Database\SQLiteConnection;
use Illuminate\Support\Facades\DB;
use MongoDB\Laravel\Tests\Models\Book;
use MongoDB\Laravel\Tests\Models\Photo;
use MongoDB\Laravel\Tests\Models\Role;
use MongoDB\Laravel\Tests\Models\SqlBook;
use MongoDB\Laravel\Tests\Models\SqlPhoto;
use MongoDB\Laravel\Tests\Models\SqlRole;
use MongoDB\Laravel\Tests\Models\SqlUser;
use MongoDB\Laravel\Tests\Models\User;
Expand All @@ -29,13 +31,18 @@ public function setUp(): void
SqlUser::executeSchema();
SqlBook::executeSchema();
SqlRole::executeSchema();
SqlPhoto::executeSchema();
}

public function tearDown(): void
{
SqlUser::truncate();
SqlBook::truncate();
SqlRole::truncate();
SqlPhoto::truncate();
Photo::truncate();
Book::truncate();
User::truncate();
}

public function testSqlRelations()
Expand Down Expand Up @@ -210,4 +217,63 @@ public function testHybridWith()
$this->assertEquals($user->id, $user->books->count());
});
}

public function testHybridMorphToMongoDB(): void
{
$user = SqlUser::create(['name' => 'John Doe']);

$photo = Photo::create(['url' => 'http://graph.facebook.com/john.doe/picture']);
$photo = $user->photos()->save($photo);

$this->assertEquals(1, $user->photos->count());
$this->assertEquals($photo->id, $user->photos->first()->id);

$user = SqlUser::find($user->id);
$this->assertEquals(1, $user->photos->count());
$this->assertEquals($photo->id, $user->photos->first()->id);

$book = SqlBook::create(['title' => 'Game of Thrones']);
$photo = Photo::create(['url' => 'http://graph.facebook.com/gameofthrones/picture']);
$book->photo()->save($photo);

$this->assertNotNull($book->photo);
$this->assertEquals($photo->id, $book->photo->id);

$book = SqlBook::where('title', $book->title)->get()->first();
$this->assertNotNull($book->photo);
$this->assertEquals($photo->id, $book->photo->id);

$photo = Photo::first();
$this->assertEquals($photo->hasImage->name, $user->name);
}

public function testHybridMorphToSql(): void
{
$user = User::create(['name' => 'John Doe']);

$photo = SqlPhoto::create(['url' => 'http://graph.facebook.com/john.doe/picture']);
$photo->save();
$photo = $user->photos()->save($photo);

$this->assertEquals(1, $user->photos->count());
$this->assertEquals($photo->id, $user->photos->first()->id);

$user = User::find($user->id);
$this->assertEquals(1, $user->photos->count());
$this->assertEquals($photo->id, $user->photos->first()->id);

$book = Book::create(['title' => 'Game of Thrones']);
$photo = SqlPhoto::create(['url' => 'http://graph.facebook.com/gameofthrones/picture']);
$book->photo()->save($photo);

$this->assertNotNull($book->photo);
$this->assertEquals($photo->id, $book->photo->id);

$book = Book::where('title', $book->title)->get()->first();
$this->assertNotNull($book->photo);
$this->assertEquals($photo->id, $book->photo->id);

$photo = SqlPhoto::first();
$this->assertEquals($photo->hasImage->name, $user->name);
}
}
6 changes: 6 additions & 0 deletions tests/Models/Book.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace MongoDB\Laravel\Tests\Models;

use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use MongoDB\Laravel\Eloquent\Model as Eloquent;

/**
Expand All @@ -28,4 +29,9 @@ public function sqlAuthor(): BelongsTo
{
return $this->belongsTo(SqlUser::class, 'author_id');
}

public function photo(): MorphOne
{
return $this->morphOne(SqlPhoto::class, 'has_image');
}
}
18 changes: 13 additions & 5 deletions tests/Models/SqlBook.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Illuminate\Database\Eloquent\Model as EloquentModel;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\SQLiteBuilder;
use Illuminate\Support\Facades\Schema;
Expand All @@ -17,16 +18,23 @@ class SqlBook extends EloquentModel
{
use HybridRelations;

protected $connection = 'sqlite';
protected $table = 'books';
protected $connection = 'sqlite';
protected $table = 'book';
protected static $unguarded = true;
protected $primaryKey = 'title';
protected $primaryKey = 'title';
public $incrementing = false;
protected $casts = ['title' => 'string'];

public function author(): BelongsTo
{
return $this->belongsTo(User::class, 'author_id');
}

public function photo(): MorphOne
{
return $this->morphOne(Photo::class, 'has_image');
}

/**
* Check if we need to run the schema.
*/
Expand All @@ -35,8 +43,8 @@ public static function executeSchema(): void
$schema = Schema::connection('sqlite');
assert($schema instanceof SQLiteBuilder);

$schema->dropIfExists('books');
$schema->create('books', function (Blueprint $table) {
$schema->dropIfExists('book');
$schema->create('book', function (Blueprint $table) {
$table->string('title');
$table->string('author_id')->nullable();
$table->integer('sql_user_id')->unsigned()->nullable();
Expand Down
46 changes: 46 additions & 0 deletions tests/Models/SqlPhoto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace MongoDB\Laravel\Tests\Models;

use Illuminate\Database\Eloquent\Model as EloquentModel;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\SQLiteBuilder;
use Illuminate\Support\Facades\Schema;
use MongoDB\Laravel\Eloquent\HybridRelations;
use MongoDB\Laravel\Relations\MorphTo;

use function assert;

class SqlPhoto extends EloquentModel
{
use HybridRelations;

protected $connection = 'sqlite';
protected $table = 'photo';
protected $fillable = ['url'];

public function hasImage(): MorphTo
{
return $this->morphTo();
}

/**
* Check if we need to run the schema.
*/
public static function executeSchema()
{
$schema = Schema::connection('sqlite');
assert($schema instanceof SQLiteBuilder);

$schema->dropIfExists('photo');
$schema->create('photo', function (Blueprint $table) {
$table->increments('id');
$table->string('url');
$table->string('has_image_id')->nullable();
$table->string('has_image_type')->nullable();
$table->timestamps();
});
}
}
6 changes: 3 additions & 3 deletions tests/Models/SqlRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class SqlRole extends EloquentModel
use HybridRelations;

protected $connection = 'sqlite';
protected $table = 'roles';
protected $table = 'role';
protected static $unguarded = true;

public function user(): BelongsTo
Expand All @@ -39,8 +39,8 @@ public static function executeSchema()
$schema = Schema::connection('sqlite');
assert($schema instanceof SQLiteBuilder);

$schema->dropIfExists('roles');
$schema->create('roles', function (Blueprint $table) {
$schema->dropIfExists('role');
$schema->create('role', function (Blueprint $table) {
$table->string('type');
$table->string('user_id');
$table->timestamps();
Expand Down
12 changes: 9 additions & 3 deletions tests/Models/SqlUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Illuminate\Database\Schema\SQLiteBuilder;
use Illuminate\Support\Facades\Schema;
use MongoDB\Laravel\Eloquent\HybridRelations;
use MongoDB\Laravel\Relations\MorphMany;

use function assert;

Expand All @@ -19,7 +20,7 @@ class SqlUser extends EloquentModel
use HybridRelations;

protected $connection = 'sqlite';
protected $table = 'users';
protected $table = 'user';
protected static $unguarded = true;

public function books(): HasMany
Expand All @@ -32,6 +33,11 @@ public function role(): HasOne
return $this->hasOne(Role::class);
}

public function photos(): MorphMany
{
return $this->morphMany(Photo::class, 'has_image');
}

public function sqlBooks(): HasMany
{
return $this->hasMany(SqlBook::class);
Expand All @@ -45,8 +51,8 @@ public static function executeSchema(): void
$schema = Schema::connection('sqlite');
assert($schema instanceof SQLiteBuilder);

$schema->dropIfExists('users');
$schema->create('users', function (Blueprint $table) {
$schema->dropIfExists('user');
$schema->create('user', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
Expand Down
6 changes: 4 additions & 2 deletions tests/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use MongoDB\Laravel\Eloquent\HybridRelations;
use MongoDB\Laravel\Eloquent\MassPrunable;
use MongoDB\Laravel\Eloquent\Model as Eloquent;
use MongoDB\Laravel\Relations\EmbedsMany;
use MongoDB\Laravel\Relations\MorphMany;

/**
* @property string $_id
Expand Down Expand Up @@ -91,12 +93,12 @@ public function groups()
return $this->belongsToMany(Group::class, 'groups', 'users', 'groups', '_id', '_id', 'groups');
}

public function photos()
public function photos(): MorphMany
{
return $this->morphMany(Photo::class, 'has_image');
}

public function addresses()
public function addresses(): EmbedsMany
{
return $this->embedsMany(Address::class);
}
Expand Down

0 comments on commit 8c353c1

Please sign in to comment.