<?php

//-------------------------------------------------------------------
// ToyModSerTests.php: test cases for ToyModSer
//-------------------------------------------------------------------
// Copyright (c) 2007 Patrick Mueller
// Using the MIT license: 
//    http://www.opensource.org/licenses/mit-license.php
//-------------------------------------------------------------------
// 2007/05 - Patrick Mueller - pmuellr@yahoo.com
//-------------------------------------------------------------------

require_once 'PHPUnit/Framework.php';
require_once 
'PHPUnit/TextUI/TestRunner.php';

require_once 
'ToyModSer.php';
require_once 
'ToyModels.php';

// running from eclipse, with the PDT, the current directory
// is always set to the directory of the php executable, but
// I need to make the current directory the directory the file
// lives in to get local includes working correctly.

$thisDir dirname(__FILE__);
chdir($thisDir);

//-------------------------------------------------------------------
// All of our tests live in this 'test case' class
//-------------------------------------------------------------------
class ToyModSerTests extends PHPUnit_Framework_TestCase {

    private 
$serBook;
    private 
$serAuthor;

    
//---------------------------------------------------------------
    // main entrypoint for the class; set the vars and run the tests
    //---------------------------------------------------------------
    
public static function main() {
        
PHPUnit_TextUI_TestRunner::run(self::suite());
    }
    
    
//---------------------------------------------------------------
    // returns the tests in this class
    //---------------------------------------------------------------
    
public static function suite() {
        
$suite = new PHPUnit_Framework_TestSuite();
        
        
$suite->addTestSuite(__CLASS__);
 
        return 
$suite;
    }
    
    
//---------------------------------------------------------------
    // initial setup run before each test
    //---------------------------------------------------------------
    
public function setUp() {
        
        
$this->serBook   = new ToyModSer('Book');
        
$this->serAuthor = new ToyModSer('Author');
    }
    
    
//---------------------------------------------------------------
    // initial setup run before each test
    //---------------------------------------------------------------
    
public function tearDown() {
    }
    
    
//---------------------------------------------------------------
    // test a book
    //---------------------------------------------------------------
    
public function testBook() {
        
$book $this->createBook();
        
        
$json  $this->serBook->toJSON($book);
        
$book2 $this->serBook->fromJSON($json);
        
        
$jsonExpected '{"title":"Breakfast of Champions","author":"Kurt Vonnegut","yearPublished":1973}';
        
        
$this->assertEquals($book$book2);
        
$this->assertEquals($jsonExpected$json);
    }
    
    
//---------------------------------------------------------------
    // test an author
    //---------------------------------------------------------------
    
public function testAuthor() {
        
$author $this->createAuthor();
        
        
$json    $this->serAuthor->toJSON($author);
        
$author2 $this->serAuthor->fromJSON($json);

        
$jsonExpected '{"name":"Kurt Vonnegut","yearBorn":1922}';
        
        
$this->assertEquals($author$author2);
        
$this->assertEquals($jsonExpected$json);
    }

    
//---------------------------------------------------------------
    // test for wrong serializer
    //---------------------------------------------------------------
    
public function testWrongSerializer() {
        
$book $this->createBook();
        
        
$passed false;
        try {
            
$json $this->serAuthor->toJSON($book);
        }
        catch (
Exception $e) {
            
$passed true;
            
$msg "attempting to serialize instance of 'Book' with a serializer for 'Author'";
            
$this->assertEquals($msg$e->getMessage());
        }
        
        
$this->assertTrue($passed);
    }        
    
    
//---------------------------------------------------------------
    // test for bogus model
    //---------------------------------------------------------------
    
public function testBogusModel() {
        
$passed false;
        try {
            
$serBogusModel = new ToyModSer('BogusModel');
        }
        catch (
Exception $e) {
            
$passed true;
            
$msg "invalid type for property 'bar': 'foo'";
            
$this->assertEquals($msg$e->getMessage());
        }
        
        
$this->assertTrue($passed);
    }

    
//---------------------------------------------------------------
    // test for invalid value
    //---------------------------------------------------------------
    
public function testInvalidValue() {
        
$book $this->createBook();
        
$book->title 0;
        
        
$passed false;
        try {
            
$json $this->serBook->toJSON($book);
        }
        catch (
Exception $e) {
            
$passed true;
            
$msg ="property 'title' is not the expected type";
            
$this->assertEquals($msg$e->getMessage());
        }
        
        
$this->assertTrue($passed);
    }
    
    
//---------------------------------------------------------------
    // test for invalid JSON value
    //---------------------------------------------------------------
    
public function testInvalidJSONValue() {
        
$json '{"title":42,"author":"Kurt Vonnegut","yearPublished":1973}';

        
$passed false;
        try {
            
$book $this->serBook->fromJSON($json);
        }
        catch (
Exception $e) {
            
$passed true;
            
$msg ="property 'title' is not the expected type";
            
$this->assertEquals($msg$e->getMessage());
        }
        
        
$this->assertTrue($passed);
    }
    
    
//---------------------------------------------------------------
    // create a new book
    //---------------------------------------------------------------
    
private function createBook() {
        
$book = new Book();
        
$book->title         "Breakfast of Champions";
        
$book->author        "Kurt Vonnegut";
        
$book->yearPublished 1973;
        
        return 
$book;
    }        
    
    
//---------------------------------------------------------------
    // create a new author
    //---------------------------------------------------------------
    
private function createAuthor() {
        
$author = new Author();
        
$author->name     "Kurt Vonnegut";
        
$author->yearBorn 1922;
        
        return 
$author;
    }        
    
}

//-------------------------------------------------------------------
// run the tests
//-------------------------------------------------------------------
ToyModSerTests::main();
?>