Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 14 | class DefaultIOTest extends \PHPUnit_Framework_TestCase |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * @return \Symfony\Component\Console\Input\InputInterface |
||
| 18 | */ |
||
| 19 | public function getInputMock() |
||
| 20 | { |
||
| 21 | return $this->getMockBuilder('\\Symfony\\Component\\Console\\Input\\InputInterface') |
||
| 22 | ->disableOriginalConstructor() |
||
| 23 | ->getMock(); |
||
| 24 | } |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @return \Symfony\Component\Console\Output\ConsoleOutputInterface |
||
| 28 | */ |
||
| 29 | public function getConsoleOutputMock() |
||
| 30 | { |
||
| 31 | return $this->getMockBuilder('\\Symfony\\Component\\Console\\Output\\ConsoleOutputInterface') |
||
| 32 | ->disableOriginalConstructor() |
||
| 33 | ->getMock(); |
||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @return \Symfony\Component\Console\Output\OutputInterface |
||
| 38 | */ |
||
| 39 | public function getOutputMock() |
||
| 40 | { |
||
| 41 | return $this->getMockBuilder('\\Symfony\\Component\\Console\\Output\\OutputInterface') |
||
| 42 | ->disableOriginalConstructor() |
||
| 43 | ->getMock(); |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @return \Symfony\Component\Console\Helper\HelperSet |
||
| 48 | */ |
||
| 49 | public function getHelperSetMock() |
||
| 50 | { |
||
| 51 | return $this->getMockBuilder('\\Symfony\\Component\\Console\\Helper\\HelperSet') |
||
| 52 | ->disableOriginalConstructor() |
||
| 53 | ->getMock(); |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @return \Symfony\Component\Console\Helper\QuestionHelper |
||
| 58 | */ |
||
| 59 | public function getQuestionHelper() |
||
| 60 | { |
||
| 61 | return $this->getMockBuilder('\\Symfony\\Component\\Console\\Helper\\QuestionHelper') |
||
| 62 | ->disableOriginalConstructor() |
||
| 63 | ->getMock(); |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Tests DefaultIO::isInteractive |
||
| 68 | */ |
||
| 69 | public function testIsInteractive() |
||
| 70 | { |
||
| 71 | $input = $this->getInputMock(); |
||
| 72 | $output = $this->getOutputMock(); |
||
| 73 | $helper = $this->getHelperSetMock(); |
||
| 74 | |||
| 75 | $input->expects($this->once())->method('isInteractive')->willReturn(false); |
||
| 76 | $io = new DefaultIO($input, $output, $helper); |
||
| 77 | |||
| 78 | $this->assertFalse($io->isInteractive()); |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Tests DefaultIO::isVerbose |
||
| 83 | */ |
||
| 84 | public function testIsVerbose() |
||
| 85 | { |
||
| 86 | $input = $this->getInputMock(); |
||
| 87 | $output = $this->getOutputMock(); |
||
| 88 | $helper = $this->getHelperSetMock(); |
||
| 89 | |||
| 90 | $output->expects($this->once())->method('getVerbosity')->willReturn(0); |
||
| 91 | $io = new DefaultIO($input, $output, $helper); |
||
| 92 | |||
| 93 | $this->assertFalse($io->isVerbose()); |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Tests DefaultIO::isVeryVerbose |
||
| 98 | */ |
||
| 99 | public function testIsVeryVerbose() |
||
| 100 | { |
||
| 101 | $input = $this->getInputMock(); |
||
| 102 | $output = $this->getOutputMock(); |
||
| 103 | $helper = $this->getHelperSetMock(); |
||
| 104 | |||
| 105 | $output->expects($this->once())->method('getVerbosity')->willReturn(0); |
||
| 106 | $io = new DefaultIO($input, $output, $helper); |
||
| 107 | |||
| 108 | $this->assertFalse($io->isVeryVerbose()); |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Tests DefaultIO::isDebug |
||
| 113 | */ |
||
| 114 | public function testIsDebug() |
||
| 115 | { |
||
| 116 | $input = $this->getInputMock(); |
||
| 117 | $output = $this->getOutputMock(); |
||
| 118 | $helper = $this->getHelperSetMock(); |
||
| 119 | |||
| 120 | $output->expects($this->once())->method('getVerbosity')->willReturn(0); |
||
| 121 | $io = new DefaultIO($input, $output, $helper); |
||
| 122 | |||
| 123 | $this->assertFalse($io->isDebug()); |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Tests DefaultIO::writeError |
||
| 128 | */ |
||
| 129 | public function testWriteError() |
||
| 130 | { |
||
| 131 | $input = $this->getInputMock(); |
||
| 132 | $output = $this->getOutputMock(); |
||
| 133 | $helper = $this->getHelperSetMock(); |
||
| 134 | |||
| 135 | $output->expects($this->once())->method('getVerbosity')->willReturn(OutputInterface::VERBOSITY_DEBUG); |
||
| 136 | $io = new DefaultIO($input, $output, $helper); |
||
| 137 | |||
| 138 | $io->writeError('foo'); |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Tests DefaultIO::ask |
||
| 143 | */ |
||
| 144 | public function testAsk() |
||
| 145 | { |
||
| 146 | $input = $this->getInputMock(); |
||
| 147 | $output = $this->getOutputMock(); |
||
| 148 | $helper = $this->getHelperSetMock(); |
||
| 149 | $questionHelper = $this->getQuestionHelper(); |
||
| 150 | |||
| 151 | $helper->expects($this->once())->method('get')->willReturn($questionHelper); |
||
| 152 | $questionHelper->expects($this->once())->method('ask')->willReturn(true); |
||
| 153 | |||
| 154 | $io = new DefaultIO($input, $output, $helper); |
||
| 155 | $answer = $io->ask('foo'); |
||
| 156 | $this->assertTrue($answer); |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Tests DefaultIO::askConfirmation |
||
| 161 | */ |
||
| 162 | public function testAskConfirmation() |
||
| 163 | { |
||
| 164 | $input = $this->getInputMock(); |
||
| 165 | $output = $this->getOutputMock(); |
||
| 166 | $helper = $this->getHelperSetMock(); |
||
| 167 | $questionHelper = $this->getQuestionHelper(); |
||
| 168 | |||
| 169 | $helper->expects($this->once())->method('get')->willReturn($questionHelper); |
||
| 170 | $questionHelper->expects($this->once())->method('ask')->willReturn(true); |
||
| 171 | |||
| 172 | $io = new DefaultIO($input, $output, $helper); |
||
| 173 | $answer = $io->askConfirmation('foo'); |
||
| 174 | $this->assertTrue($answer); |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Tests DefaultIO::askAbdValidate |
||
| 179 | */ |
||
| 180 | public function testAskAndValidate() |
||
| 181 | { |
||
| 182 | $input = $this->getInputMock(); |
||
| 183 | $output = $this->getOutputMock(); |
||
| 184 | $helper = $this->getHelperSetMock(); |
||
| 185 | $questionHelper = $this->getQuestionHelper(); |
||
| 186 | |||
| 187 | $helper->expects($this->once())->method('get')->willReturn($questionHelper); |
||
| 188 | $questionHelper->expects($this->once())->method('ask')->willReturn(true); |
||
| 189 | |||
| 190 | $io = new DefaultIO($input, $output, $helper); |
||
| 191 | $answer = $io->askAndValidate( |
||
| 192 | 'foo', |
||
| 193 | function() { |
||
| 194 | return true; |
||
| 195 | } |
||
| 196 | ); |
||
| 197 | $this->assertTrue($answer); |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Tests DefaultIO::write |
||
| 202 | */ |
||
| 203 | public function testWrite() |
||
| 204 | { |
||
| 205 | $input = $this->getInputMock(); |
||
| 206 | $output = $this->getConsoleOutputMock(); |
||
| 207 | $helper = $this->getHelperSetMock(); |
||
| 208 | |||
| 209 | $output->expects($this->once())->method('getVerbosity')->willReturn(OutputInterface::VERBOSITY_DEBUG); |
||
| 210 | $output->expects($this->once())->method('getErrorOutput')->willReturn($this->getOutputMock()); |
||
| 211 | |||
| 212 | $io = new DefaultIO($input, $output, $helper); |
||
| 213 | $io->writeError('foo'); |
||
| 214 | } |
||
| 215 | } |
||
| 216 |