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 |
||
| 15 | class FileSenderTest extends \PHPUnit_Framework_TestCase |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * checks if FileSender does nothing if nothing is needed |
||
| 19 | * |
||
| 20 | * @dataProvider requestTypeProvider |
||
| 21 | * @covers Graviton\ImportExport\Service\FileSender |
||
| 22 | * |
||
| 23 | * @param string $type type of request to test |
||
| 24 | * @return void |
||
| 25 | */ |
||
| 26 | View Code Duplication | public function testSenderDoesPlainRequestWithoutUploadField($type) |
|
| 46 | |||
| 47 | /** |
||
| 48 | * checks if FileSender does nothing if it gets a falsy file path |
||
| 49 | * |
||
| 50 | * This test was added post-implementation, be sure to check if it is really what |
||
| 51 | * you need if you read this. |
||
| 52 | * |
||
| 53 | * @dataProvider requestTypeProvider |
||
| 54 | * @covers Graviton\ImportExport\Service\FileSender |
||
| 55 | * |
||
| 56 | * @param string $type type of request to test |
||
| 57 | * @return void |
||
| 58 | */ |
||
| 59 | View Code Duplication | public function testSenderDoesPlainRequestWithFalsyUploadField($type) |
|
| 82 | |||
| 83 | /** |
||
| 84 | * test if FileSender excepts to being called without json in upload cases |
||
| 85 | * |
||
| 86 | * @dataProvider requestTypeProvider |
||
| 87 | * @covers Graviton\ImportExport\Service\FileSender |
||
| 88 | * |
||
| 89 | * @param string $type type of request to test |
||
| 90 | * @return void |
||
| 91 | */ |
||
| 92 | public function testExceptsToBeingCalledWithoutJsonDataInUploadCase($type) |
||
| 111 | |||
| 112 | |||
| 113 | /** |
||
| 114 | * checks if FileSender mangles the options before passing them to the client |
||
| 115 | * |
||
| 116 | * @dataProvider requestTypeProvider |
||
| 117 | * @covers Graviton\ImportExport\Service\FileSender |
||
| 118 | * |
||
| 119 | * @param string $type type of request to test |
||
| 120 | * @return void |
||
| 121 | */ |
||
| 122 | public function testSenderManglesOptionsIfUploadHasBeenPassed($type) |
||
| 123 | { |
||
| 124 | $method = 'PUT'; |
||
| 125 | $uri = 'http://localhost/file/example'; |
||
| 126 | $options = [ |
||
| 127 | 'upload' => __DIR__ . '/fixtures/file.txt', |
||
| 128 | 'json' => new \stdClass() |
||
| 129 | ]; |
||
| 130 | $expectedOptions = [ |
||
| 131 | 'query' => [ |
||
| 132 | 'metadata' => '{}' |
||
| 133 | ] |
||
| 134 | ]; |
||
| 135 | |||
| 136 | $clientMock = $this->getMockBuilder('GuzzleHttp\Client')->getMock(); |
||
| 137 | |||
| 138 | $promiseMock = $this->createMock('GuzzleHttp\Promise\Promise'); |
||
| 139 | |||
| 140 | $clientMock |
||
| 141 | ->method($type) |
||
| 142 | ->with( |
||
| 143 | $method, |
||
| 144 | $uri, |
||
| 145 | $this->callback( |
||
| 146 | function ($options) use ($expectedOptions) { |
||
| 147 | $this->assertArrayHasKey('body', $options); |
||
| 148 | $this->assertInternalType('resource', $options['body']); |
||
| 149 | unset($options['body']); |
||
| 150 | $this->assertEquals($options, $expectedOptions); |
||
| 151 | return true; |
||
| 152 | } |
||
| 153 | ) |
||
| 154 | ) |
||
| 155 | ->will($this->returnValue($promiseMock)); |
||
| 156 | |||
| 157 | $sut = new FileSender( |
||
| 158 | $clientMock |
||
| 159 | ); |
||
| 160 | $this->assertEquals($promiseMock, $sut->$type($method, $uri, $options)); |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @return array |
||
| 165 | */ |
||
| 166 | public function requestTypeProvider() |
||
| 173 | } |
||
| 174 |