<?php

$_SETTINGS['site_name'] = 'expo site';
$_SETTINGS['path'] = '/home/bosko/expo/';
$_SETTINGS['url'] = '/';
$_SETTINGS['db'] = 'expo.db';

require("../models/model.php");
require("../models/image-model.php");

class AnonStub {
  public function fetchAll() {
    return Array(
      'image_filename' => 'someFile.jpg',
      'image_numer' => '10',
      'gallery_name' => 'somegallery',
      'image_caption' => 'some caption',
      'image_enabled' => '1',
    );
  }
}

class ViewStub {

  static $lastFunctionCalled = '';

  public function query($q) {
    return new AnonStub();
  }

  public function renderError() {
    self::$lastFunctionCalled = 'renderError';  
  }

  public function render() {
    self::$lastFunctionCalled = 'render';
  }
}

class ImageViewStub extends ViewStub {

  public function renderImage() {

  }

}

class PDOStatementStub {
  public function fetchAll() {
    return Array(
      Array(
        'image_filename' => 'somefile.jpg',
        'image_number' => '69',
        'gallery_name' => 'women',
        'image_caption' => 'some caption',
        'image_enabled' => 'true',
      ),
    );
  }
}

class ImageModelDefanged extends ImageModel {
  public function query($q) {
    return new PDOStatementStub();
  }
}

class ImageModelTest {
/*
  public function testLoadImageQuery() {
    
    # stub for ImageView
    $im = new ImageModel(new ImageViewStub());
    
    $q = $im->loadImageQuery(2, 69);

    if($q == 'SELECT i.filename AS image_filename, i.num AS image_number, ' . 
      'i.caption AS image_caption, i.enabled AS image_enabled, g.name AS ' .
      'gallery_name, g.id AS gallery_id FROM images i INNER JOIN gallery g ON' .
      ' i.gid = g.id WHERE i.num = ' . 69 . ' AND i.gid = ' . 2 . ' LIMIT(1)') {
      return true;
    } else {
      return false;
    }

  }

  public function testNewImageQuery() {
    $im = new ImageModel(new ImageViewStub());

    $q = $im->newImageQuery(
      Array(
        'filename' => 'someFile.jpg',
        'caption' => 'some caption',
        'gid' => 69,
      )
    );

    if($q === 'INSERT INTO images (num, filename, caption, gid) VALUES ' . 
      '((SELECT COALESCE ((SELECT MAX(num) +1 FROM images WHERE gid = 69), ' .
        '1)), "someFile.jpg", "some caption", 69)') {
      return true;
    } else {
      return false;
    }
  }
*/
  public function loadImageTest() {
    
    $ivs = new ImageViewStub();
    $im = new ImageModel($ivs);

    $im->loadImage(null, 1);
    if(ImageViewStub::$lastFunctionCalled === 'renderError') {
      return true;
    } else {
      return false;
    }

    $im = new ImageModelDefanged(new ImageViewStub()); 
    $im->loadImage(1, 10);
    if(ImageViewStub::$lastFunctionCalled === 'render') {
      return True;
    } else {
      return false; 
    }
    
  }


  private function testNewImage() {
    
    $av = new ImageViewStub();
    $im = new ImageModel($av);

    $image = Array(
      'filename' => 'someFile.png',
      'caption' => 'our test image',
      'gid' => 5,
    );

    $image = $im->newImage($image);

    if(intval($image['id']) >= 1) {
      return true;
    } else {
      return false;
    }

  }

  public function go() {

    /*print("Testing ImageModel->loadImageQuery(): ");
    if($this->testLoadImageQuery()) {
      print("PASSED\n");
    } else {
      print("FAILED\n");
      exit();
    }

    print("Testing ImageModel->newImageQuery():");
    if($this->testNewImageQuery()) {
      print("PASSED\n");
    } else {
      print("FAILED\n");
      //exit();
    }*/

    print("Testing ImageModel->LoadImage(): ");
    if($this->loadImageTest()) {
      print("PASSED\n");
    } else {
      print("FAILED\n");
      exit();
    }

    print("Testing ImageModel->newImage(): ");
    if($this->testNewImage()) {
      print("PASSED\n");
    } else {
      print("FAILED\n");
      exit();
    }


    print("All tests for ImageModel successful\n");

  }

}

$imt = new ImageModelTest();
$imt->go();
