1<?php
2/**
3 * Base class for image transform driver tests
4 *
5 * @author Christian Weiske <cweiske@php.net>
6 */
7abstract class Image_Transform_Base extends PHPUnit_Framework_TestCase
8{
9    /**
10     * @var Image_Transform
11     */
12    protected $it;
13
14    /**
15    * Partial name of the driver (e.g. GD)
16    * @var string
17    */
18    protected $strDriverPart = null;
19
20    /**
21    * Location of test images
22    */
23    protected $strImageDir = null;
24
25    /**
26    * Array of temporary files that should be deleted
27    * when the test is done.
28    *
29    * @var array
30    */
31    protected $arTmpFiles = array();
32
33    /**
34    * Maximum average difference to the template.
35    * NULL to deactivate and require an exact image.
36    *
37    * This variable is reset on every setUp().
38    *
39    * @var int
40    */
41    protected $nMaxAverageDiff = null;
42
43
44
45    public function __construct()
46    {
47        $strClass            = get_class($this);
48        $this->strDriverPart = substr($strClass, strrpos($strClass, '_') + 1, -4);
49
50        require_once 'Image/Transform/Helper.php';
51        require_once 'Image/Transform.php';
52
53        $this->strImageDir = dirname(__FILE__) . '/../../images/';
54    }
55
56
57
58    /**
59     * Runs the test methods of this class.
60     *
61     * @return void
62     */
63    public static function mainImpl($strClass)
64    {
65        require_once 'PHPUnit/TextUI/TestRunner.php';
66
67        $suite  = new PHPUnit_Framework_TestSuite($strClass);
68        $result = PHPUnit_TextUI_TestRunner::run($suite);
69    }
70
71
72
73    /**
74     * Sets up the fixture, for example, opens a network connection.
75     * This method is called before a test is executed.
76     *
77     * @return void
78     */
79    protected function setUp()
80    {
81        $this->nMaxAverageDiff = null;
82
83        $this->it = Image_Transform::factory($this->strDriverPart);
84
85        $bError = PEAR::isError($this->it);
86        if ($bError) {
87            if ($this->it->getCode() == IMAGE_TRANSFORM_ERROR_UNSUPPORTED) {
88                $this->markTestSkipped($this->it->getMessage());
89            } else {
90                $this->fail($this->it->getMessage(), $this->it->getCode());
91            }
92        }
93    }//protected function setUp()
94
95
96
97    /**
98     * Removes temporary files
99     *
100     * @return void
101     */
102    protected function tearDown()
103    {
104//return;
105        //delete tmp files
106        if (count($this->arTmpFiles) > 0) {
107            foreach ($this->arTmpFiles as $strFile) {
108                if (!unlink($strFile)) {
109                    $this->fail('Could not delete temporary file: ' . $strFile);
110                }
111            }
112            $this->arTmpFiles = array();
113        }
114    }//protected function tearDown()
115
116
117
118    protected function getTmpFilename()
119    {
120        $strFile = tempnam('/tmp/', __CLASS__ . '-img-');
121        $this->arTmpFiles[] = $strFile;
122        return $strFile;
123    }//protected function getTmpFilename()
124
125
126
127    /**
128     * Resize image from 4x4 to 2x2
129     *
130     * @return void
131     */
132    public function testResize()
133    {
134        $strTmp = $this->getTmpFilename();
135        $this->assertTrue($this->it->load($this->strImageDir . 'base_4x4.png'));
136        $this->assertTrue($this->it->resize(2, 2));
137        $this->assertTrue($this->it->save($strTmp));
138        $this->assertExactlySameImage(
139            $this->strImageDir . 'base_4x4-resized-2x2.png',
140            $strTmp
141        );
142    }//public function testResize()
143
144
145
146    /**
147     * @todo Implement testScaleByX().
148     */
149    public function testScaleByX() {
150        // Remove the following lines when you implement this test.
151        $this->markTestIncomplete(
152          'This test has not been implemented yet.'
153        );
154    }
155
156    /**
157     * @todo Implement testScaleByXY().
158     */
159    public function testScaleByXY() {
160        // Remove the following lines when you implement this test.
161        $this->markTestIncomplete(
162          'This test has not been implemented yet.'
163        );
164    }
165
166    /**
167     * @todo Implement testScaleByY().
168     */
169    public function testScaleByY() {
170        // Remove the following lines when you implement this test.
171        $this->markTestIncomplete(
172          'This test has not been implemented yet.'
173        );
174    }
175
176    /**
177     * @todo Implement testScale().
178     */
179    public function testScale() {
180        // Remove the following lines when you implement this test.
181        $this->markTestIncomplete(
182          'This test has not been implemented yet.'
183        );
184    }
185
186    /**
187     * @todo Implement testScaleByPercentage().
188     */
189    public function testScaleByPercentage() {
190        // Remove the following lines when you implement this test.
191        $this->markTestIncomplete(
192          'This test has not been implemented yet.'
193        );
194    }
195
196    /**
197     * @todo Implement testScaleByFactor().
198     */
199    public function testScaleByFactor() {
200        // Remove the following lines when you implement this test.
201        $this->markTestIncomplete(
202          'This test has not been implemented yet.'
203        );
204    }
205
206    /**
207     * @todo Implement testScaleMaxLength().
208     */
209    public function testScaleMaxLength() {
210        // Remove the following lines when you implement this test.
211        $this->markTestIncomplete(
212          'This test has not been implemented yet.'
213        );
214    }
215
216    /**
217     * @todo Implement testScaleByLength().
218     */
219    public function testScaleByLength() {
220        // Remove the following lines when you implement this test.
221        $this->markTestIncomplete(
222          'This test has not been implemented yet.'
223        );
224    }
225
226    /**
227     * @todo Implement testFit().
228     */
229    public function testFit() {
230        // Remove the following lines when you implement this test.
231        $this->markTestIncomplete(
232          'This test has not been implemented yet.'
233        );
234    }
235
236    /**
237     * @todo Implement testFitOnCanvas().
238     */
239    public function testFitOnCanvas() {
240        // Remove the following lines when you implement this test.
241        $this->markTestIncomplete(
242          'This test has not been implemented yet.'
243        );
244    }
245
246    /**
247     * @todo Implement testFitX().
248     */
249    public function testFitX() {
250        // Remove the following lines when you implement this test.
251        $this->markTestIncomplete(
252          'This test has not been implemented yet.'
253        );
254    }
255
256    /**
257     * @todo Implement testFitY().
258     */
259    public function testFitY() {
260        // Remove the following lines when you implement this test.
261        $this->markTestIncomplete(
262          'This test has not been implemented yet.'
263        );
264    }
265
266    /**
267     * @todo Implement testGetHandle().
268     */
269    public function testGetHandle() {
270        // Remove the following lines when you implement this test.
271        $this->markTestIncomplete(
272          'This test has not been implemented yet.'
273        );
274    }
275
276    /**
277     * @todo Implement testNormalize().
278     */
279    public function testNormalize() {
280        // Remove the following lines when you implement this test.
281        $this->markTestIncomplete(
282          'This test has not been implemented yet.'
283        );
284    }
285
286    /**
287     * @todo Implement testLoad().
288     */
289    public function testLoad() {
290        // Remove the following lines when you implement this test.
291        $this->markTestIncomplete(
292          'This test has not been implemented yet.'
293        );
294    }
295
296    /**
297     * @todo Implement testDisplay().
298     */
299    public function testDisplay() {
300        // Remove the following lines when you implement this test.
301        $this->markTestIncomplete(
302          'This test has not been implemented yet.'
303        );
304    }
305
306    /**
307     * @todo Implement testSave().
308     */
309    public function testSave() {
310        // Remove the following lines when you implement this test.
311        $this->markTestIncomplete(
312          'This test has not been implemented yet.'
313        );
314    }
315
316    /**
317     * @todo Implement testFree().
318     */
319    public function testFree() {
320        // Remove the following lines when you implement this test.
321        $this->markTestIncomplete(
322          'This test has not been implemented yet.'
323        );
324    }
325
326    /**
327     * @todo Implement testAddText().
328     */
329    public function testAddText() {
330        // Remove the following lines when you implement this test.
331        $this->markTestIncomplete(
332          'This test has not been implemented yet.'
333        );
334    }
335
336    /**
337     * @todo Implement testAddDropShadow().
338     */
339    public function testAddDropShadow() {
340        // Remove the following lines when you implement this test.
341        $this->markTestIncomplete(
342          'This test has not been implemented yet.'
343        );
344    }
345
346    /**
347     * @todo Implement testAddBorder().
348     */
349    public function testAddBorder() {
350        // Remove the following lines when you implement this test.
351        $this->markTestIncomplete(
352          'This test has not been implemented yet.'
353        );
354    }
355
356    /**
357     * @todo Implement testCrop().
358     */
359    public function testCrop() {
360        // Remove the following lines when you implement this test.
361        $this->markTestIncomplete(
362          'This test has not been implemented yet.'
363        );
364    }
365
366    /**
367     * @todo Implement testCanvasResize().
368     */
369    public function testCanvasResize() {
370        // Remove the following lines when you implement this test.
371        $this->markTestIncomplete(
372          'This test has not been implemented yet.'
373        );
374    }
375
376    /**
377     * @todo Implement testGamma().
378     */
379    public function testGamma() {
380        // Remove the following lines when you implement this test.
381        $this->markTestIncomplete(
382          'This test has not been implemented yet.'
383        );
384    }
385
386    /**
387     * @todo Implement testRotate().
388     */
389    public function testRotate() {
390        // Remove the following lines when you implement this test.
391        $this->markTestIncomplete(
392          'This test has not been implemented yet.'
393        );
394    }
395
396    /**
397     * Test horizontal mirroring
398     * 123 -> 321
399     *
400     * @return void
401     */
402    public function testMirror()
403    {
404        $strTmp = $this->getTmpFilename();
405        $this->it->load($this->strImageDir . 'base_4x4.png');
406        $this->it->mirror();
407        $this->it->save($strTmp);
408        $this->assertExactlySameImage(
409            $this->strImageDir . 'base_4x4-mirror.png',
410            $strTmp
411        );
412    }//public function testMirror()
413
414
415
416    /**
417     * Test vertically flipping the image
418     * v -> ^
419     *
420     * @return void
421     */
422    public function testFlip()
423    {
424        $strTmp = $this->getTmpFilename();
425        $this->it->load($this->strImageDir . 'base_4x4.png');
426        $this->it->flip();
427        $this->it->save($strTmp);
428        $this->assertExactlySameImage(
429            $this->strImageDir . 'base_4x4-flip.png',
430            $strTmp
431        );
432    }//public function testFlip()
433
434
435
436    /**
437     * @todo Implement testGreyscale().
438     */
439    public function testGreyscale() {
440        // Remove the following lines when you implement this test.
441        $this->markTestIncomplete(
442          'This test has not been implemented yet.'
443        );
444    }
445
446    /**
447     * @todo Implement testGrayscale().
448     */
449    public function testGrayscale() {
450        // Remove the following lines when you implement this test.
451        $this->markTestIncomplete(
452          'This test has not been implemented yet.'
453        );
454    }
455
456
457
458    protected function assertExactlySameImage($filename1, $filename2)
459    {
460        if ($this->nMaxAverageDiff === null) {
461            $this->assertTrue(
462                Image_Transform_Helper::exactlySameFile(
463                    $filename1, $filename2
464                ),
465                'Images are not the same (' . $filename1 . ' and ' . $filename2 . ')'
466            );
467        } else {
468            $this->assertTrue(
469                Image_Transform_Helper::nearlySameFile(
470                    $filename1, $filename2, $this->nMaxAverageDiff
471                ),
472                'Images are not the same (' . $filename1 . ' and ' . $filename2 . ')'
473            );
474        }
475    }//protected function assertExactlySameImage(..)
476}
477?>