public function testToArrayDeep() { $author = new Author(); $author->setId(5678); $author->setFirstName('George'); $author->setLastName('Byron'); $book = new Book(); $book->setId(9012); $book->setTitle('Don Juan'); $book->setISBN('0140422161'); $book->setPrice(12.99); $book->setAuthor($author); $coll = new ArrayCollection(); $coll->setModel('Propel\\Tests\\Bookstore\\Book'); $coll[] = $book->toArray(TableMap::TYPE_PHPNAME, true, array(), true); $expected = array(array('Id' => 9012, 'Title' => 'Don Juan', 'ISBN' => '0140422161', 'Price' => 12.99, 'PublisherId' => null, 'AuthorId' => 5678, 'Author' => array('Id' => 5678, 'FirstName' => 'George', 'LastName' => 'Byron', 'Email' => null, 'Age' => null, 'Books' => array('Book_0' => '*RECURSION*')))); $this->assertEquals($expected, $coll->toArray()); }
/** * Tests the Base[Object]::toArray() method */ public function testToArray() { $types = array(BasePeer::TYPE_PHPNAME, BasePeer::TYPE_STUDLYPHPNAME, BasePeer::TYPE_COLNAME, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_NUM); $book = new Book(); $book->fromArray(array('Title' => 'Harry Potter and the Order of the Phoenix', 'ISBN' => '043935806X')); $expecteds = array(BasePeer::TYPE_PHPNAME => array('Title' => 'Harry Potter and the Order of the Phoenix', 'ISBN' => '043935806X'), BasePeer::TYPE_STUDLYPHPNAME => array('title' => 'Harry Potter and the Order of the Phoenix', 'iSBN' => '043935806X'), BasePeer::TYPE_COLNAME => array('book.TITLE' => 'Harry Potter and the Order of the Phoenix', 'book.ISBN' => '043935806X'), BasePeer::TYPE_FIELDNAME => array('title' => 'Harry Potter and the Order of the Phoenix', 'isbn' => '043935806X'), BasePeer::TYPE_NUM => array('1' => 'Harry Potter and the Order of the Phoenix', '2' => '043935806X')); foreach ($types as $type) { $expected = $expecteds[$type]; $result = $book->toArray($type); // remove ID since its autoincremented at each test iteration $result = array_slice($result, 1, 2, true); $this->assertEquals($expected, $result, 'expected was: ' . print_r($expected, 1) . 'but toArray() returned ' . print_r($result, 1)); } }
public function testToArrayKeyType() { $b = new Book(); $b->setTitle('Don Juan'); $arr1 = $b->toArray(TableMap::TYPE_COLNAME); $expectedKeys = array(BookTableMap::ID, BookTableMap::TITLE, BookTableMap::ISBN, BookTableMap::PRICE, BookTableMap::PUBLISHER_ID, BookTableMap::AUTHOR_ID); $this->assertEquals($expectedKeys, array_keys($arr1), 'toArray() accepts a $keyType parameter to change the result keys'); $this->assertEquals('Don Juan', $arr1[BookTableMap::TITLE], 'toArray() returns an associative array representation of the object'); }
public function testToArrayWithForeignObjects() { $types = [TableMap::TYPE_PHPNAME, TableMap::TYPE_CAMELNAME, TableMap::TYPE_COLNAME, TableMap::TYPE_FIELDNAME, TableMap::TYPE_NUM]; $review = new Review(); $review->setRecommended(true)->setReviewedBy('Someone')->setReviewDate(null); $book = new Book(); $book->setTitle('Harry Potter and the Order of the Phoenix')->setISBN('043935806X')->addReview($review)->setPrice(10); $expecteds = array(TableMap::TYPE_PHPNAME => ['Id' => null, 'Title' => 'Harry Potter and the Order of the Phoenix', 'ISBN' => '043935806X', 'Price' => 10.0, 'PublisherId' => null, 'AuthorId' => null, 'Reviews' => [['Id' => null, 'ReviewedBy' => 'Someone', 'ReviewDate' => null, 'Recommended' => true, 'Status' => null, 'BookId' => null, 'Book' => '*RECURSION*']]], TableMap::TYPE_CAMELNAME => array('id' => null, 'title' => 'Harry Potter and the Order of the Phoenix', 'iSBN' => '043935806X', 'price' => 10.0, 'publisherId' => null, 'authorId' => null, 'reviews' => [['id' => null, 'reviewedBy' => 'Someone', 'reviewDate' => null, 'recommended' => true, 'status' => null, 'bookId' => null, 'book' => '*RECURSION*']]), TableMap::TYPE_COLNAME => array('book.ID' => null, 'book.TITLE' => 'Harry Potter and the Order of the Phoenix', 'book.ISBN' => '043935806X', 'book.PRICE' => 10.0, 'book.PUBLISHER_ID' => null, 'book.AUTHOR_ID' => null, 'Reviews' => [['review.ID' => null, 'review.REVIEWED_BY' => 'Someone', 'review.REVIEW_DATE' => null, 'review.RECOMMENDED' => true, 'review.STATUS' => null, 'review.BOOK_ID' => null, 'Book' => '*RECURSION*']]), TableMap::TYPE_FIELDNAME => array('id' => null, 'title' => 'Harry Potter and the Order of the Phoenix', 'isbn' => '043935806X', 'price' => 10.0, 'publisher_id' => null, 'author_id' => null, 'reviews' => [['id' => null, 'reviewed_by' => 'Someone', 'review_date' => null, 'recommended' => true, 'status' => null, 'book_id' => null, 'book' => '*RECURSION*']]), TableMap::TYPE_NUM => array('0' => null, '1' => 'Harry Potter and the Order of the Phoenix', '2' => '043935806X', '3' => 10.0, '4' => null, '5' => null, 'Reviews' => [['0' => null, '1' => 'Someone', '2' => null, '3' => 1, '4' => null, '5' => null, 'Book' => '*RECURSION*']])); foreach ($types as $type) { $expected = $expecteds[$type]; $result = $book->toArray($type, true, [], true); $this->assertEquals($expected, $result, 'expected was: ' . print_r($expected, 1) . 'but toArray() returned ' . print_r($result, 1)); } }