88use MathiasGrimm \GlimpsePhp \ForbiddenException ;
99use MathiasGrimm \GlimpsePhp \ImageFormat ;
1010use MathiasGrimm \GlimpsePhp \ImageInfo ;
11+ use MathiasGrimm \GlimpsePhp \ImageResult ;
1112use MathiasGrimm \GlimpsePhp \RateLimitException ;
1213use MathiasGrimm \GlimpsePhp \SizeEstimate ;
1314use MathiasGrimm \GlimpsePhp \Tests \Fixtures \Images ;
@@ -30,15 +31,16 @@ function client(Factory $http, Closure|string|null $token = 'test-token', string
3031}
3132
3233test ('convert posts the base64 envelope and returns a decoded ImageResult ' , function () {
33- $ http = fakeHttp (['*/v1/convert ' => Factory::response (fakeTransformResponse ())]);
34+ $ http = fakeHttp (['*/v1/convert ' => Factory::response (fakeTransformResponse (overrides: [ ' psnr ' => 41.27 ] ))]);
3435
3536 $ result = client ($ http )->convert (Images::png (), ImageFormat::Jpg);
3637
3738 expect ($ result ->bytes )->toBe (Images::jpg ())
3839 ->and ($ result ->format )->toBe (ImageFormat::Jpg->value )
3940 ->and ($ result ->mimeType )->toBe ('image/jpeg ' )
4041 ->and ($ result ->width )->toBe (1280 )
41- ->and ($ result ->height )->toBe (720 );
42+ ->and ($ result ->height )->toBe (720 )
43+ ->and ($ result ->psnr )->toBe (41.27 );
4244
4345 $ http ->assertSent (function (Request $ request ) {
4446 return $ request ->url () === 'https://glimpseimg.com/api/v1/convert '
@@ -49,6 +51,33 @@ function client(Factory $http, Closure|string|null $token = 'test-token', string
4951 });
5052});
5153
54+ test ('a null psnr in the response stays null ' , function () {
55+ $ http = fakeHttp (['*/v1/optimize ' => Factory::response (fakeTransformResponse (overrides: ['psnr ' => null ]))]);
56+
57+ expect (client ($ http )->optimize (Images::png ())->psnr )->toBeNull ();
58+ });
59+
60+ test ('a response without a psnr field maps to a null psnr ' , function () {
61+ // Resize and thumbnail responses do not carry the field at all.
62+ $ http = fakeHttp (['*/v1/resize ' => Factory::response (fakeTransformResponse ())]);
63+
64+ expect (client ($ http )->resize (Images::png (), width: 800 )->psnr )->toBeNull ();
65+ });
66+
67+ test ('an integer psnr in the response is cast to float ' , function () {
68+ $ http = fakeHttp (['*/v1/optimize ' => Factory::response (fakeTransformResponse (overrides: ['psnr ' => 42 ]))]);
69+
70+ expect (client ($ http )->optimize (Images::png ())->psnr )->toBe (42.0 );
71+ });
72+
73+ test ('the six-argument constructor stays valid and defaults psnr to null ' , function () {
74+ // Locks backward compatibility: callers built against pre-3.1 must keep
75+ // working without passing psnr.
76+ $ result = new ImageResult ('bytes ' , 'jpg ' , 'image/jpeg ' , 100 , 20 , 10 );
77+
78+ expect ($ result ->psnr )->toBeNull ();
79+ });
80+
5281test ('convert sends optimize and quality when given ' , function () {
5382 $ http = fakeHttp (['*/v1/convert ' => Factory::response (fakeTransformResponse ())]);
5483
0 commit comments