From 27b83c19943d9c02f0329f0ef1cabb56d060403c Mon Sep 17 00:00:00 2001 From: hidenorigoto Date: Fri, 9 May 2014 10:36:03 +0900 Subject: [PATCH] Added output CruiseControl file feature --- .../Command/PluginCommand.php | 23 ++++++- .../Configuration/GeneralConfiguration.php | 15 ++++- .../Transformation/GeneralTransformer.php | 4 +- .../TestRunner/Notification/Notifier.php | 65 ++++++++++++++++++- .../ContinuousTesting/CommandLineBuilder.php | 27 +++++++- .../TestRunner/Resources/config/general.yml | 7 +- .../TestRunner/Resources/config/phpunit.yml | 2 +- .../Command/TestCase.php | 17 +++++ .../CommandLineBuilderTest.php | 8 +++ 9 files changed, 160 insertions(+), 8 deletions(-) diff --git a/src/Stagehand/TestRunner/CLI/TestRunnerApplication/Command/PluginCommand.php b/src/Stagehand/TestRunner/CLI/TestRunnerApplication/Command/PluginCommand.php index 4e1f5d8b..3b27ef7d 100644 --- a/src/Stagehand/TestRunner/CLI/TestRunnerApplication/Command/PluginCommand.php +++ b/src/Stagehand/TestRunner/CLI/TestRunnerApplication/Command/PluginCommand.php @@ -92,6 +92,12 @@ protected function configure() if ($this->getPlugin()->hasFeature('notify')) { $this->addOption('notify', 'm', InputOption::VALUE_NONE, 'Notifies test results by using the growlnotify command in Mac OS X and Windows or the notify-send command in Linux.'); + + // available since Release 3.6.3X + $this->addOption('cc-file', null, InputOption::VALUE_REQUIRED, 'Outputs a test status to a specified file with CruiseControl XML format'); + + // available since Release 3.6.3X + $this->addOption('cc-name', null, InputOption::VALUE_REQUIRED, 'Specifies a ProjectName for a CruiseControl file. If omitted, basename of cc-file option will be used.'); } if ($this->getPlugin()->hasFeature('detailed_progress')) { @@ -226,7 +232,22 @@ protected function transformToConfiguration(InputInterface $input, OutputInterfa if ($input->getOption('notify')) { $transformation->setConfigurationPart( GeneralConfiguration::getConfigurationID(), - array('notify' => true) + array('notify' => array( + 'enabled' => true, + )) + ); + } + if ($input->getOption('cc-file')) { + $projectName = $input->getOption('cc-name') ? + $input->getOption('cc-name') : + basename($input->getOption('cc-file'), '.xml'); + $transformation->setConfigurationPart( + GeneralConfiguration::getConfigurationID(), + array('notify' => array( + 'enabled' => true, + 'file' => $input->getOption('cc-file'), + 'project_name' => $projectName + )) ); } } diff --git a/src/Stagehand/TestRunner/DependencyInjection/Configuration/GeneralConfiguration.php b/src/Stagehand/TestRunner/DependencyInjection/Configuration/GeneralConfiguration.php index d866cafc..46394182 100644 --- a/src/Stagehand/TestRunner/DependencyInjection/Configuration/GeneralConfiguration.php +++ b/src/Stagehand/TestRunner/DependencyInjection/Configuration/GeneralConfiguration.php @@ -119,8 +119,19 @@ protected function defineGrammar(NodeBuilder $nodeBuilder) ->end() ->end() ->end() - ->booleanNode('notify') - ->defaultFalse() + ->arrayNode('notify') + ->addDefaultsIfNotSet() + ->children() // available since Release 3.6.3X + ->booleanNode('enabled') + ->defaultFalse() + ->end() + ->scalarNode('file') + ->defaultNull() + ->end() + ->scalarNode('project_name') + ->defaultNull() + ->end() + ->end() ->end() ->arrayNode('junit_xml') ->addDefaultsIfNotSet() diff --git a/src/Stagehand/TestRunner/DependencyInjection/Transformation/GeneralTransformer.php b/src/Stagehand/TestRunner/DependencyInjection/Transformation/GeneralTransformer.php index 6ade9fe0..7b433626 100644 --- a/src/Stagehand/TestRunner/DependencyInjection/Transformation/GeneralTransformer.php +++ b/src/Stagehand/TestRunner/DependencyInjection/Transformation/GeneralTransformer.php @@ -59,7 +59,9 @@ public function transform() $this->setParameter('continuous_testing', $this->configurationPart['autotest']['enabled']); $this->setParameter('continuous_testing_watch_dirs', $this->configurationPart['autotest']['watch_dirs']); - $this->setParameter('notify', $this->configurationPart['notify']); + $this->setParameter('notify_enabled', $this->configurationPart['notify']['enabled']); + $this->setParameter('notify_file', $this->configurationPart['notify']['file']); + $this->setParameter('notify_project_name', $this->configurationPart['notify']['project_name']); $this->setParameter('junit_xml_file', $this->configurationPart['junit_xml']['file']); $this->setParameter('junit_xml_realtime', $this->configurationPart['junit_xml']['realtime']); diff --git a/src/Stagehand/TestRunner/Notification/Notifier.php b/src/Stagehand/TestRunner/Notification/Notifier.php index 865d5698..da60670e 100644 --- a/src/Stagehand/TestRunner/Notification/Notifier.php +++ b/src/Stagehand/TestRunner/Notification/Notifier.php @@ -60,6 +60,10 @@ class Notifier const TITLE_FAILED = 'Test Failed'; const TITLE_STOPPED = 'Test Stopped'; + const CC_STATUS_PASSED = 'Success'; + const CC_STATUS_FAILED = 'Failure'; + const CC_STATUS_UNKNOWN = 'Unknown'; + public static $ICON_PASSED; public static $ICON_FAILED; public static $ICON_STOPPED; @@ -76,6 +80,26 @@ class Notifier */ protected $os; + /** + * @var string + */ + protected $file; + + /** + * @var string + */ + protected $projectName; + + /** + * @param string + * @param string + */ + function __construct($file = null, $projectName = null) + { + $this->file = $file; + $this->projectName = $projectName; + } + /** * @param \Stagehand\TestRunner\Util\OS $os * @since Method available since Release 3.0.1 @@ -85,6 +109,24 @@ public function setOS(OS $os) $this->os = $os; } + /** + * @return string + * @since Method available since Release 3.6.3X + */ + public function getFile() + { + return $this->file; + } + + /** + * @return string + * @since Method available since Release 3.6.3X + */ + public function getProjectName() + { + return $this->projectName; + } + /** * @param \Stagehand\TestRunner\Notification\Notification $notification */ @@ -111,15 +153,36 @@ protected function buildNotifyCommand(Notification $notification) if ($notification->isPassed()) { $title = self::TITLE_PASSED; $icon = self::$ICON_PASSED; + $CCStatus = self::CC_STATUS_PASSED; } elseif ($notification->isFailed()) { $title = self::TITLE_FAILED; $icon = self::$ICON_FAILED; + $CCStatus = self::CC_STATUS_FAILED; } elseif ($notification->isStopped()) { $title = self::TITLE_STOPPED; $icon = self::$ICON_STOPPED; + $CCStatus = self::CC_STATUS_UNKNOWN; } - if ($this->os->isWin()) { + if ($this->file != null) { + $projectName = $this->projectName; + $currentDate = date(DATE_ATOM); + $xml = <<< XML + + + + +XML; + file_put_contents($this->file, $xml); + + return null; + } elseif ($this->os->isWin()) { return sprintf( 'growlnotify /t:%s /p:-2 /i:%s /a:Stagehand_TestRunner /r:%s,%s,%s /n:%s /silent:true %s', escapeshellarg($title), diff --git a/src/Stagehand/TestRunner/Process/ContinuousTesting/CommandLineBuilder.php b/src/Stagehand/TestRunner/Process/ContinuousTesting/CommandLineBuilder.php index ab43cedb..09dc9f55 100644 --- a/src/Stagehand/TestRunner/Process/ContinuousTesting/CommandLineBuilder.php +++ b/src/Stagehand/TestRunner/Process/ContinuousTesting/CommandLineBuilder.php @@ -104,6 +104,17 @@ class CommandLineBuilder */ protected $options = array(); + /** + * @var string + */ + protected $cruiseControlFile; + + /** + * @var string + */ + protected $projectName; + + /** * @param \Stagehand\TestRunner\Core\Environment $environment * @param \Stagehand\TestRunner\Util\LegacyProxy $legacyProxy @@ -113,6 +124,8 @@ class CommandLineBuilder * @param \Stagehand\TestRunner\CLI\Terminal $terminal * @param \Stagehand\TestRunner\Core\TestTargetRepository $testTargetRepository * @param \Stagehand\TestRunner\Process\ContinuousTesting\CommandLineOptionBuilderInterface $commandLineOptionBuilder + * @param string available since Release 3.6.3X + * @param string available since Release 3.6.3X */ public function __construct( Environment $environment, @@ -122,7 +135,9 @@ public function __construct( Runner $runner, Terminal $terminal, TestTargetRepository $testTargetRepository, - CommandLineOptionBuilderInterface $commandLineOptionBuilder = null + CommandLineOptionBuilderInterface $commandLineOptionBuilder = null, + $cruiseControlFile = null, + $projectName = null ) { $this->environment = $environment; @@ -133,6 +148,8 @@ public function __construct( $this->terminal = $terminal; $this->testTargetRepository = $testTargetRepository; $this->commandLineOptionBuilder = $commandLineOptionBuilder; + $this->cruiseControlFile = $cruiseControlFile; + $this->projectName = $projectName; } /** @@ -205,6 +222,14 @@ protected function buildOptions($command) $options[] = '-m'; } + if ($this->cruiseControlFile) { + $options[] = '--cc-file=' . escapeshellarg($this->cruiseControlFile); + } + + if ($this->projectName) { + $options[] = '--cc-name=' . escapeshellarg($this->projectName); + } + if ($this->runner->shouldStopOnFailure()) { $options[] = '--stop-on-failure'; } diff --git a/src/Stagehand/TestRunner/Resources/config/general.yml b/src/Stagehand/TestRunner/Resources/config/general.yml index 680f3621..49a0b642 100644 --- a/src/Stagehand/TestRunner/Resources/config/general.yml +++ b/src/Stagehand/TestRunner/Resources/config/general.yml @@ -45,7 +45,9 @@ parameters: recursive: false continuous_testing: false continuous_testing_watch_dirs: [] - notify: false + notify_enabled: false # available since Release 3.6.3X + notify_file: ~ # available since Release 3.6.3X + notify_project_name: ~ # available since Release 3.6.3X test_methods: [] test_classes: [] junit_xml_file: null @@ -117,6 +119,7 @@ services: # Notification notifier: class: "%notifier.class%" + arguments: [ "%notify_file%", "%notify_project_name%" ] calls: - [ setLegacyProxy, [ "@legacy_proxy" ] ] - [ setOS, [ "@os" ] ] @@ -135,6 +138,8 @@ services: - "@terminal" - "@test_target_repository" - "@?command_line_option_builder" + - "%notify_file%" + - "%notify_project_name%" continuous_test_runner: class: "%continuous_test_runner.class%" arguments: [ "@preparer", "@command_line_builder" ] diff --git a/src/Stagehand/TestRunner/Resources/config/phpunit.yml b/src/Stagehand/TestRunner/Resources/config/phpunit.yml index 498e7c70..69f4fa99 100644 --- a/src/Stagehand/TestRunner/Resources/config/phpunit.yml +++ b/src/Stagehand/TestRunner/Resources/config/phpunit.yml @@ -65,7 +65,7 @@ services: - [ setDetailedProgress, [ "%detailed_progress%" ] ] - [ setJUnitXMLFile, [ "%junit_xml_file%" ] ] - [ setJUnitXMLRealtime, [ "%junit_xml_realtime%" ] ] - - [ setNotify, [ "%notify%" ] ] + - [ setNotify, [ "%notify_enabled%" ] ] - [ setPHPUnitConfigurationFactory, [ "@phpunit.phpunit_configuration_factory" ] ] - [ setStopOnFailure, [ "%stop_on_failure%" ] ] - [ setTerminal, [ "@terminal" ] ] diff --git a/test/Stagehand/TestRunner/CLI/TestRunnerApplication/Command/TestCase.php b/test/Stagehand/TestRunner/CLI/TestRunnerApplication/Command/TestCase.php index f9f0b25e..23602fcf 100644 --- a/test/Stagehand/TestRunner/CLI/TestRunnerApplication/Command/TestCase.php +++ b/test/Stagehand/TestRunner/CLI/TestRunnerApplication/Command/TestCase.php @@ -93,6 +93,23 @@ function (\PHPUnit_Framework_TestCase $test, ApplicationContext $applicationCont $test->assertTrue($applicationContext->createComponent('runner')->shouldNotify()); } ), + array( + array('--cc-file=path/to/somewhere'), + function (\PHPUnit_Framework_TestCase $test, ApplicationContext $applicationContext, Transformation $transformation) { + }, + function (\PHPUnit_Framework_TestCase $test, ApplicationContext $applicationContext, Transformation $transformation) { + $test->assertNotNull($applicationContext->createComponent('notifier')->getFile()); + $test->assertTrue($applicationContext->createComponent('runner')->shouldNotify()); + } + ), + array( + array('--cc-file=path/to/somewhere --cc-name="example project"'), + function (\PHPUnit_Framework_TestCase $test, ApplicationContext $applicationContext, Transformation $transformation) { + }, + function (\PHPUnit_Framework_TestCase $test, ApplicationContext $applicationContext, Transformation $transformation) { + $test->assertNotNull($applicationContext->createComponent('notifier')->getProjectName()); + } + ), array( array('--config=example.yml'), function (\PHPUnit_Framework_TestCase $test, ApplicationContext $applicationContext, Transformation $transformation) { diff --git a/test/Stagehand/TestRunner/Process/ContinuousTesting/CommandLineBuilderTest.php b/test/Stagehand/TestRunner/Process/ContinuousTesting/CommandLineBuilderTest.php index 8ebdf9ab..a283cdf5 100644 --- a/test/Stagehand/TestRunner/Process/ContinuousTesting/CommandLineBuilderTest.php +++ b/test/Stagehand/TestRunner/Process/ContinuousTesting/CommandLineBuilderTest.php @@ -100,6 +100,12 @@ public static function setUpBeforeClass() self::$configurators[] = function (ApplicationContext $applicationContext) { $applicationContext->createComponent('runner')->setDetailedProgress(true); }; + self::$configurators[] = function (ApplicationContext $applicationContext) { + $applicationContext->createComponent('notifier'); + }; + self::$configurators[] = function (ApplicationContext $applicationContext) { + $applicationContext->createComponent('notifier'); + }; } /** @@ -202,6 +208,8 @@ public function commandLineOptions() array(array(escapeshellarg(strtolower($this->getPluginID())), '-R', '--stop-on-failure'), array(true, true, true)), array(array(escapeshellarg(strtolower($this->getPluginID())), '-R', '--test-file-pattern=' . escapeshellarg('PATTERN')), array(true, true, true)), array(array(escapeshellarg(strtolower($this->getPluginID())), '-R', '--detailed-progress'), array(true, true, true)), + array(array(escapeshellarg(strtolower($this->getPluginID())), '-R', '--cc-file=' . escapeshellarg('/path/to/somewhere')), array(true, true, false)), + array(array(escapeshellarg(strtolower($this->getPluginID())), '-R', '--cc-file=' . escapeshellarg('/path/to/somewhere') . ' --cc-name="foo project"'), array(true, true, false)), ); return array_map(function (array $preservedConfiguration) {