|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpList\Core\Tests\Unit\Domain\Subscription\Service\Manager; |
| 6 | + |
| 7 | +use Doctrine\ORM\EntityManagerInterface; |
| 8 | +use PhpList\Core\Domain\Subscription\Model\Subscriber; |
| 9 | +use PhpList\Core\Domain\Subscription\Model\UserBlacklist; |
| 10 | +use PhpList\Core\Domain\Subscription\Model\UserBlacklistData; |
| 11 | +use PhpList\Core\Domain\Subscription\Repository\SubscriberRepository; |
| 12 | +use PhpList\Core\Domain\Subscription\Repository\UserBlacklistDataRepository; |
| 13 | +use PhpList\Core\Domain\Subscription\Repository\UserBlacklistRepository; |
| 14 | +use PhpList\Core\Domain\Subscription\Service\Manager\SubscriberBlacklistManager; |
| 15 | +use PHPUnit\Framework\MockObject\MockObject; |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | + |
| 18 | +class SubscriberBlacklistManagerTest extends TestCase |
| 19 | +{ |
| 20 | + private SubscriberRepository|MockObject $subscriberRepository; |
| 21 | + private UserBlacklistRepository|MockObject $userBlacklistRepository; |
| 22 | + private UserBlacklistDataRepository|MockObject $userBlacklistDataRepository; |
| 23 | + private EntityManagerInterface|MockObject $entityManager; |
| 24 | + private SubscriberBlacklistManager $manager; |
| 25 | + |
| 26 | + protected function setUp(): void |
| 27 | + { |
| 28 | + $this->subscriberRepository = $this->createMock(SubscriberRepository::class); |
| 29 | + $this->userBlacklistRepository = $this->createMock(UserBlacklistRepository::class); |
| 30 | + $this->userBlacklistDataRepository = $this->createMock(UserBlacklistDataRepository::class); |
| 31 | + $this->entityManager = $this->createMock(EntityManagerInterface::class); |
| 32 | + |
| 33 | + $this->manager = new SubscriberBlacklistManager( |
| 34 | + subscriberRepository: $this->subscriberRepository, |
| 35 | + userBlacklistRepository: $this->userBlacklistRepository, |
| 36 | + blacklistDataRepository: $this->userBlacklistDataRepository, |
| 37 | + entityManager: $this->entityManager, |
| 38 | + ); |
| 39 | + } |
| 40 | + |
| 41 | + public function testIsEmailBlacklistedReturnsValueFromRepository(): void |
| 42 | + { |
| 43 | + $this->subscriberRepository |
| 44 | + ->expects($this->once()) |
| 45 | + ->method('isEmailBlacklisted') |
| 46 | + |
| 47 | + ->willReturn(true); |
| 48 | + |
| 49 | + $result = $this-> manager-> isEmailBlacklisted( '[email protected]'); |
| 50 | + |
| 51 | + $this->assertTrue($result); |
| 52 | + } |
| 53 | + |
| 54 | + public function testGetBlacklistInfoReturnsResultFromRepository(): void |
| 55 | + { |
| 56 | + $userBlacklist = $this->createMock(UserBlacklist::class); |
| 57 | + |
| 58 | + $this->userBlacklistRepository |
| 59 | + ->expects($this->once()) |
| 60 | + ->method('findBlacklistInfoByEmail') |
| 61 | + |
| 62 | + ->willReturn($userBlacklist); |
| 63 | + |
| 64 | + $result = $this-> manager-> getBlacklistInfo( '[email protected]'); |
| 65 | + |
| 66 | + $this->assertSame($userBlacklist, $result); |
| 67 | + } |
| 68 | + |
| 69 | + public function testAddEmailToBlacklistDoesNotAddIfAlreadyBlacklisted(): void |
| 70 | + { |
| 71 | + $this->subscriberRepository |
| 72 | + ->expects($this->once()) |
| 73 | + ->method('isEmailBlacklisted') |
| 74 | + |
| 75 | + ->willReturn(true); |
| 76 | + |
| 77 | + $this->entityManager |
| 78 | + ->expects($this->never()) |
| 79 | + ->method('persist'); |
| 80 | + |
| 81 | + $this->entityManager |
| 82 | + ->expects($this->never()) |
| 83 | + ->method('flush'); |
| 84 | + |
| 85 | + $this-> manager-> addEmailToBlacklist( '[email protected]', 'reason'); |
| 86 | + } |
| 87 | + |
| 88 | + public function testAddEmailToBlacklistAddsEntryAndReason(): void |
| 89 | + { |
| 90 | + $this->subscriberRepository |
| 91 | + ->expects($this->once()) |
| 92 | + ->method('isEmailBlacklisted') |
| 93 | + |
| 94 | + ->willReturn(false); |
| 95 | + |
| 96 | + $this->entityManager |
| 97 | + ->expects($this->exactly(2)) |
| 98 | + ->method('persist') |
| 99 | + ->withConsecutive( |
| 100 | + [$this->isInstanceOf(UserBlacklist::class)], |
| 101 | + [$this->isInstanceOf(UserBlacklistData::class)] |
| 102 | + ); |
| 103 | + |
| 104 | + $this->entityManager |
| 105 | + ->expects($this->once()) |
| 106 | + ->method('flush'); |
| 107 | + |
| 108 | + $this-> manager-> addEmailToBlacklist( '[email protected]', 'test reason'); |
| 109 | + } |
| 110 | + |
| 111 | + public function testAddEmailToBlacklistAddsEntryWithoutReason(): void |
| 112 | + { |
| 113 | + $this->subscriberRepository |
| 114 | + ->expects($this->once()) |
| 115 | + ->method('isEmailBlacklisted') |
| 116 | + |
| 117 | + ->willReturn(false); |
| 118 | + |
| 119 | + $this->entityManager |
| 120 | + ->expects($this->once()) |
| 121 | + ->method('persist') |
| 122 | + ->with($this->isInstanceOf(UserBlacklist::class)); |
| 123 | + |
| 124 | + $this->entityManager |
| 125 | + ->expects($this->once()) |
| 126 | + ->method('flush'); |
| 127 | + |
| 128 | + $this-> manager-> addEmailToBlacklist( '[email protected]'); |
| 129 | + } |
| 130 | + |
| 131 | + public function testRemoveEmailFromBlacklistRemovesAllRelatedData(): void |
| 132 | + { |
| 133 | + $blacklist = $this->createMock(UserBlacklist::class); |
| 134 | + $blacklistData = $this->createMock(UserBlacklistData::class); |
| 135 | + $subscriber = $this->getMockBuilder(Subscriber::class) |
| 136 | + ->onlyMethods(['setBlacklisted']) |
| 137 | + ->getMock(); |
| 138 | + |
| 139 | + $this->userBlacklistRepository |
| 140 | + ->expects($this->once()) |
| 141 | + ->method('findOneByEmail') |
| 142 | + |
| 143 | + ->willReturn($blacklist); |
| 144 | + |
| 145 | + $this->userBlacklistDataRepository |
| 146 | + ->expects($this->once()) |
| 147 | + ->method('findOneByEmail') |
| 148 | + |
| 149 | + ->willReturn($blacklistData); |
| 150 | + |
| 151 | + $this->subscriberRepository |
| 152 | + ->expects($this->once()) |
| 153 | + ->method('findOneByEmail') |
| 154 | + |
| 155 | + ->willReturn($subscriber); |
| 156 | + |
| 157 | + $this->entityManager |
| 158 | + ->expects($this->exactly(2)) |
| 159 | + ->method('remove') |
| 160 | + ->withConsecutive([$blacklist], [$blacklistData]); |
| 161 | + |
| 162 | + $subscriber->expects($this->once())->method('setBlacklisted')->with(false); |
| 163 | + |
| 164 | + $this->entityManager |
| 165 | + ->expects($this->once()) |
| 166 | + ->method('flush'); |
| 167 | + |
| 168 | + $this-> manager-> removeEmailFromBlacklist( '[email protected]'); |
| 169 | + } |
| 170 | + |
| 171 | + public function testGetBlacklistReasonReturnsReasonOrNull(): void |
| 172 | + { |
| 173 | + $blacklistData = $this->createMock(UserBlacklistData::class); |
| 174 | + $blacklistData->expects($this->once())->method('getData')->willReturn('my reason'); |
| 175 | + |
| 176 | + $this->userBlacklistDataRepository |
| 177 | + ->expects($this->once()) |
| 178 | + ->method('findOneByEmail') |
| 179 | + |
| 180 | + ->willReturn($blacklistData); |
| 181 | + |
| 182 | + $result = $this-> manager-> getBlacklistReason( '[email protected]'); |
| 183 | + $this->assertSame('my reason', $result); |
| 184 | + } |
| 185 | + |
| 186 | + public function testGetBlacklistReasonReturnsNullIfNoData(): void |
| 187 | + { |
| 188 | + $this->userBlacklistDataRepository |
| 189 | + ->expects($this->once()) |
| 190 | + ->method('findOneByEmail') |
| 191 | + |
| 192 | + ->willReturn(null); |
| 193 | + |
| 194 | + $result = $this-> manager-> getBlacklistReason( '[email protected]'); |
| 195 | + $this->assertNull($result); |
| 196 | + } |
| 197 | +} |
0 commit comments