Skip to content

Commit bad8082

Browse files
authored
Merge pull request #43 from benfaerber/change-logger
Add a LabelImage Test
2 parents df20c40 + 552dd2e commit bad8082

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed

composer.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
"view:coverage": [
3636
"open coverage/index.html"
3737
],
38+
"test:coverage-view": [
39+
"XDEBUG_MODE=coverage ./vendor/bin/phpunit tests --verbose --bootstrap \"tests/TestUtils.php\" --coverage-html coverage",
40+
"open coverage/index.html"
41+
],
3842
"benchmark": [
3943
"php vendor/bin/phpbench run --report=main_report --output=html_report"
4044
],

tests/LabelImageTest.php

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Faerber\PdfToZpl\LabelImage;
6+
use Faerber\PdfToZpl\Settings\LabelDirection;
7+
use PHPUnit\Framework\TestCase;
8+
9+
final class LabelImageTest extends TestCase {
10+
private string $simpleZpl = "^XA^FO50,50^ADN,36,20^FDTest Label^FS^XZ";
11+
private static ?LabelImage $cachedLabel = null;
12+
13+
/**
14+
* Get a cached LabelImage to avoid hitting API rate limits
15+
* The Labelary API only allows 5 requests per second
16+
*/
17+
private function getCachedLabel(): LabelImage {
18+
if (self::$cachedLabel === null) {
19+
self::$cachedLabel = new LabelImage($this->simpleZpl);
20+
// Small delay to avoid rate limiting
21+
usleep(250000); // 250ms
22+
}
23+
return self::$cachedLabel;
24+
}
25+
26+
public function testCanCreateLabelImageAndHitsApi(): void {
27+
$label = $this->getCachedLabel();
28+
29+
// Should have image data from API
30+
$this->assertNotEmpty($label->image);
31+
$this->assertGreaterThan(100, strlen($label->image), "Image should have substantial data");
32+
}
33+
34+
public function testDownloadsValidPngImage(): void {
35+
$label = $this->getCachedLabel();
36+
$image = $label->asRaw();
37+
38+
// Check PNG magic bytes (first 8 bytes should be PNG signature)
39+
$pngSignature = "\x89PNG\r\n\x1a\n";
40+
$this->assertEquals(
41+
$pngSignature,
42+
substr($image, 0, 8),
43+
"Image should be a valid PNG"
44+
);
45+
}
46+
47+
public function testAsHtmlImageReturnsDataUri(): void {
48+
$label = $this->getCachedLabel();
49+
$html = $label->asHtmlImage();
50+
51+
// Should start with data URI prefix
52+
$this->assertStringStartsWith('data:image/png;base64,', $html);
53+
54+
// Should be valid base64 after the prefix
55+
$base64Part = substr($html, strlen('data:image/png;base64,'));
56+
$this->assertNotEmpty($base64Part);
57+
$this->assertNotFalse(base64_decode($base64Part, true));
58+
}
59+
60+
public function testAsRawReturnsImageData(): void {
61+
$label = $this->getCachedLabel();
62+
$raw = $label->asRaw();
63+
64+
$this->assertNotEmpty($raw);
65+
$this->assertEquals($label->image, $raw);
66+
}
67+
68+
public function testCanSaveImageToFile(): void {
69+
$label = $this->getCachedLabel();
70+
$tempFile = sys_get_temp_dir() . '/test_label_' . uniqid() . '.png';
71+
72+
try {
73+
$label->saveAs($tempFile);
74+
75+
// File should exist
76+
$this->assertFileExists($tempFile);
77+
78+
// File contents should match image data
79+
$this->assertEquals($label->image, file_get_contents($tempFile));
80+
81+
// Should be a valid PNG
82+
$imageInfo = getimagesize($tempFile);
83+
$this->assertNotFalse($imageInfo);
84+
$this->assertEquals('image/png', $imageInfo['mime']);
85+
} finally {
86+
// Clean up
87+
if (file_exists($tempFile)) {
88+
unlink($tempFile);
89+
}
90+
}
91+
}
92+
93+
public function testCustomDimensions(): void {
94+
// Sleep to avoid rate limiting
95+
usleep(250000); // 250ms
96+
97+
$label = new LabelImage(
98+
zpl: $this->simpleZpl,
99+
width: 2,
100+
height: 3
101+
);
102+
103+
$this->assertNotEmpty($label->image);
104+
105+
// Image should be a valid PNG
106+
$pngSignature = "\x89PNG\r\n\x1a\n";
107+
$this->assertEquals($pngSignature, substr($label->image, 0, 8));
108+
}
109+
110+
public function testCustomDirection(): void {
111+
// Sleep to avoid rate limiting
112+
usleep(250000); // 250ms
113+
114+
$label = new LabelImage(
115+
zpl: $this->simpleZpl,
116+
direction: LabelDirection::Right
117+
);
118+
119+
$this->assertNotEmpty($label->image);
120+
121+
// Image should be a valid PNG
122+
$pngSignature = "\x89PNG\r\n\x1a\n";
123+
$this->assertEquals($pngSignature, substr($label->image, 0, 8));
124+
}
125+
}

0 commit comments

Comments
 (0)