| Conditions | 1 |
| Paths | 1 |
| Total Lines | 92 |
| Code Lines | 75 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 87 | public function testProcessSwagger() |
||
| 88 | { |
||
| 89 | $content = file_get_contents(dirname(__FILE__).'/../../../resources/simple-swagger.json'); |
||
| 90 | $swagger = json_decode($content); |
||
| 91 | $orderPath = '/order/{orderId}'; |
||
| 92 | |||
| 93 | $swaggerParserMock = $this->getMockBuilder('\Swagger\Document') |
||
| 94 | ->disableOriginalConstructor() |
||
| 95 | ->setMethods(['getBasePath', 'setDocument', 'getOperationsById', 'getSchemaResolver']) |
||
| 96 | ->getMock(); |
||
| 97 | |||
| 98 | $statusCode = 200; |
||
| 99 | $responseSchema = $swagger->paths->$orderPath->get->responses->$statusCode->schema; |
||
| 100 | $orderDefinition = $swagger->definitions->Order; |
||
| 101 | $userDefinition = $swagger->definitions->User; |
||
| 102 | |||
| 103 | $responseSchemaMock = $this->getMockBuilder('\Swagger\Object\AbstractObject') |
||
| 104 | ->disableOriginalConstructor() |
||
| 105 | ->setMethods(['getDocument']) |
||
| 106 | ->getMockForAbstractClass(); |
||
| 107 | $responseSchemaMock->expects($this->any()) |
||
| 108 | ->method('getDocument') |
||
| 109 | ->will($this->onConsecutiveCalls($responseSchema, $orderDefinition, $userDefinition)); |
||
| 110 | |||
| 111 | $responseMock = $this->getMockBuilder('\Swagger\Object\Response') |
||
| 112 | ->disableOriginalConstructor() |
||
| 113 | ->setMethods(['getSchema']) |
||
| 114 | ->getMock(); |
||
| 115 | $responseMock->expects($this->once()) |
||
| 116 | ->method('getSchema') |
||
| 117 | ->willReturn($responseSchemaMock); |
||
| 118 | |||
| 119 | $responsesMock = $this->getMockBuilder('\Swagger\Object\Responses') |
||
| 120 | ->disableOriginalConstructor() |
||
| 121 | ->setMethods(['getHttpStatusCode']) |
||
| 122 | ->getMock(); |
||
| 123 | $responsesMock->expects($this->once()) |
||
| 124 | ->method('getHttpStatusCode') |
||
| 125 | ->with($this->equalTo($statusCode)) |
||
| 126 | ->willReturn($responseMock); |
||
| 127 | |||
| 128 | |||
| 129 | $operationMock = $this->getMockBuilder('\Swagger\Object\Operation') |
||
| 130 | ->disableOriginalConstructor() |
||
| 131 | ->setMethods(['getDocumentObjectProperty', 'getResponses']) |
||
| 132 | ->getMock(); |
||
| 133 | $operationMock->expects($this->once()) |
||
| 134 | ->method('getResponses') |
||
| 135 | ->willReturn($responsesMock); |
||
| 136 | |||
| 137 | |||
| 138 | $serviceMock = $this->getMockBuilder('\Swagger\OperationReference') |
||
| 139 | ->disableOriginalConstructor() |
||
| 140 | ->setMethods(['getPath', 'getMethod', 'getOperation']) |
||
| 141 | ->getMock(); |
||
| 142 | $serviceMock->expects($this->exactly(3)) |
||
| 143 | ->method('getPath') |
||
| 144 | ->will($this->onConsecutiveCalls('/user', '/user', '/order/{orderId}')); |
||
| 145 | $serviceMock->expects($this->exactly(5)) |
||
| 146 | ->method('getMethod') |
||
| 147 | ->will($this->onConsecutiveCalls('POST', 'DELETE', 'GET', 'POST', 'GET')); |
||
| 148 | $serviceMock->expects($this->exactly(2)) |
||
| 149 | ->method('getOperation') |
||
| 150 | ->willReturn($operationMock); |
||
| 151 | |||
| 152 | |||
| 153 | $operations = [$serviceMock, $serviceMock, $serviceMock]; |
||
| 154 | |||
| 155 | $schemaResolverMock = $this->getMockBuilder('\Swagger\SchemaResolver') |
||
| 156 | ->disableOriginalConstructor() |
||
| 157 | ->setMethods(['resolveReference']) |
||
| 158 | ->getMock(); |
||
| 159 | $schemaResolverMock->expects($this->exactly(2)) |
||
| 160 | ->method('resolveReference') |
||
| 161 | ->withAnyParameters() |
||
| 162 | ->willReturn($responseSchemaMock); |
||
| 163 | |||
| 164 | $swaggerParserMock |
||
| 165 | ->expects($this->once()) |
||
| 166 | ->method('getOperationsById') |
||
| 167 | ->willReturn($operations); |
||
| 168 | $swaggerParserMock |
||
| 169 | ->expects($this->exactly(2)) |
||
| 170 | ->method('getSchemaResolver') |
||
| 171 | ->willReturn($schemaResolverMock); |
||
| 172 | |||
| 173 | $this->sut = new SwaggerStrategy($swaggerParserMock); |
||
| 174 | |||
| 175 | $apiDefinition = $this->sut->process($content, array('host' => 'localhost')); |
||
| 176 | $this->assertInstanceOf('Graviton\ProxyBundle\Definition\ApiDefinition', $apiDefinition); |
||
| 177 | $this->assertCount(2, $apiDefinition->getEndpoints()); |
||
| 178 | } |
||
| 179 | } |
||
| 180 |