Skip to content

Commit

Permalink
Improves tests.
Browse files Browse the repository at this point in the history
Signed-off-by: Mior Muhammad Zaki <[email protected]>
  • Loading branch information
crynobone committed Oct 14, 2020
1 parent ca2d016 commit 8e0d2a3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
26 changes: 26 additions & 0 deletions tests/Feature/EloquentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

namespace Laravie\SerializesQuery\Tests\Feature;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Foundation\Application;
use Laravie\SerializesQuery\Eloquent;
use Laravie\SerializesQuery\Tests\Models\Post;
use Laravie\SerializesQuery\Tests\Models\User;
use Laravie\SerializesQuery\Tests\TestCase;
use Mockery as m;

class EloquentTest extends TestCase
{
Expand Down Expand Up @@ -37,6 +39,30 @@ public function it_can_serialize_a_basic_eloquent_builder()
$this->assertSame($builder->toSql(), $unserialize->toSql());
}

/** @test */
public function it_can_serialize_a_basic_eloquent_with_eager_relations()
{
$builder = User::with(['posts' => function ($query) {
return $query->where('id', '>', 10);
}]);

$serialized = Eloquent::serialize($builder);

$this->assertNotNull($serialized['model']['eager']['posts']);

$unserialize = Eloquent::unserialize($serialized);

$query = m::mock(Builder::class);

$query->shouldReceive('where')->with('id', '>', 10)->andReturnSelf();

$unserialize->getEagerLoads()['posts']($query);

$this->assertSame('select * from "users"', $unserialize->toSql());

$this->assertSame($builder->toSql(), $unserialize->toSql());
}

/** @test */
public function it_can_serialize_a_basic_eloquent_builder_on_custom_connection()
{
Expand Down
8 changes: 8 additions & 0 deletions tests/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ class User extends Model
{
protected $table = 'users';

/**
* Has many to relationship with Post.
*/
public function posts()
{
return $this->hasMany(Post::class);
}

/**
* Has many and belongs to relationship with Role.
*/
Expand Down

0 comments on commit 8e0d2a3

Please sign in to comment.