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(); ?>