forked from codeigniter4/CodeIgniter4
/
ImageTest.php
109 lines (93 loc) · 3.26 KB
/
ImageTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Images;
use CodeIgniter\Images\Exceptions\ImageException;
use CodeIgniter\Test\CIUnitTestCase;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamDirectory;
/**
* @internal
*/
final class ImageTest extends CIUnitTestCase
{
private vfsStreamDirectory $root;
private string $origin;
private string $start;
private Image $image;
protected function setUp(): void
{
// create virtual file system
$this->root = vfsStream::setup();
// copy our support files
$this->origin = '_support/Images/';
vfsStream::copyFromFileSystem(TESTPATH . $this->origin, $this->root);
// make subfolders
$structure = [
'work' => [],
'wontwork' => [],
];
vfsStream::create($structure);
// with one of them read only
$this->root->getChild('wontwork')->chmod(0400);
$this->start = $this->root->url() . '/';
$this->image = new Image($this->start . 'ci-logo.png');
}
public function testBasicPropertiesInherited()
{
$this->assertSame('ci-logo.png', $this->image->getFilename());
$this->assertSame($this->start . 'ci-logo.png', $this->image->getPathname());
$this->assertSame($this->root->url(), $this->image->getPath());
$this->assertSame('ci-logo.png', $this->image->getBasename());
}
public function testGetProperties()
{
$expected = [
'width' => 155,
'height' => 200,
'image_type' => IMAGETYPE_PNG,
'size_str' => 'width="155" height="200"',
'mime_type' => 'image/png',
];
$this->assertSame($expected, $this->image->getProperties(true));
}
public function testExtractProperties()
{
// extract properties from the image
$this->assertTrue($this->image->getProperties(false));
$this->assertSame(155, $this->image->origWidth);
$this->assertSame(200, $this->image->origHeight);
$this->assertSame(IMAGETYPE_PNG, $this->image->imageType);
$this->assertSame('width="155" height="200"', $this->image->sizeStr);
$this->assertSame('image/png', $this->image->mime);
}
public function testCopyDefaultName()
{
$targetPath = $this->start . 'work';
$this->image->copy($targetPath);
$this->assertTrue($this->root->hasChild('work/ci-logo.png'));
}
public function testCopyNewName()
{
$this->image->copy($this->root->url(), 'new-logo.png');
$this->assertTrue($this->root->hasChild('new-logo.png'));
}
public function testCopyNewFolder()
{
$targetPath = $this->start . 'work/subfolder';
$this->image->copy($targetPath, 'new-logo.png');
$this->assertTrue($this->root->hasChild('work/subfolder/new-logo.png'));
}
public function testCopyNowhere()
{
$this->expectException(ImageException::class);
$targetPath = $this->start . 'work';
$this->image->copy($targetPath, '');
}
}