Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ResponseCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ protected function replaceBinding(string $tag, Request $request): string

// Replace the matched expression (`{foo}`) with the value found in
// the request inside the tag
$tag = str_replace($match, $value, $tag);
$tag = str_replace($match, (string) $value, $tag);
}

return $this->resolvedTags[$tag] = $tag;
Expand Down
23 changes: 23 additions & 0 deletions tests/Feature/TagBindingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Matchory\ResponseCache\Tests\Feature;

use Matchory\ResponseCache\Facades\ResponseCache;

use function beforeEach;
use function it;
use function test;

beforeEach(function () {
$this->app['cache']->flush();
});

it('resolves bound models with integer keys in tags', function () {
test()->get('/models/1')->assertContent('1');
test()->get('/models/2')->assertContent('2');

ResponseCache::flush(['models.2']);

test()->get('/models/1')->assertContent('1');
test()->get('/models/2')->assertContent('3');
});
35 changes: 35 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Matchory\ResponseCache\Tests;

use Illuminate\Contracts\Routing\UrlRoutable;
use Illuminate\Routing\Middleware\SubstituteBindings;
use InvalidArgumentException;
use Matchory\ResponseCache\Http\Middleware\BypassCache;
use Matchory\ResponseCache\Http\Middleware\CacheResponse;
Expand Down Expand Up @@ -39,6 +41,39 @@ protected function defineRoutes($router): void
->middleware(CacheResponse::class)
->middleware(BypassCache::class)
->name('bypass');

// Binds like an Eloquent model with an integer key
$router->bind('model', fn(string $value) => new class ((int) $value) implements UrlRoutable {
public function __construct(private readonly int $key) {}

public function getRouteKey(): int
{
return $this->key;
}

public function getRouteKeyName(): string
{
return 'id';
}

public function resolveRouteBinding($value, $field = null): ?self
{
return null;
}

public function resolveChildRouteBinding($childType, $value, $field): ?self
{
return null;
}
});
$router
->get('/models/{model}', function () {
static $i = 0;

return ++$i;
})
->middleware([SubstituteBindings::class, CacheResponse::class . ':models.{model}'])
->name('models');
}

protected function getPackageProviders($app): array
Expand Down