#!/usr/bin/env php 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/Predis', ); 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."); } if (!isset($options['realm'])) { throw new RuntimeException("Missing 'realm' option."); } $options['fqn'] = "Predis\\Command\\Redis\\{$options['class']}"; $options['path'] = "Command/Redis/{$options['class']}.php"; $source = __DIR__.'/../src/'.$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 (empty($this->options['realm'])) { throw new RuntimeException('Invalid value for realm has been specified (empty).'); } return $this->options['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\Command\CommandInterface')) { throw new RuntimeException("Class $class must implement Predis\Command\CommandInterface."); } /* * @var CommandInterface */ $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(CommandInterface $instance) { $id = $instance->getId(); $fqn = get_class($instance); $fqnParts = explode('\\', $fqn); $class = array_pop($fqnParts) . "Test"; $realm = $this->getTestRealm(); $buffer =<<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(): void { \$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 PrefixableCommandInterface) { $buffer .=<<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()); } /** * @group disconnected */ public function testPrefixKeysIgnoredOnEmptyArguments(): void { \$command = \$this->getCommand(); \$command->prefixKeys('prefix:'); \$this->assertSame(array(), \$command->getArguments()); } PHP; } return "$buffer}\n"; } } // ------------------------------------------------------------------------- // require __DIR__.'/../autoload.php'; $generator = CommandTestCaseGenerator::fromCommandLine(); $generator->save();