[增添]添加了datasource的setting数据库以及默认值
This commit is contained in:
48
vendor/anourvalar/eloquent-serialize/.github/workflows/run-tests.yml
vendored
Normal file
48
vendor/anourvalar/eloquent-serialize/.github/workflows/run-tests.yml
vendored
Normal file
@@ -0,0 +1,48 @@
|
||||
name: run-tests
|
||||
|
||||
on: [push, pull_request]
|
||||
|
||||
jobs:
|
||||
test:
|
||||
runs-on: ${{ matrix.os }}
|
||||
strategy:
|
||||
fail-fast: true
|
||||
matrix:
|
||||
os: [ubuntu-latest]
|
||||
php: [8.3, 8.2, 8.1]
|
||||
laravel: [11.*, 10.*]
|
||||
stability: [prefer-stable]
|
||||
include:
|
||||
- laravel: 11.*
|
||||
testbench: 9.*
|
||||
- laravel: 10.*
|
||||
testbench: 8.*
|
||||
exclude:
|
||||
- laravel: 11.*
|
||||
php: 8.1
|
||||
|
||||
name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }}
|
||||
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Setup PHP
|
||||
uses: shivammathur/setup-php@v2
|
||||
with:
|
||||
php-version: ${{ matrix.php }}
|
||||
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick
|
||||
coverage: none
|
||||
|
||||
- name: Setup problem matchers
|
||||
run: |
|
||||
echo "::add-matcher::${{ runner.tool_cache }}/php.json"
|
||||
echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"
|
||||
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
|
||||
composer update --${{ matrix.stability }} --prefer-dist --no-interaction
|
||||
|
||||
- name: Execute tests
|
||||
run: vendor/bin/phpunit
|
||||
5
vendor/anourvalar/eloquent-serialize/.gitignore
vendored
Normal file
5
vendor/anourvalar/eloquent-serialize/.gitignore
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
/vendor
|
||||
composer.lock
|
||||
.idea
|
||||
.phpunit.result.cache
|
||||
.DS_Store
|
||||
11
vendor/anourvalar/eloquent-serialize/.php-cs-fixer.php
vendored
Normal file
11
vendor/anourvalar/eloquent-serialize/.php-cs-fixer.php
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
$finder = PhpCsFixer\Finder::create()
|
||||
->exclude('vendor')
|
||||
->in(__DIR__);
|
||||
|
||||
$config = new PhpCsFixer\Config();
|
||||
return $config->setRules([
|
||||
'@PSR12' => true,
|
||||
])
|
||||
->setFinder($finder);
|
||||
21
vendor/anourvalar/eloquent-serialize/LICENSE
vendored
Normal file
21
vendor/anourvalar/eloquent-serialize/LICENSE
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2019 AnourValar
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
31
vendor/anourvalar/eloquent-serialize/README.md
vendored
Normal file
31
vendor/anourvalar/eloquent-serialize/README.md
vendored
Normal file
@@ -0,0 +1,31 @@
|
||||
# Serialization for Eloquent's QueryBuilder
|
||||
Supports: Laravel 6 - Laravel 11
|
||||
|
||||
## Installation
|
||||
|
||||
```bash
|
||||
composer require anourvalar/eloquent-serialize
|
||||
```
|
||||
|
||||
|
||||
## Usage
|
||||
|
||||
### Serialize
|
||||
```php
|
||||
$package = \EloquentSerialize::serialize(
|
||||
\App\User::query()
|
||||
->with('userPhones')
|
||||
->where('id', '>', '10')
|
||||
->limit(20)
|
||||
);
|
||||
```
|
||||
|
||||
|
||||
### Unserialize
|
||||
```php
|
||||
$builder = \EloquentSerialize::unserialize($package);
|
||||
|
||||
foreach ($builder->get() as $item) {
|
||||
// ...
|
||||
}
|
||||
```
|
||||
33
vendor/anourvalar/eloquent-serialize/composer.json
vendored
Normal file
33
vendor/anourvalar/eloquent-serialize/composer.json
vendored
Normal file
@@ -0,0 +1,33 @@
|
||||
{
|
||||
"name": "anourvalar/eloquent-serialize",
|
||||
"description": "Laravel Query Builder (Eloquent) serialization",
|
||||
"keywords": ["laravel", "query", "builder", "querybuilder", "anourvalar", "serialize", "serialization", "eloquent", "serializable", "job", "queue", "copy"],
|
||||
"homepage": "https://github.com/AnourValar/eloquent-serialize",
|
||||
"license": "MIT",
|
||||
"require": {
|
||||
"php": "^7.4|^8.0",
|
||||
"laravel/framework": "^8.0|^9.0|^10.0|^11.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^9.5|^10.5",
|
||||
"orchestra/testbench": "^6.0|^7.0|^8.0|^9.0",
|
||||
"laravel/legacy-factories": "^1.1",
|
||||
"phpstan/phpstan": "^1.10",
|
||||
"friendsofphp/php-cs-fixer": "^3.26",
|
||||
"squizlabs/php_codesniffer": "^3.7",
|
||||
"psalm/plugin-laravel": "^2.8"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {"AnourValar\\EloquentSerialize\\": "src/"}
|
||||
},
|
||||
"autoload-dev": {
|
||||
"psr-4": {"AnourValar\\EloquentSerialize\\Tests\\": "tests/"}
|
||||
},
|
||||
"extra": {
|
||||
"laravel": {
|
||||
"aliases": {
|
||||
"EloquentSerialize": "AnourValar\\EloquentSerialize\\Facades\\EloquentSerializeFacade"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
37
vendor/anourvalar/eloquent-serialize/phpcs.xml
vendored
Normal file
37
vendor/anourvalar/eloquent-serialize/phpcs.xml
vendored
Normal file
@@ -0,0 +1,37 @@
|
||||
<?xml version="1.0"?>
|
||||
<!-- @see https://pear.php.net/manual/en/package.php.php-codesniffer.annotated-ruleset.php -->
|
||||
<ruleset name="PHPCS Rules">
|
||||
|
||||
<description>PHPCS ruleset</description>
|
||||
|
||||
<file>src</file>
|
||||
<file>tests</file>
|
||||
<exclude-pattern>src/resources</exclude-pattern>
|
||||
|
||||
<!-- Show progress of the run -->
|
||||
<arg value= "p"/>
|
||||
|
||||
<!-- Show sniff codes in all reports -->
|
||||
<arg value= "s"/>
|
||||
|
||||
<!-- Our base rule: set to PSR12 -->
|
||||
<rule ref="PSR12">
|
||||
<exclude name="PSR12.Operators.OperatorSpacing.NoSpaceBefore"/>
|
||||
<exclude name="PSR12.Operators.OperatorSpacing.NoSpaceAfter"/>
|
||||
<exclude name="PSR12.Traits.UseDeclaration.MultipleImport"/>
|
||||
<exclude name="Generic.Files.LineLength.TooLong"/>
|
||||
<exclude name="PSR2.Methods.FunctionClosingBrace.SpacingBeforeClose"/>
|
||||
<exclude name="PSR12.Classes.OpeningBraceSpace.Found"/>
|
||||
<exclude name="Squiz.WhiteSpace.ControlStructureSpacing.SpacingAfterOpen"/>
|
||||
<exclude name="Squiz.WhiteSpace.ControlStructureSpacing.SpacingBeforeClose"/>
|
||||
</rule>
|
||||
|
||||
<rule ref="PSR1.Methods.CamelCapsMethodName.NotCamelCaps">
|
||||
<exclude-pattern>tests/</exclude-pattern>
|
||||
</rule>
|
||||
|
||||
<rule ref="Internal.NoCodeFound">
|
||||
<exclude-pattern>tests/</exclude-pattern>
|
||||
</rule>
|
||||
|
||||
</ruleset>
|
||||
30
vendor/anourvalar/eloquent-serialize/phpstan.neon
vendored
Normal file
30
vendor/anourvalar/eloquent-serialize/phpstan.neon
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
parameters:
|
||||
|
||||
paths:
|
||||
- src
|
||||
- tests
|
||||
|
||||
# The level 9 is the highest level
|
||||
level: 5
|
||||
|
||||
ignoreErrors:
|
||||
- '#Access to protected property#'
|
||||
- '#should return#'
|
||||
- '#Call to an undefined static method#'
|
||||
- '#Call to an undefined method#'
|
||||
- '#Call to static method#'
|
||||
- '#Parameter \#1 \$callback of method Illuminate\\Database\\Eloquent\\Pending#'
|
||||
- '#Property Illuminate\\Database\\Query\\Builder\:\:\$groupLimit \(array\)#'
|
||||
|
||||
excludePaths:
|
||||
|
||||
checkFunctionNameCase: true
|
||||
checkInternalClassCaseSensitivity: true
|
||||
checkAlwaysTrueInstanceof: true
|
||||
reportMaybesInMethodSignatures: true
|
||||
reportStaticMethodSignatures: true
|
||||
checkUninitializedProperties: true
|
||||
checkDynamicProperties: true
|
||||
reportAlwaysTrueInLastCondition: true
|
||||
reportWrongPhpDocTypeInVarTag: true
|
||||
checkMissingCallableSignature: true
|
||||
26
vendor/anourvalar/eloquent-serialize/phpunit.xml
vendored
Normal file
26
vendor/anourvalar/eloquent-serialize/phpunit.xml
vendored
Normal file
@@ -0,0 +1,26 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<phpunit
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
bootstrap="vendor/autoload.php"
|
||||
backupGlobals="false"
|
||||
colors="true"
|
||||
processIsolation="true"
|
||||
stopOnFailure="true"
|
||||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
|
||||
cacheDirectory=".phpunit.cache"
|
||||
backupStaticProperties="false"
|
||||
>
|
||||
<testsuites>
|
||||
<testsuite name="EloquentSerialize">
|
||||
<directory>tests</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
<php>
|
||||
<env name="DB_CONNECTION" value="testing"/>
|
||||
</php>
|
||||
<source>
|
||||
<include>
|
||||
<directory suffix=".php">src/</directory>
|
||||
</include>
|
||||
</source>
|
||||
</phpunit>
|
||||
29
vendor/anourvalar/eloquent-serialize/phpunit9.xml
vendored
Normal file
29
vendor/anourvalar/eloquent-serialize/phpunit9.xml
vendored
Normal file
@@ -0,0 +1,29 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<phpunit
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
bootstrap="vendor/autoload.php"
|
||||
backupGlobals="false"
|
||||
backupStaticAttributes="false"
|
||||
colors="true"
|
||||
verbose="true"
|
||||
convertErrorsToExceptions="true"
|
||||
convertNoticesToExceptions="true"
|
||||
convertWarningsToExceptions="true"
|
||||
processIsolation="true"
|
||||
stopOnFailure="true"
|
||||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
|
||||
>
|
||||
<coverage>
|
||||
<include>
|
||||
<directory suffix=".php">src/</directory>
|
||||
</include>
|
||||
</coverage>
|
||||
<testsuites>
|
||||
<testsuite name="EloquentSerialize">
|
||||
<directory>tests</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
<php>
|
||||
<env name="DB_CONNECTION" value="testing"/>
|
||||
</php>
|
||||
</phpunit>
|
||||
47
vendor/anourvalar/eloquent-serialize/psalm.xml
vendored
Normal file
47
vendor/anourvalar/eloquent-serialize/psalm.xml
vendored
Normal file
@@ -0,0 +1,47 @@
|
||||
<?xml version="1.0"?>
|
||||
<psalm
|
||||
errorLevel="7"
|
||||
resolveFromConfigFile="true"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="https://getpsalm.org/schema/config"
|
||||
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
|
||||
findUnusedBaselineEntry="true"
|
||||
findUnusedCode="true"
|
||||
findUnusedPsalmSuppress="true"
|
||||
findUnusedVariablesAndParams="true"
|
||||
>
|
||||
<projectFiles>
|
||||
<directory name="src"/>
|
||||
<directory name="tests"/>
|
||||
<ignoreFiles>
|
||||
<directory name="vendor"/>
|
||||
<directory name="tests/factories/"/>
|
||||
<directory name="tests/Models/"/>
|
||||
</ignoreFiles>
|
||||
</projectFiles>
|
||||
<plugins>
|
||||
<pluginClass class="Psalm\LaravelPlugin\Plugin"/>
|
||||
</plugins>
|
||||
|
||||
<issueHandlers>
|
||||
<ForbiddenCode errorLevel="error" />
|
||||
<UnusedClosureParam errorLevel="suppress" />
|
||||
<PossiblyUnusedMethod errorLevel="suppress" />
|
||||
<UnusedClass errorLevel="suppress" />
|
||||
<PossiblyUnusedParam errorLevel="suppress" />
|
||||
<PossiblyUnusedProperty errorLevel="suppress" />
|
||||
<PossiblyUnusedReturnValue errorLevel="suppress" />
|
||||
<MissingTemplateParam errorLevel="suppress" />
|
||||
<UnsupportedPropertyReferenceUsage errorLevel="suppress" />
|
||||
<InaccessibleProperty errorLevel="suppress" />
|
||||
<MissingDependency errorLevel="suppress" />
|
||||
<UnusedMethod errorLevel="suppress" />
|
||||
</issueHandlers>
|
||||
|
||||
<forbiddenFunctions>
|
||||
<function name="var_dump" />
|
||||
<function name="dd" />
|
||||
<function name="dump" />
|
||||
<function name="print_r" />
|
||||
</forbiddenFunctions>
|
||||
</psalm>
|
||||
20
vendor/anourvalar/eloquent-serialize/src/Facades/EloquentSerializeFacade.php
vendored
Normal file
20
vendor/anourvalar/eloquent-serialize/src/Facades/EloquentSerializeFacade.php
vendored
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace AnourValar\EloquentSerialize\Facades;
|
||||
|
||||
use Illuminate\Support\Facades\Facade;
|
||||
|
||||
/**
|
||||
* @method static string serialize(\Illuminate\Database\Eloquent\Builder $builder)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder unserialize(mixed $package)
|
||||
*/
|
||||
class EloquentSerializeFacade extends Facade
|
||||
{
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
protected static function getFacadeAccessor()
|
||||
{
|
||||
return \AnourValar\EloquentSerialize\Service::class;
|
||||
}
|
||||
}
|
||||
147
vendor/anourvalar/eloquent-serialize/src/Grammars/EloquentBuilderGrammar.php
vendored
Normal file
147
vendor/anourvalar/eloquent-serialize/src/Grammars/EloquentBuilderGrammar.php
vendored
Normal file
@@ -0,0 +1,147 @@
|
||||
<?php
|
||||
|
||||
namespace AnourValar\EloquentSerialize\Grammars;
|
||||
|
||||
trait EloquentBuilderGrammar
|
||||
{
|
||||
/**
|
||||
* Serialize state for \Illuminate\Database\Eloquent\Builder
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Builder $builder
|
||||
* @return array
|
||||
*/
|
||||
protected function packEloquentBuilder(\Illuminate\Database\Eloquent\Builder $builder): array
|
||||
{
|
||||
return [
|
||||
'with' => $this->getEagers($builder), // preloaded ("eager") relations
|
||||
'removed_scopes' => $builder->removedScopes(), // global scopes
|
||||
'casts' => $builder->getModel()->getCasts(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Unserialize state for \Illuminate\Database\Eloquent\Builder
|
||||
*
|
||||
* @param array $data
|
||||
* @param \Illuminate\Database\Eloquent\Builder $builder
|
||||
* @return void
|
||||
*/
|
||||
protected function unpackEloquentBuilder(array $data, \Illuminate\Database\Eloquent\Builder &$builder): void
|
||||
{
|
||||
// Preloaded ("eager") relations
|
||||
$this->setEagers($builder, $data['with']);
|
||||
|
||||
// Global scopes
|
||||
if ($data['removed_scopes']) {
|
||||
$builder->withoutGlobalScopes($data['removed_scopes']);
|
||||
}
|
||||
|
||||
// Casts
|
||||
if (method_exists($builder->getModel(), 'mergeCasts')) { // old versions support
|
||||
$builder->getModel()->mergeCasts($data['casts']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Illuminate\Database\Eloquent\Builder $builder
|
||||
* @return array
|
||||
*/
|
||||
private function getEagers(\Illuminate\Database\Eloquent\Builder $builder): array
|
||||
{
|
||||
$result = [];
|
||||
|
||||
foreach ($builder->getEagerLoads() as $name => $value) {
|
||||
$relation = $builder;
|
||||
foreach (explode('.', $name) as $part) {
|
||||
$relation = $relation->getRelation($part); // get a relation without "constraints"
|
||||
}
|
||||
$referenceRelation = clone $relation;
|
||||
|
||||
$value($relation); // apply closure
|
||||
$result[$name] = [
|
||||
'query' => $this->packQueryBuilder($relation->getQuery()->getQuery()),
|
||||
'eloquent' => $this->packEloquentBuilder($relation->getQuery()),
|
||||
'extra' => $relation->exportExtraParametersForSerialize(),
|
||||
];
|
||||
|
||||
$relation->getQuery()->getModel()->newInstance()->with($name)->getEagerLoads()[$name]($referenceRelation);
|
||||
$this->cleanStaticConstraints($result[$name]['query'], $this->packQueryBuilder($referenceRelation->getQuery()->getQuery()));
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Illuminate\Database\Eloquent\Builder $builder
|
||||
* @param array $eagers
|
||||
* @return void
|
||||
*/
|
||||
private function setEagers(\Illuminate\Database\Eloquent\Builder $builder, array $eagers): void
|
||||
{
|
||||
foreach ($eagers as &$value) {
|
||||
$value = function ($query) use ($value) {
|
||||
if (isset($value['extra'])) {
|
||||
$query->importExtraParametersForSerialize($value['extra']);
|
||||
}
|
||||
|
||||
// Input argument may be different depends on context
|
||||
while (! ($query instanceof \Illuminate\Database\Eloquent\Builder)) {
|
||||
$query = $query->getQuery();
|
||||
}
|
||||
if (isset($value['eloquent'])) {
|
||||
$this->unpackEloquentBuilder($value['eloquent'], $query);
|
||||
}
|
||||
|
||||
// Input argument may be different depends on context
|
||||
while (! ($query instanceof \Illuminate\Database\Query\Builder)) {
|
||||
$query = $query->getQuery();
|
||||
}
|
||||
|
||||
$this->unpackQueryBuilder(isset($value['query']) ? $value['query'] : $value, $query);
|
||||
};
|
||||
}
|
||||
unset($value);
|
||||
|
||||
$builder->setEagerLoads($eagers);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $packedQueryBuilder
|
||||
* @param array $packedReferenceQueryBuilder
|
||||
* @return void
|
||||
*/
|
||||
private function cleanStaticConstraints(array &$packedQueryBuilder, array $packedReferenceQueryBuilder): void
|
||||
{
|
||||
$properties = [
|
||||
'aggregate', 'columns', 'distinct', 'wheres', 'groups', 'havings', 'orders', 'limit', 'offset', 'unions',
|
||||
'unionLimit', 'unionOffset', 'unionOrders', 'joins', 'groupLimit',
|
||||
];
|
||||
|
||||
foreach ($properties as $property) {
|
||||
if (! is_array($packedQueryBuilder[$property] ?? null)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($packedQueryBuilder[$property] as $key => $item) {
|
||||
if (in_array($item, (array) ($packedReferenceQueryBuilder[$property] ?? null), true)) {
|
||||
unset($packedQueryBuilder[$property][$key]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($packedQueryBuilder['bindings'] as $binding => $data) {
|
||||
if (! is_array($data)) {
|
||||
continue; // just in case ;)
|
||||
}
|
||||
|
||||
foreach ($data as $key => $value) {
|
||||
if (
|
||||
isset($packedReferenceQueryBuilder['bindings'][$binding][$key])
|
||||
&& $packedReferenceQueryBuilder['bindings'][$binding][$key] === $value
|
||||
) {
|
||||
unset($packedQueryBuilder['bindings'][$binding][$key]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
69
vendor/anourvalar/eloquent-serialize/src/Grammars/ModelGrammar.php
vendored
Normal file
69
vendor/anourvalar/eloquent-serialize/src/Grammars/ModelGrammar.php
vendored
Normal file
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace AnourValar\EloquentSerialize\Grammars;
|
||||
|
||||
trait ModelGrammar
|
||||
{
|
||||
/**
|
||||
* Pack
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Builder $builder
|
||||
* @return \AnourValar\EloquentSerialize\Package
|
||||
*/
|
||||
protected function pack(\Illuminate\Database\Eloquent\Builder $builder): \AnourValar\EloquentSerialize\Package
|
||||
{
|
||||
$this->setup();
|
||||
|
||||
return new \AnourValar\EloquentSerialize\Package([
|
||||
'model' => get_class($builder->getModel()),
|
||||
'connection' => $builder->getModel()->getConnectionName(),
|
||||
'eloquent' => $this->packEloquentBuilder($builder),
|
||||
'query' => $this->packQueryBuilder($builder->getQuery()),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unpack
|
||||
*
|
||||
* @param \AnourValar\EloquentSerialize\Package $package
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
protected function unpack(\AnourValar\EloquentSerialize\Package $package): \Illuminate\Database\Eloquent\Builder
|
||||
{
|
||||
$this->setup();
|
||||
|
||||
$builder = $package->get('model');
|
||||
$builder = $builder::on($package->get('connection'));
|
||||
|
||||
$this->unpackEloquentBuilder($package->get('eloquent'), $builder);
|
||||
$this->unpackQueryBuilder($package->get('query'), $builder->getQuery());
|
||||
|
||||
return $builder;
|
||||
}
|
||||
|
||||
/**
|
||||
* init
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function setup(): void
|
||||
{
|
||||
\Illuminate\Database\Eloquent\Relations\Relation::macro('importExtraParametersForSerialize', function (array $params) {
|
||||
foreach ($params as $key => $value) {
|
||||
$this->$key = $value;
|
||||
}
|
||||
});
|
||||
|
||||
\Illuminate\Database\Eloquent\Relations\Relation::macro('exportExtraParametersForSerialize', function () {
|
||||
if ($this instanceof \Illuminate\Database\Eloquent\Relations\MorphTo) {
|
||||
return [
|
||||
'morphableEagerLoads' => $this->morphableEagerLoads,
|
||||
'morphableEagerLoadCounts' => $this->morphableEagerLoadCounts,
|
||||
'morphableConstraints' => $this->morphableConstraints,
|
||||
];
|
||||
}
|
||||
|
||||
return null;
|
||||
});
|
||||
}
|
||||
}
|
||||
182
vendor/anourvalar/eloquent-serialize/src/Grammars/QueryBuilderGrammar.php
vendored
Normal file
182
vendor/anourvalar/eloquent-serialize/src/Grammars/QueryBuilderGrammar.php
vendored
Normal file
@@ -0,0 +1,182 @@
|
||||
<?php
|
||||
|
||||
namespace AnourValar\EloquentSerialize\Grammars;
|
||||
|
||||
trait QueryBuilderGrammar
|
||||
{
|
||||
/**
|
||||
* Serialize state for \Illuminate\Database\Query\Builder
|
||||
*
|
||||
* @param \Illuminate\Database\Query\Builder $builder
|
||||
* @return array
|
||||
*/
|
||||
protected function packQueryBuilder(\Illuminate\Database\Query\Builder $builder): array
|
||||
{
|
||||
return array_filter([
|
||||
'bindings' => $builder->bindings,
|
||||
'aggregate' => $builder->aggregate,
|
||||
'columns' => $builder->columns,
|
||||
'distinct' => $builder->distinct,
|
||||
'from' => $builder->from,
|
||||
'wheres' => $this->packWheres($builder->wheres),
|
||||
'groups' => $builder->groups,
|
||||
'havings' => $builder->havings,
|
||||
'groupLimit' => $builder->groupLimit ?? null,
|
||||
'orders' => $builder->orders,
|
||||
'limit' => $builder->limit,
|
||||
'offset' => $builder->offset,
|
||||
'unions' => $this->packUnions($builder->unions),
|
||||
'unionLimit' => $builder->unionLimit,
|
||||
'unionOffset' => $builder->unionOffset,
|
||||
'unionOrders' => $builder->unionOrders,
|
||||
|
||||
'joins' => $this->packJoins($builder->joins), // must be the last
|
||||
], fn ($item) => isset($item));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @param \Illuminate\Database\Query\Builder $builder
|
||||
* @return \Illuminate\Database\Query\Builder
|
||||
*/
|
||||
protected function unpackQueryBuilder(array $data, \Illuminate\Database\Query\Builder $builder): \Illuminate\Database\Query\Builder
|
||||
{
|
||||
foreach ($data as $key => $value) {
|
||||
if ($key == 'wheres') {
|
||||
$value = $this->unpackWheres($value, $builder);
|
||||
}
|
||||
|
||||
if ($key == 'unions') {
|
||||
$value = $this->unpackUnions($value);
|
||||
}
|
||||
|
||||
if ($key == 'joins') {
|
||||
$value = $this->unpackJoins($value, $builder);
|
||||
}
|
||||
|
||||
if (is_array($builder->$key) && is_array($value)) {
|
||||
$builder->$key = array_merge_recursive($builder->$key, $value);
|
||||
} else {
|
||||
$builder->$key = $value;
|
||||
}
|
||||
}
|
||||
|
||||
return $builder;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $wheres
|
||||
* @return mixed
|
||||
*/
|
||||
private function packWheres($wheres)
|
||||
{
|
||||
foreach ($wheres as &$item) {
|
||||
if (isset($item['query'])) {
|
||||
$item['query'] = $this->packQueryBuilder($item['query']);
|
||||
}
|
||||
}
|
||||
unset($item);
|
||||
|
||||
return $wheres;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $unions
|
||||
* @return mixed
|
||||
*/
|
||||
private function packUnions($unions)
|
||||
{
|
||||
if (! is_array($unions)) {
|
||||
return $unions;
|
||||
}
|
||||
|
||||
foreach ($unions as &$item) {
|
||||
if (isset($item['query'])) {
|
||||
$item['query'] = $this->pack($item['query']);
|
||||
}
|
||||
}
|
||||
unset($item);
|
||||
|
||||
return $unions;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $joins
|
||||
* @return mixed
|
||||
*/
|
||||
private function packJoins($joins)
|
||||
{
|
||||
if (! is_array($joins)) {
|
||||
return $joins;
|
||||
}
|
||||
|
||||
foreach ($joins as &$item) {
|
||||
$item = array_replace(
|
||||
['type' => $item->type, 'table' => $item->table],
|
||||
$this->packQueryBuilder($item)
|
||||
);
|
||||
}
|
||||
unset($item);
|
||||
|
||||
return $joins;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $wheres
|
||||
* @param \Illuminate\Database\Query\Builder $builder
|
||||
* @return mixed
|
||||
*/
|
||||
private function unpackWheres($wheres, \Illuminate\Database\Query\Builder $builder)
|
||||
{
|
||||
foreach ($wheres as &$item) {
|
||||
if (isset($item['query'])) {
|
||||
$item['query'] = $this->unpackQueryBuilder($item['query'], $builder->newQuery());
|
||||
}
|
||||
}
|
||||
unset($item);
|
||||
|
||||
return $wheres;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $unions
|
||||
* @return mixed
|
||||
*/
|
||||
private function unpackUnions($unions)
|
||||
{
|
||||
if (! is_array($unions)) {
|
||||
return $unions;
|
||||
}
|
||||
|
||||
foreach ($unions as &$item) {
|
||||
if (isset($item['query'])) {
|
||||
$item['query'] = $this->unpack($item['query']);
|
||||
}
|
||||
}
|
||||
unset($item);
|
||||
|
||||
return $unions;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $joins
|
||||
* @param \Illuminate\Database\Query\Builder $builder
|
||||
* @return mixed
|
||||
*/
|
||||
private function unpackJoins($joins, \Illuminate\Database\Query\Builder $builder)
|
||||
{
|
||||
if (! is_array($joins)) {
|
||||
return $joins;
|
||||
}
|
||||
|
||||
foreach ($joins as &$item) {
|
||||
$parentQuery = new \Illuminate\Database\Query\JoinClause($builder, $item['type'], $item['table']);
|
||||
unset($item['type'], $item['table']);
|
||||
|
||||
$item = $this->unpackQueryBuilder($item, $parentQuery);
|
||||
}
|
||||
unset($item);
|
||||
|
||||
return $joins;
|
||||
}
|
||||
}
|
||||
33
vendor/anourvalar/eloquent-serialize/src/Package.php
vendored
Normal file
33
vendor/anourvalar/eloquent-serialize/src/Package.php
vendored
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace AnourValar\EloquentSerialize;
|
||||
|
||||
class Package
|
||||
{
|
||||
/**
|
||||
* $var array
|
||||
*/
|
||||
private $data;
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(array $data)
|
||||
{
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @return mixed
|
||||
*/
|
||||
public function get(string $key = null)
|
||||
{
|
||||
if (is_null($key)) {
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
return ($this->data[$key] ?? null);
|
||||
}
|
||||
}
|
||||
44
vendor/anourvalar/eloquent-serialize/src/Service.php
vendored
Normal file
44
vendor/anourvalar/eloquent-serialize/src/Service.php
vendored
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace AnourValar\EloquentSerialize;
|
||||
|
||||
class Service
|
||||
{
|
||||
use \AnourValar\EloquentSerialize\Grammars\ModelGrammar;
|
||||
use \AnourValar\EloquentSerialize\Grammars\EloquentBuilderGrammar;
|
||||
use \AnourValar\EloquentSerialize\Grammars\QueryBuilderGrammar;
|
||||
|
||||
/**
|
||||
* Pack
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Builder $builder
|
||||
* @return string
|
||||
*/
|
||||
public function serialize(\Illuminate\Database\Eloquent\Builder $builder): string
|
||||
{
|
||||
$package = $this->pack($builder);
|
||||
|
||||
return serialize($package); // important!
|
||||
}
|
||||
|
||||
/**
|
||||
* Unpack
|
||||
*
|
||||
* @param mixed $package
|
||||
* @throws \LogicException
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function unserialize($package): \Illuminate\Database\Eloquent\Builder
|
||||
{
|
||||
// Prepare data
|
||||
if (is_string($package)) {
|
||||
$package = unserialize($package);
|
||||
}
|
||||
if (! ($package instanceof Package)) {
|
||||
throw new \LogicException('Incorrect argument.');
|
||||
}
|
||||
|
||||
// Unpack
|
||||
return $this->unpack($package);
|
||||
}
|
||||
}
|
||||
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