-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathStreamingTest.php
109 lines (96 loc) · 3.57 KB
/
StreamingTest.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
namespace Jcupitt\Vips\Test;
use Generator;
use Jcupitt\Vips\FFI;
use Jcupitt\Vips\Exception;
use Jcupitt\Vips\Image;
use Jcupitt\Vips\Source;
use Jcupitt\Vips\SourceResource;
use Jcupitt\Vips\Target;
use Jcupitt\Vips\TargetResource;
use PHPUnit\Framework\TestCase;
class StreamingTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
if (!FFI::atLeast(8, 9)) {
$this->markTestSkipped('libvips too old for streaming tests');
}
}
/**
* @throws Exception
*/
public function sourceAndTargetProvider(): Generator
{
$sources = [
'File' => fn() => Source::newFromFile(__DIR__ . '/images/img_0076.jpg'),
'Memory' => fn() => Source::newFromMemory(file_get_contents(__DIR__ . '/images/img_0076.jpg')),
'Resource' => fn() => new SourceResource(fopen(__DIR__ . '/images/img_0076.jpg', 'rb'))
];
$targets = [
'File' => fn() => Target::newToFile(tempnam(sys_get_temp_dir(), 'image')),
'Memory' => fn() => Target::newToMemory(),
'Resource' => fn() => new TargetResource(fopen('php://memory', 'wb+')),
'Resource (not readable)' => fn() => new TargetResource(fopen('php://memory', 'wb'))
];
foreach ($sources as $sourceName => $source) {
foreach ($targets as $targetName => $target) {
yield "$sourceName => $targetName" => [$source(), $target()];
}
}
}
/**
* @dataProvider sourceAndTargetProvider
*/
public function testFromSourceToTarget(Source $source, Target $target): void
{
$image = Image::newFromSource($source);
$image->writeToTarget($target, '.jpg[Q=95]');
// Try delete temporary file
if ($target->filename() !== null) {
@unlink($target->filename());
}
}
/**
* This test case is extra since it's the easiest to make sure we can "reload" the saved image
*/
public function testFromFileToFile(): void
{
$source = Source::newFromFile(__DIR__ . '/images/img_0076.jpg');
$target = Target::newToFile(tempnam(sys_get_temp_dir(), 'image'));
$image = Image::newFromSource($source);
$image->writeToTarget($target, '.jpg[Q=95]');
// Make sure we can load the file
$image = Image::newFromFile($target->filename());
$image->writeToBuffer('.jpg[Q=95]');
unlink($target->filename());
}
public function testNoLeak(): void
{
$lastUsage = 0;
$leaked = false;
for ($i = 0; $i < 10; $i++) {
$filename = tempnam(sys_get_temp_dir(), 'image');
$source = new SourceResource(fopen(__DIR__ . '/images/img_0076.jpg', 'rb'));
$target = new TargetResource(fopen($filename, 'wb+'));
$image = Image::newFromSource($source);
$image->writeToTarget($target, '.jpg[Q=95]');
unlink($filename);
$usage = memory_get_peak_usage(true);
$diff = $usage - $lastUsage;
if ($lastUsage !== 0 && $diff > 0) {
$leaked = true;
}
$lastUsage = $usage;
}
$this->assertFalse($leaked, 'Streaming leaked memory');
}
public function testFromFileToDescriptor(): void
{
// NOTE(L3tum): There is no way to get a file descriptor in PHP :)
// In theory we could use the known fds like stdin or stdout,
// but that would spam those channels full with an entire image file.
// Because of that I've chosen to omit this test.
}
}