mirror of
https://github.com/barryvdh/laravel-ide-helper.git
synced 2026-08-18 01:57:13 +00:00
Test dynamic relationships (#1017)
* Test dynamic * Try/catch relationships * Update snapshot * Test output
This commit is contained in:
@@ -570,7 +570,11 @@ class ModelsCommand extends Command
|
|||||||
// can cause errors. Since we don't need constraints we can
|
// can cause errors. Since we don't need constraints we can
|
||||||
// disable them when we fetch the relation to avoid errors.
|
// disable them when we fetch the relation to avoid errors.
|
||||||
$relationObj = Relation::noConstraints(function () use ($model, $method) {
|
$relationObj = Relation::noConstraints(function () use ($model, $method) {
|
||||||
return $model->$method();
|
try {
|
||||||
|
return $model->$method();
|
||||||
|
} catch (\Throwable $e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
if ($relationObj instanceof Relation) {
|
if ($relationObj instanceof Relation) {
|
||||||
|
|||||||
@@ -0,0 +1,38 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\DynamicRelations\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||||
|
|
||||||
|
class Dynamic extends Model
|
||||||
|
{
|
||||||
|
/** @var \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\DynamicRelations\OtherModels\Account */
|
||||||
|
protected $account;
|
||||||
|
|
||||||
|
// Regular relations
|
||||||
|
public function regularHasMany(): HasMany
|
||||||
|
{
|
||||||
|
return $this->hasMany(Dynamic::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Dynamic relations
|
||||||
|
public function dynamicHasMany(): HasMany
|
||||||
|
{
|
||||||
|
return $this->hasMany(Dynamic::class)->where('date', '>=', $this->account->created_at);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function dynamicHasOne(): HasOne
|
||||||
|
{
|
||||||
|
return $this->hasOne(Dynamic::class)->where('date', '>=', $this->account->created_at);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function dynamicBelongsTo(): BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(Dynamic::class)->where('date', '>=', $this->account->created_at);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\DynamicRelations\OtherModels;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
class Account extends Model
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,56 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\DynamicRelations;
|
||||||
|
|
||||||
|
use Barryvdh\LaravelIdeHelper\Console\ModelsCommand;
|
||||||
|
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\AbstractModelsCommand;
|
||||||
|
use Illuminate\Filesystem\Filesystem;
|
||||||
|
use Mockery;
|
||||||
|
|
||||||
|
class Test extends AbstractModelsCommand
|
||||||
|
{
|
||||||
|
protected function getEnvironmentSetUp($app)
|
||||||
|
{
|
||||||
|
parent::getEnvironmentSetUp($app);
|
||||||
|
|
||||||
|
$app['config']->set('ide-helper', [
|
||||||
|
'model_locations' => [
|
||||||
|
// This is calculated from the base_path() which points to
|
||||||
|
// vendor/orchestra/testbench-core/laravel
|
||||||
|
'/../../../../tests/Console/ModelsCommand/DynamicRelations/Models',
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test(): void
|
||||||
|
{
|
||||||
|
$actualContent = null;
|
||||||
|
$mockFilesystem = Mockery::mock(Filesystem::class);
|
||||||
|
$mockFilesystem
|
||||||
|
->shouldReceive('get')
|
||||||
|
->andReturn(file_get_contents(__DIR__ . '/Models/Dynamic.php'))
|
||||||
|
->once();
|
||||||
|
$mockFilesystem
|
||||||
|
->shouldReceive('put')
|
||||||
|
->with(
|
||||||
|
Mockery::any(),
|
||||||
|
Mockery::capture($actualContent)
|
||||||
|
)
|
||||||
|
->andReturn(1) // Simulate we wrote _something_ to the file
|
||||||
|
->once();
|
||||||
|
|
||||||
|
$this->instance(Filesystem::class, $mockFilesystem);
|
||||||
|
|
||||||
|
$command = $this->app->make(ModelsCommand::class);
|
||||||
|
|
||||||
|
$tester = $this->runCommand($command, [
|
||||||
|
'--write' => true,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->assertSame(0, $tester->getStatusCode());
|
||||||
|
$this->assertStringContainsString('Written new phpDocBlock to', $tester->getDisplay());
|
||||||
|
$this->assertMatchesPhpSnapshot($actualContent);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\DynamicRelations\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\DynamicRelations\Models\Dynamic
|
||||||
|
*
|
||||||
|
* @property-read \Illuminate\Database\Eloquent\Collection|Dynamic[] $regularHasMany
|
||||||
|
* @property-read int|null $regular_has_many_count
|
||||||
|
* @method static \Illuminate\Database\Eloquent\Builder|Dynamic newModelQuery()
|
||||||
|
* @method static \Illuminate\Database\Eloquent\Builder|Dynamic newQuery()
|
||||||
|
* @method static \Illuminate\Database\Eloquent\Builder|Dynamic query()
|
||||||
|
* @mixin \Eloquent
|
||||||
|
*/
|
||||||
|
class Dynamic extends Model
|
||||||
|
{
|
||||||
|
/** @var \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\DynamicRelations\OtherModels\Account */
|
||||||
|
protected $account;
|
||||||
|
|
||||||
|
// Regular relations
|
||||||
|
public function regularHasMany(): HasMany
|
||||||
|
{
|
||||||
|
return $this->hasMany(Dynamic::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Dynamic relations
|
||||||
|
public function dynamicHasMany(): HasMany
|
||||||
|
{
|
||||||
|
return $this->hasMany(Dynamic::class)->where('date', '>=', $this->account->created_at);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function dynamicHasOne(): HasOne
|
||||||
|
{
|
||||||
|
return $this->hasOne(Dynamic::class)->where('date', '>=', $this->account->created_at);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function dynamicBelongsTo(): BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(Dynamic::class)->where('date', '>=', $this->account->created_at);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,7 +9,7 @@ class SnapshotPhpDriver implements Driver
|
|||||||
{
|
{
|
||||||
public function serialize($data): string
|
public function serialize($data): string
|
||||||
{
|
{
|
||||||
return $data;
|
return (string) $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function extension(): string
|
public function extension(): string
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ class SnapshotTxtDriver implements Driver
|
|||||||
{
|
{
|
||||||
public function serialize($data): string
|
public function serialize($data): string
|
||||||
{
|
{
|
||||||
return $data;
|
return (string) $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function extension(): string
|
public function extension(): string
|
||||||
|
|||||||
Reference in New Issue
Block a user