| Conditions | 5 |
| Paths | 1 |
| Total Lines | 76 |
| Code Lines | 55 |
| 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 |
||
| 16 | public function testAddRequestAndGetReplies() |
||
| 17 | { |
||
| 18 | $body = 'body'; |
||
| 19 | $server = 'server'; |
||
| 20 | $requestId = 'request_1'; |
||
| 21 | $routingKey = ''; |
||
| 22 | $expiration = 2; |
||
| 23 | |||
| 24 | $serializer = Serializer::factory('json'); |
||
| 25 | |||
| 26 | $connection = static::getMockBuilder(AbstractConnection::class) |
||
| 27 | ->disableOriginalConstructor() |
||
| 28 | ->getMockForAbstractClass(); |
||
| 29 | $channel = static::getMockBuilder(AMQPChannel::class) |
||
| 30 | ->disableOriginalConstructor() |
||
| 31 | ->getMock(); |
||
| 32 | |||
| 33 | $channel->expects(static::once()) |
||
| 34 | ->method('queue_declare') |
||
| 35 | ->with('', false, false, true, false) |
||
| 36 | ->willReturn(['queue-name', null, null]); |
||
| 37 | |||
| 38 | $channel->expects(static::once()) |
||
| 39 | ->method('basic_publish') |
||
| 40 | ->with( |
||
| 41 | static::callback(function (AMQPMessage $a) use ($body, $requestId, $serializer) { |
||
| 42 | return $a->body === $serializer->serialize($body) |
||
| 43 | && $a->get('reply_to') === 'queue-name' |
||
| 44 | && $a->get('correlation_id') === $requestId |
||
| 45 | && $a->get('delivery_mode') === 1 |
||
| 46 | && $a->get('expiration') === 2000; |
||
| 47 | }), |
||
| 48 | $server, |
||
| 49 | $routingKey |
||
| 50 | ); |
||
| 51 | |||
| 52 | /* @var AbstractConnection $connection */ |
||
| 53 | $rpcClient = new RpcClient($connection, $channel); |
||
| 54 | $rpcClient->setSerializer($serializer); |
||
| 55 | |||
| 56 | $rpcClient->addRequest($body, $server, $requestId, $routingKey, $expiration); |
||
| 57 | |||
| 58 | $message = new AMQPMessage(); |
||
| 59 | $message->body = $serializer->serialize('response'); |
||
| 60 | $message->set('correlation_id', $requestId); |
||
| 61 | |||
| 62 | $channel->expects(static::once()) |
||
| 63 | ->method('basic_consume') |
||
| 64 | ->with('queue-name', '', false, true, false, false, static::callback(function ($a) { |
||
| 65 | return is_callable($a); |
||
| 66 | })) |
||
| 67 | ->willReturn('consumer_tag'); |
||
| 68 | |||
| 69 | $channel->expects(static::once()) |
||
| 70 | ->method('wait') |
||
| 71 | ->with( |
||
| 72 | static::callback(function ($a) use ($rpcClient, $message) { |
||
| 73 | $rpcClient->processMessage($message); |
||
| 74 | |||
| 75 | return is_null($a); |
||
| 76 | }), |
||
| 77 | false, |
||
| 78 | 2 |
||
| 79 | ); |
||
| 80 | |||
| 81 | $channel->expects(static::once()) |
||
| 82 | ->method('basic_cancel') |
||
| 83 | ->with('consumer_tag'); |
||
| 84 | |||
| 85 | $replies = $rpcClient->getReplies(); |
||
| 86 | |||
| 87 | static::assertInternalType('array', $replies); |
||
| 88 | static::assertCount(1, $replies); |
||
| 89 | static::assertArrayHasKey($requestId, $replies); |
||
| 90 | static::assertEquals('response', $replies[$requestId]); |
||
| 91 | } |
||
| 92 | } |
||
| 93 |