|
2 | 2 |
|
3 | 3 | namespace ChrisHalbert\PhpBCC\Console; |
4 | 4 |
|
| 5 | +use ChrisHalbert\PhpBCC\Exceptions\InvalidArgumentException; |
| 6 | +use ChrisHalbert\PhpBCC\Input\CloverInput; |
| 7 | +use ChrisHalbert\PhpBCC\Output\AuthorOutput; |
| 8 | +use ChrisHalbert\PhpBCC\Output\TextOutput; |
| 9 | +use ChrisHalbert\PhpBCC\VCS\GitVCS; |
| 10 | +use Symfony\Component\Console\Input\InputInterface as SymfonyInput; |
| 11 | +use Symfony\Component\Console\Output\OutputInterface as SymfonyOutput; |
| 12 | + |
| 13 | + |
5 | 14 | class CommandTest extends \PHPUnit_Framework_TestCase |
6 | 15 | { |
7 | 16 | protected $command; |
8 | 17 |
|
| 18 | + protected $reflection; |
| 19 | + |
| 20 | + protected $output; |
| 21 | + |
| 22 | + protected $input; |
| 23 | + |
9 | 24 | public function setUp() |
10 | 25 | { |
11 | 26 | $this->command = new Command(); |
| 27 | + $this->reflection = new \ReflectionClass(Command::class); |
| 28 | + |
| 29 | + $this->output = $this->getMockBuilder(SymfonyOutput::class) |
| 30 | + ->setMethods(get_class_methods(SymfonyOutput::class)) |
| 31 | + ->getMock(); |
| 32 | + |
| 33 | + $this->input = $this->getMockBuilder(SymfonyInput::class) |
| 34 | + ->setMethods(get_class_methods(SymfonyInput::class)) |
| 35 | + ->getMock(); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @dataProvider dataProviderTestLoadInputFormat |
| 40 | + */ |
| 41 | + public function testLoadInputFormat($option, $instance = null, $exception = null) |
| 42 | + { |
| 43 | + if ($exception) { |
| 44 | + $this->setExpectedException($exception); |
| 45 | + } |
| 46 | + |
| 47 | + $this->input->expects($this->any()) |
| 48 | + ->method('getArgument') |
| 49 | + ->with('path') |
| 50 | + ->willReturn(dirname(__FILE__) . '/../../resources/clover-input.xml'); |
| 51 | + |
| 52 | + $this->input->expects($this->any()) |
| 53 | + ->method('getOption') |
| 54 | + ->with('input-format') |
| 55 | + ->willReturn($option); |
| 56 | + |
| 57 | + $actual = $this->runMethod('loadInputFormat', [$this->input]); |
| 58 | + |
| 59 | + $this->assertInstanceOf($instance, $actual); |
| 60 | + } |
| 61 | + |
| 62 | + public function dataProviderTestLoadInputFormat() |
| 63 | + { |
| 64 | + return [ |
| 65 | + ['clover', CloverInput::class, null], |
| 66 | + ['fake', null, InvalidArgumentException::class] |
| 67 | + ]; |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | + /** |
| 72 | + * @dataProvider dataProviderTestLoadVCSType |
| 73 | + */ |
| 74 | + public function testLoadVCSType($option, $instance = null, $exception = null) |
| 75 | + { |
| 76 | + if ($exception) { |
| 77 | + $this->setExpectedException($exception); |
| 78 | + } |
| 79 | + |
| 80 | + $this->input->expects($this->any()) |
| 81 | + ->method('getOption') |
| 82 | + ->with('vcs') |
| 83 | + ->willReturn($option); |
| 84 | + |
| 85 | + $actual = $this->runMethod('loadVCSType', [$this->input]); |
| 86 | + |
| 87 | + $this->assertInstanceOf($instance, $actual); |
| 88 | + } |
| 89 | + |
| 90 | + public function dataProviderTestLoadVCSType() |
| 91 | + { |
| 92 | + return [ |
| 93 | + ['git', GitVCS::class, null], |
| 94 | + ['fake', null, InvalidArgumentException::class] |
| 95 | + ]; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * @dataProvider dataProviderTestLoadOutputFormat |
| 100 | + */ |
| 101 | + public function testLoadOutputFormat($option, $instance = null, $exception = null) |
| 102 | + { |
| 103 | + if ($exception) { |
| 104 | + $this->setExpectedException($exception); |
| 105 | + } |
| 106 | + |
| 107 | + $this->input->expects($this->any()) |
| 108 | + ->method('getOption') |
| 109 | + ->with('output-format') |
| 110 | + ->willReturn($option); |
| 111 | + |
| 112 | + $actual = $this->runMethod('loadOutputFormat', [$this->input]); |
| 113 | + |
| 114 | + $this->assertInstanceOf($instance, $actual); |
| 115 | + } |
| 116 | + |
| 117 | + public function dataProviderTestLoadOutputFormat() |
| 118 | + { |
| 119 | + return [ |
| 120 | + ['text', TextOutput::class, null], |
| 121 | + ['author', AuthorOutput::class, null], |
| 122 | + ['fake', null, InvalidArgumentException::class] |
| 123 | + ]; |
| 124 | + } |
| 125 | + |
| 126 | + protected function runMethod($methodName, array $args = []) |
| 127 | + { |
| 128 | + $method = $this->reflection->getMethod($methodName); |
| 129 | + $method->setAccessible(true); |
| 130 | + return $method->invokeArgs($this->command, $args); |
12 | 131 | } |
13 | 132 | } |
0 commit comments