| Conditions | 3 |
| Paths | 4 |
| Total Lines | 61 |
| Code Lines | 34 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 declare(strict_types = 1); |
||
| 31 | public function testSingleDocumentBeingQueuedForBulkIndexing(): void |
||
| 32 | { |
||
| 33 | BulkIndexer::resetIndexedPayload(); |
||
| 34 | |||
| 35 | // only one job is created per index when there are dirty objects to index. This is dealt with within the |
||
| 36 | // queued jobs module, in that the job parameters are identical. One job will already be present from |
||
| 37 | // loading of the fixtures. As such, clear the queue |
||
| 38 | DB::query('DELETE FROM "QueuedJobDescriptor"'); |
||
| 39 | |||
| 40 | $before = QueuedJobDescriptor::get()->count(); |
||
| 41 | |||
| 42 | $page = $this->objFromFixture(SiteTree::class, 'sitetree_10'); |
||
| 43 | $page->Title = 'This is a new title'; |
||
| 44 | $page->write(); |
||
| 45 | |||
| 46 | $after = QueuedJobDescriptor::get()->count(); |
||
| 47 | |||
| 48 | // assert that 1 job has been created, then inspect the contents |
||
| 49 | $this->assertEquals(1, $after - $before); |
||
| 50 | |||
| 51 | // check the contents of the job queue |
||
| 52 | $jobDescriptor = QueuedJobDescriptor::get()->first(); |
||
| 53 | $this->assertEquals('Bulk Index Dirty DataObjects', $jobDescriptor->JobTitle); |
||
| 54 | $this->assertEquals( |
||
| 55 | 'O:8:"stdClass":1:{s:9:"indexName";s:8:"sitetree";}', |
||
| 56 | $jobDescriptor->SavedJobData |
||
| 57 | ); |
||
| 58 | |||
| 59 | // create the job class |
||
| 60 | $impl = $jobDescriptor->Implementation; |
||
| 61 | /** @var \Suilven\FreeTextSearch\Tests\Factory\BulkIndexDirtyJob $job */ |
||
| 62 | $job = Injector::inst()->create($impl); |
||
| 63 | |||
| 64 | // populate data - taken from QueuedJobService |
||
| 65 | $jobData = null; |
||
|
|
|||
| 66 | $messages = null; |
||
| 67 | |||
| 68 | // switching to php's serialize methods... not sure why this wasn't done from the start! |
||
| 69 | $jobData = @\unserialize($jobDescriptor->SavedJobData); |
||
| 70 | $messages = @\unserialize($jobDescriptor->SavedJobMessages); |
||
| 71 | |||
| 72 | // try decoding as json if null |
||
| 73 | $jobData = $jobData |
||
| 74 | ? $jobData |
||
| 75 | : \json_decode($jobDescriptor->SavedJobData); |
||
| 76 | $messages = $messages |
||
| 77 | ? $messages |
||
| 78 | : \json_decode($jobDescriptor->SavedJobMessages); |
||
| 79 | |||
| 80 | $job->setJobData( |
||
| 81 | $jobDescriptor->TotalSteps, |
||
| 82 | $jobDescriptor->StepsProcessed, |
||
| 83 | $jobDescriptor->JobStatus === QueuedJob::STATUS_COMPLETE, |
||
| 84 | $jobData, |
||
| 85 | $messages |
||
| 86 | ); |
||
| 87 | |||
| 88 | $job->setup(); |
||
| 89 | $job->process(); |
||
| 90 | |||
| 91 | $this->checkBulkIndexedPayload(); |
||
| 92 | } |
||
| 127 |