-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #281 from gaussian1/fix-280
Add Notification Eager Load Support
- Loading branch information
Showing
10 changed files
with
178 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
use Fenos\Notifynder\Builder\Notification; | ||
use Fenos\Notifynder\Parsers\NotificationParser; | ||
use Illuminate\Database\Eloquent\ModelNotFoundException; | ||
|
||
class NotificationParserTest extends NotifynderTestCase | ||
{ | ||
public function testParseThrowsModelNotFoundExceptionIfCategoryIsNull() | ||
{ | ||
$from = $this->createUser(); | ||
$to = $this->createUser(); | ||
$notification = new Notification(); | ||
$notification->set('category_id', null); | ||
$notification->set('from_id', $from->getKey()); | ||
$notification->set('to_id', $to->getKey()); | ||
|
||
$this->expectException(ModelNotFoundException::class); | ||
$parser = new NotificationParser(); | ||
$parser->parse($notification); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
class NotifableEagerLoadingTest extends NotifynderTestCase | ||
{ | ||
public function testGetLazyNotificationRelationDoesnotEagerLoad() | ||
{ | ||
$user = $this->createUser(); | ||
$this->sendNotificationTo($user); | ||
|
||
$notification = $user->getLazyNotificationRelation()->first(); | ||
$this->assertModelHasNoLoadedRelations($notification, ['category', 'from', 'to']); | ||
} | ||
|
||
public function testGetNotificationRelationReadsConfigurationParameterIfNothingIsPassed() | ||
{ | ||
$user = $this->createUser(); | ||
$this->sendNotificationTo($user); | ||
|
||
$notification = $user->getNotificationRelation()->first(); | ||
$this->assertModelHasNoLoadedRelations($notification, ['category', 'from', 'to']); | ||
} | ||
|
||
public function testGetNotificationRelationCanEagerLoadAllRelationsIfTrueIsPassed() | ||
{ | ||
$user = $this->createUser(); | ||
$this->sendNotificationTo($user); | ||
|
||
$notification = $user->getNotificationRelation(true)->first(); | ||
|
||
$this->assertModelHasLoadedRelations($notification, ['category', 'from', 'to']); | ||
} | ||
|
||
public function testGetNotificationRelationCanEagerLoadASubsetOfRelationsIfAnArrayIsPassed() | ||
{ | ||
$user = $this->createUser(); | ||
$this->sendNotificationTo($user); | ||
|
||
$notification = $user->getNotificationRelation(['category'])->first(); | ||
|
||
$this->assertModelHasLoadedRelations($notification, ['category']); | ||
$this->assertModelHasNoLoadedRelations($notification, ['from', 'to']); | ||
} | ||
|
||
public function testGetNotificationRelationDoesnotEagerLoadIfConfigurationParameterIsMissing() | ||
{ | ||
$user = $this->createUser(); | ||
$this->sendNotificationTo($user); | ||
|
||
$config = app('notifynder.config'); | ||
$config->forget('eager_load'); | ||
|
||
$notification = $user->getNotificationRelation()->first(); | ||
$this->assertModelHasNoLoadedRelations($notification, ['category', 'from', 'to']); | ||
} | ||
|
||
public function testGetNotificationsReadsEagerLoadConfigurationParameter() | ||
{ | ||
$user = $this->createUser(); | ||
$this->sendNotificationTo($user); | ||
|
||
$config = app('notifynder.config'); | ||
|
||
$config->set('eager_load', true); | ||
$notifications = $user->getNotifications(); | ||
$this->assertModelHasLoadedRelations($notifications[0], ['category', 'from', 'to']); | ||
|
||
$config->set('eager_load', false); | ||
$notifications = $user->getNotifications(); | ||
$this->assertModelHasNoLoadedRelations($notifications[0], ['category', 'from', 'to']); | ||
|
||
$config->set('eager_load', ['category', 'from']); | ||
$notifications = $user->getNotifications(); | ||
$this->assertModelHasLoadedRelations($notifications[0], ['category', 'from']); | ||
$this->assertModelHasNoLoadedRelations($notifications[0], ['to']); | ||
} | ||
|
||
protected function assertModelHasLoadedRelations($model, $relationNames = []) | ||
{ | ||
$modelLoadedRelations = $model->getRelations(); | ||
foreach ($relationNames as $relationName) { | ||
$this->assertArrayHasKey($relationName, $modelLoadedRelations, $relationName.' relation was not eager loaded'); | ||
} | ||
} | ||
|
||
protected function assertModelHasNoLoadedRelations($model, $relationNames = []) | ||
{ | ||
$modelLoadedRelations = $model->getRelations(); | ||
foreach ($relationNames as $relationName) { | ||
$this->assertArrayNotHasKey($relationName, $modelLoadedRelations, $relationName.' relation was eager loaded'); | ||
} | ||
} | ||
} |