|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use Bnussbau\TrmnlPipeline\Model; |
| 6 | +use Bnussbau\TrmnlPipeline\Stages\BrowserStage; |
| 7 | +use Bnussbau\TrmnlPipeline\Stages\ImageStage; |
| 8 | +use Bnussbau\TrmnlPipeline\TrmnlPipeline; |
| 9 | + |
| 10 | +afterEach(function (): void { |
| 11 | + // Clean up after each test |
| 12 | + TrmnlPipeline::restore(); |
| 13 | +}); |
| 14 | + |
| 15 | +it('can enable and disable fake mode', function (): void { |
| 16 | + expect(TrmnlPipeline::isFake())->toBeFalse(); |
| 17 | + |
| 18 | + TrmnlPipeline::fake(); |
| 19 | + expect(TrmnlPipeline::isFake())->toBeTrue(); |
| 20 | + |
| 21 | + TrmnlPipeline::restore(); |
| 22 | + expect(TrmnlPipeline::isFake())->toBeFalse(); |
| 23 | +}); |
| 24 | + |
| 25 | +it('returns mock image path when browser stage is in fake mode', function (): void { |
| 26 | + TrmnlPipeline::fake(); |
| 27 | + |
| 28 | + $browserStage = new BrowserStage; |
| 29 | + $browserStage->html('<html><body>Test</body></html>'); |
| 30 | + |
| 31 | + $result = $browserStage(null); |
| 32 | + |
| 33 | + expect($result)->toBeString(); |
| 34 | + expect(file_exists($result))->toBeTrue(); |
| 35 | + expect(str_contains($result, 'browsershot_fake_'))->toBeTrue(); |
| 36 | + expect(str_ends_with($result, '.png'))->toBeTrue(); |
| 37 | +}); |
| 38 | + |
| 39 | +it('returns mock processed image path when image stage is in fake mode', function (): void { |
| 40 | + TrmnlPipeline::fake(); |
| 41 | + |
| 42 | + // Create a temporary input image |
| 43 | + $inputImage = tempnam(sys_get_temp_dir(), 'test_input_').'.png'; |
| 44 | + $image = imagecreate(10, 10); |
| 45 | + $white = imagecolorallocate($image, 255, 255, 255); |
| 46 | + imagefill($image, 0, 0, $white); |
| 47 | + imagepng($image, $inputImage); |
| 48 | + imagedestroy($image); |
| 49 | + |
| 50 | + $imageStage = new ImageStage; |
| 51 | + $imageStage->format('png')->width(800)->height(480); |
| 52 | + |
| 53 | + $result = $imageStage($inputImage); |
| 54 | + |
| 55 | + expect($result)->toBeString(); |
| 56 | + expect(file_exists($result))->toBeTrue(); |
| 57 | + expect(str_contains($result, '_processed.png'))->toBeTrue(); |
| 58 | + |
| 59 | + // Clean up |
| 60 | + unlink($inputImage); |
| 61 | + unlink($result); |
| 62 | +}); |
| 63 | + |
| 64 | +it('processes full pipeline in fake mode', function (): void { |
| 65 | + TrmnlPipeline::fake(); |
| 66 | + |
| 67 | + $html = '<html><body>Test Content</body></html>'; |
| 68 | + |
| 69 | + $result = (new TrmnlPipeline) |
| 70 | + ->model(Model::OG) |
| 71 | + ->pipe((new BrowserStage)->html($html)) |
| 72 | + ->pipe(new ImageStage) |
| 73 | + ->process(); |
| 74 | + |
| 75 | + expect($result)->toBeString(); |
| 76 | + expect(file_exists($result))->toBeTrue(); |
| 77 | + expect(str_contains((string) $result, '_processed.png'))->toBeTrue(); |
| 78 | + |
| 79 | + // Clean up |
| 80 | + unlink($result); |
| 81 | +}); |
| 82 | + |
| 83 | +it('respects image stage configuration in fake mode', function (): void { |
| 84 | + TrmnlPipeline::fake(); |
| 85 | + |
| 86 | + // Create a temporary input image |
| 87 | + $inputImage = tempnam(sys_get_temp_dir(), 'test_input_').'.png'; |
| 88 | + $image = imagecreate(10, 10); |
| 89 | + $white = imagecolorallocate($image, 255, 255, 255); |
| 90 | + imagefill($image, 0, 0, $white); |
| 91 | + imagepng($image, $inputImage); |
| 92 | + imagedestroy($image); |
| 93 | + |
| 94 | + $imageStage = new ImageStage; |
| 95 | + $imageStage |
| 96 | + ->format('bmp') |
| 97 | + ->width(400) |
| 98 | + ->height(300) |
| 99 | + ->outputPath('/tmp/custom_output.bmp'); |
| 100 | + |
| 101 | + $result = $imageStage($inputImage); |
| 102 | + |
| 103 | + expect($result)->toBe('/tmp/custom_output.bmp'); |
| 104 | + expect(file_exists($result))->toBeTrue(); |
| 105 | + |
| 106 | + // Clean up |
| 107 | + unlink($inputImage); |
| 108 | + unlink($result); |
| 109 | +}); |
| 110 | + |
| 111 | +it('still validates input in fake mode', function (): void { |
| 112 | + TrmnlPipeline::fake(); |
| 113 | + |
| 114 | + $browserStage = new BrowserStage; |
| 115 | + |
| 116 | + expect(fn (): string => $browserStage(null)) |
| 117 | + ->toThrow(Exception::class, 'No HTML content provided for browser rendering'); |
| 118 | +}); |
| 119 | + |
| 120 | +it('still validates file existence in fake mode', function (): void { |
| 121 | + TrmnlPipeline::fake(); |
| 122 | + |
| 123 | + $imageStage = new ImageStage; |
| 124 | + |
| 125 | + expect(fn (): string => $imageStage('/nonexistent/file.png')) |
| 126 | + ->toThrow(Exception::class, 'Invalid or missing image file'); |
| 127 | +}); |
0 commit comments