Skip to content

Commit

Permalink
Merge pull request #46 from pavel-mironchik/belongsto
Browse files Browse the repository at this point in the history
Fix cloning the BelongsTo relationship
  • Loading branch information
denisdulici committed Dec 23, 2020
2 parents e56d2c3 + 4feba23 commit 825ad2c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
12 changes: 9 additions & 3 deletions src/Cloner.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ public function duplicate($model, $relation = null) {
$this->dispatchOnCloningEvent($clone, $relation, $model);

if ($relation) {
$relation->save($clone);
if (!is_a($relation, 'Illuminate\Database\Eloquent\Relations\BelongsTo')) {
$relation->save($clone);
}
} else {
$clone->save();
}
Expand Down Expand Up @@ -210,7 +212,11 @@ protected function duplicatePivotedRelation($relation, $relation_name, $clone) {
*/
protected function duplicateDirectRelation($relation, $relation_name, $clone) {
$relation->get()->each(function($foreign) use ($clone, $relation_name) {
$this->duplicate($foreign, $clone->$relation_name());
});
$cloned_relation = $this->duplicate($foreign, $clone->$relation_name());
if (is_a($clone->$relation_name(), 'Illuminate\Database\Eloquent\Relations\BelongsTo')) {
$clone->$relation_name()->associate($cloned_relation);
$clone->save();
}
});
}
}
15 changes: 15 additions & 0 deletions stubs/Image.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php namespace Bkwld\Cloner\Stubs;

use Bkwld\Cloner\Cloneable as Cloneable;

class Image extends Photo {
use Cloneable;

public $cloneable_relations = ['article'];

protected $table = 'photos';

public function onCloning() {
$this->uid = 2;
}
}
13 changes: 10 additions & 3 deletions tests/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Bkwld\Cloner\Adapters\Upchuck;
use Bkwld\Cloner\Stubs\Article;
use Bkwld\Cloner\Stubs\Author;
use Bkwld\Cloner\Stubs\Image;
use Bkwld\Cloner\Stubs\Photo;
use Bkwld\Cloner\Stubs\User;
use Bkwld\Upchuck\Helpers;
Expand All @@ -19,7 +20,7 @@
use VirtualFileSystem\FileSystem as Vfs;
use PHPUnit\Framework\TestCase;

class FunctionalTest extends TestCase
class FunctionalTest extends TestCase
{
use MockeryPHPUnitIntegration;

Expand Down Expand Up @@ -58,7 +59,7 @@ protected function initUpchuck()
*/
protected function mockEvents()
{
return m::mock('Illuminate\Events\Dispatcher', ['dispatch' => null]);
return m::mock('Illuminate\Contracts\Events\Dispatcher', ['dispatch' => null]);
}

// https://github.com/laracasts/TestDummy/blob/master/tests/FactoryTest.php#L18
Expand Down Expand Up @@ -212,7 +213,13 @@ function testExists()
$path = $this->helpers->path($photo->image);
$this->assertTrue($this->disk->has($path));
$this->assertEquals('contents', $this->disk->read($path));
}

// Test one to many inverse (BelongsTo)
$image = Image::first();
$clone = $cloner->duplicate($image);
$this->assertNotNull($clone->article);
$this->assertNotEquals($this->article->id, $clone->article->id);
}

// Test that model is created in a differetnt database. These checks don't
// use eloquent because Laravel has issues with relationships on models in
Expand Down

0 comments on commit 825ad2c

Please sign in to comment.