33class RelationsTest extends PHPUnit_Framework_TestCase {
44
55 public function setUp () {
6+ User::truncate ();
7+ Book::truncate ();
8+ Item::truncate ();
9+ Role::truncate ();
10+ Client::truncate ();
611 }
712
813 public function tearDown ()
@@ -11,8 +16,9 @@ public function tearDown()
1116 Book::truncate ();
1217 Item::truncate ();
1318 Role::truncate ();
19+ Client::truncate ();
1420 }
15-
21+
1622 public function testHasMany ()
1723 {
1824 $ author = User::create (array ('name ' => 'George R. R. Martin ' ));
@@ -101,28 +107,90 @@ public function testWithHasOne()
101107 $ this ->assertInstanceOf ('Role ' , $ role );
102108 $ this ->assertEquals ('admin ' , $ role ->type );
103109 }
104-
105- public function testEasyRelation ()
110+
111+ public function testHasManyAndBelongsTo ()
106112 {
107- // Has Many
108- $ user = User::create (array ('name ' => 'John Doe ' ));
109- $ item = Item::create (array ('type ' => 'knife ' ));
110- $ user ->items ()->save ($ item );
111-
112- $ user = User::find ($ user ->_id );
113- $ items = $ user ->items ;
114- $ this ->assertEquals (1 , count ($ items ));
115- $ this ->assertInstanceOf ('Item ' , $ items [0 ]);
116-
117- // Has one
118113 $ user = User::create (array ('name ' => 'John Doe ' ));
119- $ role = Role::create (array ('type ' => 'admin ' ));
120- $ user ->role ()->save ($ role );
121-
122- $ user = User::find ($ user ->_id );
123- $ role = $ user ->role ;
124- $ this ->assertInstanceOf ('Role ' , $ role );
125- $ this ->assertEquals ('admin ' , $ role ->type );
114+
115+ $ user ->clients ()->save (new Client (array ('name ' => 'Pork Pies Ltd. ' )));
116+ $ user ->clients ()->create (array ('name ' => 'Buffet Bar Inc. ' ));
117+
118+ $ user = User::with ('clients ' )->find ($ user ->_id );
119+
120+ $ client = Client::with ('users ' )->first ();
121+
122+ $ clients = $ client ->getRelation ('users ' );
123+ $ users = $ user ->getRelation ('clients ' );
124+
125+ $ this ->assertInstanceOf ('Illuminate\Database\Eloquent\Collection ' , $ users );
126+ $ this ->assertInstanceOf ('Illuminate\Database\Eloquent\Collection ' , $ clients );
127+ $ this ->assertInstanceOf ('Client ' , $ users [0 ]);
128+ $ this ->assertInstanceOf ('User ' , $ clients [0 ]);
129+ $ this ->assertCount (2 , $ user ->clients );
130+ $ this ->assertCount (1 , $ client ->users );
131+
132+ // Now create a new user to an existing client
133+ $ client ->users ()->create (array ('name ' => 'Jane Doe ' ));
134+
135+ $ otherClient = User::where ('name ' , '= ' , 'Jane Doe ' )->first ()->clients ()->get ();
136+
137+ $ this ->assertInstanceOf ('Illuminate\Database\Eloquent\Collection ' , $ otherClient );
138+ $ this ->assertInstanceOf ('Client ' , $ otherClient [0 ]);
139+ $ this ->assertCount (1 , $ otherClient );
140+
141+ // Now attach an existing client to an existing user
142+ $ user = User::where ('name ' , '= ' , 'Jane Doe ' )->first ();
143+ $ client = Client::Where ('name ' , '= ' , 'Buffet Bar Inc. ' )->first ();
144+
145+ // Check the models are what they should be
146+ $ this ->assertInstanceOf ('Client ' , $ client );
147+ $ this ->assertInstanceOf ('User ' , $ user );
148+
149+ // Assert they are not attached
150+ $ this ->assertFalse (in_array ($ client ->_id , $ user ->client_ids ));
151+ $ this ->assertFalse (in_array ($ user ->_id , $ client ->user_ids ));
152+
153+ // Attach the client to the user
154+ $ user ->clients ()->attach ($ client );
155+
156+ // Get the new user model
157+ $ user = User::where ('name ' , '= ' , 'Jane Doe ' )->first ();
158+ $ client = Client::Where ('name ' , '= ' , 'Buffet Bar Inc. ' )->first ();
159+
160+ // Assert they are attached
161+ $ this ->assertTrue (in_array ($ client ->_id , $ user ->client_ids ));
162+ $ this ->assertTrue (in_array ($ user ->_id , $ client ->user_ids ));
126163 }
127164
165+ public function testHasManyAndBelongsToAttachesExistingModels ()
166+ {
167+ $ user = User::create (array ('name ' => 'John Doe ' , 'client_ids ' => array ('1234523 ' )));
168+
169+ $ clients = array (
170+ Client::create (array ('name ' => 'Pork Pies Ltd. ' ))->_id ,
171+ Client::create (array ('name ' => 'Buffet Bar Inc. ' ))->_id
172+ );
173+
174+ $ moreClients = array (
175+ Client::create (array ('name ' => 'Boloni Ltd. ' ))->_id ,
176+ Client::create (array ('name ' => 'Meatballs Inc. ' ))->_id
177+ );
178+
179+ $ user ->clients ()->sync ($ clients );
180+
181+ $ user = User::with ('clients ' )->find ($ user ->_id );
182+
183+ // Assert non attached ID's are detached succesfully
184+ $ this ->assertFalse (in_array ('1234523 ' , $ user ->client_ids ));
185+
186+ // Assert there are two client objects in the relationship
187+ $ this ->assertCount (2 , $ user ->clients );
188+
189+ $ user ->clients ()->sync ($ moreClients );
190+
191+ $ user = User::with ('clients ' )->find ($ user ->_id );
192+
193+ // Assert there are now 4 client objects in the relationship
194+ $ this ->assertCount (4 , $ user ->clients );
195+ }
128196}
0 commit comments