PageRenderTime 37ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/CoreConsole/Commands/GenerateTest.php

https://github.com/CodeYellowBV/piwik
PHP | 189 lines | 128 code | 28 blank | 33 comment | 15 complexity | 6518fb3eb3fb0a2a30e7bd6332ba8734 MD5 | raw file
Possible License(s): LGPL-3.0, JSON, MIT, GPL-3.0, LGPL-2.1, GPL-2.0, AGPL-1.0, BSD-2-Clause, BSD-3-Clause
  1. <?php
  2. /**
  3. * Piwik - free/libre analytics platform
  4. *
  5. * @link http://piwik.org
  6. * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  7. *
  8. */
  9. namespace Piwik\Plugins\CoreConsole\Commands;
  10. use Piwik\Common;
  11. use Symfony\Component\Console\Input\InputInterface;
  12. use Symfony\Component\Console\Input\InputOption;
  13. use Symfony\Component\Console\Output\OutputInterface;
  14. /**
  15. */
  16. class GenerateTest extends GeneratePluginBase
  17. {
  18. protected function configure()
  19. {
  20. $this->setName('generate:test')
  21. ->setDescription('Adds a test to an existing plugin')
  22. ->addOption('pluginname', null, InputOption::VALUE_REQUIRED, 'The name of an existing plugin')
  23. ->addOption('testname', null, InputOption::VALUE_REQUIRED, 'The name of the test to create')
  24. ->addOption('testtype', null, InputOption::VALUE_REQUIRED, 'Whether you want to create a "unit", "integration" or "database" test');
  25. }
  26. protected function execute(InputInterface $input, OutputInterface $output)
  27. {
  28. $pluginName = $this->getPluginName($input, $output);
  29. $testName = $this->getTestName($input, $output);
  30. $testType = $this->getTestType($input, $output);
  31. $exampleFolder = PIWIK_INCLUDE_PATH . '/plugins/ExamplePlugin';
  32. $replace = array(
  33. 'ExamplePlugin' => $pluginName,
  34. 'SimpleTest' => $testName,
  35. 'SimpleIntegrationTest' => $testName,
  36. '@group Plugins' => '@group ' . $testType
  37. );
  38. $testClass = $this->getTestClass($testType);
  39. if(!empty($testClass)) {
  40. $replace['\PHPUnit_Framework_TestCase'] = $testClass;
  41. }
  42. $whitelistFiles = $this->getTestFilesWhitelist($testType);
  43. $this->copyTemplateToPlugin($exampleFolder, $pluginName, $replace, $whitelistFiles);
  44. $this->writeSuccessMessage($output, array(
  45. sprintf('Test %s for plugin %s generated.', $testName, $pluginName),
  46. 'You can now start writing beautiful tests!',
  47. ));
  48. $this->writeSuccessMessage($output, array(
  49. 'To run all your plugin tests, execute the command: ',
  50. sprintf('./console tests:run %s', $pluginName),
  51. 'To run only this test: ',
  52. sprintf('./console tests:run %s', $testName),
  53. 'Enjoy!'
  54. ));
  55. }
  56. /**
  57. * @param InputInterface $input
  58. * @param OutputInterface $output
  59. * @return string
  60. * @throws \RunTimeException
  61. */
  62. private function getTestName(InputInterface $input, OutputInterface $output)
  63. {
  64. $testname = $input->getOption('testname');
  65. $validate = function ($testname) {
  66. if (empty($testname)) {
  67. throw new \InvalidArgumentException('You have to enter a valid test name ');
  68. }
  69. return $testname;
  70. };
  71. if (empty($testname)) {
  72. $dialog = $this->getHelperSet()->get('dialog');
  73. $testname = $dialog->askAndValidate($output, 'Enter the name of the test: ', $validate);
  74. } else {
  75. $validate($testname);
  76. }
  77. if (!Common::stringEndsWith(strtolower($testname), 'test')) {
  78. $testname = $testname . 'Test';
  79. }
  80. $testname = ucfirst($testname);
  81. return $testname;
  82. }
  83. /**
  84. * @param InputInterface $input
  85. * @param OutputInterface $output
  86. * @return array
  87. * @throws \RunTimeException
  88. */
  89. protected function getPluginName(InputInterface $input, OutputInterface $output)
  90. {
  91. $pluginNames = $this->getPluginNames();
  92. $invalidName = 'You have to enter the name of an existing plugin';
  93. return $this->askPluginNameAndValidate($input, $output, $pluginNames, $invalidName);
  94. }
  95. /**
  96. * @param InputInterface $input
  97. * @return string
  98. */
  99. private function getTestClass($testType)
  100. {
  101. if ('Database' == $testType) {
  102. return '\DatabaseTestCase';
  103. }
  104. if ('Unit' == $testType) {
  105. return '\PHPUnit_Framework_TestCase';
  106. }
  107. return false;
  108. }
  109. public function getValidTypes()
  110. {
  111. return array('unit', 'integration', 'database');
  112. }
  113. /**
  114. * @param InputInterface $input
  115. * @param OutputInterface $output
  116. * @return string Unit, Integration, Database
  117. */
  118. private function getTestType(InputInterface $input, OutputInterface $output)
  119. {
  120. $testtype = $input->getOption('testtype');
  121. $self = $this;
  122. $validate = function ($testtype) use ($self) {
  123. if (empty($testtype) || !in_array($testtype, $self->getValidTypes())) {
  124. throw new \InvalidArgumentException('You have to enter a valid test type: ' . implode(" or ", $self->getValidTypes()));
  125. }
  126. return $testtype;
  127. };
  128. if (empty($testtype)) {
  129. $dialog = $this->getHelperSet()->get('dialog');
  130. $testtype = $dialog->askAndValidate($output, 'Enter the type of the test to generate ('. implode(", ", $this->getValidTypes()).'): ', $validate, false, null, $this->getValidTypes());
  131. } else {
  132. $validate($testtype);
  133. }
  134. $testtype = ucfirst($testtype);
  135. return $testtype;
  136. }
  137. /**
  138. * @return array
  139. */
  140. protected function getTestFilesWhitelist($testType)
  141. {
  142. if('Integration' == $testType) {
  143. return array(
  144. '/.gitignore',
  145. '/tests',
  146. '/tests/SimpleIntegrationTest.php',
  147. '/tests/expected',
  148. '/tests/expected/test___API.get_day.xml',
  149. '/tests/expected/test___Goals.getItemsSku_day.xml',
  150. '/tests/processed',
  151. '/tests/processed/.gitignore',
  152. '/tests/fixtures',
  153. '/tests/fixtures/SimpleFixtureTrackFewVisits.php'
  154. );
  155. }
  156. return array(
  157. '/tests',
  158. '/tests/SimpleTest.php'
  159. );
  160. }
  161. }