| Conditions | 1 |
| Paths | 1 |
| Total Lines | 80 |
| Code Lines | 49 |
| 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 |
||
| 14 | public function testToHtml() |
||
| 15 | { |
||
| 16 | $converter = new JsonToHtml(); |
||
| 17 | $converter->addConverter(new BlockquoteConverter()); |
||
| 18 | $converter->addConverter(new HeadingConverter()); |
||
| 19 | $converter->addConverter(new IframeConverter()); |
||
| 20 | $converter->addConverter(new ImageConverter()); |
||
| 21 | $converter->addConverter(new BaseConverter()); |
||
| 22 | |||
| 23 | // let's try a basic json |
||
| 24 | $json = json_encode(array( |
||
| 25 | 'data' => array(array( |
||
| 26 | 'type' => 'quote', |
||
| 27 | 'data' => array( |
||
| 28 | 'text' => 'Text', |
||
| 29 | 'cite' => 'Cite' |
||
| 30 | ) |
||
| 31 | )) |
||
| 32 | )); |
||
| 33 | $html = $converter->toHtml($json); |
||
| 34 | $this->assertEquals( |
||
| 35 | $html, |
||
| 36 | "<blockquote><p>Text</p>\n<cite><p>Cite</p>\n</cite></blockquote>" |
||
| 37 | ); |
||
| 38 | |||
| 39 | |||
| 40 | // Lets convert a normal text type that uses the default converter |
||
| 41 | $json = json_encode(array( |
||
| 42 | 'data' => array(array( |
||
| 43 | 'type' => 'text', |
||
| 44 | 'data' => array( |
||
| 45 | 'text' => 'test' |
||
| 46 | ) |
||
| 47 | )) |
||
| 48 | )); |
||
| 49 | $html = $converter->toHtml($json); |
||
| 50 | $this->assertEquals($html, "<p>test</p>\n"); |
||
| 51 | |||
| 52 | // the video conversion |
||
| 53 | $json = json_encode(array( |
||
| 54 | 'data' => array(array( |
||
| 55 | 'type' => 'video', |
||
| 56 | 'data' => array( |
||
| 57 | 'source' => 'youtube', |
||
| 58 | 'remote_id' => '4BXpi7056RM' |
||
| 59 | ) |
||
| 60 | )) |
||
| 61 | )); |
||
| 62 | $html = $converter->toHtml($json); |
||
| 63 | $this->assertEquals( |
||
| 64 | $html, |
||
| 65 | "<iframe src=\"//www.youtube.com/embed/4BXpi7056RM?rel=0\" frameborder=\"0\" allowfullscreen></iframe>\n" |
||
| 66 | ); |
||
| 67 | |||
| 68 | // The heading |
||
| 69 | $json = json_encode(array( |
||
| 70 | 'data' => array(array( |
||
| 71 | 'type' => 'heading', |
||
| 72 | 'data' => array( |
||
| 73 | 'text' => 'test' |
||
| 74 | ) |
||
| 75 | )) |
||
| 76 | )); |
||
| 77 | $html = $converter->toHtml($json); |
||
| 78 | $this->assertEquals($html, "<h2>test</h2>\n"); |
||
| 79 | |||
| 80 | // images |
||
| 81 | $json = json_encode(array( |
||
| 82 | 'data' => array(array( |
||
| 83 | 'type' => 'image', |
||
| 84 | 'data' => array( |
||
| 85 | 'file' => array( |
||
| 86 | 'url' => '/frontend/files/sir-trevor/images/IMG_3867.JPG' |
||
| 87 | ) |
||
| 88 | ) |
||
| 89 | )) |
||
| 90 | )); |
||
| 91 | $html = $converter->toHtml($json); |
||
| 92 | $this->assertEquals($html, "<img src=\"/frontend/files/sir-trevor/images/IMG_3867.JPG\" />\n"); |
||
| 93 | } |
||
| 94 | } |
||
| 95 |