[增添]添加了datasource的setting数据库以及默认值
This commit is contained in:
146
vendor/anourvalar/eloquent-serialize/tests/AbstractSuite.php
vendored
Normal file
146
vendor/anourvalar/eloquent-serialize/tests/AbstractSuite.php
vendored
Normal file
@@ -0,0 +1,146 @@
|
||||
<?php
|
||||
|
||||
namespace AnourValar\EloquentSerialize\Tests;
|
||||
|
||||
use AnourValar\EloquentSerialize\Tests\Models\File;
|
||||
use AnourValar\EloquentSerialize\Tests\Models\User;
|
||||
use AnourValar\EloquentSerialize\Tests\Models\UserPhoneNote;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use AnourValar\EloquentSerialize\Tests\Models\Post;
|
||||
use AnourValar\EloquentSerialize\Tests\Models\Tag;
|
||||
|
||||
abstract class AbstractSuite extends \Orchestra\Testbench\TestCase
|
||||
{
|
||||
/**
|
||||
* @var \AnourValar\EloquentSerialize\Service
|
||||
*/
|
||||
protected $service;
|
||||
|
||||
/**
|
||||
* Init
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->withFactories(__DIR__.'/factories');
|
||||
$this->setUpDatabase($this->app);
|
||||
$this->setUpSeeder();
|
||||
|
||||
\DB::enableQueryLog();
|
||||
|
||||
$this->service = \App::make(\AnourValar\EloquentSerialize\Service::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Illuminate\Foundation\Application $app
|
||||
* @return void
|
||||
*/
|
||||
protected function setUpDatabase(\Illuminate\Foundation\Application $app)
|
||||
{
|
||||
$app['db']->connection()->getSchemaBuilder()->create('users', function (Blueprint $table) {
|
||||
$table->increments('id');
|
||||
$table->string('title');
|
||||
$table->integer('sort');
|
||||
$table->jsonb('meta')->nullable();
|
||||
$table->timestamps();
|
||||
$table->softDeletes();
|
||||
});
|
||||
|
||||
$app['db']->connection()->getSchemaBuilder()->create('user_phones', function (Blueprint $table) {
|
||||
$table->increments('id');
|
||||
$table->integer('user_id');
|
||||
$table->string('phone');
|
||||
$table->boolean('is_primary');
|
||||
$table->timestamps();
|
||||
});
|
||||
|
||||
$app['db']->connection()->getSchemaBuilder()->create('user_phone_notes', function (Blueprint $table) {
|
||||
$table->increments('id');
|
||||
$table->integer('user_phone_id');
|
||||
$table->string('note');
|
||||
$table->timestamps();
|
||||
});
|
||||
|
||||
$app['db']->connection()->getSchemaBuilder()->create('posts', function (Blueprint $table) {
|
||||
$table->increments('id');
|
||||
$table->integer('user_id');
|
||||
$table->string('title');
|
||||
$table->text('body');
|
||||
$table->timestamps();
|
||||
});
|
||||
|
||||
$app['db']->connection()->getSchemaBuilder()->create('files', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->foreignIdFor(User::class);
|
||||
$table->string('type');
|
||||
$table->timestamps();
|
||||
});
|
||||
|
||||
$app['db']->connection()->getSchemaBuilder()->create('tags', function (Blueprint $table) {
|
||||
$table->increments('id');
|
||||
$table->string('title');
|
||||
$table->bigInteger('taggable_id');
|
||||
$table->string('taggable_type');
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
protected function setUpSeeder()
|
||||
{
|
||||
factory(UserPhoneNote::class)->times(80)->create();
|
||||
factory(File::class)->times(40)->create();
|
||||
|
||||
factory(Post::class)->times(10)->create();
|
||||
factory(Tag::class)->times(10)->create();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Illuminate\Database\Eloquent\Builder $builder
|
||||
* @param bool $execute
|
||||
* @return void
|
||||
*/
|
||||
protected function compare(\Illuminate\Database\Eloquent\Builder $builder, bool $execute = true): void
|
||||
{
|
||||
$referenceBuilder = clone $builder;
|
||||
$referenceSerialize = $this->service->serialize($builder);
|
||||
|
||||
for ($i = 1; $i <= 3; $i++) {
|
||||
$builder = $this->service->serialize($builder);
|
||||
$this->assertSame($referenceSerialize, $builder, "#$i");
|
||||
|
||||
$builder = json_encode($builder);
|
||||
$builder = json_decode($builder, true);
|
||||
$builder = $this->service->unserialize($builder);
|
||||
$this->assertSame($this->getScheme($referenceBuilder, $execute), $this->getScheme($builder, $execute), "#$i");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Illuminate\Database\Eloquent\Builder $builder
|
||||
* @param bool $execute
|
||||
* @return string
|
||||
*/
|
||||
private function getScheme(\Illuminate\Database\Eloquent\Builder $builder, bool $execute): string
|
||||
{
|
||||
\DB::flushQueryLog();
|
||||
if ($execute) {
|
||||
$result = $builder->get();
|
||||
} else {
|
||||
$result = [];
|
||||
}
|
||||
$logs = \DB::getQueryLog();
|
||||
|
||||
foreach ($logs as &$log) {
|
||||
unset($log['time']);
|
||||
}
|
||||
unset($log);
|
||||
|
||||
return json_encode(['query' => $logs, 'result' => $result], JSON_PRETTY_PRINT);
|
||||
}
|
||||
}
|
||||
36
vendor/anourvalar/eloquent-serialize/tests/Casts/TestCast.php
vendored
Normal file
36
vendor/anourvalar/eloquent-serialize/tests/Casts/TestCast.php
vendored
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace AnourValar\EloquentSerialize\Tests\Casts;
|
||||
|
||||
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
|
||||
|
||||
class TestCast implements CastsAttributes
|
||||
{
|
||||
/**
|
||||
* Cast the given value.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Model $model
|
||||
* @param string $key
|
||||
* @param mixed $value
|
||||
* @param array $attributes
|
||||
* @return mixed
|
||||
*/
|
||||
public function get($model, string $key, $value, array $attributes)
|
||||
{
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare the given value for storage.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Model $model
|
||||
* @param string $key
|
||||
* @param mixed $value
|
||||
* @param array $attributes
|
||||
* @return mixed
|
||||
*/
|
||||
public function set($model, string $key, $value, array $attributes)
|
||||
{
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
40
vendor/anourvalar/eloquent-serialize/tests/ConnectionTest.php
vendored
Normal file
40
vendor/anourvalar/eloquent-serialize/tests/ConnectionTest.php
vendored
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace AnourValar\EloquentSerialize\Tests;
|
||||
|
||||
use AnourValar\EloquentSerialize\Tests\Models\User;
|
||||
|
||||
class ConnectionTest extends AbstractSuite
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testDefault()
|
||||
{
|
||||
$package = $this->service->serialize(User::where('id', '!=', 1));
|
||||
$package = json_encode($package);
|
||||
|
||||
$package = json_decode($package, true);
|
||||
$builder = $this->service->unserialize($package);
|
||||
|
||||
$this->assertEquals('testing', $builder->getQuery()->getConnection()->getName());
|
||||
$this->assertEquals('testing', $builder->first()->getConnectionName());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testSimple()
|
||||
{
|
||||
config(['database.connections.foo' => config('database.connections.testing')]);
|
||||
|
||||
$package = $this->service->serialize((new User())->setConnection('foo')->where('id', '!=', 1));
|
||||
$package = json_encode($package);
|
||||
|
||||
$package = json_decode($package, true);
|
||||
$builder = $this->service->unserialize($package);
|
||||
|
||||
$this->assertEquals('foo', $builder->getQuery()->getConnection()->getName());
|
||||
$this->assertEquals('foo', $builder->getModel()->getConnectionName());
|
||||
}
|
||||
}
|
||||
16
vendor/anourvalar/eloquent-serialize/tests/DistinctTest.php
vendored
Normal file
16
vendor/anourvalar/eloquent-serialize/tests/DistinctTest.php
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace AnourValar\EloquentSerialize\Tests;
|
||||
|
||||
use AnourValar\EloquentSerialize\Tests\Models\User;
|
||||
|
||||
class DistinctTest extends AbstractSuite
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testSimple()
|
||||
{
|
||||
$this->compare(User::distinct('title'));
|
||||
}
|
||||
}
|
||||
355
vendor/anourvalar/eloquent-serialize/tests/EagerTest.php
vendored
Normal file
355
vendor/anourvalar/eloquent-serialize/tests/EagerTest.php
vendored
Normal file
@@ -0,0 +1,355 @@
|
||||
<?php
|
||||
|
||||
namespace AnourValar\EloquentSerialize\Tests;
|
||||
|
||||
use AnourValar\EloquentSerialize\Tests\Models\User;
|
||||
use AnourValar\EloquentSerialize\Tests\Models\UserPhone;
|
||||
use AnourValar\EloquentSerialize\Tests\Models\Post;
|
||||
use AnourValar\EloquentSerialize\Tests\Models\UserPhoneNote;
|
||||
use AnourValar\EloquentSerialize\Tests\Models\Tag;
|
||||
|
||||
class EagerTest extends AbstractSuite
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testSimple()
|
||||
{
|
||||
// ...
|
||||
$this->compare(User::query());
|
||||
|
||||
// with
|
||||
$this->compare(User::with('userPhones'));
|
||||
|
||||
// with count
|
||||
$this->compare(User::withCount('userPhones'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testComplex()
|
||||
{
|
||||
// with
|
||||
$this->compare(User::with('userPhonesSorted'));
|
||||
$this->compare(User::with('userPhonesPrimary'));
|
||||
$this->compare(User::with(['userPhonesSorted', 'userPhonesPrimary']));
|
||||
$this->compare(User::query()->with('filesAB', 'filesC', 'filesDE'));
|
||||
|
||||
// with count
|
||||
$this->compare(User::withCount('userPhonesSorted'));
|
||||
$this->compare(User::withCount('userPhonesPrimary'));
|
||||
$this->compare(User::withCount(['userPhonesSorted', 'userPhonesPrimary']));
|
||||
$this->compare(User::query()->withCount('filesAB', 'filesC', 'filesDE'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testNested()
|
||||
{
|
||||
// with
|
||||
$this->compare(User::with('userPhones.userPhoneNote'));
|
||||
$this->compare(User::with('userPhones.userPhoneNote:id,user_phone_id,note'));
|
||||
$this->compare(User::with(['userPhones' => ['userPhoneNote']]));
|
||||
$this->compare(User::with(['userPhones' => fn ($query) => $query->with('userPhoneNote')]));
|
||||
|
||||
// with (reverse)
|
||||
$this->compare(UserPhone::with('user.userPhones'));
|
||||
$this->compare(UserPhone::with(['user' => ['userPhones']]));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testNestedComplex()
|
||||
{
|
||||
// with
|
||||
$this->compare(User::with('userPhonesSorted.userPhoneNote'));
|
||||
$this->compare(User::with('userPhonesPrimary.userPhoneNote'));
|
||||
|
||||
// with (reverse)
|
||||
$this->compare(UserPhone::with('user.userPhonesSorted'));
|
||||
$this->compare(UserPhone::with('user.userPhonesPrimary'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testWithBuilder()
|
||||
{
|
||||
// 1 level
|
||||
$this->compare(
|
||||
User::with(['userPhones' => function ($query) {
|
||||
$query->orderBy('id', 'ASC')->limit(1)->select(['id', 'phone']);
|
||||
}])
|
||||
);
|
||||
|
||||
// 2 levels
|
||||
$this->compare(
|
||||
User::with(['userPhones' => function ($query) {
|
||||
$query->where(function ($query) {
|
||||
$query->where('phone', '=', '111')->orWhere('phone', '!=', '222')->limit(5);
|
||||
});
|
||||
}])
|
||||
);
|
||||
|
||||
// 3 levels
|
||||
$this->compare(
|
||||
User::with(['userPhones' => function ($query) {
|
||||
$query->where(function ($query) {
|
||||
$query
|
||||
->where('phone', '=', '111')
|
||||
->orWhere(function ($query) {
|
||||
$query->where('phone', '=', '222')->orWhere('created_at', '>', '2010-01-01');
|
||||
});
|
||||
});
|
||||
}])
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testWithComplexBuilder()
|
||||
{
|
||||
// 1 level
|
||||
$this->compare(
|
||||
User::with(['userPhonesPrimary' => function ($query) {
|
||||
$query->orderBy('id', 'ASC')->limit(1)->select(['id', 'phone']);
|
||||
}])
|
||||
);
|
||||
|
||||
// 2 levels
|
||||
$this->compare(
|
||||
User::with(['userPhonesPrimary' => function ($query) {
|
||||
$query->where(function ($query) {
|
||||
$query->where('phone', '=', '111')->orWhere('phone', '!=', '222');
|
||||
});
|
||||
}])
|
||||
);
|
||||
|
||||
// 3 levels
|
||||
$this->compare(
|
||||
User::with(['userPhonesPrimary' => function ($query) {
|
||||
$query->where(function ($query) {
|
||||
$query
|
||||
->where('phone', '=', '111')
|
||||
->orWhere(function ($query) {
|
||||
$query->where('phone', '=', '222')->orWhere('created_at', '>', '2010-01-01');
|
||||
});
|
||||
});
|
||||
}])
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testWithCountBuilder()
|
||||
{
|
||||
// 1 level
|
||||
$this->compare(
|
||||
User::withCount(['userPhones' => function ($query) {
|
||||
$query->limit(2);
|
||||
}])
|
||||
);
|
||||
|
||||
// 2 levels
|
||||
$this->compare(
|
||||
User::withCount(['userPhones' => function ($query) {
|
||||
$query->where(function ($query) {
|
||||
$query->where('phone', '=', '111')->orWhere('phone', '!=', '222');
|
||||
});
|
||||
}])
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testWithComplexCountBuilder()
|
||||
{
|
||||
// 1 level
|
||||
$this->compare(
|
||||
User::withCount(['userPhonesSorted' => function ($query) {
|
||||
$query->limit(2);
|
||||
}])
|
||||
);
|
||||
|
||||
// 2 levels
|
||||
$this->compare(
|
||||
User::withCount(['userPhonesSorted' => function ($query) {
|
||||
$query->where(function ($query) {
|
||||
$query->where('phone', '=', '111')->orWhere('phone', '!=', '222');
|
||||
});
|
||||
}])
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testWithCountAlias()
|
||||
{
|
||||
// simple
|
||||
$this->compare(
|
||||
User::withCount('userPhones as test')
|
||||
);
|
||||
|
||||
// builder
|
||||
$this->compare(
|
||||
User::withCount([
|
||||
'userPhones as primary' => function ($query) {
|
||||
$query->where(function ($query) {
|
||||
$query->where('is_primary', true);
|
||||
});
|
||||
},
|
||||
'userPhones as not_primary' => function ($query) {
|
||||
$query->where('is_primary', false);
|
||||
},
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testBelongs()
|
||||
{
|
||||
// simple
|
||||
$this->compare(
|
||||
UserPhone::with('user')
|
||||
);
|
||||
|
||||
// simple count
|
||||
$this->compare(
|
||||
UserPhone::withCount('user')
|
||||
);
|
||||
|
||||
// builder
|
||||
$this->compare(
|
||||
UserPhone::with([
|
||||
'user' => function ($query) {
|
||||
$query->where('title', '=', 'admin')->limit(1);
|
||||
},
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testHasManyThrough()
|
||||
{
|
||||
// simple
|
||||
$this->compare(User::with('userPhoneNote'));
|
||||
$this->compare(User::with('userPhonesSorted'));
|
||||
$this->compare(User::with('userPhonesPrimary'));
|
||||
|
||||
// simple count
|
||||
$this->compare(User::withCount('userPhoneNote'));
|
||||
$this->compare(User::withCount('userPhonesSorted'));
|
||||
$this->compare(User::withCount('userPhonesPrimary'));
|
||||
|
||||
// builder
|
||||
$this->compare(
|
||||
User::with([
|
||||
'userPhoneNote' => function ($query) {
|
||||
$query->limit(1);
|
||||
},
|
||||
])
|
||||
);
|
||||
$this->compare(
|
||||
User::with([
|
||||
'userPhonesSorted' => function ($query) {
|
||||
$query->limit(1);
|
||||
},
|
||||
])
|
||||
);
|
||||
$this->compare(
|
||||
User::with([
|
||||
'userPhonesPrimary' => function ($query) {
|
||||
$query->limit(1);
|
||||
},
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testThroughBuilder()
|
||||
{
|
||||
// simple
|
||||
$this->compare(User::with('userPhoneNoteAlt'));
|
||||
|
||||
// simple count
|
||||
$this->compare(User::withCount('userPhoneNoteAlt'));
|
||||
|
||||
// builder
|
||||
$this->compare(
|
||||
User::with([
|
||||
'userPhoneNoteAlt' => function ($query) {
|
||||
$query->limit(1);
|
||||
},
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testHasOne()
|
||||
{
|
||||
// simple
|
||||
$this->compare(
|
||||
UserPhone::with('userPhoneNote')
|
||||
);
|
||||
|
||||
// simple count
|
||||
$this->compare(
|
||||
UserPhone::withCount('userPhoneNote')
|
||||
);
|
||||
|
||||
// builder
|
||||
$this->compare(
|
||||
UserPhone::with([
|
||||
'userPhoneNote' => function ($query) {
|
||||
$query->whereNotNull('note');
|
||||
},
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testMorphTo()
|
||||
{
|
||||
// Nested
|
||||
$this->compare(
|
||||
Tag::with(['taggable' => function (\Illuminate\Database\Eloquent\Relations\MorphTo $morphTo) {
|
||||
$morphTo->morphWith([
|
||||
Post::class => ['user'],
|
||||
]);
|
||||
}])
|
||||
);
|
||||
|
||||
// Nested count
|
||||
$this->compare(
|
||||
Tag::with(['taggable' => function (\Illuminate\Database\Eloquent\Relations\MorphTo $morphTo) {
|
||||
$morphTo->morphWithCount([
|
||||
Post::class => ['user'],
|
||||
]);
|
||||
}])
|
||||
);
|
||||
|
||||
// Nested (reverse)
|
||||
$this->compare(
|
||||
Post::with(['tag' => function (\Illuminate\Database\Eloquent\Relations\MorphOne $morphOne) {
|
||||
$morphOne->with('taggable');
|
||||
}])
|
||||
);
|
||||
}
|
||||
}
|
||||
36
vendor/anourvalar/eloquent-serialize/tests/FromTest.php
vendored
Normal file
36
vendor/anourvalar/eloquent-serialize/tests/FromTest.php
vendored
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace AnourValar\EloquentSerialize\Tests;
|
||||
|
||||
use AnourValar\EloquentSerialize\Tests\Models\User;
|
||||
|
||||
class FromTest extends AbstractSuite
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testSimple()
|
||||
{
|
||||
$this->compare(
|
||||
User::whereExists(function ($query) {
|
||||
$query
|
||||
->from('user_phones')
|
||||
->whereRaw('user_phones.user_id = users.id');
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testAlias()
|
||||
{
|
||||
$this->compare(
|
||||
User::whereExists(function ($query) {
|
||||
$query
|
||||
->from('user_phones AS up')
|
||||
->whereRaw('up.user_id = users.id');
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
17
vendor/anourvalar/eloquent-serialize/tests/GroupByTest.php
vendored
Normal file
17
vendor/anourvalar/eloquent-serialize/tests/GroupByTest.php
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace AnourValar\EloquentSerialize\Tests;
|
||||
|
||||
use AnourValar\EloquentSerialize\Tests\Models\User;
|
||||
|
||||
class GroupByTest extends AbstractSuite
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testSimple()
|
||||
{
|
||||
// One column
|
||||
$this->compare(User::groupBy('title'));
|
||||
}
|
||||
}
|
||||
23
vendor/anourvalar/eloquent-serialize/tests/HavingTest.php
vendored
Normal file
23
vendor/anourvalar/eloquent-serialize/tests/HavingTest.php
vendored
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace AnourValar\EloquentSerialize\Tests;
|
||||
|
||||
use AnourValar\EloquentSerialize\Tests\Models\User;
|
||||
|
||||
class HavingTest extends AbstractSuite
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testSimple()
|
||||
{
|
||||
// One column
|
||||
$this->compare(User::groupBy(['id'])->having('id', '>', 1));
|
||||
|
||||
// Two columns
|
||||
$this->compare(User::groupBy(['id', 'title'])->having('id', '>', 1)->orHaving('title', '=', 'abc'));
|
||||
|
||||
// Raw
|
||||
$this->compare(User::groupBy('id')->havingRaw('COUNT(id) > ?', [1]));
|
||||
}
|
||||
}
|
||||
88
vendor/anourvalar/eloquent-serialize/tests/JoinTest.php
vendored
Normal file
88
vendor/anourvalar/eloquent-serialize/tests/JoinTest.php
vendored
Normal file
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
namespace AnourValar\EloquentSerialize\Tests;
|
||||
|
||||
use AnourValar\EloquentSerialize\Tests\Models\Post;
|
||||
use AnourValar\EloquentSerialize\Tests\Models\User;
|
||||
use AnourValar\EloquentSerialize\Tests\Models\UserPhone;
|
||||
|
||||
class JoinTest extends AbstractSuite
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testLeft()
|
||||
{
|
||||
$this->compare(
|
||||
User::leftJoin('user_phones', 'users.id', '=', 'user_phones.user_id')->selectRaw('users.*, user_phones.phone')
|
||||
);
|
||||
|
||||
$this->compare(
|
||||
UserPhone::leftJoin('users', 'users.id', '=', 'user_phones.user_id')->groupBy('users.id')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testInner()
|
||||
{
|
||||
$this->compare(
|
||||
UserPhone::join('users', 'users.id', '=', 'user_phones.user_id')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testCross()
|
||||
{
|
||||
$this->compare(UserPhone::crossJoin('users'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testMultiple()
|
||||
{
|
||||
$this->compare(
|
||||
UserPhone::join('users', 'users.id', '=', 'user_phones.user_id')
|
||||
->join('posts', 'users.id', '=', 'posts.user_id')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testExpression()
|
||||
{
|
||||
$this->compare(
|
||||
User::join('posts', function ($join) {
|
||||
$join->on('users.id', '=', 'posts.user_id')->orOn('users.id', '=', 'posts.user_id');
|
||||
})
|
||||
);
|
||||
|
||||
$this->compare(
|
||||
User::join('posts', function ($join) {
|
||||
$join->on('users.id', '=', 'posts.user_id')->where('posts.title', '=', 'abc');
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testSubQuery()
|
||||
{
|
||||
$latestPosts = Post::select('user_id', \DB::raw('MAX(created_at) as last_post_created_at'))
|
||||
->groupBy('user_id');
|
||||
|
||||
$this->compare($latestPosts);
|
||||
|
||||
$this->compare(
|
||||
User::joinSub($latestPosts, 'latest_posts', function ($join) {
|
||||
$join->on('users.id', '=', 'latest_posts.user_id');
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
29
vendor/anourvalar/eloquent-serialize/tests/LimitTest.php
vendored
Normal file
29
vendor/anourvalar/eloquent-serialize/tests/LimitTest.php
vendored
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace AnourValar\EloquentSerialize\Tests;
|
||||
|
||||
use AnourValar\EloquentSerialize\Tests\Models\User;
|
||||
|
||||
class LimitTest extends AbstractSuite
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testSimple()
|
||||
{
|
||||
// Limit
|
||||
$this->compare(User::limit(10));
|
||||
|
||||
// Limit with offset
|
||||
$this->compare(User::offset(20)->limit(10));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testNested()
|
||||
{
|
||||
$this->compare(User::with(['userPhones' => fn ($query) => $query->limit(1)]));
|
||||
$this->compare(User::with(['userPhones' => fn ($query) => $query->limit(2)]));
|
||||
}
|
||||
}
|
||||
7
vendor/anourvalar/eloquent-serialize/tests/Models/File.php
vendored
Normal file
7
vendor/anourvalar/eloquent-serialize/tests/Models/File.php
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace AnourValar\EloquentSerialize\Tests\Models;
|
||||
|
||||
class File extends \Illuminate\Database\Eloquent\Model
|
||||
{
|
||||
}
|
||||
16
vendor/anourvalar/eloquent-serialize/tests/Models/Post.php
vendored
Normal file
16
vendor/anourvalar/eloquent-serialize/tests/Models/Post.php
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace AnourValar\EloquentSerialize\Tests\Models;
|
||||
|
||||
class Post extends \Illuminate\Database\Eloquent\Model
|
||||
{
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
public function tag()
|
||||
{
|
||||
return $this->morphOne(Tag::class, 'taggable');
|
||||
}
|
||||
}
|
||||
11
vendor/anourvalar/eloquent-serialize/tests/Models/Tag.php
vendored
Normal file
11
vendor/anourvalar/eloquent-serialize/tests/Models/Tag.php
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace AnourValar\EloquentSerialize\Tests\Models;
|
||||
|
||||
class Tag extends \Illuminate\Database\Eloquent\Model
|
||||
{
|
||||
public function taggable()
|
||||
{
|
||||
return $this->morphTo();
|
||||
}
|
||||
}
|
||||
76
vendor/anourvalar/eloquent-serialize/tests/Models/User.php
vendored
Normal file
76
vendor/anourvalar/eloquent-serialize/tests/Models/User.php
vendored
Normal file
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
namespace AnourValar\EloquentSerialize\Tests\Models;
|
||||
|
||||
class User extends \Illuminate\Database\Eloquent\Model
|
||||
{
|
||||
use \Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
||||
/**
|
||||
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
||||
*/
|
||||
public function userPhones()
|
||||
{
|
||||
return $this->hasMany(UserPhone::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
||||
*/
|
||||
public function userPhonesSorted()
|
||||
{
|
||||
return $this->hasMany(UserPhone::class)->orderBy('phone', 'ASC');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
||||
*/
|
||||
public function userPhonesPrimary()
|
||||
{
|
||||
return $this->hasMany(UserPhone::class)->where('is_primary', '=', true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
|
||||
*/
|
||||
public function userPhoneNote()
|
||||
{
|
||||
return $this->hasManyThrough(UserPhoneNote::class, UserPhone::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Illuminate\Database\Eloquent\Relations\HasOneThrough
|
||||
*/
|
||||
public function userPhoneNoteAlt()
|
||||
{
|
||||
return $this->through($this->userPhones())
|
||||
->has(fn (UserPhone $userPhone) => $userPhone->userPhoneNote());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
||||
*/
|
||||
public function filesAB()
|
||||
{
|
||||
return $this->hasMany(File::class, 'user_id')->whereIn('type', ['a', 'b']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
||||
*/
|
||||
public function filesC()
|
||||
{
|
||||
return $this->hasMany(File::class, 'user_id', 'id')->where('type', 'c');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
||||
*/
|
||||
public function filesDE()
|
||||
{
|
||||
return $this->hasOne(File::class)
|
||||
->whereNotIn('type', ['f', 'g'])
|
||||
->whereIn('type', ['d', 'e'])
|
||||
->whereNotIn('type', ['a', 'b', 'c']);
|
||||
}
|
||||
}
|
||||
44
vendor/anourvalar/eloquent-serialize/tests/Models/UserPhone.php
vendored
Normal file
44
vendor/anourvalar/eloquent-serialize/tests/Models/UserPhone.php
vendored
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace AnourValar\EloquentSerialize\Tests\Models;
|
||||
|
||||
class UserPhone extends \Illuminate\Database\Eloquent\Model
|
||||
{
|
||||
/**
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
*/
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Illuminate\Database\Eloquent\Relations\HasOne
|
||||
*/
|
||||
public function userPhoneNote()
|
||||
{
|
||||
return $this->hasOne(UserPhoneNote::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @param bool $isPrimary
|
||||
* @return void
|
||||
*/
|
||||
public function scopeMajor(\Illuminate\Database\Eloquent\Builder $query, bool $isPrimary)
|
||||
{
|
||||
$query->where('is_primary', '=', $isPrimary);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @param string $pattern
|
||||
* @return void
|
||||
*/
|
||||
public function scopeSearch(\Illuminate\Database\Eloquent\Builder $query, string $pattern)
|
||||
{
|
||||
$query->where(function ($query) use ($pattern) {
|
||||
$query->where('phone', 'LIKE', "%$pattern%");
|
||||
});
|
||||
}
|
||||
}
|
||||
11
vendor/anourvalar/eloquent-serialize/tests/Models/UserPhoneNote.php
vendored
Normal file
11
vendor/anourvalar/eloquent-serialize/tests/Models/UserPhoneNote.php
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace AnourValar\EloquentSerialize\Tests\Models;
|
||||
|
||||
class UserPhoneNote extends \Illuminate\Database\Eloquent\Model
|
||||
{
|
||||
public function userPhone()
|
||||
{
|
||||
return $this->belongsTo(UserPhone::class);
|
||||
}
|
||||
}
|
||||
37
vendor/anourvalar/eloquent-serialize/tests/OrderByTest.php
vendored
Normal file
37
vendor/anourvalar/eloquent-serialize/tests/OrderByTest.php
vendored
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace AnourValar\EloquentSerialize\Tests;
|
||||
|
||||
use AnourValar\EloquentSerialize\Tests\Models\User;
|
||||
use AnourValar\EloquentSerialize\Tests\Models\UserPhone;
|
||||
|
||||
class OrderByTest extends AbstractSuite
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testSimple()
|
||||
{
|
||||
// One column
|
||||
$this->compare(User::orderBy('id', 'ASC'));
|
||||
|
||||
// Two columns
|
||||
$this->compare(User::orderBy('id', 'ASC')->orderBy('sort', 'DESC'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testExpression()
|
||||
{
|
||||
// ASC
|
||||
$this->compare(
|
||||
User::orderBy(UserPhone::select('created_at')->whereColumn('user_id', 'users.id')->limit(1)->orderBy('created_at', 'ASC'))
|
||||
);
|
||||
|
||||
// DESC
|
||||
$this->compare(
|
||||
User::orderByDesc(UserPhone::select('phone')->whereColumn('user_id', 'users.id')->limit(1)->orderBy('phone', 'DESC'))
|
||||
);
|
||||
}
|
||||
}
|
||||
36
vendor/anourvalar/eloquent-serialize/tests/ScopeTest.php
vendored
Normal file
36
vendor/anourvalar/eloquent-serialize/tests/ScopeTest.php
vendored
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace AnourValar\EloquentSerialize\Tests;
|
||||
|
||||
use AnourValar\EloquentSerialize\Tests\Models\User;
|
||||
use AnourValar\EloquentSerialize\Tests\Models\UserPhone;
|
||||
|
||||
class ScopeTest extends AbstractSuite
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testSimple()
|
||||
{
|
||||
// One way
|
||||
$this->compare(User::withTrashed());
|
||||
|
||||
// Reverted
|
||||
$this->compare(User::withTrashed()->withoutTrashed());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testWithParams()
|
||||
{
|
||||
// Primary
|
||||
$this->compare(UserPhone::major(true));
|
||||
|
||||
// NOT primary
|
||||
$this->compare(UserPhone::major(false));
|
||||
|
||||
// Combine
|
||||
$this->compare(UserPhone::major(false)->search('906'));
|
||||
}
|
||||
}
|
||||
31
vendor/anourvalar/eloquent-serialize/tests/SelectTest.php
vendored
Normal file
31
vendor/anourvalar/eloquent-serialize/tests/SelectTest.php
vendored
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace AnourValar\EloquentSerialize\Tests;
|
||||
|
||||
use AnourValar\EloquentSerialize\Tests\Models\User;
|
||||
use AnourValar\EloquentSerialize\Tests\Models\UserPhone;
|
||||
|
||||
class SelectTest extends AbstractSuite
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testSimple()
|
||||
{
|
||||
// List of columns
|
||||
$this->compare(User::select(['id', 'title']));
|
||||
|
||||
// Raw
|
||||
$this->compare(User::selectRaw('id, (SELECT COUNT(*) FROM user_phones WHERE user_id = users.id)'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testExpression()
|
||||
{
|
||||
$this->compare(
|
||||
User::select(['users.*', 'test' => UserPhone::selectRaw('MAX(created_at)')->whereColumn('user_id', 'users.id')])
|
||||
);
|
||||
}
|
||||
}
|
||||
79
vendor/anourvalar/eloquent-serialize/tests/UnionTest.php
vendored
Normal file
79
vendor/anourvalar/eloquent-serialize/tests/UnionTest.php
vendored
Normal file
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
namespace AnourValar\EloquentSerialize\Tests;
|
||||
|
||||
use AnourValar\EloquentSerialize\Tests\Models\User;
|
||||
|
||||
class UnionTest extends AbstractSuite
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testSimple()
|
||||
{
|
||||
$this->compare(
|
||||
User::whereIn('title', ['a', 'b'])->union(User::whereIn('id', ['1', '2']))
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testNested()
|
||||
{
|
||||
// 2 levels
|
||||
$union = User::where('id', '=', '1');
|
||||
$union = User::where('id', '=', '2')->union($union);
|
||||
|
||||
$this->compare(
|
||||
User::whereIn('title', ['a', 'b'])->union($union)
|
||||
);
|
||||
|
||||
// 3 levels
|
||||
$union = User::where('id', '=', '1');
|
||||
$union = User::where('id', '=', '2')->union($union);
|
||||
$union = User::where('id', '=', '3')->union($union);
|
||||
|
||||
$this->compare(
|
||||
User::whereIn('title', ['a', 'b'])->union($union)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testExpression()
|
||||
{
|
||||
$union1 = User::where(function ($query) {
|
||||
$query->whereDoesnthave('userPhones', function ($query) {
|
||||
$query->where(function ($query) {
|
||||
$query
|
||||
->where('created_at', '>=', '2010-01-01')
|
||||
->orWhere('id', '=', '1');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
$union2 = User::whereHas('userPhones', function ($query) {
|
||||
$query->where('created_at', '>=', '2010-01-01');
|
||||
});
|
||||
|
||||
$union2 = User::union($union2)->where(function ($query) {
|
||||
$query
|
||||
->where('id', '=', '1')
|
||||
->orWhere(function ($query) {
|
||||
$query
|
||||
->where('id', '=', '2')
|
||||
->orWhere(function ($query) {
|
||||
$query
|
||||
->where('title', '!=', 'admin')
|
||||
->orWhere('id', '=', '3');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
$this->compare(
|
||||
User::whereNotIn('title', ['a', 'b'])->union($union1)->union($union2, true)
|
||||
);
|
||||
}
|
||||
}
|
||||
285
vendor/anourvalar/eloquent-serialize/tests/WhereTest.php
vendored
Normal file
285
vendor/anourvalar/eloquent-serialize/tests/WhereTest.php
vendored
Normal file
@@ -0,0 +1,285 @@
|
||||
<?php
|
||||
|
||||
namespace AnourValar\EloquentSerialize\Tests;
|
||||
|
||||
use AnourValar\EloquentSerialize\Tests\Models\Post;
|
||||
use AnourValar\EloquentSerialize\Tests\Models\User;
|
||||
use AnourValar\EloquentSerialize\Tests\Models\UserPhone;
|
||||
|
||||
class WhereTest extends AbstractSuite
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testSimple()
|
||||
{
|
||||
// One column
|
||||
$this->compare(User::where('id', '=', '1'));
|
||||
|
||||
// Two columns
|
||||
$this->compare(User::where('id', '=', '1')->orWhere('id', '=', '2'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testExpression()
|
||||
{
|
||||
// Raw
|
||||
$this->compare(
|
||||
User::whereRaw('(id = ? or (SELECT COUNT(*) FROM user_phones WHERE user_id = users.id) > ?)', [5, 1])
|
||||
);
|
||||
|
||||
// DB Raw
|
||||
$this->compare(
|
||||
User::where(\DB::raw('(id = ? or (SELECT COUNT(*) FROM user_phones WHERE user_id = users.id) > ?)', [5, 1]))
|
||||
);
|
||||
|
||||
// 1 level
|
||||
$this->compare(
|
||||
User::where(function ($query) {
|
||||
$query->where('id', '=', '1')->orWhere('id', '=', 2);
|
||||
})
|
||||
);
|
||||
|
||||
// 2 levels
|
||||
$this->compare(
|
||||
User::where(function ($query) {
|
||||
$query
|
||||
->where('id', '=', '1')
|
||||
->orWhere(function ($query) {
|
||||
$query->where('id', '=', '2')->where('title', '!=', 'admin');
|
||||
});
|
||||
})
|
||||
);
|
||||
|
||||
// 3 levels
|
||||
$this->compare(
|
||||
User::where(function ($query) {
|
||||
$query
|
||||
->where('id', '=', '1')
|
||||
->orWhere(function ($query) {
|
||||
$query
|
||||
->where('id', '=', '2')
|
||||
->orWhere(function ($query) {
|
||||
$query
|
||||
->where('title', '!=', 'admin')
|
||||
->orWhere('id', '=', '3');
|
||||
});
|
||||
});
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testHas()
|
||||
{
|
||||
// has
|
||||
$this->compare(User::has('userPhones'));
|
||||
$this->compare(User::has('filesAB')->has('filesC'));
|
||||
|
||||
// whereHas, 1 level
|
||||
$this->compare(
|
||||
User::whereHas('userPhones', function ($query) {
|
||||
$query->where('created_at', '>=', '2010-01-01');
|
||||
})
|
||||
);
|
||||
|
||||
$this->compare(
|
||||
User::whereHas('filesAB', function ($query) {
|
||||
$query->whereIn('type', ['f', 'g']);
|
||||
})
|
||||
);
|
||||
|
||||
// whereHas, X levels
|
||||
$this->compare(
|
||||
User::where(function ($query) {
|
||||
$query->whereHas('userPhones', function ($query) {
|
||||
$query->where(function ($query) {
|
||||
$query
|
||||
->where('created_at', '>=', '2010-01-01')
|
||||
->orWhere('id', '=', '1');
|
||||
});
|
||||
});
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testWithHas()
|
||||
{
|
||||
// withWhereHas, 1 level
|
||||
$this->compare(
|
||||
User::withWhereHas('userPhones', function ($query) {
|
||||
$query->where('created_at', '>=', '2010-01-01');
|
||||
})
|
||||
);
|
||||
|
||||
$this->compare(
|
||||
User::withWhereHas('userPhones:id,is_primary')
|
||||
);
|
||||
|
||||
$this->compare(
|
||||
User::withWhereHas('filesAB', function ($query) {
|
||||
$query->whereIn('type', ['f', 'g']);
|
||||
})
|
||||
);
|
||||
|
||||
// withWhereHas, X levels
|
||||
$this->compare(
|
||||
User::where(function ($query) {
|
||||
$query->withWhereHas('userPhones', function ($query) {
|
||||
$query->where(function ($query) {
|
||||
$query
|
||||
->where('created_at', '>=', '2010-01-01')
|
||||
->orWhere('id', '=', '1');
|
||||
});
|
||||
});
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testNestedHas()
|
||||
{
|
||||
// has
|
||||
$this->compare(User::has('userPhones.userPhoneNote'));
|
||||
|
||||
// whereHas, 1 level
|
||||
$this->compare(
|
||||
User::whereHas('userPhones.userPhoneNote', function ($query) {
|
||||
$query->where('created_at', '>=', '2010-01-01');
|
||||
})
|
||||
);
|
||||
|
||||
// whereHas, X levels
|
||||
$this->compare(
|
||||
User::where(function ($query) {
|
||||
$query->whereHas('userPhones.userPhoneNote', function ($query) {
|
||||
$query->where(function ($query) {
|
||||
$query
|
||||
->where('created_at', '>=', '2010-01-01')
|
||||
->orWhere('id', '=', '1');
|
||||
});
|
||||
});
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testNestedWithHas()
|
||||
{
|
||||
// withWhereHas, 1 level
|
||||
$this->compare(
|
||||
User::withWhereHas('userPhones.userPhoneNote', function ($query) {
|
||||
$query->where('created_at', '>=', '2010-01-01');
|
||||
})
|
||||
);
|
||||
|
||||
// withWhereHas, X levels
|
||||
$this->compare(
|
||||
User::where(function ($query) {
|
||||
$query->withWhereHas('userPhones.userPhoneNote', function ($query) {
|
||||
$query->where(function ($query) {
|
||||
$query
|
||||
->where('created_at', '>=', '2010-01-01')
|
||||
->orWhere('id', '=', '1');
|
||||
});
|
||||
});
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testDoesnthave()
|
||||
{
|
||||
// doesnthave
|
||||
$this->compare(User::doesnthave('userPhones'));
|
||||
$this->compare(User::doesnthave('filesAB')->doesnthave('filesC'));
|
||||
|
||||
// whereDoesnthave
|
||||
$this->compare(
|
||||
User::whereDoesnthave('userPhones', function ($query) {
|
||||
$query->where('created_at', '>=', '2010-01-01');
|
||||
})
|
||||
);
|
||||
|
||||
// whereDoesnthave, X levels
|
||||
$this->compare(
|
||||
User::where(function ($query) {
|
||||
$query->whereDoesnthave('userPhones', function ($query) {
|
||||
$query->where(function ($query) {
|
||||
$query
|
||||
->where('created_at', '>=', '2010-01-01')
|
||||
->orWhere('id', '=', '1');
|
||||
});
|
||||
});
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testJson()
|
||||
{
|
||||
$this->compare(User::where('meta->foo', 'a'));
|
||||
|
||||
$this->compare(User::whereJsonContains('meta->foo', ['a']), false);
|
||||
|
||||
$this->compare(User::whereJsonDoesntContain('meta->foo', ['a']), false);
|
||||
|
||||
$this->compare(User::whereJsonLength('meta->foo', 0));
|
||||
$this->compare(User::whereJsonLength('meta->foo', '>', 1));
|
||||
|
||||
$this->compare(User::whereJsonContainsKey('meta->foo'));
|
||||
$this->compare(User::whereJsonContainsKey('meta->foo[0]'));
|
||||
$this->compare(User::whereJsonContainsKey('meta->foo->bar'));
|
||||
$this->compare(User::whereJsonContainsKey('meta->foo->bar[0]'));
|
||||
|
||||
$this->compare(User::whereJsonDoesntContainKey('meta->foo'));
|
||||
$this->compare(User::whereJsonDoesntContainKey('meta->foo[0]'));
|
||||
$this->compare(User::whereJsonDoesntContainKey('meta->foo->bar'));
|
||||
$this->compare(User::whereJsonDoesntContainKey('meta->foo->bar[0]'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testFullText()
|
||||
{
|
||||
// Simple
|
||||
$this->compare(Post::whereFullText('body', 'said'), false);
|
||||
|
||||
// With options
|
||||
$this->compare(Post::whereFullText('body', 'said', ['language' => 'russian']), false);
|
||||
|
||||
// Inside closure
|
||||
$this->compare(
|
||||
User::whereHas('userPhoneNote', function ($query) {
|
||||
$query->whereFullText('note', 'another', ['language' => 'russian']);
|
||||
}),
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testBelongsTo()
|
||||
{
|
||||
$this->compare(
|
||||
UserPhone::whereBelongsTo(UserPhone::has('user')->first()->user)
|
||||
);
|
||||
}
|
||||
}
|
||||
36
vendor/anourvalar/eloquent-serialize/tests/WithCastsTest.php
vendored
Normal file
36
vendor/anourvalar/eloquent-serialize/tests/WithCastsTest.php
vendored
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace AnourValar\EloquentSerialize\Tests;
|
||||
|
||||
use AnourValar\EloquentSerialize\Tests\Models\User;
|
||||
use AnourValar\EloquentSerialize\Tests\Models\UserPhone;
|
||||
|
||||
class WithCastsTest extends AbstractSuite
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testSimple()
|
||||
{
|
||||
// Common
|
||||
$this->compare(User::withCasts(['user_id' => 'integer']));
|
||||
|
||||
// Custom
|
||||
$this->compare(User::withCasts(['user_id' => \AnourValar\EloquentSerialize\Tests\Casts\TestCast::class]));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testSelectRaw()
|
||||
{
|
||||
$this->compare(
|
||||
User::select([
|
||||
'users.*',
|
||||
'last_phone_created_at' => UserPhone::selectRaw('MAX(created_at)')->whereColumn('user_id', 'users.id'),
|
||||
])->withCasts([
|
||||
'last_phone_created_at' => 'datetime',
|
||||
])
|
||||
);
|
||||
}
|
||||
}
|
||||
103
vendor/anourvalar/eloquent-serialize/tests/factories/Factory.php
vendored
Normal file
103
vendor/anourvalar/eloquent-serialize/tests/factories/Factory.php
vendored
Normal file
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
/** @var \Illuminate\Database\Eloquent\Factory $factory */
|
||||
|
||||
use AnourValar\EloquentSerialize\Tests\Models\File;
|
||||
use AnourValar\EloquentSerialize\Tests\Models\Tag;
|
||||
use AnourValar\EloquentSerialize\Tests\Models\Post;
|
||||
use AnourValar\EloquentSerialize\Tests\Models\User;
|
||||
use AnourValar\EloquentSerialize\Tests\Models\UserPhone;
|
||||
use AnourValar\EloquentSerialize\Tests\Models\UserPhoneNote;
|
||||
use Faker\Generator as Faker;
|
||||
|
||||
$factory->define(User::class, function (Faker $faker, array $attributes) {
|
||||
return [
|
||||
'title' => 'admin',
|
||||
'sort' => $faker->numberBetween(1, 10),
|
||||
'meta' => $faker->randomElement([json_encode(['foo' => 'a']), json_encode(['foo' => ['bar' => ['hello']]])]),
|
||||
'deleted_at' => mt_rand(0, 5) ? null : $faker->date('Y-m-d H:i:s'),
|
||||
];
|
||||
});
|
||||
|
||||
$factory->define(UserPhone::class, function (Faker $faker, array $attributes) {
|
||||
static $counter;
|
||||
$counter++;
|
||||
|
||||
return [
|
||||
'user_id' => function () use ($counter) {
|
||||
if (! ($counter % 2)) {
|
||||
$id = User::max('id');
|
||||
if ($id) {
|
||||
return $id;
|
||||
}
|
||||
}
|
||||
|
||||
return factory(User::class)->create();
|
||||
},
|
||||
'phone' => $faker->phoneNumber,
|
||||
'is_primary' => $faker->boolean,
|
||||
];
|
||||
});
|
||||
|
||||
$factory->define(UserPhoneNote::class, function (Faker $faker, array $attributes) {
|
||||
static $counter;
|
||||
$counter++;
|
||||
|
||||
return [
|
||||
'user_phone_id' => function () use ($counter) {
|
||||
if (! ($counter % 2)) {
|
||||
$id = UserPhone::max('id');
|
||||
if ($id) {
|
||||
return $id;
|
||||
}
|
||||
}
|
||||
|
||||
return factory(UserPhone::class)->create();
|
||||
},
|
||||
'note' => $faker->realText(100),
|
||||
];
|
||||
});
|
||||
|
||||
$factory->define(File::class, function (Faker $faker, array $attributes) {
|
||||
static $users;
|
||||
if (! $users) {
|
||||
$users = User::get(['id']);
|
||||
}
|
||||
|
||||
return [
|
||||
'user_id' => function () use ($users) {
|
||||
return $users->shuffle()->first();
|
||||
},
|
||||
'type' => $faker->randomElement(['a', 'b', 'c', 'd', 'e', 'f']),
|
||||
];
|
||||
});
|
||||
|
||||
$factory->define(Post::class, function (Faker $faker, array $attributes) {
|
||||
static $users;
|
||||
if (! $users) {
|
||||
$users = User::get(['id']);
|
||||
}
|
||||
|
||||
return [
|
||||
'user_id' => function () use ($users) {
|
||||
return $users->shuffle()->first();
|
||||
},
|
||||
'title' => $faker->sentence(),
|
||||
'body' => $faker->sentence(),
|
||||
];
|
||||
});
|
||||
|
||||
$factory->define(Tag::class, function (Faker $faker, array $attributes) {
|
||||
static $posts;
|
||||
if (! $posts) {
|
||||
$posts = Post::get(['id']);
|
||||
}
|
||||
|
||||
return [
|
||||
'title' => $faker->sentence(),
|
||||
'taggable_id' => function () use ($posts) {
|
||||
return $posts->shuffle()->first();
|
||||
},
|
||||
'taggable_type' => Post::class,
|
||||
];
|
||||
});
|
||||
Reference in New Issue
Block a user