From 700bb02374c33447d75f64c7e5e9263e4a9f17f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Sun, 26 Dec 2021 01:12:32 +0100 Subject: [PATCH] notification: add test for unread consistency --- .../ChillMainBundle/Entity/Notification.php | 9 ++++++-- .../ChillMainBundle/Form/NotificationType.php | 1 + .../Tests/Entity/NotificationTest.php | 23 +++++++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 src/Bundle/ChillMainBundle/Tests/Entity/NotificationTest.php diff --git a/src/Bundle/ChillMainBundle/Entity/Notification.php b/src/Bundle/ChillMainBundle/Entity/Notification.php index c484f9826..b50b9de53 100644 --- a/src/Bundle/ChillMainBundle/Entity/Notification.php +++ b/src/Bundle/ChillMainBundle/Entity/Notification.php @@ -92,7 +92,7 @@ class Notification public function addUnreadBy(User $user): self { if (!$this->unreadBy->contains($user)) { - $this->unreadBy->add($user); + $this->unreadBy[] = $user; } return $this; @@ -136,6 +136,11 @@ class Notification return $this->sender; } + public function getUnreadBy(): Collection + { + return $this->unreadBy; + } + public function isReadBy(User $user): bool { return !$this->unreadBy->contains($user); @@ -154,7 +159,7 @@ class Notification public function removeAddressee(User $addressee): self { $this->addressees->removeElement($addressee); - $this->unreadBy->removeElement($addressee); + $this->removeUnreadBy($addressee); return $this; } diff --git a/src/Bundle/ChillMainBundle/Form/NotificationType.php b/src/Bundle/ChillMainBundle/Form/NotificationType.php index af884dfbb..b54752cde 100644 --- a/src/Bundle/ChillMainBundle/Form/NotificationType.php +++ b/src/Bundle/ChillMainBundle/Form/NotificationType.php @@ -28,6 +28,7 @@ class NotificationType extends AbstractType 'class' => User::class, 'choice_label' => 'label', 'multiple' => true, + 'by_reference' => true, ]) ->add('message', ChillTextareaType::class, [ 'required' => false, diff --git a/src/Bundle/ChillMainBundle/Tests/Entity/NotificationTest.php b/src/Bundle/ChillMainBundle/Tests/Entity/NotificationTest.php new file mode 100644 index 000000000..9be00dde3 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Tests/Entity/NotificationTest.php @@ -0,0 +1,23 @@ +addAddressee($user1 = new User()); + $notification->addAddressee($user2 = new User()); + + $this->assertCount(2, $notification->getAddressees()); + $this->assertCount(2, $notification->getUnreadBy()); + $this->assertContains($user1, $notification->getUnreadBy()->toArray()); + $this->assertContains($user2, $notification->getUnreadBy()->toArray()); + } + +}