WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit ad1be80

Browse files
committed
add methods and tests for participant string
1 parent f8166c5 commit ad1be80

File tree

5 files changed

+123
-1
lines changed

5 files changed

+123
-1
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
],
1212
"require": {
1313
"php": ">=5.4.0",
14+
"illuminate/config": "~4.0",
1415
"illuminate/support": "~4.0",
1516
"illuminate/database": "~4.0",
1617
"nesbot/carbon": "~1.0"

src/Cmgmyr/Messenger/Models/Thread.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,4 +201,57 @@ public function activateAllParticipants()
201201
$participant->restore();
202202
}
203203
}
204+
205+
/**
206+
* Generates a string of participant information
207+
*
208+
* @param null $userId
209+
* @param array $columns
210+
* @return string
211+
*/
212+
public function participantsString($userId=null, $columns=['first_name', 'last_name'])
213+
{
214+
$selectString = $this->createSelectString($columns);
215+
216+
$participantNames = $this->getConnection()->table('users')
217+
->join('participants', 'users.id', '=', 'participants.user_id')
218+
->where('participants.thread_id', $this->id)
219+
->select($this->getConnection()->raw($selectString));
220+
221+
if ($userId !== null) {
222+
$participantNames->where('users.id', '!=', $userId);
223+
}
224+
225+
$userNames = $participantNames->lists('users.name');
226+
227+
return implode(', ', $userNames);
228+
}
229+
230+
/**
231+
* Generates a select string used in participantsString()
232+
*
233+
* @param $columns
234+
* @return string
235+
*/
236+
private function createSelectString($columns)
237+
{
238+
$dbDriver = $this->getConnection()->getDriverName();
239+
240+
switch ($dbDriver) {
241+
case 'pgsql':
242+
case 'sqlite':
243+
$columnString = implode(" || ' ' || users.", $columns);
244+
$selectString = "(users." . $columnString . ") as name";
245+
break;
246+
case 'sqlsrv':
247+
$columnString = implode(" + ' ' + users.", $columns);
248+
$selectString = "(users." . $columnString . ") as name";
249+
break;
250+
default:
251+
$columnString = implode(", ' ', users.", $columns);
252+
$selectString = "concat(users." . $columnString . ") as name";
253+
}
254+
255+
return $selectString;
256+
}
204257
}

src/Cmgmyr/Messenger/examples/views/index.blade.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
<?php $class = $thread->isUnread($currentUserId) ? 'alert-info' : ''; ?>
1212
<div class="media alert {{$class}}">
1313
<h4 class="media-heading">{{link_to('messages/' . $thread->id, $thread->subject)}}</h4>
14-
{{$thread->latestMessage()->body}}
14+
<p>{{$thread->latestMessage()->body}}</p>
15+
<p><small><strong>Participants:</strong> {{ $thread->participantsString(Auth::id()) }}</small></p>
1516
</div>
1617
@endforeach
1718
@else

tests/EloquentThreadTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use Carbon\Carbon;
44
use Cmgmyr\Messenger\Models\Thread;
55
use Illuminate\Database\Eloquent\Model as Eloquent;
6+
use ReflectionClass;
67

78
class 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
}

tests/TestCase.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ private function migrateTables()
5555
$this->createThreadsTable();
5656
$this->createMessagesTable();
5757
$this->createParticipantsTable();
58+
59+
$this->seedUsersTable();
5860
}
5961

6062
/**
@@ -75,6 +77,16 @@ function ($table) {
7577
);
7678
}
7779

80+
/**
81+
* Create some users for the tests to use
82+
*/
83+
private function seedUsersTable()
84+
{
85+
DB::insert('INSERT INTO users (id, first_name, last_name, email, created_at, updated_at) VALUES (?, ?, ?, ?, datetime(), datetime())', [1, 'Chris', 'Gmyr', '[email protected]']);
86+
DB::insert('INSERT INTO users (id, first_name, last_name, email, created_at, updated_at) VALUES (?, ?, ?, ?, datetime(), datetime())', [2, 'Adam', 'Wathan', '[email protected]']);
87+
DB::insert('INSERT INTO users (id, first_name, last_name, email, created_at, updated_at) VALUES (?, ?, ?, ?, datetime(), datetime())', [3, 'Taylor', 'Otwell', '[email protected]']);
88+
}
89+
7890
/**
7991
* Create the threads table in the database
8092
*/

0 commit comments

Comments
 (0)