CWIS Developer Documentation
Image--Test.php
Go to the documentation of this file.
1 <?PHP
2 class Image_Test extends PHPUnit\Framework\TestCase
3 {
4  protected static $TempDir;
8  static function setUpBeforeClass()
9  {
10  self::$TempDir = "tmp/Image--Test-".getmypid();
11  mkdir(self::$TempDir);
12  }
13 
17  static function tearDownAfterClass()
18  {
19  $Files = glob(self::$TempDir."/*");
20  foreach ($Files as $File)
21  {
22  if (is_file($File))
23  {
24  unlink($File);
25  }
26  }
27  rmdir(self::$TempDir);
28  }
29 
30  function testAll()
31  {
32  # construct Image with invalid file
33  $BadImage = new Image("abc123");
34  $this->assertEquals($BadImage->Status(), AI_FILEUNREADABLE);
35 
36  # Test SaveAs() and Status() with no file changes
37  $ImageJpg = new Image("lib/ScoutLib/tests/files/TestImage--600x400.jpg");
38  $ImageJpg->SaveAs(self::$TempDir."/TestImage2--600x400.jpg");
39  $this->assertEquals($ImageJpg->Status(), AI_OKAY, "Testing SaveAs() for JPG");
40 
41  # Test Xsize() and Ysize() of 600x400 JPG image.
42  $this->assertEquals($ImageJpg->XSize(), 600,
43  "Testing Xsize() of 600px width image");
44  $this->assertEquals($ImageJpg->YSize(), 400,
45  "Testing Ysize() of 400px height image");
46 
47  # Test ScaleTo() without maintaining aspect ratio and converting
48  # image to gif
49  $ImageJpg->ScaleTo(100, 200);
50  $ImageJpg->SaveAs(self::$TempDir."/TestImage--100x200.gif",
52 
53  $ImageGif = new Image(self::$TempDir."/TestImage--100x200.gif");
54  $this->assertEquals($ImageGif->Status(), AI_OKAY, "Testing SaveAs() for GIF");
55  $this->assertEquals($ImageGif->XSize(), 100,
56  "Testing ScaleTo() of 100px width image");
57  $this->assertEquals($ImageGif->YSize(), 200,
58  "Testing ScaleTo() of 200px height image");
59 
60  # Test ScaleTo() with maintaining aspect ratio and converting image to png
61  # Because the aspect ratio is maintained, we should expect a 200x400 image.
62  $ImageGif->ScaleTo(200, 300, TRUE);
63  $ImageGif->SaveAs(self::$TempDir."/TestImage--200x400.png",
65 
66  $ImagePng = new Image(self::$TempDir."/TestImage--200x400.png");
67  $this->assertEquals($ImagePng->Status(), AI_OKAY, "Testing SaveAs() for PNG");
68  $this->assertEquals($ImagePng->XSize(), 200,
69  "Testing ScaleTo() of 200px width image, with aspect ratio maintained");
70  $this->assertEquals($ImagePng->YSize(), 400,
71  "Testing ScaleTo() of 400px height image, with aspect ratio maintained");
72 
73  # Test CropTo() and converting image to bmp
74  $ImagePng->CropTo(100, 150);
75  $ImagePng->SaveAs(self::$TempDir."/TestImage--100x150.bmp");
76 
77  $ImageBmp = new Image(self::$TempDir."/TestImage--100x150.bmp");
78  $this->assertEquals($ImageBmp->Status(), AI_OKAY, "Testing SaveAs() for BMP");
79  $this->assertEquals($ImageBmp->XSize(), 100,
80  "Testing CropTo() of 100px width image");
81  $this->assertEquals($ImageBmp->YSize(), 150,
82  "Testing CropTo() of 150px height image");
83 
84  # Test Type()
85  $this->assertEquals($ImageJpg->Type(), Image::IMGTYPE_JPEG, "Testing Type()");
86  $this->assertEquals($ImageJpg->Type(self::$TempDir."/TestImage2--600x400.jpg"),
87  Image::IMGTYPE_JPEG, "Testing Type() with 'TestImage2--600x400.jpg'");
88  $this->assertEquals($ImageJpg->Type(self::$TempDir."/TestImage--100x200.gif"),
89  Image::IMGTYPE_GIF, "Testing Type() with 'TestImage--100x200.gif'");
90  $this->assertEquals($ImageJpg->Type(self::$TempDir."/TestImage--100x150.bmp"),
91  Image::IMGTYPE_BMP, "Testing Type() with 'TestImage--100x150.bmp'");
92  $this->assertEquals($ImageJpg->Type(self::$TempDir."/TestImage--200x400.png"),
93  Image::IMGTYPE_PNG, "Testing Type() with 'TestImage--200x400.png'");
94  $this->assertEquals($ImageJpg->Type("FakeFile.xyz"), Image::IMGTYPE_UNKNOWN,
95  "Testing Type() with 'FakeFile.xyz'");
96 
97  # Test MimeType()
98  $this->assertEquals($ImageJpg->Mimetype(), "image/jpeg",
99  "Testing Mimetype() for jpg image");
100  $this->assertEquals($ImageGif->Mimetype(), "image/gif",
101  "Testing Mimetype() for gif image");
102  $this->assertEquals($ImageBmp->Mimetype(), "image/bmp",
103  "Testing Mimetype() for bmp image");
104  $this->assertEquals($ImagePng->Mimetype(), "image/png",
105  "Testing Mimetype() for png image");
106 
107  # Test Extension()
108  $this->assertEquals($ImageJpg->Extension(), "jpg", "Testing Extension()");
109 
110  # Test ExtensionForType()
111  $this->assertEquals(Image::ExtensionForType(Image::IMGTYPE_JPEG), "jpg",
112  "Testing ExtensionForType() with jpg");
113  $this->assertEquals(Image::ExtensionForType(Image::IMGTYPE_GIF), "gif",
114  "Testing ExtensionForType() with gif");
115  $this->assertEquals(Image::ExtensionForType(Image::IMGTYPE_BMP), "bmp",
116  "Testing ExtensionForType() with bmp");
117  $this->assertEquals(Image::ExtensionForType(Image::IMGTYPE_PNG), "png",
118  "Testing ExtensionForType() with png");
119  $this->assertEquals(Image::ExtensionForType(123), NULL,
120  "Testing ExtensionForType() with 123");
121 
122  # Test JpegQuality()
123  $this->assertEquals($ImageJpg->JpegQuality(50), 50,
124  "Testing JpegQuality() with 80");
125 
126  # Test SupportedFormats()
127  # we assume that jpeg, gif, bmp, and png images will all be supported
128  $this->assertEquals(Image::SupportedFormats(), Image::IMGTYPE_JPEG |
130  "Testing SupportedFormats()");
131 
132  # Test SupportedFormatNames()
133  # we assume that jpeg, gif, bmp, and png images will all be supported
134  $this->assertContains("JPG", Image::SupportedFormatNames(),
135  "Testing SupportedFormatNames contains 'JPG'");
136  $this->assertContains("GIF", Image::SupportedFormatNames(),
137  "Testing SupportedFormatNames contains 'GIF'");
138  $this->assertContains("BMP", Image::SupportedFormatNames(),
139  "Testing SupportedFormatNames contains 'BMP'");
140  $this->assertContains("PNG", Image::SupportedFormatNames(),
141  "Testing SupportedFormatNames contains 'PNG'");
142 
143  # Test SaveAs() error checking
144  $ImageJpg->SaveAs(self::$TempDir."/TestImage--100x200.gif",
145  "xyz");
146  $this->assertEquals($ImageJpg->Status(), AI_UNSUPPORTEDFORMAT,
147  "Testing SaveAs() with AI_UNSUPPORTEDFORMAT");
148  }
149 }
static SupportedFormatNames()
Definition: Image.php:437
const AI_OKAY
Definition: Image.php:551
static $TempDir
Definition: Image--Test.php:4
static tearDownAfterClass()
Destroy sandbox folder and contents.
Definition: Image--Test.php:17
const IMGTYPE_GIF
Definition: Image.php:23
static setUpBeforeClass()
Create sandbox folder for storing new images.
Definition: Image--Test.php:8
static ExtensionForType($Type)
return the file name extension for the image, given a type.
Definition: Image.php:379
const IMGTYPE_UNKNOWN
Definition: Image.php:21
const AI_FILEUNREADABLE
Definition: Image.php:552
const IMGTYPE_PNG
Definition: Image.php:25
const IMGTYPE_JPEG
Definition: Image.php:22
Definition: Image.php:15
const IMGTYPE_BMP
Definition: Image.php:24
static SupportedFormats()
Definition: Image.php:399
const AI_UNSUPPORTEDFORMAT
Definition: Image.php:557