Skip to content

Commit a007539

Browse files
committed
Add tests to cover morphTo with hybrid MongoDB and SQL
1 parent 899a235 commit a007539

File tree

8 files changed

+171
-13
lines changed

8 files changed

+171
-13
lines changed

tests/HybridRelationsTest.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
use Illuminate\Database\SQLiteConnection;
88
use Illuminate\Support\Facades\DB;
99
use MongoDB\Laravel\Tests\Models\Book;
10+
use MongoDB\Laravel\Tests\Models\Photo;
1011
use MongoDB\Laravel\Tests\Models\Role;
1112
use MongoDB\Laravel\Tests\Models\SqlBook;
13+
use MongoDB\Laravel\Tests\Models\SqlPhoto;
1214
use MongoDB\Laravel\Tests\Models\SqlRole;
1315
use MongoDB\Laravel\Tests\Models\SqlUser;
1416
use MongoDB\Laravel\Tests\Models\User;
@@ -29,13 +31,18 @@ public function setUp(): void
2931
SqlUser::executeSchema();
3032
SqlBook::executeSchema();
3133
SqlRole::executeSchema();
34+
SqlPhoto::executeSchema();
3235
}
3336

3437
public function tearDown(): void
3538
{
3639
SqlUser::truncate();
3740
SqlBook::truncate();
3841
SqlRole::truncate();
42+
SqlPhoto::truncate();
43+
Photo::truncate();
44+
Book::truncate();
45+
User::truncate();
3946
}
4047

4148
public function testSqlRelations()
@@ -210,4 +217,63 @@ public function testHybridWith()
210217
$this->assertEquals($user->id, $user->books->count());
211218
});
212219
}
220+
221+
public function testHybridMorphToMongoDB(): void
222+
{
223+
$user = SqlUser::create(['name' => 'John Doe']);
224+
225+
$photo = Photo::create(['url' => 'http://graph.facebook.com/john.doe/picture']);
226+
$photo = $user->photos()->save($photo);
227+
228+
$this->assertEquals(1, $user->photos->count());
229+
$this->assertEquals($photo->id, $user->photos->first()->id);
230+
231+
$user = SqlUser::find($user->id);
232+
$this->assertEquals(1, $user->photos->count());
233+
$this->assertEquals($photo->id, $user->photos->first()->id);
234+
235+
$book = SqlBook::create(['title' => 'Game of Thrones']);
236+
$photo = Photo::create(['url' => 'http://graph.facebook.com/gameofthrones/picture']);
237+
$book->photo()->save($photo);
238+
239+
$this->assertNotNull($book->photo);
240+
$this->assertEquals($photo->id, $book->photo->id);
241+
242+
$book = SqlBook::where('title', $book->title)->get()->first();
243+
$this->assertNotNull($book->photo);
244+
$this->assertEquals($photo->id, $book->photo->id);
245+
246+
$photo = Photo::first();
247+
$this->assertEquals($photo->hasImage->name, $user->name);
248+
}
249+
250+
public function testHybridMorphToSql(): void
251+
{
252+
$user = User::create(['name' => 'John Doe']);
253+
254+
$photo = SqlPhoto::create(['url' => 'http://graph.facebook.com/john.doe/picture']);
255+
$photo->save();
256+
$photo = $user->photos()->save($photo);
257+
258+
$this->assertEquals(1, $user->photos->count());
259+
$this->assertEquals($photo->id, $user->photos->first()->id);
260+
261+
$user = User::find($user->id);
262+
$this->assertEquals(1, $user->photos->count());
263+
$this->assertEquals($photo->id, $user->photos->first()->id);
264+
265+
$book = Book::create(['title' => 'Game of Thrones']);
266+
$photo = SqlPhoto::create(['url' => 'http://graph.facebook.com/gameofthrones/picture']);
267+
$book->photo()->save($photo);
268+
269+
$this->assertNotNull($book->photo);
270+
$this->assertEquals($photo->id, $book->photo->id);
271+
272+
$book = Book::where('title', $book->title)->get()->first();
273+
$this->assertNotNull($book->photo);
274+
$this->assertEquals($photo->id, $book->photo->id);
275+
276+
$photo = SqlPhoto::first();
277+
$this->assertEquals($photo->hasImage->name, $user->name);
278+
}
213279
}

tests/Models/Book.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace MongoDB\Laravel\Tests\Models;
66

77
use Illuminate\Database\Eloquent\Relations\BelongsTo;
8+
use Illuminate\Database\Eloquent\Relations\MorphOne;
89
use MongoDB\Laravel\Eloquent\Model as Eloquent;
910

1011
/**
@@ -28,4 +29,9 @@ public function sqlAuthor(): BelongsTo
2829
{
2930
return $this->belongsTo(SqlUser::class, 'author_id');
3031
}
32+
33+
public function photo(): MorphOne
34+
{
35+
return $this->morphOne(SqlPhoto::class, 'has_image');
36+
}
3137
}

tests/Models/SqlBook.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Illuminate\Database\Eloquent\Model as EloquentModel;
88
use Illuminate\Database\Eloquent\Relations\BelongsTo;
9+
use Illuminate\Database\Eloquent\Relations\MorphOne;
910
use Illuminate\Database\Schema\Blueprint;
1011
use Illuminate\Database\Schema\SQLiteBuilder;
1112
use Illuminate\Support\Facades\Schema;
@@ -17,16 +18,23 @@ class SqlBook extends EloquentModel
1718
{
1819
use HybridRelations;
1920

20-
protected $connection = 'sqlite';
21-
protected $table = 'books';
21+
protected $connection = 'sqlite';
22+
protected $table = 'book';
2223
protected static $unguarded = true;
23-
protected $primaryKey = 'title';
24+
protected $primaryKey = 'title';
25+
public $incrementing = false;
26+
protected $casts = ['title' => 'string'];
2427

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

33+
public function photo(): MorphOne
34+
{
35+
return $this->morphOne(Photo::class, 'has_image');
36+
}
37+
3038
/**
3139
* Check if we need to run the schema.
3240
*/
@@ -35,8 +43,8 @@ public static function executeSchema(): void
3543
$schema = Schema::connection('sqlite');
3644
assert($schema instanceof SQLiteBuilder);
3745

