Completed
Branch master (4d68bf)
by John
02:46
created

FieldRendererFixedTextTest::testRender()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 98
Code Lines 85

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 85
nc 1
nop 0
dl 0
loc 98
rs 8.3272
c 0
b 0
f 0

How to fix   Long Method   

Long Method

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:

1
<?php
2
3
namespace Graze\CiffRenderer\Test\Unit\Renderer\FieldRenderer;
4
5
use Mockery as m;
6
use Intervention\Image\Gd\Font;
7
use Intervention\Image\Image;
8
use Intervention\Image\ImageManager;
9
use Graze\CiffRenderer\Parser\FieldParser\FieldParserInterface;
10
use Graze\CiffRenderer\Renderer\FieldRenderer\FieldRendererFixedText;
11
12
class FieldRendererFixedTextTest extends \PHPUnit_Framework_TestCase
13
{
14
    public function testRender()
15
    {
16
        $text = 'i am text';
17
        $file = 'i am file';
18
        $size = 4.2;
19
        $width = 2;
20
        $height = 4;
21
        $font = m::mock(Font::class)
22
            ->shouldReceive('text')
23
            ->with($text)
24
            ->once()
25
            ->shouldReceive('file')
26
            ->with($file)
27
            ->once()
28
            ->shouldReceive('size')
29
            ->with($size)
30
            ->once()
31
            ->shouldReceive('color')
32
            ->with('#000')
33
            ->once()
34
            ->shouldReceive('valign')
35
            ->with('top')
36
            ->once()
37
            ->shouldReceive('getBoxSize')
38
            ->andReturn(['width' => $width, 'height' => $height])
39
            ->once()
40
            ->getMock();
41
        $textExpectation = function ($actualClosure) use ($font) {
42
            $fontActual = null;
43
            $actualClosure($fontActual);
44
            return $fontActual == $font;
45
        };
46
47
        $canvasLine = m::mock(Image::class)
48
            ->shouldReceive('text')
49
            ->with('', null, null, m::on($textExpectation))
50
            ->shouldReceive('resize')
51
            ->with(round($width * FieldRendererFixedText::LETTER_SPACING_ADJUSTMENT), $height)
52
            ->once()
53
            ->shouldReceive('getWidth')
54
            ->andReturn($width)
55
            ->once()
56
            ->shouldReceive('getHeight')
57
            ->andReturn($height)
58
            ->twice()
59
            ->getMock();
60
61
        $orientation = 90;
62
63
        $canvas = m::mock(Image::class)
64
            ->shouldReceive('insert')
65
            ->with($canvasLine, 'top-left', 0, 0)
66
            ->once()
67
            ->shouldReceive('rotate')
68
            ->with(360 - $orientation)
69
            ->once()
70
            ->getMock();
71
72
        $imageManager = m::mock(ImageManager::class)
73
            ->shouldReceive('canvas')
74
            ->with($width, $height)
75
            ->andReturn($canvasLine)
76
            ->once()
77
            ->shouldReceive('canvas')
78
            ->with($width, $height)
79
            ->andReturn($canvas)
80
            ->once()
81
            ->getMock();
82
83
        $faceExpected = 'i am  face';
84
        $parser = m::mock(FieldParserInterface::class)
85
            ->shouldReceive('getText')
86
            ->andReturn($text)
87
            ->once()
88
            ->shouldReceive('getFontFace')
89
            ->andReturn($faceExpected)
90
            ->once()
91
            ->shouldReceive('getFontSize')
92
            ->andReturn($size)
93
            ->once()
94
            ->shouldReceive('getOrientation')
95
            ->andReturn($orientation)
96
            ->once()
97
            ->getMock();
98
99
        $fontResolver = function ($face) use ($faceExpected, $file) {
100
            $this->assertEquals($faceExpected, $face);
101
            return $file;
102
        };
103
104
        $renderer = new FieldRendererFixedText($font);
105
        $image = $renderer->render(
106
            $imageManager,
107
            $parser,
108
            $fontResolver
109
        );
110
111
        $this->assertEquals($canvas, $image);
112
    }
113
}
114