Skip to content

Commit 2bb45b5

Browse files
committed
Improved test helper
1 parent a48c904 commit 2bb45b5

File tree

1 file changed

+73
-11
lines changed

1 file changed

+73
-11
lines changed

tests/lib/Helper/TestHelper.php

Lines changed: 73 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -397,27 +397,83 @@ public function runCRUDTests(ExtendedCacheItemPoolInterface $pool)
397397
$cacheKey = 'cache_key_' . bin2hex(random_bytes(8) . '_' . random_int(100, 999));
398398
$cacheValue = 'cache_data_' . random_int(1000, 999999);
399399
$cacheTag = 'cache_tag_' . bin2hex(random_bytes(8) . '_' . random_int(100, 999));
400+
$cacheTag2 = 'cache_tag_' . bin2hex(random_bytes(8) . '_' . random_int(100, 999));
400401
$cacheItem = $pool->getItem($cacheKey);
401402
$this->printInfoText('Using cache key: ' . $cacheKey);
402403

403404
$cacheItem->set($cacheValue)
404405
->expiresAfter(600)
405-
->addTag($cacheTag);
406+
->addTags([$cacheTag, $cacheTag2]);
406407

407408
if ($pool->save($cacheItem)) {
408409
$this->assertPass('The pool successfully saved an item.');
409410
} else {
410411
$this->assertFail('The pool failed to save an item.');
411412
return;
412413
}
414+
unset($cacheItem);
415+
$pool->detachAllItems();
413416

414-
/***
415-
* Detach the items to force "re-pull" from the backend
417+
/**
418+
* Tag strategy ALL success and fail
416419
*/
420+
421+
$this->printInfoText('Re-fetching item <green>by its tags</green> <red>and an unknown tag</red> (tag strategy "<yellow>ALL</yellow>")...');
422+
$cacheItems = $pool->getItemsByTags([$cacheTag, $cacheTag2, 'unknown_tag'], $pool::TAG_STRATEGY_ALL);
423+
424+
if (!isset($cacheItems[$cacheKey])) {
425+
$this->assertPass('The pool expectedly failed to retrieve the cache item.');
426+
} else {
427+
$this->assertFail('The pool unexpectedly retrieved the cache item.');
428+
return;
429+
}
430+
unset($cacheItems);
431+
$pool->detachAllItems();
432+
433+
$this->printInfoText('Re-fetching item <green>by its tags</green> (tag strategy "<yellow>ALL</yellow>")...');
434+
$cacheItems = $pool->getItemsByTags([$cacheTag, $cacheTag2], $pool::TAG_STRATEGY_ALL);
435+
436+
if (isset($cacheItems[$cacheKey])) {
437+
$this->assertPass('The pool successfully retrieved the cache item.');
438+
} else {
439+
$this->assertFail('The pool failed to retrieve the cache item.');
440+
return;
441+
}
442+
unset($cacheItems);
443+
$pool->detachAllItems();
444+
445+
/**
446+
* Tag strategy ONLY success and fail
447+
*/
448+
$this->printInfoText('Re-fetching item <green>by its tags</green> <red>and an unknown tag</red> (tag strategy "<yellow>ONLY</yellow>")...');
449+
$cacheItems = $pool->getItemsByTags([$cacheTag, $cacheTag2, 'unknown_tag'], $pool::TAG_STRATEGY_ONLY);
450+
451+
if (!isset($cacheItems[$cacheKey])) {
452+
$this->assertPass('The pool expectedly failed to retrieve the cache item.');
453+
} else {
454+
$this->assertFail('The pool unexpectedly retrieved the cache item.');
455+
return;
456+
}
457+
unset($cacheItems);
458+
$pool->detachAllItems();
459+
460+
$this->printInfoText('Re-fetching item <green>by one of its tags</green> (tag strategy "<yellow>ONLY</yellow>")...');
461+
$cacheItems = $pool->getItemsByTags([$cacheTag, $cacheTag2], $pool::TAG_STRATEGY_ONLY);
462+
463+
if (isset($cacheItems[$cacheKey])) {
464+
$this->assertPass('The pool successfully retrieved the cache item.');
465+
} else {
466+
$this->assertFail('The pool failed to retrieve the cache item.');
467+
return;
468+
}
469+
unset($cacheItems);
417470
$pool->detachAllItems();
418471

419-
$this->printInfoText('Re-fetching item by its tag...');
420-
$cacheItems = $pool->getItemsByTag($cacheTag);
472+
/**
473+
* Tag strategy ONE success and fail
474+
*/
475+
$this->printInfoText('Re-fetching item by one of its tags <red>and an unknown tag</red> (tag strategy "<yellow>ONE</yellow>")...');
476+
$cacheItems = $pool->getItemsByTags([$cacheTag, 'unknown_tag'], $pool::TAG_STRATEGY_ONE);
421477

422478
if (isset($cacheItems[$cacheKey]) && $cacheItems[$cacheKey]->getKey() === $cacheKey) {
423479
$this->assertPass('The pool successfully retrieved the cache item.');
@@ -444,22 +500,17 @@ public function runCRUDTests(ExtendedCacheItemPoolInterface $pool)
444500
} else {
445501
$this->assertFail('The pool failed to commit deferred cache item.');
446502
}
447-
448-
/***
449-
* Detach the items to force "re-pull" from the backend
450-
*/
451503
$pool->detachAllItems();
452504
unset($cacheItem);
453-
$cacheItem = $pool->getItem($cacheKey);
454505

506+
$cacheItem = $pool->getItem($cacheKey);
455507
if ($cacheItem->get() === $cacheValue) {
456508
$this->assertPass('The pool successfully retrieved the expected new value.');
457509
} else {
458510
$this->assertFail('The pool failed to retrieve the expected new value.');
459511
return;
460512
}
461513

462-
463514
if ($pool->deleteItem($cacheKey)) {
464515
$this->assertPass('The pool successfully deleted the cache item.');
465516
} else {
@@ -471,8 +522,19 @@ public function runCRUDTests(ExtendedCacheItemPoolInterface $pool)
471522
} else {
472523
$this->assertFail('The cluster failed to clear.');
473524
}
525+
$pool->detachAllItems();
526+
unset($cacheItem);
527+
528+
$cacheItem = $pool->getItem($cacheKey);
529+
if (!$cacheItem->isHit()) {
530+
$this->assertPass('The cache item does no longer exists in pool.');
531+
} else {
532+
$this->assertFail('The cache item still exists in pool.');
533+
return;
534+
}
474535

475536
$this->printInfoText(sprintf('I/O stats: %d HIT, %s MISS, %d WRITE', $pool->getIO()->getReadHit(), $pool->getIO()->getReadMiss(), $pool->getIO()->getWriteHit()));
537+
$this->printInfoText('<yellow>Driver info</yellow>: <magenta>' . $pool->getStats()->getInfo() . '</magenta>');
476538
}
477539

478540

0 commit comments

Comments
 (0)