Skip to content

Commit

Permalink
refs #1673 automatically attach "Test" to testname if needed, added o…
Browse files Browse the repository at this point in the history
…ptional option to define type of test
  • Loading branch information
tsteur committed Nov 7, 2013
1 parent 9d48059 commit a1c6c2e
Showing 1 changed file with 39 additions and 3 deletions.
42 changes: 39 additions & 3 deletions plugins/CoreConsole/GenerateTest.php
Expand Up @@ -11,6 +11,7 @@

namespace Piwik\Plugins\CoreConsole;

use Piwik\Common;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -25,16 +26,24 @@ protected function configure()
$this->setName('generate:test')
->setDescription('Adds a test to an existing plugin')
->addOption('pluginname', null, InputOption::VALUE_REQUIRED, 'The name of an existing plugin')
->addOption('testname', null, InputOption::VALUE_REQUIRED, 'The name of the test you want to create');
->addOption('testname', null, InputOption::VALUE_REQUIRED, 'The name of the test you want to create')
->addOption('testtype', 't', InputOption::VALUE_OPTIONAL, 'Whether you want to create a "unit", "integration" or "database" test', 'unit');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$pluginName = $this->getPluginName($input, $output);
$testName = $this->getTestName($input, $output);
$testClass = $this->getTestClass($input);

$exampleFolder = PIWIK_INCLUDE_PATH . '/plugins/ExamplePlugin';
$replace = array('ExamplePlugin' => $pluginName, 'SimpleTest' => $testName);
$replace = array(
'ExamplePlugin' => $pluginName,
'SimpleTest' => $testName,
'\PHPUnit_Framework_TestCase' => $testClass,
'@group Plugins' => '@group ' . $this->getTestType($input)
);

$whitelistFiles = array('/tests', '/tests/SimpleTest.php');

$this->copyTemplateToPlugin($exampleFolder, $pluginName, $replace, $whitelistFiles);
Expand Down Expand Up @@ -65,12 +74,16 @@ private function getTestName(InputInterface $input, OutputInterface $output)
};

if (empty($testname)) {
$dialog = $this->getHelperSet()->get('dialog');
$dialog = $this->getHelperSet()->get('dialog');
$testname = $dialog->askAndValidate($output, 'Enter the name of the test: ', $validate);
} else {
$validate($testname);
}

if (!Common::stringEndsWith(strtolower($testname), 'test')) {
$testname = $testname . 'Test';
}

$testname = ucfirst($testname);

return $testname;
Expand Down Expand Up @@ -108,5 +121,28 @@ private function getPluginName(InputInterface $input, OutputInterface $output)
return $pluginName;
}

/**
* @param InputInterface $input
* @return string
*/
private function getTestClass(InputInterface $input)
{
$testClass = '\PHPUnit_Framework_TestCase';
if ('integration' == $input->getOption('testtype')) {
$testClass = '\IntegrationTestCase';
} elseif ('database' == $input->getOption('testtype')) {
$testClass = '\DatabaseTestCase';
}

return $testClass;
}

/**
* @param InputInterface $input
* @return string
*/
private function getTestType(InputInterface $input)
{
return 'unit' == $input->getOption('testtype') ? 'Plugins' : 'Integration';
}
}

0 comments on commit a1c6c2e

Please sign in to comment.