|
9 | 9 | use Codeception\Exception\ModuleException;
|
10 | 10 | use Codeception\TestInterface;
|
11 | 11 | use Exception;
|
| 12 | +use PHPUnit\Framework\ExpectationFailedException; |
12 | 13 | use Predis\Client as RedisDriver;
|
| 14 | +use SebastianBergmann\Comparator\ComparisonFailure; |
| 15 | +use SebastianBergmann\Comparator\Factory as ComparatorFactory; |
13 | 16 |
|
14 | 17 | /**
|
15 | 18 | * This module uses the [Predis](https://github.com/nrk/predis) library
|
@@ -363,10 +366,15 @@ public function haveInRedis(string $type, string $key, $value): void
|
363 | 366 | */
|
364 | 367 | public function dontSeeInRedis(string $key, $value = null): void
|
365 | 368 | {
|
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 | + } |
370 | 378 | }
|
371 | 379 |
|
372 | 380 | /**
|
@@ -447,10 +455,17 @@ public function dontSeeRedisKeyContains(string $key, $item, $itemValue = null):
|
447 | 455 | */
|
448 | 456 | public function seeInRedis(string $key, $value = null): void
|
449 | 457 | {
|
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 | + } |
454 | 469 | }
|
455 | 470 |
|
456 | 471 | /**
|
@@ -620,12 +635,16 @@ private function checkKeyContains(string $key, $item, $itemValue = null): bool
|
620 | 635 | * @param mixed $value Optional. If specified, also checks the key has this
|
621 | 636 | * value. Booleans will be converted to 1 and 0 (even inside arrays)
|
622 | 637 | */
|
623 |
| - private function checkKeyExists(string $key, $value = null): bool |
| 638 | + private function checkKeyExists(string $key, $value): bool |
624 | 639 | {
|
625 | 640 | $type = $this->driver->type($key);
|
626 | 641 |
|
| 642 | + if ($type == 'none') { |
| 643 | + return false; |
| 644 | + } |
| 645 | + |
627 | 646 | if (is_null($value)) {
|
628 |
| - return $type != 'none'; |
| 647 | + return true; |
629 | 648 | }
|
630 | 649 |
|
631 | 650 | $value = $this->boolToString($value);
|
@@ -666,7 +685,30 @@ private function checkKeyExists(string $key, $value = null): bool
|
666 | 685 | break;
|
667 | 686 |
|
668 | 687 | 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; |
670 | 712 | }
|
671 | 713 |
|
672 | 714 | return $result;
|
|
0 commit comments