Skip to content

Commit

Permalink
Merge pull request #12 from lewisf2001uk/master
Browse files Browse the repository at this point in the history
Add ability for models to have string Ids
  • Loading branch information
fico7489 authored Dec 14, 2017
2 parents 5e0b219 + dca48d6 commit 2d94f09
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/Relations/BelongsToManyCustom.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private function getIdsWithAttributes($id, $attributes = [])
$ids[$attributesArray] = $attributes;
}
}
} elseif (is_int($id)) {
} elseif (is_int($id) || is_string($id)) {
$ids[$id] = $attributes;
}

Expand Down
18 changes: 18 additions & 0 deletions tests/Models/Seller.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,30 @@

namespace Fico7489\Laravel\Pivot\Tests\Models;

use Fico7489\Laravel\Pivot\Traits\PivotEventTrait;

class Seller extends BaseModel
{
use PivotEventTrait;

public $incrementing = false;

protected $table = 'sellers';

protected $fillable = ['name'];

/**
* Boot the String Id for the model.
*
* @return void
*/
public static function boot()
{
static::creating(function ($model) {
$model->{$model->getKeyName()} = str_random(255);
});
}

public function users()
{
return $this->belongsToMany(User::class)
Expand Down
6 changes: 6 additions & 0 deletions tests/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,10 @@ public function roles()
return $this->belongsToMany(Role::class)
->withPivot(['value']);
}

public function sellers()
{
return $this->belongsToMany(Seller::class)
->withPivot(['value']);
}
}
70 changes: 50 additions & 20 deletions tests/PivotEventTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Fico7489\Laravel\Pivot\Tests;

use Fico7489\Laravel\Pivot\Tests\Models\Role;
use Fico7489\Laravel\Pivot\Tests\Models\Seller;
use Fico7489\Laravel\Pivot\Tests\Models\User;

class PivotEventTraitTest extends TestCase
Expand All @@ -16,13 +17,16 @@ public function setUp()
User::create(['name' => 'example@example.com']);
User::create(['name' => 'example2@example.com']);

Seller::create(['name' => 'seller 1']);

Role::create(['name' => 'admin']);
Role::create(['name' => 'manager']);
Role::create(['name' => 'customer']);
Role::create(['name' => 'driver']);

$this->assertEquals(0, \DB::table('role_user')->count());

$this->assertEquals(0, \DB::table('seller_user')->count());

