@@ -58,4 +58,49 @@ public function testHasOne()
5858 $ this ->assertEquals ('admin ' , $ role ->type );
5959 }
6060
61+ public function testWithBelongsTo ()
62+ {
63+ $ user = User::create (array ('name ' => 'John Doe ' ));
64+ Item::create (array ('type ' => 'knife ' , 'user_id ' => $ user ->_id ));
65+ Item::create (array ('type ' => 'shield ' , 'user_id ' => $ user ->_id ));
66+ Item::create (array ('type ' => 'sword ' , 'user_id ' => $ user ->_id ));
67+ Item::create (array ('type ' => 'bag ' , 'user_id ' => null ));
68+
69+ $ items = Item::with ('user ' )->get ();
70+
71+ $ user = $ items [0 ]->getRelation ('user ' );
72+ $ this ->assertInstanceOf ('User ' , $ user );
73+ $ this ->assertEquals ('John Doe ' , $ user ->name );
74+ $ this ->assertEquals (1 , count ($ items [0 ]->getRelations ()));
75+ $ this ->assertEquals (null , $ items [3 ]->getRelation ('user ' ));
76+ }
77+
78+ public function testWithHashMany ()
79+ {
80+ $ user = User::create (array ('name ' => 'John Doe ' ));
81+ Item::create (array ('type ' => 'knife ' , 'user_id ' => $ user ->_id ));
82+ Item::create (array ('type ' => 'shield ' , 'user_id ' => $ user ->_id ));
83+ Item::create (array ('type ' => 'sword ' , 'user_id ' => $ user ->_id ));
84+ Item::create (array ('type ' => 'bag ' , 'user_id ' => null ));
85+
86+ $ user = User::with ('items ' )->find ($ user ->_id );
87+
88+ $ items = $ user ->getRelation ('items ' );
89+ $ this ->assertEquals (3 , count ($ items ));
90+ $ this ->assertInstanceOf ('Item ' , $ items [0 ]);
91+ }
92+
93+ public function testWithHasOne ()
94+ {
95+ $ user = User::create (array ('name ' => 'John Doe ' ));
96+ Role::create (array ('type ' => 'admin ' , 'user_id ' => $ user ->_id ));
97+ Role::create (array ('type ' => 'guest ' , 'user_id ' => $ user ->_id ));
98+
99+ $ user = User::with ('role ' )->find ($ user ->_id );
100+
101+ $ role = $ user ->getRelation ('role ' );
102+ $ this ->assertInstanceOf ('Role ' , $ role );
103+ $ this ->assertEquals ('admin ' , $ role ->type );
104+ }
105+
61106}
0 commit comments