VersionedImageTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 86
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 28 1
A tearDown() 0 9 2
A testVersionChangeResamplesImage() 0 20 1
A resampleProvider() 0 10 1
1
<?php
2
/**
3
 * @package    silverstripe-versionedfiles
4
 * @subpackage tests
5
 */
6
class VersionedImageTest extends FunctionalTest
7
{
8
    protected $usesDatabase = true;
9
10
    protected $image;
11
    
12
    protected $reference;
13
14
    public function setUp()
15
    {
16
        parent::setUp();
17
18
        $path = ASSETS_PATH . '/test-image.png';
19
        $referencePath = ASSETS_PATH . '/test-image-reference.png';
20
21
        // Create two base images, A and B.
22
        $first = imagecreatetruecolor(200, 100);
23
        imagefill($first, 0, 0, imagecolorallocate($first, 0, 0, 0));
24
        imagepng($first, $path);
25
        imagepng($first, $referencePath);
26
27
        $this->image = new Image();
28
        $this->image->Filename = 'assets/test-image.png';
29
        $this->image->write();
30
31
        $this->reference = new Image();
32
        $this->reference->Filename = 'assets/test-image-reference.png';
33
        $this->reference->write();
34
35
        // Add version #2 to image A.
36
        $second = imagecreatetruecolor(100, 200);
37
        imagefill($second, 0, 0, imagecolorallocate($first, 255, 255, 255));
38
        imagepng($second, $path);
39
40
        $this->image->createVersion();
41
    }
42
43
    public function tearDown()
44
    {
45
        $this->image->delete();
46
        if ($this->reference) {
47
            $this->reference->delete();
48
        }
49
50
        parent::tearDown();
51
    }
52
53
    /**
54
     * Reverting the file should also regenerate all the transformed files.
55
     * We check this here by applying the transform to both the tested file and the reference file,
56
     * then reverting and checking if the transform still applies.
57
     *
58
     * @dataProvider resampleProvider
59
     */
60
    public function testVersionChangeResamplesImage($method, array $arguments)
61
    {
62
        // Ensure files exist
63
        call_user_func_array(array($this->image, $method), $arguments);
64
        $referenceImage = call_user_func_array(array($this->reference, $method), $arguments);
65
66
        $this->image->setVersionNumber(1);
67
        $this->image->write();
68
69
        // Get reference and tested cache name
70
        $referenceFilename = $referenceImage->getFullPath();
71
        $testedFilename = str_replace($this->reference->Name, $this->image->Name, $referenceFilename);
72
73
        $this->assertNotEquals($testedFilename, $referenceFilename);
0 ignored issues
show
Bug introduced by
The method assertNotEquals() does not seem to exist on object<VersionedImageTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
74
        $this->assertFileEquals(
0 ignored issues
show
Bug introduced by
The method assertFileEquals() does not seem to exist on object<VersionedImageTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
75
            $referenceFilename,
76
            $testedFilename,
77
            'Reverting an image version re-applies all transforms already in place.'
78
        );
79
    }
80
81
    public function resampleProvider()
82
    {
83
        return array(
84
            array('SetSize',      array(50, 50)),
85
            array('PaddedImage',  array(500, 200, '999999')),
86
            array('SetWidth',     array(80)),
87
            array('SetHeight',    array(80)),
88
            array('SetRatioSize', array(150, 300))
89
        );
90
    }
91
}
92