| Conditions | 5 |
| Paths | 1 |
| Total Lines | 81 |
| Code Lines | 59 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 31 | public function testEnqueueDequeueAndAcknowledge() |
||
| 32 | { |
||
| 33 | |||
| 34 | $payload = json_decode($this->payload, true); |
||
| 35 | $payload['job'] = new Job(1, 'demo'); |
||
| 36 | $btJob2 = Mockery::mock('\Pheanstalk\Job') |
||
| 37 | ->shouldReceive('getData') |
||
| 38 | ->andReturn(json_encode($payload)) |
||
| 39 | ->getMock(); |
||
| 40 | $btClient = Mockery::mock('\Pheanstalk\Pheanstalk') |
||
| 41 | ->shouldReceive('useTube') |
||
| 42 | ->with('mail_queue') |
||
| 43 | ->andReturnSelf() |
||
| 44 | ->shouldReceive('put') |
||
| 45 | ->andReturn(1) |
||
| 46 | ->shouldReceive('watchOnly') |
||
| 47 | ->andReturnSelf() |
||
| 48 | ->shouldReceive('statsTube') |
||
| 49 | ->twice() |
||
| 50 | ->andReturnUsing( |
||
| 51 | function () { |
||
| 52 | static $f; |
||
| 53 | if ($f === null) { |
||
| 54 | $f = true; |
||
| 55 | } |
||
| 56 | $f = !$f; |
||
| 57 | |||
| 58 | return $f |
||
| 59 | ? new ArrayResponse( |
||
| 60 | 'test', [ |
||
| 61 | 'current-jobs-delayed' => 0, |
||
| 62 | 'current-jobs-urgent' => 0, |
||
| 63 | 'current-jobs-ready' => 0 |
||
| 64 | ] |
||
| 65 | ) |
||
| 66 | : new ArrayResponse('test', ['current-jobs-delayed' => 1,]); |
||
| 67 | } |
||
| 68 | ) |
||
| 69 | ->shouldReceive('reserve') |
||
| 70 | ->with(0) |
||
| 71 | ->andReturnUsing(function() use ($btJob2) { |
||
| 72 | static $f; |
||
| 73 | if($f === null) { |
||
| 74 | $f = false; |
||
| 75 | } |
||
| 76 | $f = !$f; |
||
| 77 | return $f ? $btJob2 : null; |
||
| 78 | }) |
||
| 79 | ->shouldReceive('delete') |
||
| 80 | ->andReturn(1) |
||
| 81 | ->getMock(); |
||
| 82 | |||
| 83 | $btStoreConnection = Mockery::mock('\Da\Mailer\Queue\Backend\Beanstalk\BeanstalkdQueueStoreConnection') |
||
| 84 | ->shouldReceive('connect') |
||
| 85 | ->andReturnSelf() |
||
| 86 | ->shouldReceive('getInstance') |
||
| 87 | ->andReturn($btClient) |
||
| 88 | ->getMock(); |
||
| 89 | |||
| 90 | $btQueueStore = new BeanstalkdQueueStoreAdapter($btStoreConnection); |
||
| 91 | |||
| 92 | $this->assertSame($btQueueStore, $btQueueStore->init()); |
||
| 93 | $this->assertTrue($btQueueStore->enqueue($this->mailJob) > 0); |
||
| 94 | |||
| 95 | $this->assertTrue($btQueueStore->isEmpty() === false); |
||
| 96 | |||
| 97 | $mailJob = $btQueueStore->dequeue(); |
||
| 98 | |||
| 99 | $this->assertTrue($btQueueStore->isEmpty() === true); |
||
| 100 | |||
| 101 | $this->assertTrue(!empty($mailJob->getMessage())); |
||
| 102 | |||
| 103 | $dequeuedMailMessage = MailMessage::fromArray(json_decode($mailJob->getMessage(), true)); |
||
| 104 | |||
| 105 | $this->assertEquals(FixtureHelper::getMailMessage(), $dequeuedMailMessage); |
||
| 106 | |||
| 107 | $mailJob->markAsCompleted(); |
||
| 108 | $btQueueStore->ack($mailJob); |
||
|
|
|||
| 109 | |||
| 110 | $this->assertTrue($btQueueStore->dequeue() === null); |
||
| 111 | } |
||
| 112 | |||
| 254 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: