forked from predis/predis
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[tests] Rewrite the whole test suite to allow more granular testing.
Fix also a few bugs found while rewriting the test suite. In order to be able to run integration tests, the test suite requires a version of Redis >= 2.4.0. The units have been splitted into several different groups using PHPUnit @group annotation to allow developers to enable, disable and combine certain types of tests. The available groups are: - disconnected: can run without a Redis server online - connected: active connection to a Redis server is required - commands: test dedicated to a specific Redis command - slow: performs operations that can slow down execution; A list of the available groups can be obtained by running phpunit --list-groups Groups of tests can be disabled or enabled via the XML configuration file or the standard command-line test runner. Please note that due to a bug in PHPUnit, older versions ignore the --group option when the group is excluded in the XML configuration file. Please refer to https://github.com/sebastianbergmann/phpunit/issues/320 for details Integration tests in the @connected group check if the command being tested is defined in the selected server profile (see the value of the TEST_SERVER_VERSION constant in phpunit.xml). If the command is not defined in the target server profile, the integration test is automatically marked as skipped. We also provide an helper script in the bin directory that can be used to automatically generate a file with the scheleton of a test case for a Redis command by specifying the name of the class in the Predis\Commands namespace. For example, to generate a test case for SET (represented by the Predis\Commands\StringSet class): ./bin/generate-command-test.php --class=StringSet The realm of a command is automatically inferred from the name of the class, but it can be set using the --realm option.
- Loading branch information
Showing
234 changed files
with
24,387 additions
and
3,911 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
phpunit.xml | ||
experiments/ | ||
test/enable.tests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,237 @@ | ||
#!/usr/bin/env php | ||
<?php | ||
|
||
/* | ||
* This file is part of the Predis package. | ||
* | ||
* (c) Daniele Alessandri <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
use Predis\Commands\ICommand; | ||
use Predis\Commands\IPrefixable; | ||
|
||
class CommandTestCaseGenerator | ||
{ | ||
private $options; | ||
|
||
public function __construct(Array $options) | ||
{ | ||
if (!isset($options['class'])) { | ||
throw new RuntimeException("Missing 'class' option."); | ||
} | ||
$this->options = $options; | ||
} | ||
|
||
public static function fromCommandLine() | ||
{ | ||
$parameters = array( | ||
'c:' => 'class:', | ||
'r::' => 'realm::', | ||
'o::' => 'output::', | ||
'x::' => 'overwrite::' | ||
); | ||
|
||
$getops = getopt(implode(array_keys($parameters)), $parameters); | ||
|
||
$options = array( | ||
'overwrite' => false, | ||
'tests' => __DIR__.'/../tests', | ||
); | ||
|
||
foreach ($getops as $option => $value) { | ||
switch ($option) { | ||
case 'c': | ||
case 'class': | ||
$options['class'] = $value; | ||
break; | ||
|
||
case 'r': | ||
case 'realm': | ||
$options['realm'] = $value; | ||
break; | ||
|
||
case 'o': | ||
case 'output': | ||
$options['output'] = $value; | ||
break; | ||
|
||
case 'x': | ||
case 'overwrite': | ||
$options['overwrite'] = true; | ||
break; | ||
} | ||
} | ||
|
||
if (!isset($options['class'])) { | ||
throw new RuntimeException("Missing 'class' option."); | ||
} | ||
|
||
$options['fqn'] = "Predis\\Commands\\{$options['class']}"; | ||
$options['path'] = "Predis/Commands/{$options['class']}.php"; | ||
|
||
$source = __DIR__.'/../lib/'.$options['path']; | ||
if (!file_exists($source)) { | ||
throw new RuntimeException("Cannot find class file for {$options['fqn']} in $source."); | ||
} | ||
|
||
if (!isset($options['output'])) { | ||
$options['output'] = sprintf("%s/%s", $options['tests'], str_replace('.php', 'Test.php', $options['path'])); | ||
} | ||
|
||
return new self($options); | ||
} | ||
|
||
protected function getTestRealm() | ||
{ | ||
if (isset($this->options['realm'])) { | ||
if (!$this->options['realm']) { | ||
throw new RuntimeException('Invalid value for realm has been sepcified (empty).'); | ||
} | ||
return $this->options['realm']; | ||
} | ||
|
||
$class = array_pop(explode('\\', $this->options['fqn'])); | ||
list($realm,) = preg_split('/([[:upper:]][[:lower:]]+)/', $class, 2, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); | ||
|
||
return strtolower($realm); | ||
} | ||
|
||
public function generate() | ||
{ | ||
$reflection = new ReflectionClass($class = $this->options['fqn']); | ||
|
||
if (!$reflection->isInstantiable()) { | ||
throw new RuntimeException("Class $class must be instantiable, abstract classes or interfaces are not allowed."); | ||
} | ||
if (!$reflection->implementsInterface('Predis\Commands\ICommand')) { | ||
throw new RuntimeException("Class $class must implement the Predis\Commands\ICommand interface."); | ||
} | ||
|
||
$instance = $reflection->newInstance(); | ||
$buffer = $this->getTestCaseBuffer($instance); | ||
|
||
return $buffer; | ||
} | ||
|
||
public function save() | ||
{ | ||
$options = $this->options; | ||
if (file_exists($options['output']) && !$options['overwrite']) { | ||
throw new RuntimeException("File {$options['output']} already exist. Specify the --overwrite option to overwrite the existing file."); | ||
} | ||
file_put_contents($options['output'], $this->generate()); | ||
} | ||
|
||
protected function getTestCaseBuffer(ICommand $instance) | ||
{ | ||
$id = $instance->getId(); | ||
$fqn = get_class($instance); | ||
$class = array_pop(explode('\\', $fqn)) . "Test"; | ||
$realm = $this->getTestRealm(); | ||
|
||
$buffer =<<<PHP | ||
<?php | ||
/* | ||
* This file is part of the Predis package. | ||
* | ||
* (c) Daniele Alessandri <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
namespace Predis\Commands; | ||
use \PHPUnit_Framework_TestCase as StandardTestCase; | ||
/** | ||
* @group commands | ||
* @group realm-$realm | ||
*/ | ||
class $class extends CommandTestCase | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function getExpectedCommand() | ||
{ | ||
return '$fqn'; | ||
} | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function getExpectedId() | ||
{ | ||
return '$id'; | ||
} | ||
/** | ||
* @group disconnected | ||
*/ | ||
public function testFilterArguments() | ||
{ | ||
\$this->markTestIncomplete('This test has not been implemented yet.'); | ||
\$arguments = array(/* add arguments */); | ||
\$expected = array(/* add arguments */); | ||
\$command = \$this->getCommand(); | ||
\$command->setArguments(\$arguments); | ||
\$this->assertSame(\$expected, \$command->getArguments()); | ||
} | ||
/** | ||
* @group disconnected | ||
*/ | ||
public function testParseResponse() | ||
{ | ||
\$this->markTestIncomplete('This test has not been implemented yet.'); | ||
\$raw = null; | ||
\$expected = null; | ||
\$command = \$this->getCommand(); | ||
\$this->assertSame(\$expected, \$command->parseResponse(\$raw)); | ||
} | ||
PHP; | ||
|
||
if ($instance instanceof IPrefixable) { | ||
$buffer .=<<<PHP | ||
/** | ||
* @group disconnected | ||
*/ | ||
public function testPrefixKeys() | ||
{ | ||
\$this->markTestIncomplete('This test has not been implemented yet.'); | ||
\$arguments = array(/* add arguments */); | ||
\$expected = array(/* add arguments */); | ||
\$command = \$this->getCommandWithArgumentsArray(\$arguments); | ||
\$command->prefixKeys('prefix:'); | ||
\$this->assertSame(\$expected, \$command->getArguments()); | ||
} | ||
PHP; | ||
} | ||
|
||
return "$buffer}\n"; | ||
} | ||
} | ||
|
||
// ------------------------------------------------------------------------- // | ||
|
||
require __DIR__.'/../autoload.php'; | ||
|
||
$generator = CommandTestCaseGenerator::fromCommandLine(); | ||
$generator->save(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
/** | ||
* @link https://redis.io/commands/keys | ||
* @author Daniele Alessandri <[email protected]> | ||
* @deprecated | ||
*/ | ||
class KeyKeysV12x extends KeyKeys | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,7 @@ | |
* @link https://redis.io/commands/randomkey | ||
* @author Daniele Alessandri <[email protected]> | ||
*/ | ||
class KeyRandom extends PrefixableCommand | ||
class KeyRandom extends Command | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
|
Oops, something went wrong.