|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpList\Core\Tests\Unit\Domain\Messaging\Service; |
| 6 | + |
| 7 | +use Doctrine\ORM\EntityManagerInterface; |
| 8 | +use PhpList\Core\Domain\Messaging\Model\Bounce; |
| 9 | +use PhpList\Core\Domain\Messaging\Model\BounceRegex; |
| 10 | +use PhpList\Core\Domain\Messaging\Model\BounceRegexBounce; |
| 11 | +use PhpList\Core\Domain\Messaging\Repository\BounceRegexRepository; |
| 12 | +use PhpList\Core\Domain\Messaging\Service\BounceRegexManager; |
| 13 | +use PHPUnit\Framework\MockObject\MockObject; |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use ReflectionProperty; |
| 16 | + |
| 17 | +class BounceRegexManagerTest extends TestCase |
| 18 | +{ |
| 19 | + private BounceRegexRepository&MockObject $regexRepository; |
| 20 | + private EntityManagerInterface&MockObject $entityManager; |
| 21 | + private BounceRegexManager $manager; |
| 22 | + |
| 23 | + protected function setUp(): void |
| 24 | + { |
| 25 | + $this->regexRepository = $this->createMock(BounceRegexRepository::class); |
| 26 | + $this->entityManager = $this->createMock(EntityManagerInterface::class); |
| 27 | + |
| 28 | + $this->manager = new BounceRegexManager( |
| 29 | + bounceRegexRepository: $this->regexRepository, |
| 30 | + entityManager: $this->entityManager |
| 31 | + ); |
| 32 | + } |
| 33 | + |
| 34 | + public function testCreateNewRegex(): void |
| 35 | + { |
| 36 | + $pattern = 'user unknown'; |
| 37 | + $expectedHash = md5($pattern); |
| 38 | + |
| 39 | + $this->regexRepository->expects($this->once()) |
| 40 | + ->method('findOneByRegexHash') |
| 41 | + ->with($expectedHash) |
| 42 | + ->willReturn(null); |
| 43 | + |
| 44 | + $this->regexRepository->expects($this->once()) |
| 45 | + ->method('save') |
| 46 | + ->with($this->isInstanceOf(BounceRegex::class)); |
| 47 | + |
| 48 | + $regex = $this->manager->createOrUpdateFromPattern( |
| 49 | + regex: $pattern, |
| 50 | + action: 'delete', |
| 51 | + listOrder: 5, |
| 52 | + admin: 1, |
| 53 | + comment: 'test', |
| 54 | + status: 'active' |
| 55 | + ); |
| 56 | + |
| 57 | + $this->assertInstanceOf(BounceRegex::class, $regex); |
| 58 | + $this->assertSame($pattern, $regex->getRegex()); |
| 59 | + $this->assertSame($expectedHash, $regex->getRegexHash()); |
| 60 | + $this->assertSame('delete', $regex->getAction()); |
| 61 | + $this->assertSame(5, $regex->getListOrder()); |
| 62 | + $this->assertSame(1, $regex->getAdmin()); |
| 63 | + $this->assertSame('test', $regex->getComment()); |
| 64 | + $this->assertSame('active', $regex->getStatus()); |
| 65 | + } |
| 66 | + |
| 67 | + public function testUpdateExistingRegex(): void |
| 68 | + { |
| 69 | + $pattern = 'mailbox full'; |
| 70 | + $hash = md5($pattern); |
| 71 | + |
| 72 | + $existing = new BounceRegex( |
| 73 | + regex: $pattern, |
| 74 | + regexHash: $hash, |
| 75 | + action: 'keep', |
| 76 | + listOrder: 0, |
| 77 | + admin: null, |
| 78 | + comment: null, |
| 79 | + status: 'inactive', |
| 80 | + count: 3 |
| 81 | + ); |
| 82 | + |
| 83 | + $this->regexRepository->expects($this->once()) |
| 84 | + ->method('findOneByRegexHash') |
| 85 | + ->with($hash) |
| 86 | + ->willReturn($existing); |
| 87 | + |
| 88 | + $this->regexRepository->expects($this->once()) |
| 89 | + ->method('save') |
| 90 | + ->with($existing); |
| 91 | + |
| 92 | + $updated = $this->manager->createOrUpdateFromPattern( |
| 93 | + regex: $pattern, |
| 94 | + action: 'delete', |
| 95 | + listOrder: 10, |
| 96 | + admin: 2, |
| 97 | + comment: 'upd', |
| 98 | + status: 'active' |
| 99 | + ); |
| 100 | + |
| 101 | + $this->assertSame('delete', $updated->getAction()); |
| 102 | + $this->assertSame(10, $updated->getListOrder()); |
| 103 | + $this->assertSame(2, $updated->getAdmin()); |
| 104 | + $this->assertSame('upd', $updated->getComment()); |
| 105 | + $this->assertSame('active', $updated->getStatus()); |
| 106 | + $this->assertSame($hash, $updated->getRegexHash()); |
| 107 | + } |
| 108 | + |
| 109 | + public function testDeleteRegex(): void |
| 110 | + { |
| 111 | + $model = $this->createMock(BounceRegex::class); |
| 112 | + |
| 113 | + $this->regexRepository->expects($this->once()) |
| 114 | + ->method('remove') |
| 115 | + ->with($model); |
| 116 | + |
| 117 | + $this->manager->delete($model); |
| 118 | + } |
| 119 | + |
| 120 | + public function testAssociateBounceIncrementsCountAndPersistsRelation(): void |
| 121 | + { |
| 122 | + $regex = new BounceRegex(regex: 'x', regexHash: md5('x')); |
| 123 | + |
| 124 | + $refRegex = new ReflectionProperty(BounceRegex::class, 'id'); |
| 125 | + $refRegex->setValue($regex, 7); |
| 126 | + |
| 127 | + $bounce = $this->createMock(Bounce::class); |
| 128 | + $bounce->method('getId')->willReturn(11); |
| 129 | + |
| 130 | + $this->entityManager->expects($this->once()) |
| 131 | + ->method('persist') |
| 132 | + ->with($this->callback(function ($entity) use ($regex) { |
| 133 | + return $entity instanceof BounceRegexBounce |
| 134 | + && $entity->getRegex() === $regex->getId(); |
| 135 | + })); |
| 136 | + |
| 137 | + $this->entityManager->expects($this->once()) |
| 138 | + ->method('flush'); |
| 139 | + |
| 140 | + $this->assertSame(0, $regex->getCount()); |
| 141 | + $this->manager->associateBounce($regex, $bounce); |
| 142 | + $this->assertSame(1, $regex->getCount()); |
| 143 | + } |
| 144 | +} |
0 commit comments