|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpList\Core\Tests\Unit\Domain\Subscription\Service\Provider; |
| 6 | + |
| 7 | +use PhpList\Core\Domain\Subscription\Model\Dto\ChangeSetDto; |
| 8 | +use PhpList\Core\Domain\Subscription\Model\Subscriber; |
| 9 | +use PhpList\Core\Domain\Subscription\Model\SubscriberAttributeDefinition; |
| 10 | +use PhpList\Core\Domain\Subscription\Model\SubscriberAttributeValue; |
| 11 | +use PhpList\Core\Domain\Subscription\Repository\SubscriberAttributeValueRepository; |
| 12 | +use PhpList\Core\Domain\Subscription\Service\Provider\SubscriberAttributeChangeSetProvider; |
| 13 | +use PhpList\Core\Domain\Subscription\Service\Resolver\AttributeValueResolver; |
| 14 | +use PHPUnit\Framework\MockObject\MockObject; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | + |
| 17 | +final class SubscriberAttributeChangeSetProviderTest extends TestCase |
| 18 | +{ |
| 19 | + private AttributeValueResolver&MockObject $resolver; |
| 20 | + private SubscriberAttributeValueRepository&MockObject $repository; |
| 21 | + private SubscriberAttributeChangeSetProvider $provider; |
| 22 | + |
| 23 | + protected function setUp(): void |
| 24 | + { |
| 25 | + $this->resolver = $this->createMock(AttributeValueResolver::class); |
| 26 | + $this->resolver |
| 27 | + ->method('resolve') |
| 28 | + ->willReturnCallback(function (SubscriberAttributeValue $attr) { |
| 29 | + return $attr->getValue(); |
| 30 | + }); |
| 31 | + |
| 32 | + $this->repository = $this->createMock(SubscriberAttributeValueRepository::class); |
| 33 | + |
| 34 | + $this->provider = new SubscriberAttributeChangeSetProvider( |
| 35 | + resolver: $this->resolver, |
| 36 | + attributesRepository: $this->repository, |
| 37 | + ); |
| 38 | + } |
| 39 | + |
| 40 | + public function testNoChangesWhenNewAndExistingAreIdenticalCaseInsensitive(): void |
| 41 | + { |
| 42 | + $subscriber = new Subscriber(); |
| 43 | + $existing = [ |
| 44 | + $this->attr('Name', 'John', $subscriber), |
| 45 | + $this->attr('Age', '30', $subscriber), |
| 46 | + ]; |
| 47 | + |
| 48 | + $this->repository->expects(self::once()) |
| 49 | + ->method('getForSubscriber') |
| 50 | + ->with($subscriber) |
| 51 | + ->willReturn($existing); |
| 52 | + |
| 53 | + $newData = [ |
| 54 | + 'name' => 'John', |
| 55 | + 'AGE' => '30', |
| 56 | + ]; |
| 57 | + |
| 58 | + $changeSet = $this->provider->getAttributeChangeSet($subscriber, $newData); |
| 59 | + |
| 60 | + self::assertInstanceOf(ChangeSetDto::class, $changeSet); |
| 61 | + self::assertFalse($changeSet->hasChanges()); |
| 62 | + self::assertSame([], $changeSet->getChanges()); |
| 63 | + } |
| 64 | + |
| 65 | + public function testAddedAttributeAppearsWithNullOldValue(): void |
| 66 | + { |
| 67 | + $subscriber = new Subscriber(); |
| 68 | + $existing = [ |
| 69 | + $this->attr('Name', 'John', $subscriber), |
| 70 | + ]; |
| 71 | + |
| 72 | + $this->repository->method('getForSubscriber')->willReturn($existing); |
| 73 | + |
| 74 | + $newData = [ |
| 75 | + 'name' => 'John', |
| 76 | + 'city' => 'NY', |
| 77 | + ]; |
| 78 | + |
| 79 | + $changeSet = $this->provider->getAttributeChangeSet($subscriber, $newData); |
| 80 | + |
| 81 | + self::assertTrue($changeSet->hasField('city')); |
| 82 | + self::assertSame([null, 'NY'], $changeSet->getFieldChange('city')); |
| 83 | + |
| 84 | + self::assertSame(['city' => [null, 'NY']], $changeSet->getChanges()); |
| 85 | + } |
| 86 | + |
| 87 | + public function testRemovedAttributeAppearsWithNullNewValue(): void |
| 88 | + { |
| 89 | + $subscriber = new Subscriber(); |
| 90 | + $existing = [ |
| 91 | + $this->attr('Country', 'US', $subscriber), |
| 92 | + ]; |
| 93 | + |
| 94 | + $this->repository->method('getForSubscriber')->willReturn($existing); |
| 95 | + |
| 96 | + $changeSet = $this->provider->getAttributeChangeSet($subscriber, []); |
| 97 | + |
| 98 | + self::assertTrue($changeSet->hasField('country')); |
| 99 | + self::assertSame(['US', null], $changeSet->getFieldChange('country')); |
| 100 | + self::assertSame(['country' => ['US', null]], $changeSet->getChanges()); |
| 101 | + } |
| 102 | + |
| 103 | + public function testChangedAttributeShowsOldAndNewValues(): void |
| 104 | + { |
| 105 | + $subscriber = new Subscriber(); |
| 106 | + $existing = [ |
| 107 | + $this->attr('Phone', '123', $subscriber), |
| 108 | + ]; |
| 109 | + |
| 110 | + $this->repository->method('getForSubscriber')->willReturn($existing); |
| 111 | + |
| 112 | + $newData = [ |
| 113 | + 'phone' => '456', |
| 114 | + ]; |
| 115 | + |
| 116 | + $changeSet = $this->provider->getAttributeChangeSet($subscriber, $newData); |
| 117 | + |
| 118 | + self::assertSame(['123', '456'], $changeSet->getFieldChange('phone')); |
| 119 | + self::assertSame(['phone' => ['123', '456']], $changeSet->getChanges()); |
| 120 | + } |
| 121 | + |
| 122 | + public function testIgnoredAttributesAreExcluded(): void |
| 123 | + { |
| 124 | + $subscriber = new Subscriber(); |
| 125 | + $existing = [ |
| 126 | + $this->attr('Password', 'old', $subscriber), |
| 127 | + $this->attr('Modified', 'yesterday', $subscriber), |
| 128 | + $this->attr('Nickname', 'Bob', $subscriber), |
| 129 | + ]; |
| 130 | + |
| 131 | + $this->repository->method('getForSubscriber')->willReturn($existing); |
| 132 | + |
| 133 | + $newData = [ |
| 134 | + 'password' => 'new', |
| 135 | + 'MODIFIED' => null, |
| 136 | + 'nickname' => 'Bobby', |
| 137 | + ]; |
| 138 | + |
| 139 | + $changeSet = $this->provider->getAttributeChangeSet($subscriber, $newData); |
| 140 | + |
| 141 | + self::assertFalse($changeSet->hasField('password')); |
| 142 | + self::assertFalse($changeSet->hasField('modified')); |
| 143 | + self::assertTrue($changeSet->hasField('nickname')); |
| 144 | + self::assertSame(['Bob', 'Bobby'], $changeSet->getFieldChange('nickname')); |
| 145 | + self::assertSame(['nickname' => ['Bob', 'Bobby']], $changeSet->getChanges()); |
| 146 | + } |
| 147 | + |
| 148 | + public function testCaseInsensitiveKeyComparisonAndResultLowercasing(): void |
| 149 | + { |
| 150 | + $subscriber = new Subscriber(); |
| 151 | + $existing = [ |
| 152 | + $this->attr('FirstName', 'Ann', $subscriber), |
| 153 | + ]; |
| 154 | + |
| 155 | + $this->repository->method('getForSubscriber')->willReturn($existing); |
| 156 | + |
| 157 | + $newData = [ |
| 158 | + 'firstname' => 'Anna', |
| 159 | + ]; |
| 160 | + |
| 161 | + $changeSet = $this->provider->getAttributeChangeSet($subscriber, $newData); |
| 162 | + |
| 163 | + self::assertTrue($changeSet->hasField('firstname')); |
| 164 | + self::assertSame(['Ann', 'Anna'], $changeSet->getFieldChange('firstname')); |
| 165 | + self::assertSame(['firstname' => ['Ann', 'Anna']], $changeSet->getChanges()); |
| 166 | + } |
| 167 | + |
| 168 | + private function attr(string $name, ?string $value, Subscriber $subscriber): SubscriberAttributeValue |
| 169 | + { |
| 170 | + $def = (new SubscriberAttributeDefinition())->setName($name); |
| 171 | + $attr = new SubscriberAttributeValue($def, $subscriber); |
| 172 | + $attr->setValue($value); |
| 173 | + return $attr; |
| 174 | + } |
| 175 | +} |
0 commit comments