| Conditions | 1 |
| Paths | 1 |
| Total Lines | 58 |
| Code Lines | 33 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| 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 |
||
| 60 | public function testGenerateLink() |
||
| 61 | { |
||
| 62 | $generator = new ThumbnailGenerator(); |
||
| 63 | |||
| 64 | ThumbnailGenerator::config()->set('thumbnail_links', [ |
||
| 65 | AssetStore::VISIBILITY_PROTECTED => ThumbnailGenerator::INLINE, |
||
| 66 | AssetStore::VISIBILITY_PUBLIC => ThumbnailGenerator::URL, |
||
| 67 | ]); |
||
| 68 | |||
| 69 | // Build image |
||
| 70 | $image = new Image(); |
||
| 71 | $image->setFromLocalFile(__DIR__.'/../Forms/fixtures/testimage.png', 'TestImage.png'); |
||
| 72 | $image->write(); |
||
| 73 | |||
| 74 | // Non-images are ignored |
||
| 75 | $file = new File(); |
||
| 76 | $link = $generator->generateLink($file); |
||
| 77 | $this->assertNull($link); |
||
| 78 | |||
| 79 | // original image |
||
| 80 | $thumbnail = $generator->generateLink($image); |
||
| 81 | $this->assertStringStartsWith('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAASwAAACW', $thumbnail); |
||
| 82 | |||
| 83 | // protected image should have inline thumbnail |
||
| 84 | $thumbnail = $generator->generateThumbnailLink($image, 100, 200); |
||
| 85 | $this->assertStringStartsWith('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAAAy', $thumbnail); |
||
|
|
|||
| 86 | |||
| 87 | // but not if it's too big |
||
| 88 | Config::nest(); |
||
| 89 | Config::modify()->set(ThumbnailGenerator::class, 'max_thumbnail_bytes', 1); |
||
| 90 | // Without graceful thumbnails, it should come back null |
||
| 91 | $thumbnail = $generator->generateThumbnailLink($image, 100, 200); |
||
| 92 | $this->assertNull($thumbnail); |
||
| 93 | // With graceful thumbnails, it should come back as a URL |
||
| 94 | $thumbnail = $generator->generateThumbnailLink($image, 100, 200, true); |
||
| 95 | $this->assertRegExp('#/assets/[A-Za-z0-9]+/TestImage__FitMaxWzEwMCwyMDBd\.png$#', $thumbnail); |
||
| 96 | Config::unnest(); |
||
| 97 | |||
| 98 | // public image should have url |
||
| 99 | $image->publishSingle(); |
||
| 100 | $thumbnail = $generator->generateThumbnailLink($image, 100, 200); |
||
| 101 | $this->assertEquals('/assets/ThumbnailGeneratorTest/TestImage__FitMaxWzEwMCwyMDBd.png', $thumbnail); |
||
| 102 | |||
| 103 | // Public assets can be set to inline |
||
| 104 | ThumbnailGenerator::config()->merge('thumbnail_links', [ |
||
| 105 | AssetStore::VISIBILITY_PUBLIC => ThumbnailGenerator::INLINE, |
||
| 106 | ]); |
||
| 107 | $thumbnail = $generator->generateThumbnailLink($image, 100, 200); |
||
| 108 | $this->assertStringStartsWith('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAAAy', $thumbnail); |
||
| 109 | |||
| 110 | // Protected assets can be set to url |
||
| 111 | // This uses protected asset adapter, so not direct asset link |
||
| 112 | ThumbnailGenerator::config()->merge('thumbnail_links', [ |
||
| 113 | AssetStore::VISIBILITY_PROTECTED => ThumbnailGenerator::URL, |
||
| 114 | ]); |
||
| 115 | $image->doUnpublish(); |
||
| 116 | $thumbnail = $generator->generateThumbnailLink($image, 100, 200); |
||
| 117 | $this->assertEquals('/assets/906835357d/TestImage__FitMaxWzEwMCwyMDBd.png', $thumbnail); |
||
| 118 | } |
||
| 120 |