33use Carbon \Carbon ;
44use Cmgmyr \Messenger \Models \Thread ;
55use Illuminate \Database \Eloquent \Model as Eloquent ;
6+ use ReflectionClass ;
67
78class EloquentThreadTest extends TestCase
89{
@@ -12,6 +13,20 @@ public function setUp()
1213 Eloquent::unguard ();
1314 }
1415
16+ /**
17+ * Activate private/protected methods for testing
18+ *
19+ * @param $name
20+ * @return \ReflectionMethod
21+ */
22+ protected static function getMethod ($ name )
23+ {
24+ $ class = new ReflectionClass ('Cmgmyr\Messenger\Models\Thread ' );
25+ $ method = $ class ->getMethod ($ name );
26+ $ method ->setAccessible (true );
27+ return $ method ;
28+ }
29+
1530 /** @test */
1631 public function it_should_create_a_new_thread ()
1732 {
@@ -196,4 +211,44 @@ public function it_should_activate_all_deleted_participants()
196211 $ participants = $ thread ->participants ();
197212 $ this ->assertEquals (3 , $ participants ->count ());
198213 }
214+
215+ /** @test */
216+ public function it_should_generate_participant_select_string ()
217+ {
218+ $ method = self ::getMethod ('createSelectString ' );
219+ $ thread = new Thread ();
220+
221+ $ columns = ['name ' ];
222+ $ select = $ method ->invokeArgs ($ thread , [$ columns ]);
223+ $ this ->assertEquals ("(users.name) as name " , $ select );
224+
225+ $ columns = ['first_name ' , 'last_name ' ];
226+ $ select = $ method ->invokeArgs ($ thread , [$ columns ]);
227+ $ this ->assertEquals ("(users.first_name || ' ' || users.last_name) as name " , $ select );
228+
229+ $ columns = ['first_name ' , 'last_name ' , 'email ' ];
230+ $ select = $ method ->invokeArgs ($ thread , [$ columns ]);
231+ $ this ->assertEquals ("(users.first_name || ' ' || users.last_name || ' ' || users.email) as name " , $ select );
232+ }
233+
234+ /** @test */
235+ public function it_should_get_participants_string ()
236+ {
237+ $ thread = $ this ->faktory ->create ('thread ' );
238+
239+ $ participant_1 = $ this ->faktory ->build ('participant ' );
240+ $ participant_2 = $ this ->faktory ->build ('participant ' , ['user_id ' => 2 ]);
241+ $ participant_3 = $ this ->faktory ->build ('participant ' , ['user_id ' => 3 ]);
242+
243+ $ thread ->participants ()->saveMany ([$ participant_1 , $ participant_2 , $ participant_3 ]);
244+
245+ $ string = $ thread ->participantsString ();
246+ $ this ->assertEquals ("Chris Gmyr, Adam Wathan, Taylor Otwell " , $ string );
247+
248+ $ string = $ thread ->participantsString (1 );
249+ $ this ->assertEquals ("Adam Wathan, Taylor Otwell " , $ string );
250+
251+ $ string = $ thread ->participantsString (1 , ['first_name ' ]);
252+ $ this ->assertEquals ("Adam, Taylor " , $ string );
253+ }
199254}
0 commit comments