|
12 | 12 |
|
13 | 13 |
|
14 | 14 | use ConsoleHelpers\SVNBuddy\Repository\Connector\Command;
|
| 15 | +use Prophecy\Argument; |
15 | 16 | use Prophecy\Prophecy\ObjectProphecy;
|
| 17 | +use Symfony\Component\Process\Exception\ProcessFailedException; |
| 18 | +use Symfony\Component\Process\Process; |
| 19 | +use Tests\ConsoleHelpers\SVNBuddy\ProphecyToken\RegExToken; |
16 | 20 |
|
17 | 21 | class CommandTest extends \PHPUnit_Framework_TestCase
|
18 | 22 | {
|
@@ -56,15 +60,261 @@ protected function setUp()
|
56 | 60 | $this->_command = $this->_createCommand();
|
57 | 61 | }
|
58 | 62 |
|
59 |
| - public function testRunWithoutCallbackAndWithoutCallback() |
| 63 | + /** |
| 64 | + * @dataProvider runWithoutCachingDataProvider |
| 65 | + */ |
| 66 | + public function testRunWithoutCaching($callback, $is_xml) |
| 67 | + { |
| 68 | + if ( $is_xml ) { |
| 69 | + $command_line = 'svn log --xml'; |
| 70 | + $process_output = '<log><logentry/></log>'; |
| 71 | + } |
| 72 | + else { |
| 73 | + $command_line = 'svn log'; |
| 74 | + $process_output = 'OK'; |
| 75 | + } |
| 76 | + |
| 77 | + $this->_process->getCommandLine()->willReturn($command_line)->shouldBeCalled(); |
| 78 | + |
| 79 | + $this->_process->mustRun($callback)->shouldBeCalled(); |
| 80 | + $this->_process->getOutput()->willReturn($process_output)->shouldBeCalled(); |
| 81 | + |
| 82 | + $this->_io->isVerbose()->willReturn(false)->shouldBeCalled(); |
| 83 | + $this->_cacheManager->getCache(Argument::any())->shouldNotBeCalled(); |
| 84 | + |
| 85 | + $this->assertCommandOutput($callback, $is_xml, $process_output); |
| 86 | + } |
| 87 | + |
| 88 | + public function runWithoutCachingDataProvider() |
| 89 | + { |
| 90 | + $callback = function ($output, $type) {}; |
| 91 | + |
| 92 | + return array( |
| 93 | + 'w/o callback, not xml' => array(null, false), |
| 94 | + 'w/o callback, is xml' => array(null, true), |
| 95 | + 'w callback, not xml' => array($callback, false), |
| 96 | + 'w callback, is xml' => array($callback, true), |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * @dataProvider runWithCacheDataProvider |
| 102 | + */ |
| 103 | + public function testRunWithMissingCache($duration, $invalidator, $callback, $is_xml) |
| 104 | + { |
| 105 | + if ( $is_xml ) { |
| 106 | + $command_line = 'svn log --xml'; |
| 107 | + $process_output = '<log><logentry/></log>'; |
| 108 | + } |
| 109 | + else { |
| 110 | + $command_line = 'svn log'; |
| 111 | + $process_output = 'OK'; |
| 112 | + } |
| 113 | + |
| 114 | + $this->_process->getCommandLine()->willReturn($command_line)->shouldBeCalled(); |
| 115 | + $this->_process->mustRun($callback)->shouldBeCalled(); |
| 116 | + $this->_process->getOutput()->willReturn($process_output)->shouldBeCalled(); |
| 117 | + |
| 118 | + $this->_io->isVerbose()->willReturn(false)->shouldBeCalled(); |
| 119 | + |
| 120 | + $this->_cacheManager |
| 121 | + ->getCache('command:' . $command_line, $invalidator) |
| 122 | + ->willReturn(null) |
| 123 | + ->shouldBeCalled(); |
| 124 | + $this->_cacheManager |
| 125 | + ->setCache('command:' . $command_line, $process_output, $invalidator, $duration) |
| 126 | + ->shouldBeCalled(); |
| 127 | + |
| 128 | + if ( isset($duration) ) { |
| 129 | + $this->_command->setCacheDuration($duration); |
| 130 | + } |
| 131 | + |
| 132 | + if ( isset($invalidator) ) { |
| 133 | + $this->_command->setCacheInvalidator($invalidator); |
| 134 | + } |
| 135 | + |
| 136 | + $this->assertCommandOutput($callback, $is_xml, $process_output); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * @dataProvider runWithCacheDataProvider |
| 141 | + */ |
| 142 | + public function testRunWithExistingCache($duration, $invalidator, $callback, $is_xml) |
| 143 | + { |
| 144 | + if ( $is_xml ) { |
| 145 | + $command_line = 'svn log --xml'; |
| 146 | + $process_output = '<log><logentry/></log>'; |
| 147 | + } |
| 148 | + else { |
| 149 | + $command_line = 'svn log'; |
| 150 | + $process_output = 'OK'; |
| 151 | + } |
| 152 | + |
| 153 | + $this->_process->getCommandLine()->willReturn($command_line)->shouldBeCalled(); |
| 154 | + $this->_process->mustRun($callback)->shouldNotBeCalled(); |
| 155 | + |
| 156 | + $this->_cacheManager |
| 157 | + ->getCache('command:' . $command_line, $invalidator) |
| 158 | + ->willReturn($process_output) |
| 159 | + ->shouldBeCalled(); |
| 160 | + $this->_cacheManager->setCache(Argument::any())->shouldNotBeCalled(); |
| 161 | + |
| 162 | + if ( isset($duration) ) { |
| 163 | + $this->_command->setCacheDuration($duration); |
| 164 | + } |
| 165 | + |
| 166 | + if ( isset($invalidator) ) { |
| 167 | + $this->_command->setCacheInvalidator($invalidator); |
| 168 | + } |
| 169 | + |
| 170 | + $this->assertCommandOutput($callback, $is_xml, $process_output); |
| 171 | + } |
| 172 | + |
| 173 | + public function runWithCacheDataProvider() |
| 174 | + { |
| 175 | + $callback = function ($output, $type) {}; |
| 176 | + |
| 177 | + return array( |
| 178 | + 'duration only, w/o callback, not xml' => array(100, null, null, false), |
| 179 | + 'invalidator only, w/o callback, not xml' => array(null, 'invalidator', null, false), |
| 180 | + 'duration and invalidator, w/o callback, not xml' => array(100, 'invalidator', null, false), |
| 181 | + |
| 182 | + 'duration only, w/o callback, is xml' => array(100, null, null, true), |
| 183 | + 'invalidator only, w/o callback, is xml' => array(null, 'invalidator', null, true), |
| 184 | + 'duration and invalidator, w/o callback, is xml' => array(100, 'invalidator', null, true), |
| 185 | + |
| 186 | + 'duration only, w callback, not xml' => array(100, null, $callback, false), |
| 187 | + 'invalidator only, w callback, not xml' => array(null, 'invalidator', $callback, false), |
| 188 | + 'duration and invalidator, w callback, not xml' => array(100, 'invalidator', $callback, false), |
| 189 | + |
| 190 | + 'duration only, w callback, is xml' => array(100, null, $callback, true), |
| 191 | + 'invalidator only, w callback, is xml' => array(null, 'invalidator', $callback, true), |
| 192 | + 'duration and invalidator, w callback, is xml' => array(100, 'invalidator', $callback, true), |
| 193 | + ); |
| 194 | + } |
| 195 | + |
| 196 | + /** |
| 197 | + * Asserts command output. |
| 198 | + * |
| 199 | + * @param callable $callback Callback. |
| 200 | + * @param boolean $is_xml Is xml expected. |
| 201 | + * @param string $process_output Process output. |
| 202 | + * |
| 203 | + * @return void |
| 204 | + */ |
| 205 | + protected function assertCommandOutput($callback, $is_xml, $process_output) |
| 206 | + { |
| 207 | + $actual_command_output = $this->_command->run($callback); |
| 208 | + |
| 209 | + if ( $is_xml ) { |
| 210 | + $this->assertInstanceOf('SimpleXMLElement', $actual_command_output); |
| 211 | + $expected_command_output = new \SimpleXMLElement($process_output); |
| 212 | + $this->assertEquals($expected_command_output->asXML(), $actual_command_output->asXML()); |
| 213 | + } |
| 214 | + else { |
| 215 | + $this->assertEquals($process_output, $actual_command_output); |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + public function testVerboseOutput() |
60 | 220 | {
|
61 | 221 | $this->_process->getCommandLine()->willReturn('svn log')->shouldBeCalled();
|
62 | 222 | $this->_process->mustRun(null)->shouldBeCalled();
|
63 | 223 | $this->_process->getOutput()->willReturn('OK')->shouldBeCalled();
|
64 | 224 |
|
65 |
| - $this->_io->isVerbose()->willReturn(false); |
| 225 | + $this->_io->isVerbose()->willReturn(true)->shouldBeCalled(); |
| 226 | + $this->_cacheManager->getCache(Argument::any())->shouldNotBeCalled(); |
| 227 | + |
| 228 | + $this->_io |
| 229 | + ->writeln(new RegExToken("#^\n<fg=white;bg=magenta>\[svn, [\d\.]+s\]: svn log</>$#s")) |
| 230 | + ->shouldBeCalled(); |
| 231 | + |
| 232 | + $this->_command->run(); |
| 233 | + } |
| 234 | + |
| 235 | + /** |
| 236 | + * @dataProvider runLiveDataProvider |
| 237 | + */ |
| 238 | + public function testRunLive($output_type, $expected_output) |
| 239 | + { |
| 240 | + $this->_process->getCommandLine()->willReturn('svn log')->shouldBeCalled(); |
| 241 | + $this->_process |
| 242 | + ->mustRun(Argument::type('callable')) |
| 243 | + ->will(function (array $args) use ($output_type) { |
| 244 | + return call_user_func($args[0], $output_type, 'TEXT'); |
| 245 | + }) |
| 246 | + ->shouldBeCalled(); |
| 247 | + $this->_process->getOutput()->willReturn('OK')->shouldBeCalled(); |
| 248 | + |
| 249 | + $this->_io->isVerbose()->willReturn(false)->shouldBeCalled(); |
| 250 | + $this->_io->write($expected_output)->shouldBeCalled(); |
| 251 | + $this->_cacheManager->getCache(Argument::any())->shouldNotBeCalled(); |
| 252 | + |
| 253 | + $actual_result = $this->_command->runLive(array('TE' => 'KE')); |
| 254 | + $this->assertEquals('OK', $actual_result); |
| 255 | + } |
| 256 | + |
| 257 | + public function runLiveDataProvider() |
| 258 | + { |
| 259 | + return array( |
| 260 | + array(Process::OUT, 'KEXT'), |
| 261 | + array(Process::ERR, '<error>ERR:</error> KEXT'), |
| 262 | + ); |
| 263 | + } |
| 264 | + |
| 265 | + public function testRunError() |
| 266 | + { |
| 267 | + $this->_process->getCommandLine()->willReturn('svn log')->shouldBeCalled(); |
| 268 | + |
| 269 | + $process = $this->_process; |
| 270 | + $this->_process |
| 271 | + ->mustRun(null) |
| 272 | + ->will(function () use ($process) { |
| 273 | + $mock_definition = array( |
| 274 | + 'isSuccessful' => false, |
| 275 | + 'getExitCode' => 1, |
| 276 | + 'getExitCodeText' => 'exit code text', |
| 277 | + 'isOutputDisabled' => false, |
| 278 | + 'getOutput' => 'normal output', |
| 279 | + 'getErrorOutput' => 'error output', |
| 280 | + ); |
| 281 | + |
| 282 | + foreach ( $mock_definition as $method_name => $return_value ) { |
| 283 | + $process->{$method_name}()->willReturn($return_value)->shouldBeCalled(); |
| 284 | + } |
| 285 | + |
| 286 | + throw new ProcessFailedException($process->reveal()); |
| 287 | + }) |
| 288 | + ->shouldBeCalled(); |
| 289 | + |
| 290 | + $this->_process->getOutput()->shouldNotBeCalled(); |
| 291 | + $this->_io->isVerbose()->shouldNotBeCalled(); |
| 292 | + |
| 293 | + $this->_cacheManager->getCache(Argument::any())->shouldNotBeCalled(); |
| 294 | + |
| 295 | + $thrown_exception = null; |
| 296 | + |
| 297 | + try { |
| 298 | + $this->_command->run(); |
| 299 | + } |
| 300 | + catch ( \Exception $thrown_exception ) { |
| 301 | + $this->assertEquals( |
| 302 | + 'ConsoleHelpers\\SVNBuddy\\Exception\\RepositoryCommandException', |
| 303 | + get_class($thrown_exception), |
| 304 | + 'Exception of correct class was thrown' |
| 305 | + ); |
| 306 | + } |
| 307 | + |
| 308 | + $this->assertNotNull($thrown_exception, 'Exception was thrown when command execution failed'); |
| 309 | + |
| 310 | + $error_msg = <<<MSG |
| 311 | +Command: |
| 312 | +svn log |
| 313 | +Error #0: |
| 314 | +error output |
| 315 | +MSG; |
66 | 316 |
|
67 |
| - $this->assertEquals('OK', $this->_command->run()); |
| 317 | + $this->assertEquals($error_msg, $thrown_exception->getMessage()); |
68 | 318 | }
|
69 | 319 |
|
70 | 320 | /**
|
|
0 commit comments