<?php

require_once './fundRaisingImageCreator.php';
$fundRaisingImage = new fundRaisingImageCreator();
$fundRaisingImage->setFontLocation('./fonts/arial_black.ttf');
$imagesLocation = './images/';
$availableImagesToUse = array('heart' => 'heart.png', 'hour_glass' => 'hour_glass.png', 'heart_for_underlay' => 'heart_for_underlay.png', 'thermometer_for_underlay' => 'thermometer_for_underlay.png');
// The images we have available for the user to choose from
if (array_key_exists('raised_amount', $_REQUEST)) {
    $fundRaisingImage->setRaisedAmount($_REQUEST['raised_amount']);
}
if (array_key_exists('goal_amount', $_REQUEST)) {
    $fundRaisingImage->setGoalAmount($_REQUEST['goal_amount']);
}
if (array_key_exists('image_to_use', $_REQUEST) && array_key_exists($_REQUEST['image_to_use'], $availableImagesToUse)) {
    // If the user has given us a choice of image, and that image is a valid choice, we use their chosen image
    $fundRaisingImage->setFundRaisingImage($imagesLocation . $availableImagesToUse[$_REQUEST['image_to_use']]);
} else {
    // Otherwise we'll use the heart by default
    $fundRaisingImage->setFundRaisingImage($imagesLocation . $availableImagesToUse['heart']);
}
if (array_key_exists('border_width', $_REQUEST)) {
    $fundRaisingImage->setBorderWidth($_REQUEST['border_width']);
}
if (array_key_exists('border_color', $_REQUEST)) {
    $fundRaisingImage->setBorderColor($_REQUEST['border_color']);
}
if (array_key_exists('goal_indicator_color', $_REQUEST)) {
    $fundRaisingImage->setGoalIndicatorColor($_REQUEST['goal_indicator_color']);
}
if (array_key_exists('goal_indicator_opacity', $_REQUEST)) {
 /**
  * This method will test that image caching works correctly
  * 
  * @covers fundRaisingImageCreator::displayFundRaisingImage
  * @covers fundRaisingImageCreator::getImageLoadedFromCache
  * @covers fundRaisingImageCreator::_getCachedImageLocation
  * @covers fundRaisingImageCreator::_buildCachedImageName
  * 
  */
 public function testImageCaching()
 {
     $fundRaisingImageCreator = new fundRaisingImageCreator();
     $fundRaisingImageCreator->setFundRaisingImage("./images/heart.png");
     $fundRaisingImageCreator->setFontLocation("./fonts/arial_black.ttf");
     $fundRaisingImageCreator->setImageCacheLocation("./image_cache/");
     $fundRaisingImageCreator->setRaisedAmount(300);
     $fundRaisingImageCreator->setGoalAmount(250);
     $fundRaisingImageCreator->setBorderWidth(1);
     $fundRaisingImageCreator->setBorderColor("black");
     $fundRaisingImageCreator->setGoalIndicatorColor("white");
     $fundRaisingImageCreator->setGoalIndicatorOpacity(80);
     $fundRaisingImageCreator->setGoalIndicatorType("overlay");
     $fundRaisingImageCreator->clearErrors();
     // Any errors that occur when using the above methods have been checked for in other test methods
     // Case 1: Image isn't cached, we call displayFundRaisingImage to generate/cache it, we call displayFundRaisingImage again to load from cache
     $pathOfImageWhenCached = "./image_cache/heart100overlay255255255801000.png";
     if (file_exists($pathOfImageWhenCached)) {
         // If this test has previously failed, our generated image wouldn't have been deleted
         unlink($pathOfImageWhenCached);
     }
     ob_start();
     $fundRaisingImageCreator->displayFundRaisingImage();
     // We don't really need the created image, we just don't want any errors, hence the ob_start and ob_end_clean
     ob_end_clean();
     $this->assertTrue(file_exists($pathOfImageWhenCached), "testImageCaching case 1: Image was not cached and should have been.");
     ob_start();
     $fundRaisingImageCreator->displayFundRaisingImage();
     // Call displayFundRaisingImage a second time to load from cache
     // We don't really need the created image, we just don't want any errors, hence the ob_start and ob_end_clean
     ob_end_clean();
     $this->assertTrue($fundRaisingImageCreator->getImageLoadedFromCache(), "testImageCaching case 1: Image was not loaded from cache and should have been.");
     unlink($pathOfImageWhenCached);
     // Delete our cached image
     // Case 2: We change options so we don't include border options in the file name, and it will cause us to calculate a percent of goal complete
     $pathOfImageWhenCached = "./image_cache/heart80overlay255255255801000.png";
     $fundRaisingImageCreator->setRaisedAmount(200);
     $fundRaisingImageCreator->setBorderWidth(1);
     if (file_exists($pathOfImageWhenCached)) {
         // If this test has previously failed, our generated image wouldn't have been deleted
         unlink($pathOfImageWhenCached);
     }
     ob_start();
     $fundRaisingImageCreator->displayFundRaisingImage();
     // We don't really need the created image, we just don't want any errors, hence the ob_start and ob_end_clean
     ob_end_clean();
     $this->assertTrue(file_exists($pathOfImageWhenCached), "testImageCaching case 2: Image was not cached and should have been.");
     unlink($pathOfImageWhenCached);
     // Delete our cached image
     // Case 3: We change our raised amount to 0, which will remove goal indicator options from the file name
     $pathOfImageWhenCached = "./image_cache/heart01000.png";
     $fundRaisingImageCreator->setRaisedAmount(0);
     if (file_exists($pathOfImageWhenCached)) {
         // If this test has previously failed, our generated image wouldn't have been deleted
         unlink($pathOfImageWhenCached);
     }
     ob_start();
     $fundRaisingImageCreator->displayFundRaisingImage();
     // We don't really need the created image, we just don't want any errors, hence the ob_start and ob_end_clean
     ob_end_clean();
     $this->assertTrue(file_exists($pathOfImageWhenCached), "testImageCaching case 3: Image was not cached and should have been.");
     unlink($pathOfImageWhenCached);
     // Delete our cached image
 }