Skip to content

Commit a064944

Browse files
committed
test(OnConflict): add tests for cache key generation with update parameters
1 parent f63c166 commit a064944

1 file changed

Lines changed: 48 additions & 0 deletions

File tree

tests/Database/Unit/Query/OnConflictTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Cycle\Database\Exception\BuilderException;
88
use Cycle\Database\Injection\Expression;
9+
use Cycle\Database\Injection\Parameter;
910
use Cycle\Database\Query\ConflictAction;
1011
use Cycle\Database\Query\OnConflict;
1112
use Cycle\Database\Query\QueryParameters;
@@ -170,4 +171,51 @@ public function testCacheKeyDiffersForDifferentUpdateColumns(): void
170171
$b->getCacheKey(new QueryParameters()),
171172
);
172173
}
174+
175+
public function testCacheKeyPushesFragmentParametersFromUpdateMap(): void
176+
{
177+
$expr = new Expression('counters.n + ?', 5);
178+
$c = OnConflict::target('key')->doUpdate(['n' => $expr]);
179+
180+
$params = new QueryParameters();
181+
$key = $c->getCacheKey($params);
182+
183+
$this->assertCount(1, $params->getParameters());
184+
$this->assertStringContainsString((string) $expr, $key);
185+
}
186+
187+
public function testCacheKeyWrapsScalarUpdateValueAsParameter(): void
188+
{
189+
$c = OnConflict::target('key')->doUpdate(['n' => 42]);
190+
191+
$params = new QueryParameters();
192+
$key = $c->getCacheKey($params);
193+
194+
$pushed = $params->getParameters();
195+
$this->assertCount(1, $pushed);
196+
$this->assertInstanceOf(Parameter::class, $pushed[0]);
197+
$this->assertSame(42, $pushed[0]->getValue());
198+
$this->assertStringContainsString('P?', $key);
199+
}
200+
201+
public function testCacheKeyAcceptsExistingParameterInterfaceWithoutRewrapping(): void
202+
{
203+
$param = new Parameter(7);
204+
$c = OnConflict::target('key')->doUpdate(['n' => $param]);
205+
206+
$params = new QueryParameters();
207+
$key = $c->getCacheKey($params);
208+
209+
$pushed = $params->getParameters();
210+
$this->assertCount(1, $pushed);
211+
$this->assertSame($param, $pushed[0]);
212+
$this->assertStringContainsString('P?', $key);
213+
}
214+
215+
public function testFromReturnsSameInstance(): void
216+
{
217+
$c = OnConflict::target('email')->doUpdate(['name']);
218+
219+
$this->assertSame($c, OnConflict::from($c));
220+
}
173221
}

0 commit comments

Comments
 (0)