From c2d474558ab0786849c77928ffc80f74fe80bba0 Mon Sep 17 00:00:00 2001 From: Tom Witkowski Date: Thu, 16 Jun 2016 12:08:13 +0200 Subject: [PATCH 1/2] fix & update the pagination behaviour in the Manager and add unittests --- .../Notifications/NotificationManager.php | 30 ++++++++-------- .../Notifications/NotificationTest.php | 35 +++++++++++++++++++ 2 files changed, 51 insertions(+), 14 deletions(-) diff --git a/src/Notifynder/Notifications/NotificationManager.php b/src/Notifynder/Notifications/NotificationManager.php index 44a9f87..b2ecd3d 100755 --- a/src/Notifynder/Notifications/NotificationManager.php +++ b/src/Notifynder/Notifications/NotificationManager.php @@ -7,6 +7,7 @@ use Fenos\Notifynder\Contracts\NotifynderNotification; use Fenos\Notifynder\Exceptions\NotificationNotFoundException; use Fenos\Notifynder\Models\Notification as NotificationModel; +use Fenos\Notifynder\Models\NotifynderCollection; use Illuminate\Pagination\LengthAwarePaginator; /** @@ -174,17 +175,11 @@ public function getNotRead($toId, $limit = null, $paginate = null, $orderDate = { $notifications = $this->notifynderRepo->getNotRead( $toId, $this->entity, - $limit, $paginate, $orderDate, + $limit, null, $orderDate, $filterScope ); - if (is_int(intval($paginate)) && $paginate) { - return new LengthAwarePaginator($notifications->parse(), $notifications->total(), $limit, $paginate, [ - 'path' => LengthAwarePaginator::resolveCurrentPath(), - ]); - } - - return $notifications->parse(); + return $this->getPaginatedIfNeeded($notifications, $limit, $paginate); } /** @@ -201,17 +196,24 @@ public function getAll($toId, $limit = null, $paginate = null, $orderDate = 'des { $notifications = $this->notifynderRepo->getAll( $toId, $this->entity, - $limit, $paginate, $orderDate, + $limit, null, $orderDate, $filterScope ); - if (is_int(intval($paginate)) && $paginate) { - return new LengthAwarePaginator($notifications->parse(), $notifications->total(), $limit, $paginate, [ - 'path' => LengthAwarePaginator::resolveCurrentPath(), - ]); + return $this->getPaginatedIfNeeded($notifications, $limit, $paginate); + } + + protected function getPaginatedIfNeeded(NotifynderCollection $notifications, $limit, $paginate) + { + if($paginate === false || is_null($paginate)) { + return $notifications->parse(); + } elseif($paginate === true) { + $paginate = null; } - return $notifications->parse(); + return new LengthAwarePaginator($notifications->parse(), $notifications->count(), $limit, $paginate, [ + 'path' => LengthAwarePaginator::resolveCurrentPath(), + ]); } /** diff --git a/tests/integration/Notifications/NotificationTest.php b/tests/integration/Notifications/NotificationTest.php index c91575c..94a6006 100755 --- a/tests/integration/Notifications/NotificationTest.php +++ b/tests/integration/Notifications/NotificationTest.php @@ -67,6 +67,40 @@ public function it_retrieve_notification_by_limiting_the_number() $this->assertCount(1, $notifications); } + /** @test */ + public function it_retrieve_notification_by_disable_pagination() + { + app('config')->set('notifynder.polymorphic', false); + $extraValues = json_encode(['look' => 'Amazing']); + + $category = $this->createCategory(['text' => 'parse this {extra.look} value']); + + $notification = $this->createNotification(['extra' => $extraValues, 'category_id' => $category->id]); + $this->createMultipleNotifications(['to_id' => $notification->to_id]); + + $notifications = $this->notification->getNotRead($notification->to->id, 10, false); + + $this->assertCount(10, $notifications); + $this->assertInstanceOf(\Fenos\Notifynder\Models\NotifynderCollection::class, $notifications); + } + + /** @test */ + public function it_retrieve_notification_by_paginating_with_bool() + { + app('config')->set('notifynder.polymorphic', false); + $extraValues = json_encode(['look' => 'Amazing']); + + $category = $this->createCategory(['text' => 'parse this {extra.look} value']); + + $notification = $this->createNotification(['extra' => $extraValues, 'category_id' => $category->id]); + $this->createMultipleNotifications(['to_id' => $notification->to_id]); + + $notifications = $this->notification->getNotRead($notification->to->id, 5, true); + + $this->assertCount(5, $notifications); + $this->assertInstanceOf(\Illuminate\Contracts\Pagination\LengthAwarePaginator::class, $notifications); + } + /** @test */ public function it_retrieve_notification_by_paginating_the_number() { @@ -81,6 +115,7 @@ public function it_retrieve_notification_by_paginating_the_number() $notifications = $this->notification->getNotRead($notification->to->id, 5, 1); $this->assertCount(5, $notifications); + $this->assertInstanceOf(\Illuminate\Contracts\Pagination\LengthAwarePaginator::class, $notifications); } /** From 5d0270099334691ea9c217ce7e54e78babfde6e7 Mon Sep 17 00:00:00 2001 From: Tom Witkowski Date: Thu, 16 Jun 2016 06:08:25 -0400 Subject: [PATCH 2/2] Applied fixes from StyleCI --- src/Notifynder/Notifications/NotificationManager.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Notifynder/Notifications/NotificationManager.php b/src/Notifynder/Notifications/NotificationManager.php index b2ecd3d..703a877 100755 --- a/src/Notifynder/Notifications/NotificationManager.php +++ b/src/Notifynder/Notifications/NotificationManager.php @@ -205,9 +205,9 @@ public function getAll($toId, $limit = null, $paginate = null, $orderDate = 'des protected function getPaginatedIfNeeded(NotifynderCollection $notifications, $limit, $paginate) { - if($paginate === false || is_null($paginate)) { + if ($paginate === false || is_null($paginate)) { return $notifications->parse(); - } elseif($paginate === true) { + } elseif ($paginate === true) { $paginate = null; }