Skip to content

Commit

Permalink
Merge b209359 into b92c3f2
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmedbally committed Sep 10, 2023
2 parents b92c3f2 + b209359 commit c83d21f
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 3 deletions.
25 changes: 25 additions & 0 deletions src/Traits/OptimusEncodedRouteKey.php
Expand Up @@ -40,6 +40,31 @@ public function resolveRouteBinding($value, $field = null)
return $this->where($field, $value)->first();
}

/**
* Retrieve the model for a bound value.
*
* @param \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Relations\Relation $query
* @param mixed $value
* @param string|null $field
* @return \Illuminate\Database\Eloquent\Relations\Relation
*/
public function resolveRouteBindingQuery($query, $value, $field = null)
{
if ($field === null) {
$field = $query->getRouteKeyName();
}

if (is_string($value) && ctype_digit($value)) {
$value = (int) $value;
}

if (is_int($value) && $field === $this->getRouteKeyName()) {
$value = $this->getOptimus()->decode($value);
}

return $query->where($field, $value);
}

/**
* Get the Optimus instance.
*
Expand Down
@@ -0,0 +1,23 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddParentIdToUsersTable extends Migration
{
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->foreignId('parent_id')
->nullable();
});
}

public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn(['parent_id']);
});
}
}
12 changes: 12 additions & 0 deletions tests/Stubs/Models/UserWithCustomOptimusConnection.php
Expand Up @@ -4,6 +4,8 @@

use Cog\Laravel\Optimus\Traits\OptimusEncodedRouteKey;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

final class UserWithCustomOptimusConnection extends Model
{
Expand All @@ -14,4 +16,14 @@ final class UserWithCustomOptimusConnection extends Model
protected $table = 'users';

protected $guarded = [];

public function nestedUsers(): HasMany
{
return $this->hasMany(UserWithCustomOptimusConnection::class, 'parent_id');
}

public function parentUser(): BelongsTo
{
return$this->belongsTo(UserWithCustomOptimusConnection::class, 'parent_id');
}
}
12 changes: 12 additions & 0 deletions tests/Stubs/Models/UserWithDefaultOptimusConnection.php
Expand Up @@ -4,6 +4,8 @@

use Cog\Laravel\Optimus\Traits\OptimusEncodedRouteKey;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

final class UserWithDefaultOptimusConnection extends Model
{
Expand All @@ -12,4 +14,14 @@ final class UserWithDefaultOptimusConnection extends Model
protected $table = 'users';

protected $guarded = [];

public function nestedUsers(): HasMany
{
return $this->hasMany(UserWithDefaultOptimusConnection::class, 'parent_id');
}

public function parentUser(): BelongsTo
{
return$this->belongsTo(UserWithDefaultOptimusConnection::class, 'parent_id');
}
}
19 changes: 16 additions & 3 deletions tests/Traits/OptimusEncodedRouteKeyTest.php
Expand Up @@ -28,6 +28,7 @@ protected function setUp(): void
parent::setUp();

$this->loadLaravelMigrations(config('database.default'));
$this->loadMigrationsFrom(__DIR__.'/../Stubs/Migrations');
$this->configurePrimeNumbers();
}

Expand All @@ -51,6 +52,18 @@ public function testOptimusConnectionCanBeConfigured(): void
$this->assertNotEquals($incorrectEncodedId, $routeKey);
}

public function testResolveChildRouteWithEncodedKey(): void
{
$user = $this->createUserWithDefaultOptimusConnection();
$nestedUser = $this->createUserWithDefaultOptimusConnection([
'email' => 'test1@user.com',
]);
$nestedUser->parentUser()->associate($user)->save();
$resolvedNestedUser = $user->resolveChildRouteBinding('nested_user', $nestedUser->getRouteKey(), 'id');

$this->assertEquals($nestedUser->id, $resolvedNestedUser->id);
}

public function testResolveModelWithEncodedKey(): void
{
$user = $this->createUserWithDefaultOptimusConnection();
Expand Down Expand Up @@ -172,13 +185,13 @@ public function testExistingIntegerValuesBelow256AreResolved()
*
* @return \Cog\Tests\Laravel\Optimus\Stubs\Models\UserWithDefaultOptimusConnection
*/
protected function createUserWithDefaultOptimusConnection(): UserWithDefaultOptimusConnection
protected function createUserWithDefaultOptimusConnection(array $data = []): UserWithDefaultOptimusConnection
{
return UserWithDefaultOptimusConnection::create([
return UserWithDefaultOptimusConnection::create(array_merge([
'name' => 'Default Test User',
'email' => 'test@user.com',
'password' => 'p4ssw0rd',
]);
], $data));
}

/**
Expand Down

0 comments on commit c83d21f

Please sign in to comment.