Skip to content
Open
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
6 changes: 3 additions & 3 deletions src/Cache/CacheKeyAndTagsGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ public function key(
ksort($args);
foreach ($args as $key => $value) {
$parts[] = $key;
$parts[] = is_array($value)
? \Safe\json_encode($value)
: $value;
$parts[] = is_object($value)
? $value
: \Safe\json_encode($value);
}

return implode(self::SEPARATOR, $parts);
Expand Down
62 changes: 62 additions & 0 deletions tests/Integration/Cache/CacheDirectiveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -523,4 +523,66 @@ public function testUseFalsyResultsInCache(): void
],
]);
}

public function testDistinguishesFalseAndNullArgs(): void
{
$this->mockResolver(static fn (mixed $root, array $args): string => var_export($args['flag'], true));

$this->schema = /** @lang GraphQL */ <<<'GRAPHQL'
type Query {
foo(flag: Boolean): String @mock @cache
}
GRAPHQL;

$this->graphQL(/** @lang GraphQL */ <<<'GRAPHQL'
{
foo(flag: false)
}
GRAPHQL)->assertExactJson([
'data' => [
'foo' => 'false',
],
]);

$this->graphQL(/** @lang GraphQL */ <<<'GRAPHQL'
{
foo(flag: null)
}
GRAPHQL)->assertExactJson([
'data' => [
'foo' => 'NULL',
],
]);
}

public function testDistinguishesNullAndStringNullArgs(): void
{
$this->mockResolver(static fn (mixed $root, array $args): string => var_export($args['arg'], true));

$this->schema = /** @lang GraphQL */ <<<'GRAPHQL'
type Query {
foo(arg: String): String @mock @cache
}
GRAPHQL;

$this->graphQL(/** @lang GraphQL */ <<<'GRAPHQL'
{
foo(arg: "null")
}
GRAPHQL)->assertExactJson([
'data' => [
'foo' => "'null'",
],
]);

$this->graphQL(/** @lang GraphQL */ <<<'GRAPHQL'
{
foo(arg: null)
}
GRAPHQL)->assertExactJson([
'data' => [
'foo' => 'NULL',
],
]);
}
}
69 changes: 69 additions & 0 deletions tests/Unit/Cache/CacheKeyAndTagsGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php declare(strict_types=1);

namespace Tests\Unit\Cache;

use Nuwave\Lighthouse\Cache\CacheKeyAndTagsGenerator;
use PHPUnit\Framework\TestCase;

final class CacheKeyAndTagsGeneratorTest extends TestCase
{
/** @param array<string, mixed> $args */
private function key(array $args): string
{
return (new CacheKeyAndTagsGenerator())->key(
user: null,
isPrivate: false,
parentName: 'Query',
id: null,
fieldName: 'foo',
args: $args,
path: ['foo'],
);
}

public function testEncodesScalarArgs(): void
{
$this->assertSame('lighthouse:Query::foo:bar:"baz":first:5', $this->key(['first' => 5, 'bar' => 'baz']));
}

public function testEncodesArrayArgs(): void
{
$this->assertSame('lighthouse:Query::foo:ids:[1,2]', $this->key(['ids' => [1, 2]]));
}

public function testEncodesStringableArgs(): void
{
$value = new class implements \Stringable {
public function __toString(): string
{
return 'baz';
}
};

$this->assertSame('lighthouse:Query::foo:bar:baz', $this->key(['bar' => $value]));
}

public function testDistinguishesArgsOfDifferentTypes(): void
{
$keys = [
$this->key(['arg' => false]),
$this->key(['arg' => null]),
$this->key(['arg' => '']),
$this->key(['arg' => true]),
$this->key(['arg' => 1]),
$this->key(['arg' => 1.5]),
$this->key(['arg' => 'null']),
$this->key(['arg' => 'true']),
$this->key(['arg' => 'false']),
$this->key(['arg' => '1']),
$this->key(['arg' => '1.5']),
$this->key(['arg' => '[1]']),
$this->key(['arg' => [1]]),
$this->key(['arg' => 'a:b:c']),
$this->key(['arg' => 'a', 'b' => 'c']),
$this->key([]),
];

$this->assertSame($keys, array_unique($keys));
}
}