\Event::listen('eloquent.*', function ($eventName, array $data) {
if (strpos($eventName, 'eloquent.retrieved') !== 0) {
self::$events[] = ['name' => $eventName, 'model' => $data['model'], 'relation' => $data['relation'], 'pivotIds' => $data['pivotIds'], 'pivotIdsAttributes' => $data['pivotIdsAttributes']];
Expand All @@ -33,22 +37,35 @@ public function setUp()
private function startListening()
{
self::$events = [];
return User::find(1);
}

public function test_attach_int()
{
$user = $this->startListening();
$this->startListening();
$user = User::find(1);
$user->roles()->attach(1, ['value' => 123]);

$this->check_events(['eloquent.pivotAttaching: ' . User::class, 'eloquent.pivotAttached: ' . User::class]);
$this->check_variables(0, [1], [1 => ['value' => 123]]);
$this->check_database(1, 123, 0, 'value');
}

public function test_attach_string()
{
$this->startListening();
$user = User::find(1);
$seller = Seller::first();
$user->sellers()->attach($seller->id, ['value' => 123]);

$this->check_events(['eloquent.pivotAttaching: ' . User::class, 'eloquent.pivotAttached: ' . User::class]);
$this->check_variables(0, [$seller->id], [$seller->id => ['value' => 123]], 'sellers');
$this->check_database(1, 123, 0, 'value', 'seller_user');
}

public function test_attach_array()
{
$user = $this->startListening();
$this->startListening();
$user = User::find(1);
$user->roles()->attach([1 => ['value' => 123], 2 => ['value' => 456]], ['value2' => 789]);

$this->assertEquals(2, \DB::table('role_user')->count());
Expand All @@ -60,7 +77,8 @@ public function test_attach_array()

public function test_attach_model()
{
$user = $this->startListening();
$this->startListening();
$user = User::find(1);
$role = Role::find(1);
$user->roles()->attach($role, ['value' => 123]);

Expand All @@ -72,7 +90,8 @@ public function test_attach_model()

public function test_attach_collection()
{
$user = $this->startListening();
$this->startListening();
$user = User::find(1);
$roles = Role::take(2)->get();
$user->roles()->attach($roles, ['value' => 123]);

Expand All @@ -85,7 +104,8 @@ public function test_attach_collection()

public function test_detach_int()
{
$user = $this->startListening();
$this->startListening();
$user = User::find(1);
$user->roles()->attach([1, 2 ,3]);
$this->assertEquals(3, \DB::table('role_user')->count());

Expand All @@ -99,7 +119,8 @@ public function test_detach_int()

public function test_detach_array()
{
$user = $this->startListening();
$this->startListening();
$user = User::find(1);
$user->roles()->attach([1, 2 ,3]);
$this->assertEquals(3, \DB::table('role_user')->count());

Expand All @@ -113,7 +134,8 @@ public function test_detach_array()

public function test_detach_model()
{
$user = $this->startListening();
$this->startListening();
$user = User::find(1);
$user->roles()->attach([1, 2 ,3]);
$this->assertEquals(3, \DB::table('role_user')->count());

Expand All @@ -128,7 +150,8 @@ public function test_detach_model()

public function test_detach_collection()
{
$user = $this->startListening();
$this->startListening();
$user = User::find(1);
$user->roles()->attach([1, 2 ,3]);
$this->assertEquals(3, \DB::table('role_user')->count());

Expand All @@ -143,7 +166,8 @@ public function test_detach_collection()

public function test_update()
{
$user = $this->startListening();
$this->startListening();
$user = User::find(1);
$user->roles()->attach([1, 2 ,3]);

$this->startListening();
Expand All @@ -158,7 +182,8 @@ public function test_update()

public function test_sync_int()
{
$user = $this->startListening();
$this->startListening();
$user = User::find(1);
$user->roles()->attach([2 ,3]);
$this->assertEquals(2, \DB::table('role_user')->count());

Expand All @@ -176,7 +201,8 @@ public function test_sync_int()

public function test_sync_array()
{
$user = $this->startListening();
$this->startListening();
$user = User::find(1);
$user->roles()->attach([2 ,3]);
$this->assertEquals(2, \DB::table('role_user')->count());

Expand All @@ -194,7 +220,8 @@ public function test_sync_array()

public function test_sync_model()
{
$user = $this->startListening();
$this->startListening();
$user = User::find(1);
$user->roles()->attach([2, 3]);
$this->assertEquals(2, \DB::table('role_user')->count());

Expand All @@ -213,7 +240,8 @@ public function test_sync_model()

public function test_sync_collection()
{
$user = $this->startListening();
$this->startListening();
$user = User::find(1);
$user->roles()->attach([1, 2]);
$this->assertEquals(2, \DB::table('role_user')->count());

Expand All @@ -236,7 +264,8 @@ public function test_sync_collection()

public function test_standard_update()
{
$user = $this->startListening();
$this->startListening();
$user = User::find(1);

$this->startListening();
$user->update(['name' => 'different']);
Expand All @@ -251,7 +280,8 @@ public function test_standard_update()

public function test_relation_is_null()
{
$user = $this->startListening();
$this->startListening();
$user = User::find(1);
$user->update(['name' => 'new_name']);

$eventName = 'eloquent.updating: ' . User::class;
Expand All @@ -275,9 +305,9 @@ private function check_variables($number, $ids, $idsAttributes = [], $relation =
$this->assertEquals(self::$events[$number]['relation'], $relation);
}

private function check_database($count, $value, $number = 0, $attribute = 'value')
private function check_database($count, $value, $number = 0, $attribute = 'value', $table = 'role_user')
{
$this->assertEquals($value, \DB::table('role_user')->get()->get($number)->$attribute);
$this->assertEquals($count, \DB::table('role_user')->count());
$this->assertEquals($value, \DB::table($table)->get()->get($number)->$attribute);
$this->assertEquals($count, \DB::table($table)->count());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class CreateDatabase extends Migration
public function up()
{
Schema::create('sellers', function (Blueprint $table) {
$table->increments('id');
$table->string('id');
$table->string('name');

$table->timestamps();
Expand Down Expand Up @@ -54,8 +54,8 @@ public function up()
$table->timestamps();
});

Schema::create('user_seller', function (Blueprint $table) {
$table->integer('seller_id')->unsigned()->index();
Schema::create('seller_user', function (Blueprint $table) {
$table->string('seller_id')->index();
$table->integer('user_id')->unsigned()->index();

$table->primary(['seller_id', 'user_id']);
Expand Down

0 comments on commit 2d94f09

Please sign in to comment.