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 |
||
| 17 | class GitCommandTest extends CommandTestCase |
||
| 18 | { |
||
| 19 | protected $client; |
||
| 20 | |||
| 21 | protected $shellHandler; |
||
| 22 | |||
| 23 | protected function setUp(): void: void |
||
|
|
|||
| 24 | { |
||
| 25 | $this->client = self::createClient(); |
||
| 26 | |||
| 27 | $this->shellHandler = $this->getMockBuilder('Fabrica\Bundle\CoreBundle\Git\ShellHandler') |
||
| 28 | ->disableOriginalConstructor() |
||
| 29 | ->getMock() |
||
| 30 | ; |
||
| 31 | $this->client->setShellHandler($this->shellHandler); |
||
| 32 | |||
| 33 | $this->client->startIsolation(); |
||
| 34 | } |
||
| 35 | |||
| 36 | public function tearDown(): void |
||
| 37 | { |
||
| 38 | $this->client->stopIsolation(); |
||
| 39 | } |
||
| 40 | |||
| 41 | public function testShellInformation() |
||
| 42 | { |
||
| 43 | $this->shellHandler |
||
| 44 | ->expects($this->once()) |
||
| 45 | ->method('getOriginalCommand') |
||
| 46 | ->will($this->returnValue(null)) |
||
| 47 | ; |
||
| 48 | |||
| 49 | list($statusCode ,$output) = $this->runCommand($this->client, 'fabrica:git --stderr=no alice'); |
||
| 50 | |||
| 51 | $this->assertContains('You are identified as alice', $output); |
||
| 52 | $this->assertRegexp('/Foobar[ ]+Developer[ ]/', $output); |
||
| 53 | $this->assertRegexp('/Barbaz[ ]+Lead developer[ ]/', $output); |
||
| 54 | } |
||
| 55 | |||
| 56 | public function testIllegalAction() |
||
| 57 | { |
||
| 58 | $this->shellHandler |
||
| 59 | ->expects($this->once()) |
||
| 60 | ->method('getOriginalCommand') |
||
| 61 | ->will($this->returnValue('git-bla-pack \'foobar.git\'')) |
||
| 62 | ; |
||
| 63 | |||
| 64 | list($statusCode ,$output) = $this->runCommand($this->client, 'fabrica:git --stderr=no alice'); |
||
| 65 | |||
| 66 | $this->assertContains('Action seems illegal', $output); |
||
| 67 | } |
||
| 68 | |||
| 69 | public function testPullNonAuthorizedProject() |
||
| 70 | { |
||
| 71 | $this->shellHandler |
||
| 72 | ->expects($this->once()) |
||
| 73 | ->method('getOriginalCommand') |
||
| 74 | ->will($this->returnValue('git-upload-pack \'barbaz.git\'')) |
||
| 75 | ; |
||
| 76 | |||
| 77 | $this->shellHandler |
||
| 78 | ->expects($this->never()) |
||
| 79 | ->method('handle') |
||
| 80 | ; |
||
| 81 | |||
| 82 | list($statusCode ,$output) = $this->runCommand($this->client, 'fabrica:git --stderr=no bob'); |
||
| 83 | |||
| 84 | $this->assertContains('You are not allowed', $output); |
||
| 85 | } |
||
| 86 | |||
| 87 | public function testPullAuthorizedProject() |
||
| 88 | { |
||
| 89 | $this->shellHandler |
||
| 90 | ->expects($this->once()) |
||
| 91 | ->method('getOriginalCommand') |
||
| 92 | ->will($this->returnValue('git-upload-pack \'foobar.git\'')) |
||
| 93 | ; |
||
| 94 | |||
| 95 | $this->shellHandler |
||
| 96 | ->expects($this->once()) |
||
| 97 | ->method('handle') |
||
| 98 | ; |
||
| 99 | |||
| 100 | list($statusCode ,$output) = $this->runCommand($this->client, 'fabrica:git --stderr=no bob'); |
||
| 101 | } |
||
| 102 | |||
| 103 | public function testPushNonAuthorizedProject() |
||
| 104 | { |
||
| 105 | $this->shellHandler |
||
| 106 | ->expects($this->once()) |
||
| 107 | ->method('getOriginalCommand') |
||
| 108 | ->will($this->returnValue('git-receive-pack \'barbaz.git\'')) |
||
| 109 | ; |
||
| 110 | |||
| 111 | $this->shellHandler |
||
| 112 | ->expects($this->never()) |
||
| 113 | ->method('handle') |
||
| 114 | ; |
||
| 115 | |||
| 116 | list($statusCode ,$output) = $this->runCommand($this->client, 'fabrica:git --stderr=no bob'); |
||
| 117 | |||
| 118 | $this->assertContains('You are not allowed', $output); |
||
| 119 | } |
||
| 120 | |||
| 121 | public function testPushAuthorizedProject() |
||
| 122 | { |
||
| 123 | $this->shellHandler |
||
| 124 | ->expects($this->once()) |
||
| 125 | ->method('getOriginalCommand') |
||
| 126 | ->will($this->returnValue('git-receive-pack \'foobar.git\'')) |
||
| 127 | ; |
||
| 128 | |||
| 129 | $this->shellHandler |
||
| 130 | ->expects($this->once()) |
||
| 131 | ->method('handle') |
||
| 132 | ; |
||
| 133 | |||
| 134 | list($statusCode ,$output) = $this->runCommand($this->client, 'fabrica:git --stderr=no bob'); |
||
| 135 | } |
||
| 136 | } |
||
| 137 |