@@ -85,35 +85,108 @@ public function testImageUrlAddsARandomGetParameterByDefault()
85
85
self ::assertMatchesRegularExpression ('#\w*# ' , $ splitUrl [1 ]);
86
86
}
87
87
88
- public function testDownloadWithDefaults ()
88
+ public function testImageUrlThrowsExceptionOnInvalidImageFormat ()
89
89
{
90
- $ curlPing = curl_init (Image::BASE_URL );
91
- curl_setopt ($ curlPing , CURLOPT_TIMEOUT , 5 );
92
- curl_setopt ($ curlPing , CURLOPT_CONNECTTIMEOUT , 5 );
93
- curl_setopt ($ curlPing , CURLOPT_RETURNTRANSFER , true );
94
- curl_setopt ($ curlPing , CURLOPT_FOLLOWLOCATION , true );
95
- $ data = curl_exec ($ curlPing );
96
- $ httpCode = curl_getinfo ($ curlPing , CURLINFO_HTTP_CODE );
97
- curl_close ($ curlPing );
90
+ $ this ->expectException (\InvalidArgumentException::class);
91
+ Image::imageUrl (
92
+ 800 ,
93
+ 400 ,
94
+ 'nature ' ,
95
+ false ,
96
+ 'Faker ' ,
97
+ true ,
98
+ 'foo '
99
+ );
100
+ }
98
101
99
- if ($ httpCode < 200 | $ httpCode > 300 ) {
100
- self ::markTestSkipped ('Placeholder.com is offline, skipping image download ' );
102
+ public function testImageUrlAcceptsDifferentImageFormats ()
103
+ {
104
+ foreach (Image::getFormats () as $ format ) {
105
+ $ imageUrl = Image::imageUrl (
106
+ 800 ,
107
+ 400 ,
108
+ 'nature ' ,
109
+ false ,
110
+ 'Faker ' ,
111
+ true ,
112
+ $ format
113
+ );
114
+
115
+ self ::assertMatchesRegularExpression (
116
+ "#^https://via.placeholder.com/800x400. {$ format }/CCCCCC\?text=nature\+Faker# " ,
117
+ $ imageUrl
118
+ );
101
119
}
120
+ }
121
+
122
+ public function testDownloadWithDefaults ()
123
+ {
124
+ self ::checkUrlConnection (Image::BASE_URL );
102
125
103
126
$ file = Image::image (sys_get_temp_dir ());
104
127
self ::assertFileExists ($ file );
105
128
129
+ self ::checkImageProperties ($ file , 640 , 480 , 'png ' );
130
+ }
131
+
132
+ public function testDownloadWithDifferentImageFormats ()
133
+ {
134
+ self ::checkUrlConnection (Image::BASE_URL );
135
+
136
+ foreach (Image::getFormats () as $ format ) {
137
+ $ width = 800 ;
138
+ $ height = 400 ;
139
+ $ file = Image::image (
140
+ sys_get_temp_dir (),
141
+ $ width ,
142
+ $ height ,
143
+ 'nature ' ,
144
+ true ,
145
+ false ,
146
+ 'Faker ' ,
147
+ true ,
148
+ $ format
149
+ );
150
+ self ::assertFileExists ($ file );
151
+
152
+ self ::checkImageProperties ($ file , $ width , $ height , $ format );
153
+ }
154
+ }
155
+
156
+ private static function checkImageProperties (
157
+ string $ file ,
158
+ int $ width ,
159
+ int $ height ,
160
+ string $ format
161
+ ) {
106
162
if (function_exists ('getimagesize ' )) {
107
- [$ width , $ height , $ type , $ attr ] = getimagesize ($ file );
108
- self ::assertEquals (640 , $ width );
109
- self ::assertEquals (480 , $ height );
110
- self ::assertEquals (constant ('IMAGETYPE_PNG ' ), $ type );
163
+ $ imageConstants = Image::getFormatConstants ();
164
+ [$ actualWidth , $ actualHeight , $ type , $ attr ] = getimagesize ($ file );
165
+ self ::assertEquals ($ width , $ actualWidth );
166
+ self ::assertEquals ($ height , $ actualHeight );
167
+ self ::assertEquals ($ imageConstants [$ format ], $ type );
111
168
} else {
112
- self ::assertEquals (' png ' , pathinfo ($ file , PATHINFO_EXTENSION ));
169
+ self ::assertEquals ($ format , pathinfo ($ file , PATHINFO_EXTENSION ));
113
170
}
114
171
115
172
if (file_exists ($ file )) {
116
173
unlink ($ file );
117
174
}
118
175
}
176
+
177
+ private static function checkUrlConnection (string $ url )
178
+ {
179
+ $ curlPing = curl_init ($ url );
180
+ curl_setopt ($ curlPing , CURLOPT_TIMEOUT , 5 );
181
+ curl_setopt ($ curlPing , CURLOPT_CONNECTTIMEOUT , 5 );
182
+ curl_setopt ($ curlPing , CURLOPT_RETURNTRANSFER , true );
183
+ curl_setopt ($ curlPing , CURLOPT_FOLLOWLOCATION , true );
184
+ $ data = curl_exec ($ curlPing );
185
+ $ httpCode = curl_getinfo ($ curlPing , CURLINFO_HTTP_CODE );
186
+ curl_close ($ curlPing );
187
+
188
+ if ($ httpCode < 200 | $ httpCode > 300 ) {
189
+ self ::markTestSkipped (sprintf ('"%s" is offline, skipping test ' , $ url ));
190
+ }
191
+ }
119
192
}
0 commit comments