From c852650d7e0ab0100135f2f5a1ae197d65062e34 Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Tue, 25 Aug 2020 08:01:16 +0200 Subject: [PATCH] Test dynamic relationships (#1017) * Test dynamic * Try/catch relationships * Update snapshot * Test output --- src/Console/ModelsCommand.php | 6 +- .../DynamicRelations/Models/Dynamic.php | 38 +++++++++++++ .../DynamicRelations/OtherModels/Account.php | 12 ++++ .../ModelsCommand/DynamicRelations/Test.php | 56 +++++++++++++++++++ .../__snapshots__/Test__test__1.php | 48 ++++++++++++++++ tests/SnapshotPhpDriver.php | 2 +- tests/SnapshotTxtDriver.php | 2 +- 7 files changed, 161 insertions(+), 3 deletions(-) create mode 100644 tests/Console/ModelsCommand/DynamicRelations/Models/Dynamic.php create mode 100644 tests/Console/ModelsCommand/DynamicRelations/OtherModels/Account.php create mode 100644 tests/Console/ModelsCommand/DynamicRelations/Test.php create mode 100644 tests/Console/ModelsCommand/DynamicRelations/__snapshots__/Test__test__1.php diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index ee656f9..a7e8081 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -570,7 +570,11 @@ class ModelsCommand extends Command // can cause errors. Since we don't need constraints we can // disable them when we fetch the relation to avoid errors. $relationObj = Relation::noConstraints(function () use ($model, $method) { - return $model->$method(); + try { + return $model->$method(); + } catch (\Throwable $e) { + return null; + } }); if ($relationObj instanceof Relation) { diff --git a/tests/Console/ModelsCommand/DynamicRelations/Models/Dynamic.php b/tests/Console/ModelsCommand/DynamicRelations/Models/Dynamic.php new file mode 100644 index 0000000..48745a2 --- /dev/null +++ b/tests/Console/ModelsCommand/DynamicRelations/Models/Dynamic.php @@ -0,0 +1,38 @@ +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); + } +} diff --git a/tests/Console/ModelsCommand/DynamicRelations/OtherModels/Account.php b/tests/Console/ModelsCommand/DynamicRelations/OtherModels/Account.php new file mode 100644 index 0000000..64882dc --- /dev/null +++ b/tests/Console/ModelsCommand/DynamicRelations/OtherModels/Account.php @@ -0,0 +1,12 @@ +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); + } +} diff --git a/tests/Console/ModelsCommand/DynamicRelations/__snapshots__/Test__test__1.php b/tests/Console/ModelsCommand/DynamicRelations/__snapshots__/Test__test__1.php new file mode 100644 index 0000000..b92e1cf --- /dev/null +++ b/tests/Console/ModelsCommand/DynamicRelations/__snapshots__/Test__test__1.php @@ -0,0 +1,48 @@ +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); + } +} diff --git a/tests/SnapshotPhpDriver.php b/tests/SnapshotPhpDriver.php index d8c1371..69ff362 100644 --- a/tests/SnapshotPhpDriver.php +++ b/tests/SnapshotPhpDriver.php @@ -9,7 +9,7 @@ class SnapshotPhpDriver implements Driver { public function serialize($data): string { - return $data; + return (string) $data; } public function extension(): string diff --git a/tests/SnapshotTxtDriver.php b/tests/SnapshotTxtDriver.php index 5cabc00..6dc3acd 100644 --- a/tests/SnapshotTxtDriver.php +++ b/tests/SnapshotTxtDriver.php @@ -9,7 +9,7 @@ class SnapshotTxtDriver implements Driver { public function serialize($data): string { - return $data; + return (string) $data; } public function extension(): string