From 501b1cf8d720df96c044b475bacdbdb72287892f Mon Sep 17 00:00:00 2001 From: ARCANEDEV Date: Sat, 11 Feb 2017 13:19:04 +0000 Subject: [PATCH 1/3] Add users relationship to Discussion model And fixing doc comments --- src/Bases/Migration.php | 8 ++--- src/Bases/Model.php | 4 +-- src/Contracts/Discussion.php | 25 +++++++++----- src/Contracts/Message.php | 8 ++--- src/Contracts/Participant.php | 14 ++++---- src/LaravelMessengerServiceProvider.php | 24 +++++++------ src/Models/Discussion.php | 45 ++++++++++++++++--------- src/Models/Message.php | 16 ++++----- src/Models/Participant.php | 20 +++++------ src/Traits/ConfigHelper.php | 4 +-- src/Traits/Messagable.php | 20 +++++------ tests/Models/DiscussionTest.php | 17 ++++++++++ 12 files changed, 122 insertions(+), 83 deletions(-) diff --git a/src/Bases/Migration.php b/src/Bases/Migration.php index 20ed435..f7d0aa4 100644 --- a/src/Bases/Migration.php +++ b/src/Bases/Migration.php @@ -11,15 +11,15 @@ */ abstract class Migration extends BaseMigration { - /* ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- | Traits - | ------------------------------------------------------------------------------------------------ + | ----------------------------------------------------------------- */ use ConfigHelper; - /* ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- | Constructor - | ------------------------------------------------------------------------------------------------ + | ----------------------------------------------------------------- */ /** * Migration constructor. diff --git a/src/Bases/Model.php b/src/Bases/Model.php index 6685812..4ae27ea 100644 --- a/src/Bases/Model.php +++ b/src/Bases/Model.php @@ -17,9 +17,9 @@ abstract class Model extends BaseModel */ use ConfigHelper; - /* ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- | Constructor - | ------------------------------------------------------------------------------------------------ + | ----------------------------------------------------------------- */ /** * Create a new Eloquent model instance. diff --git a/src/Contracts/Discussion.php b/src/Contracts/Discussion.php index 74acaed..eeea4ee 100644 --- a/src/Contracts/Discussion.php +++ b/src/Contracts/Discussion.php @@ -26,9 +26,9 @@ */ interface Discussion { - /* ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- | Relationships - | ------------------------------------------------------------------------------------------------ + | ----------------------------------------------------------------- */ /** * Participants relationship. @@ -44,6 +44,13 @@ public function participants(); */ public function messages(); + /** + * User's relationship. + * + * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany + */ + public function users(); + /** * Get the user that created the first message. * @@ -51,9 +58,9 @@ public function messages(); */ public function creator(); - /* ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- | Scopes - | ------------------------------------------------------------------------------------------------ + | ----------------------------------------------------------------- */ /** * Scope discussions that the user is associated with. @@ -96,9 +103,9 @@ public function scopeBetween(Builder $query, array $userIds); */ public function scopeSubject(Builder $query, $subject, $strict = false); - /* ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- | Getters & Setters - | ------------------------------------------------------------------------------------------------ + | ----------------------------------------------------------------- */ /** * Get the latest_message attribute. @@ -107,9 +114,9 @@ public function scopeSubject(Builder $query, $subject, $strict = false); */ public function getLatestMessageAttribute(); - /* ------------------------------------------------------------------------------------------------ - | Main Functions - | ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- + | Main Methods + | ----------------------------------------------------------------- */ /** * Returns all of the latest discussions by `updated_at` date. diff --git a/src/Contracts/Message.php b/src/Contracts/Message.php index a6de16d..3259f12 100644 --- a/src/Contracts/Message.php +++ b/src/Contracts/Message.php @@ -20,9 +20,9 @@ */ interface Message { - /* ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- | Relationships - | ------------------------------------------------------------------------------------------------ + | ----------------------------------------------------------------- */ /** * Discussion relationship. @@ -52,9 +52,9 @@ public function user(); */ public function participants(); - /* ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- | Getters & Setters - | ------------------------------------------------------------------------------------------------ + | ----------------------------------------------------------------- */ /** * Recipients of this message. diff --git a/src/Contracts/Participant.php b/src/Contracts/Participant.php index 95d4876..031bcb3 100644 --- a/src/Contracts/Participant.php +++ b/src/Contracts/Participant.php @@ -18,9 +18,9 @@ */ interface Participant { - /* ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- | Relationships - | ------------------------------------------------------------------------------------------------ + | ----------------------------------------------------------------- */ /** * Discussion relationship. @@ -36,9 +36,9 @@ public function discussion(); */ public function user(); - /* ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- | Getters & Setters - | ------------------------------------------------------------------------------------------------ + | ----------------------------------------------------------------- */ /** * Get the participant string info. @@ -47,9 +47,9 @@ public function user(); */ public function stringInfo(); - /* ------------------------------------------------------------------------------------------------ - | Main Functions - | ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- + | Main Methods + | ----------------------------------------------------------------- */ /** * Restore a soft-deleted model instance. diff --git a/src/LaravelMessengerServiceProvider.php b/src/LaravelMessengerServiceProvider.php index 3d8ad2f..543da56 100644 --- a/src/LaravelMessengerServiceProvider.php +++ b/src/LaravelMessengerServiceProvider.php @@ -1,7 +1,7 @@ */ -class LaravelMessengerServiceProvider extends ServiceProvider +class LaravelMessengerServiceProvider extends PackageServiceProvider { - /* ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- | Properties - | ------------------------------------------------------------------------------------------------ + | ----------------------------------------------------------------- */ /** * Package name. @@ -22,9 +22,9 @@ class LaravelMessengerServiceProvider extends ServiceProvider */ protected $package = 'laravel-messenger'; - /* ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- | Getters & Setters - | ------------------------------------------------------------------------------------------------ + | ----------------------------------------------------------------- */ /** * Get the base path of the package. @@ -36,15 +36,17 @@ public function getBasePath() return dirname(__DIR__); } - /* ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- | Main Functions - | ------------------------------------------------------------------------------------------------ + | ----------------------------------------------------------------- */ /** * Register the service provider. */ public function register() { + parent::register(); + $this->registerConfig(); $this->bindModels(); } @@ -74,9 +76,9 @@ public function provides() ]; } - /* ------------------------------------------------------------------------------------------------ - | Other Functions - | ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- + | Other Methods + | ----------------------------------------------------------------- */ /** * Bind the models. diff --git a/src/Models/Discussion.php b/src/Models/Discussion.php index a0665ac..cd91d51 100644 --- a/src/Models/Discussion.php +++ b/src/Models/Discussion.php @@ -34,15 +34,15 @@ */ class Discussion extends Model implements DiscussionContract { - /* ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- | Traits - | ------------------------------------------------------------------------------------------------ + | ----------------------------------------------------------------- */ use SoftDeletes; - /* ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- | Properties - | ------------------------------------------------------------------------------------------------ + | ----------------------------------------------------------------- */ /** * The attributes that can be set with Mass Assignment. @@ -67,9 +67,9 @@ class Discussion extends Model implements DiscussionContract 'id' => 'integer', ]; - /* ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- | Constructor - | ------------------------------------------------------------------------------------------------ + | ----------------------------------------------------------------- */ /** * Create a new Eloquent model instance. @@ -85,9 +85,9 @@ public function __construct(array $attributes = []) parent::__construct($attributes); } - /* ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- | Relationships - | ------------------------------------------------------------------------------------------------ + | ----------------------------------------------------------------- */ /** * Participants relationship. @@ -113,6 +113,21 @@ public function messages() ); } + /** + * User's relationship. + * + * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany + */ + public function users() + { + return $this->belongsToMany( + $this->getModelFromConfig('users', \App\User::class), + 'participants', + 'discussion_id', + 'user_id' + ); + } + /** * Get the user that created the first message. * @@ -123,9 +138,9 @@ public function creator() return $this->messages()->oldest()->first()->user(); } - /* ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- | Scopes - | ------------------------------------------------------------------------------------------------ + | ----------------------------------------------------------------- */ /** * Scope discussions that the user is associated with. @@ -215,9 +230,9 @@ public function scopeSubject(Builder $query, $subject, $strict = false) return $query->where('subject', 'like', $subject); } - /* ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- | Getters & Setters - | ------------------------------------------------------------------------------------------------ + | ----------------------------------------------------------------- */ /** * Get the latest_message attribute. @@ -229,9 +244,9 @@ public function getLatestMessageAttribute() return $this->messages->sortByDesc('created_at')->first(); } - /* ------------------------------------------------------------------------------------------------ - | Main Functions - | ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- + | Main Methods + | ----------------------------------------------------------------- */ /** * Returns all of the latest discussions by `updated_at` date. diff --git a/src/Models/Message.php b/src/Models/Message.php index 639c6ff..9d03972 100644 --- a/src/Models/Message.php +++ b/src/Models/Message.php @@ -23,9 +23,9 @@ */ class Message extends Model implements MessageContract { - /* ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- | Properties - | ------------------------------------------------------------------------------------------------ + | ----------------------------------------------------------------- */ /** * The relationships that should be touched on save. @@ -50,9 +50,9 @@ class Message extends Model implements MessageContract 'id' => 'integer', ]; - /* ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- | Constructor - | ------------------------------------------------------------------------------------------------ + | ----------------------------------------------------------------- */ /** * Create a new Eloquent model instance. @@ -68,9 +68,9 @@ public function __construct(array $attributes = []) parent::__construct($attributes); } - /* ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- | Relationships - | ------------------------------------------------------------------------------------------------ + | ----------------------------------------------------------------- */ /** * Discussion relationship. @@ -120,9 +120,9 @@ public function participants() ); } - /* ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- | Getters & Setters - | ------------------------------------------------------------------------------------------------ + | ----------------------------------------------------------------- */ /** * Recipients of this message. diff --git a/src/Models/Participant.php b/src/Models/Participant.php index cdc021c..3bcfa47 100644 --- a/src/Models/Participant.php +++ b/src/Models/Participant.php @@ -22,15 +22,15 @@ */ class Participant extends Model implements ParticipantContract { - /* ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- | Traits - | ------------------------------------------------------------------------------------------------ + | ----------------------------------------------------------------- */ use SoftDeletes; - /* ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- | Properties - | ------------------------------------------------------------------------------------------------ + | ----------------------------------------------------------------- */ /** * The attributes that can be set with Mass Assignment. @@ -57,9 +57,9 @@ class Participant extends Model implements ParticipantContract 'user_id' => 'integer', ]; - /* ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- | Constructor - | ------------------------------------------------------------------------------------------------ + | ----------------------------------------------------------------- */ /** * Create a new Eloquent model instance. @@ -75,9 +75,9 @@ public function __construct(array $attributes = []) parent::__construct($attributes); } - /* ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- | Relationships - | ------------------------------------------------------------------------------------------------ + | ----------------------------------------------------------------- */ /** * Discussion relationship. @@ -103,9 +103,9 @@ public function user() ); } - /* ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- | Getters & Setters - | ------------------------------------------------------------------------------------------------ + | ----------------------------------------------------------------- */ /** * Get the participant string info. diff --git a/src/Traits/ConfigHelper.php b/src/Traits/ConfigHelper.php index cdb1fff..6cd4f95 100644 --- a/src/Traits/ConfigHelper.php +++ b/src/Traits/ConfigHelper.php @@ -8,9 +8,9 @@ */ trait ConfigHelper { - /* ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- | Helper Functions - | ------------------------------------------------------------------------------------------------ + | ----------------------------------------------------------------- */ /** * Get table from config. diff --git a/src/Traits/Messagable.php b/src/Traits/Messagable.php index 8651648..a17b397 100644 --- a/src/Traits/Messagable.php +++ b/src/Traits/Messagable.php @@ -1,7 +1,5 @@ get(); } - /* ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- | Required Methods - | ------------------------------------------------------------------------------------------------ + | ----------------------------------------------------------------- */ /** * Define a many-to-many relationship. diff --git a/tests/Models/DiscussionTest.php b/tests/Models/DiscussionTest.php index d68ca10..dc88ef3 100644 --- a/tests/Models/DiscussionTest.php +++ b/tests/Models/DiscussionTest.php @@ -521,6 +521,23 @@ public function it_can_get_all_discussions_for_a_user() $this->assertCount(2, $discussions); } + /** @test */ + public function it_can_get_all_users_for_a_discussion() + { + /** @var \Arcanedev\LaravelMessenger\Models\Discussion $discussion */ + $discussion = $this->factory->create(Discussion::class); + + $discussion->participants()->saveMany([ + $this->factory->make(Participant::class, ['user_id' => 1]), + $this->factory->make(Participant::class, ['user_id' => 2]), + ]); + + $discussionUsers = $discussion->users()->get(); + + $this->assertCount(2, $discussionUsers); + $this->assertSame([1, 2], $discussionUsers->pluck('id')->toArray()); + } + /** @test */ public function it_can_get_all_discussions_for_a_user_with_new_messages() { From e37e9d2e389953ea05c51bebcf287e0d73588a97 Mon Sep 17 00:00:00 2001 From: ARCANEDEV Date: Sat, 11 Feb 2017 13:19:13 +0000 Subject: [PATCH 2/3] Updating composer.json --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index a4674e9..bc9f58a 100644 --- a/composer.json +++ b/composer.json @@ -20,8 +20,8 @@ "arcanedev/support": "~3.0" }, "require-dev": { - "phpunit/phpcov": "~2.0|~3.0", - "phpunit/phpunit": "~4.0|~5.0" + "phpunit/phpcov": "~3.0", + "phpunit/phpunit": "~5.0" }, "autoload": { "psr-4": { From d1df89a0a47f59cd6dbce26a8ad0be26c183c54c Mon Sep 17 00:00:00 2001 From: ARCANEDEV Date: Sat, 11 Feb 2017 13:52:37 +0000 Subject: [PATCH 3/3] Fixing the tests --- .travis.yml | 2 +- tests/Stubs/Models/User.php | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 34a54d8..f98f105 100644 --- a/.travis.yml +++ b/.travis.yml @@ -24,7 +24,7 @@ before_script: script: - composer validate - mkdir -p build/logs - - vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover + - ./vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover after_script: - if [ "$TRAVIS_PHP_VERSION" != "nightly" ]; then wget https://scrutinizer-ci.com/ocular.phar; fi diff --git a/tests/Stubs/Models/User.php b/tests/Stubs/Models/User.php index b094afb..731509a 100644 --- a/tests/Stubs/Models/User.php +++ b/tests/Stubs/Models/User.php @@ -17,9 +17,13 @@ class User extends Model */ use Messagable; - /* ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- | Properties - | ------------------------------------------------------------------------------------------------ + | ----------------------------------------------------------------- */ protected $fillable = ['name', 'email']; + + protected $casts = [ + 'id' => 'integer', + ]; }