Skip to content

Commit a4a185a

Browse files
authored
seeInRedis displays difference between expected value to actual value (#14)
* seeInRedis compares expected value to actual value * Remove boolean flag * Compare zset as string because ArrayComparator considers out of order arrays as equal
1 parent cc04c3d commit a4a185a

File tree

3 files changed

+63
-12
lines changed

3 files changed

+63
-12
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"require": {
1818
"php": "^7.4 || ^8.0",
1919
"codeception/codeception": "^4.0",
20-
"predis/predis": "^1.0"
20+
"predis/predis": "^1.0",
21+
"sebastian/comparator": "^4.0"
2122
},
2223
"autoload": {
2324
"classmap": [

src/Codeception/Module/Redis.php

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
use Codeception\Exception\ModuleException;
1010
use Codeception\TestInterface;
1111
use Exception;
12+
use PHPUnit\Framework\ExpectationFailedException;
1213
use Predis\Client as RedisDriver;
14+
use SebastianBergmann\Comparator\ComparisonFailure;
15+
use SebastianBergmann\Comparator\Factory as ComparatorFactory;
1316

1417
/**
1518
* This module uses the [Predis](https://github.com/nrk/predis) library
@@ -363,10 +366,15 @@ public function haveInRedis(string $type, string $key, $value): void
363366
*/
364367
public function dontSeeInRedis(string $key, $value = null): void
365368
{
366-
$this->assertFalse(
367-
$this->checkKeyExists($key, $value),
368-
sprintf('The key "%s" exists', $key) . ($value ? ' and its value matches the one provided' : '')
369-
);
369+
try {
370+
$this->assertFalse(
371+
$this->checkKeyExists($key, $value),
372+
sprintf('The key "%s" exists', $key) . ($value ? ' and its value matches the one provided' : '')
373+
);
374+
} catch (ComparisonFailure $failure) {
375+
// values are different
376+
$this->assertFalse(false);
377+
}
370378
}
371379

372380
/**
@@ -447,10 +455,17 @@ public function dontSeeRedisKeyContains(string $key, $item, $itemValue = null):
447455
*/
448456
public function seeInRedis(string $key, $value = null): void
449457
{
450-
$this->assertTrue(
451-
$this->checkKeyExists($key, $value),
452-
sprintf('Cannot find key "%s"', $key) . ($value ? ' with the provided value' : '')
453-
);
458+
try {
459+
$this->assertTrue(
460+
$this->checkKeyExists($key, $value),
461+
sprintf('Cannot find key "%s"', $key)
462+
);
463+
} catch (ComparisonFailure $failure) {
464+
throw new ExpectationFailedException(
465+
sprintf("Value of key \"%s\" does not match expected value", $key),
466+
$failure
467+
);
468+
}
454469
}
455470

456471
/**
@@ -620,12 +635,16 @@ private function checkKeyContains(string $key, $item, $itemValue = null): bool
620635
* @param mixed $value Optional. If specified, also checks the key has this
621636
* value. Booleans will be converted to 1 and 0 (even inside arrays)
622637
*/
623-
private function checkKeyExists(string $key, $value = null): bool
638+
private function checkKeyExists(string $key, $value): bool
624639
{
625640
$type = $this->driver->type($key);
626641

642+
if ($type == 'none') {
643+
return false;
644+
}
645+
627646
if (is_null($value)) {
628-
return $type != 'none';
647+
return true;
629648
}
630649

631650
$value = $this->boolToString($value);
@@ -666,7 +685,30 @@ private function checkKeyExists(string $key, $value = null): bool
666685
break;
667686

668687
default:
669-
$result = false;
688+
throw new ModuleException(
689+
$this,
690+
sprintf("Unexpected value type %s", $type)
691+
);
692+
}
693+
694+
if (!$result) {
695+
$comparatorFactory = new ComparatorFactory();
696+
$comparator = $comparatorFactory->getComparatorFor($value, $reply);
697+
$comparator->assertEquals($value, $reply);
698+
699+
if ($type == 'zset') {
700+
/**
701+
* ArrayComparator considers out of order assoc arrays as equal
702+
* So we have to compare them as strings
703+
*/
704+
$replyAsString = var_export($reply, true);
705+
$valueAsString = var_export($value, true);
706+
$comparator = $comparatorFactory->getComparatorFor($valueAsString, $replyAsString);
707+
$comparator->assertEquals($valueAsString, $replyAsString);
708+
}
709+
// If comparator things that values are equal, then we trust it
710+
// This shouldn't happen in practice.
711+
return true;
670712
}
671713

672714
return $result;

tests/unit/Codeception/Module/RedisTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -956,12 +956,15 @@ public function testDontSeeRedisKeyContainsHashWithIncorrectFieldWithValue()
956956
public function testSeeInRedisNonExistingKeyWithoutValue()
957957
{
958958
$this->shouldFail();
959+
$this->expectExceptionMessage('Cannot find key "doesnotexist"');
959960
$this->module->seeInRedis('doesnotexist');
960961
}
961962

962963
public function testSeeInRedisNonExistingKeyWithValue()
963964
{
964965
$this->shouldFail();
966+
$this->expectExceptionMessageMatches('/^Cannot find key "doesnotexist"' .
967+
"\n" . 'Failed asserting that false is true.$/');
965968
$this->module->seeInRedis(
966969
'doesnotexist',
967970
'some value'
@@ -994,6 +997,7 @@ public function testSeeInRedisExistingStringWithCorrectValue()
994997
public function testSeeInRedisExistingStringWithIncorrectValue()
995998
{
996999
$this->shouldFail();
1000+
$this->expectExceptionMessage('Value of key "test:string" does not match expected value');
9971001
$this->module->seeInRedis(
9981002
self::$keys['string']['name'],
9991003
'incorrect value'
@@ -1024,6 +1028,7 @@ public function testSeeInRedisExistingListWithCorrectValueDifferentOrder()
10241028
public function testSeeInRedisExistingListWithIncorrectValue()
10251029
{
10261030
$this->shouldFail();
1031+
$this->expectExceptionMessage('Value of key "test:list" does not match expected value');
10271032
$this->module->seeInRedis(
10281033
self::$keys['list']['name'],
10291034
['incorrect', 'value']
@@ -1053,6 +1058,7 @@ public function testSeeInRedisExistingSetWithCorrectValueDifferentOrder()
10531058
public function testSeeInRedisExistingSetWithIncorrectValue()
10541059
{
10551060
$this->shouldFail();
1061+
$this->expectExceptionMessage('Value of key "test:set" does not match expected value');
10561062
$this->module->seeInRedis(
10571063
self::$keys['set']['name'],
10581064
['incorrect', 'value']
@@ -1092,6 +1098,7 @@ public function testSeeInRedisExistingZSetWithCorrectValueDifferentOrder()
10921098
public function testSeeInRedisExistingZSetWithIncorrectValue()
10931099
{
10941100
$this->shouldFail();
1101+
$this->expectExceptionMessage('Value of key "test:zset" does not match expected value');
10951102
$this->module->seeInRedis(
10961103
self::$keys['zset']['name'],
10971104
['incorrect' => 1, 'value' => 2]
@@ -1121,6 +1128,7 @@ public function testSeeInRedisExistingHashWithCorrectValueDifferentOrder()
11211128
public function testSeeInRedisExistingHashWithIncorrectValue()
11221129
{
11231130
$this->shouldFail();
1131+
$this->expectExceptionMessage('Value of key "test:hash" does not match expected value');
11241132
$this->module->seeInRedis(
11251133
self::$keys['hash']['name'],
11261134
['incorrect' => 'value']

0 commit comments

Comments
 (0)