notification: add test for unread consistency

This commit is contained in:
Julien Fastré 2021-12-26 01:12:32 +01:00
parent bd3919efcb
commit 700bb02374
3 changed files with 31 additions and 2 deletions

View File

@ -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;
}

View File

@ -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,

View File

@ -0,0 +1,23 @@
<?php
namespace Entity;
use Chill\MainBundle\Entity\Notification;
use Chill\MainBundle\Entity\User;
use PHPUnit\Framework\TestCase;
class NotificationTest extends TestCase
{
public function testAddAddresseeStoreAnUread()
{
$notification = new Notification();
$notification->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());
}
}