38-
$schema->dropIfExists('books');
39-
$schema->create('books', function (Blueprint $table) {
46+
$schema->dropIfExists('book');
47+
$schema->create('book', function (Blueprint $table) {
4048
$table->string('title');
4149
$table->string('author_id')->nullable();
4250
$table->integer('sql_user_id')->unsigned()->nullable();

tests/Models/SqlPhoto.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MongoDB\Laravel\Tests\Models;
6+
7+
use Illuminate\Database\Eloquent\Model as EloquentModel;
8+
use Illuminate\Database\Schema\Blueprint;
9+
use Illuminate\Database\Schema\SQLiteBuilder;
10+
use Illuminate\Support\Facades\Schema;
11+
use MongoDB\Laravel\Eloquent\HybridRelations;
12+
use MongoDB\Laravel\Relations\MorphTo;
13+
14+
use function assert;
15+
16+
class SqlPhoto extends EloquentModel
17+
{
18+
use HybridRelations;
19+
20+
protected $connection = 'sqlite';
21+
protected $table = 'photo';
22+
protected $fillable = ['url'];
23+
24+
public function hasImage(): MorphTo
25+
{
26+
return $this->morphTo();
27+
}
28+
29+
/**
30+
* Check if we need to run the schema.
31+
*/
32+
public static function executeSchema()
33+
{
34+
$schema = Schema::connection('sqlite');
35+
assert($schema instanceof SQLiteBuilder);
36+
37+
$schema->dropIfExists('photo');
38+
$schema->create('photo', function (Blueprint $table) {
39+
$table->increments('id');
40+
$table->string('url');
41+
$table->string('has_image_id')->nullable();
42+
$table->string('has_image_type')->nullable();
43+
$table->timestamps();
44+
});
45+
}
46+
}

tests/Models/SqlRole.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class SqlRole extends EloquentModel
1818
use HybridRelations;
1919

2020
protected $connection = 'sqlite';
21-
protected $table = 'roles';
21+
protected $table = 'role';
2222
protected static $unguarded = true;
2323

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

42-
$schema->dropIfExists('roles');
43-
$schema->create('roles', function (Blueprint $table) {
42+
$schema->dropIfExists('role');
43+
$schema->create('role', function (Blueprint $table) {
4444
$table->string('type');
4545
$table->string('user_id');
4646
$table->timestamps();

tests/Models/SqlUser.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Illuminate\Database\Schema\SQLiteBuilder;
1212
use Illuminate\Support\Facades\Schema;
1313
use MongoDB\Laravel\Eloquent\HybridRelations;
14+
use MongoDB\Laravel\Relations\MorphMany;
1415

1516
use function assert;
1617

@@ -19,7 +20,7 @@ class SqlUser extends EloquentModel
1920
use HybridRelations;
2021

2122
protected $connection = 'sqlite';
22-
protected $table = 'users';
23+
protected $table = 'user';
2324
protected static $unguarded = true;
2425

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

36+
public function photos(): MorphMany
37+
{
38+
return $this->morphMany(Photo::class, 'has_image');
39+
}
40+
3541
public function sqlBooks(): HasMany
3642
{
3743
return $this->hasMany(SqlBook::class);
@@ -45,8 +51,8 @@ public static function executeSchema(): void
4551
$schema = Schema::connection('sqlite');
4652
assert($schema instanceof SQLiteBuilder);
4753

48-
$schema->dropIfExists('users');
49-
$schema->create('users', function (Blueprint $table) {
54+
$schema->dropIfExists('user');
55+
$schema->create('user', function (Blueprint $table) {
5056
$table->increments('id');
5157
$table->string('name');
5258
$table->timestamps();

tests/Models/Subscription.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MongoDB\Laravel\Tests\Models;
6+
7+
use Illuminate\Database\Eloquent\Factories\HasFactory;
8+
use MongoDB\Laravel\Eloquent\Model;
9+
use MongoDB\Laravel\Relations\MorphTo;
10+
11+
class Subscription extends Model
12+
{
13+
use HasFactory;
14+
15+
public function subscribable(): MorphTo
16+
{
17+
return $this->morphTo();
18+
}
19+
20+
public function user()
21+
{
22+
return $this->belongsTo(User::class);
23+
}
24+
}

tests/Models/User.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
use MongoDB\Laravel\Eloquent\HybridRelations;
1818
use MongoDB\Laravel\Eloquent\MassPrunable;
1919
use MongoDB\Laravel\Eloquent\Model as Eloquent;
20+
use MongoDB\Laravel\Relations\EmbedsMany;
21+
use MongoDB\Laravel\Relations\MorphMany;
2022

2123
/**
2224
* @property string $_id
@@ -91,12 +93,12 @@ public function groups()
9193
return $this->belongsToMany(Group::class, 'groups', 'users', 'groups', '_id', '_id', 'groups');
9294
}
9395

94-
public function photos()
96+
public function photos(): MorphMany
9597
{
9698
return $this->morphMany(Photo::class, 'has_image');
9799
}
98100

99-
public function addresses()
101+
public function addresses(): EmbedsMany
100102
{
101103
return $this->embedsMany(Address::class);
102104
}

0 commit comments

Comments
 (0)