Advertisement
Guest User

Stephen Melrose

a guest
Jan 21st, 2010
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.20 KB | None | 0 0
  1. public function testSetBackgroundColor()
  2. {
  3.     // Get the test image
  4.     $image = $this->getTestImage();
  5.  
  6.     // Validate default background color
  7.     $this->assertEquals(NULL, $image->getBackgroundColor());
  8.  
  9.     // Test invalid background colors
  10.     $invalidBackgroundColors = array(
  11.         NULL,
  12.         true,
  13.         false,
  14.         array(),
  15.         '',
  16.         'red',
  17.         'green',
  18.         '255,255,255',
  19.         12
  20.     );
  21.     foreach($invalidBackgroundColors as $invalidBackgroundColor)
  22.     {
  23.         try
  24.         {
  25.             $image->setBackgroundColor($invalidBackgroundColor);
  26.         }
  27.         catch(SM_Image_Exception $e) { continue; }
  28.  
  29.         $this->fail('An invalid background color did not throw an expected SM_Image_Exception.');
  30.     }
  31.  
  32.     // Set the background color (SM_Image_Exception would be thrown if error)
  33.     $image->setBackgroundColor(array(100,150,200));
  34.  
  35.     // Validate the background color was set
  36.     $backgroundColor = $image->getBackgroundColor();
  37.     $this->assertType('array', $backgroundColor);
  38.     $this->assertArrayHasKey('red', $backgroundColor);
  39.     $this->assertArrayHasKey('green', $backgroundColor);
  40.     $this->assertArrayHasKey('blue', $backgroundColor);
  41.     $this->assertEquals(100, $backgroundColor['red']);
  42.     $this->assertEquals(150, $backgroundColor['green']);
  43.     $this->assertEquals(200, $backgroundColor['blue']);
  44.  
  45.     // Try and set the background color again, fail as you shouldn't be able to
  46.     try
  47.     {
  48.         $image->setBackgroundColor(array(150,50,120));
  49.     }
  50.     catch (SM_Image_Exception $e2) {}
  51.     if (isset($e2))
  52.     {
  53.         $this->fail('Trying to set the background color again did not throw an expected SM_Image_Exception (1).');
  54.     }
  55.  
  56.     // Clean-up
  57.     unset($image);
  58.  
  59.     // Get the test image
  60.     $image = $this->getTestImage(IMAGETYPE_PNG);
  61.  
  62.     // Set the background color to transparent
  63.     $image->setBackgroundColor(IMG_COLOR_TRANSPARENT);
  64.  
  65.     // Validate the background color was set
  66.     $this->assertEquals(IMG_COLOR_TRANSPARENT, $image->getBackgroundColor());
  67.  
  68.     // Try and set the background color again, fail as you shouldn't be able to
  69.     try
  70.     {
  71.         $image->setBackgroundColor(IMG_COLOR_TRANSPARENT);
  72.     }
  73.     catch (SM_Image_Exception $e3) {}
  74.     if (isset($e3))
  75.     {
  76.         $this->fail('Trying to set the background color again did not throw an expected SM_Image_Exception (2).');
  77.     }
  78.  
  79.     // Clean-up
  80.     unset($image);
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement