Skip to content

Commit

Permalink
Tab intended my changes
Browse files Browse the repository at this point in the history
  • Loading branch information
korridor committed Mar 31, 2018
1 parent dad7d2b commit 00553fc
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 46 deletions.
14 changes: 7 additions & 7 deletions src/Cloner.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,13 @@ protected function duplicatePivotedRelation($relation, $relation_name, $clone) {
if ($this->write_connection) return;

// Loop trough current relations and attach to clone
$relation->get()->each(function($foreign) use ($clone, $relation_name) {
$pivot_attributes = array_except($foreign->pivot->getAttributes(), [
$foreign->pivot->getRelatedKey(),
$foreign->pivot->getForeignKey(),
$foreign->pivot->getCreatedAtColumn(),
$foreign->pivot->getUpdatedAtColumn()
]);
$relation->get()->each(function ($foreign) use ($clone, $relation_name) {
$pivot_attributes = array_except($foreign->pivot->getAttributes(), [
$foreign->pivot->getRelatedKey(),
$foreign->pivot->getForeignKey(),
$foreign->pivot->getCreatedAtColumn(),
$foreign->pivot->getUpdatedAtColumn()
]);
$clone->$relation_name()->attach($foreign, $pivot_attributes);
});
}
Expand Down
4 changes: 2 additions & 2 deletions stubs/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ public function authors() {
}

public function ratings() {
return $this->belongsToMany(User::class)->withPivot('rating')->withTimestamps();
}
return $this->belongsToMany(User::class)->withPivot('rating')->withTimestamps();
}
}
7 changes: 4 additions & 3 deletions stubs/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

class User extends Eloquent {

public function rated_articles() {
return $this->belongsToMany(Article::class)->withPivot('rating')->withTimestamps();
}
public function rated_articles() {
return $this->belongsToMany(Article::class)->withPivot('rating')->withTimestamps();
}

}
68 changes: 34 additions & 34 deletions tests/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

class FunctionalTest extends PHPUnit_Framework_TestCase {

private $article;
private $article;

protected function initUpchuck() {

Expand Down Expand Up @@ -99,18 +99,18 @@ protected function migrateTables($connection = 'default') {
$table->timestamps();
});

DB::connection($connection)->getSchemaBuilder()->create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});

DB::connection($connection)->getSchemaBuilder()->create('article_user', function (Blueprint $table) {
$table->integer('rating')->unsigned();
$table->integer('user_id')->unsigned();
$table->integer('article_id')->unsigned();
$table->timestamps();
});
DB::connection($connection)->getSchemaBuilder()->create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});

DB::connection($connection)->getSchemaBuilder()->create('article_user', function (Blueprint $table) {
$table->integer('rating')->unsigned();
$table->integer('user_id')->unsigned();
$table->integer('article_id')->unsigned();
$table->timestamps();
});
}

protected function seed() {
Expand All @@ -125,10 +125,10 @@ protected function seed() {
]));

$this->article->ratings()->attach(User::create([
'name' => 'Peter'
]), [
'rating' => 4
]);
'name' => 'Peter'
]), [
'rating' => 4
]);

$this->disk->write('test.jpg', 'contents');

Expand All @@ -147,8 +147,8 @@ function testExists() {
$this->migrateTables();
$this->seed();

// Wait 1.5 seconds to be able to detect a difference in the timestamps
usleep(1500000);
// Wait 1.5 seconds to be able to detect a difference in the timestamps
usleep(1500000);

$cloner = new Cloner($this->upchuck_adapter, $this->mockEvents());
$clone = $cloner->duplicate($this->article);
Expand All @@ -159,28 +159,28 @@ function testExists() {
$this->assertEquals('Test', $clone->title);

// Test if the timestamps have been reset
$this->assertNotNull($clone->created_at);
$this->assertTrue($this->article->created_at->lt($clone->created_at));
$this->assertNotNull($clone->updated_at);
$this->assertTrue($this->article->created_at->lt($clone->updated_at));
$this->assertNotNull($clone->created_at);
$this->assertTrue($this->article->created_at->lt($clone->created_at));
$this->assertNotNull($clone->updated_at);
$this->assertTrue($this->article->created_at->lt($clone->updated_at));

// Test many to many
$this->assertEquals(1, $clone->authors()->count());
$this->assertEquals('Steve', $clone->authors()->first()->name);
$this->assertEquals(2, DB::table('article_author')->count());

// Test many to many with pivot
$this->assertEquals(1, $clone->ratings()->count());
$this->assertEquals('Peter', $clone->ratings()->first()->name);
$this->assertEquals(2, DB::table('article_user')->count());

// Test if the timestamps in the pivot table have been reset
$this->assertNotNull($this->article->ratings()->first()->pivot->created_at);
$this->assertNotNull($clone->ratings()->first()->pivot->created_at);
$this->assertTrue($this->article->ratings()->first()->pivot->created_at->lt($clone->ratings()->first()->pivot->created_at));
$this->assertNotNull($this->article->ratings()->first()->pivot->updated_at);
$this->assertNotNull($clone->ratings()->first()->pivot->updated_at);
$this->assertTrue($this->article->ratings()->first()->pivot->updated_at->lt($clone->ratings()->first()->pivot->updated_at));
$this->assertEquals(1, $clone->ratings()->count());
$this->assertEquals('Peter', $clone->ratings()->first()->name);
$this->assertEquals(2, DB::table('article_user')->count());

// Test if the timestamps in the pivot table have been reset
$this->assertNotNull($this->article->ratings()->first()->pivot->created_at);
$this->assertNotNull($clone->ratings()->first()->pivot->created_at);
$this->assertTrue($this->article->ratings()->first()->pivot->created_at->lt($clone->ratings()->first()->pivot->created_at));
$this->assertNotNull($this->article->ratings()->first()->pivot->updated_at);
$this->assertNotNull($clone->ratings()->first()->pivot->updated_at);
$this->assertTrue($this->article->ratings()->first()->pivot->updated_at->lt($clone->ratings()->first()->pivot->updated_at));

// Test one to many
$this->assertEquals(1, $clone->photos()->count());
Expand Down

0 comments on commit 00553fc

Please sign in to comment.