From d7b24bb81c963c65062307814f918a580842bc37 Mon Sep 17 00:00:00 2001 From: Daniel Seifert Date: Thu, 16 Jan 2025 15:16:56 +0100 Subject: [PATCH] can reset subscription from subscriber entity --- src/Entities/Subscriber.php | 26 +++++++++++++++++ tests/unit/Entities/SubscriberTest.php | 40 ++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/src/Entities/Subscriber.php b/src/Entities/Subscriber.php index 9a5c16c..49f361a 100644 --- a/src/Entities/Subscriber.php +++ b/src/Entities/Subscriber.php @@ -368,6 +368,32 @@ class Subscriber extends Entity } } + /** + * @param string $listId + * @param string|null $tagId + * @param array|null $fields + * @param string|null $smsNumber + * + * @return void + * @throws CommunicationException + */ + public function resubscribe( + string $listId, + ?string $tagId = null, + ?array $fields = null, + ?string $smsNumber = null + ): void { + if (!$this->isSubscribed()) { + $this->endpoint->subscribe( + $this->getEmailAddress(), + $listId, + $tagId, + $fields, + $smsNumber + ); + } + } + // missing getters (return is timestamp list) // smart_links SmartLinks diff --git a/tests/unit/Entities/SubscriberTest.php b/tests/unit/Entities/SubscriberTest.php index 3e7c27e..d7c063c 100644 --- a/tests/unit/Entities/SubscriberTest.php +++ b/tests/unit/Entities/SubscriberTest.php @@ -978,4 +978,44 @@ class SubscriberTest extends TestCase yield ['getStartedCampaignTime', 'getStartedCampaigns']; yield ['getFinishedCampaignTime', 'getFinishedCampaigns']; } + + /** + * @test + * @throws ReflectionException + * @covers \D3\KlicktippPhpClient\Entities\Subscriber::resubscribe + * @dataProvider resubscribeDataProvider + */ + public function testReSubscribe( + bool $endpointSet, + InvokedCount $subscribeExpections, + bool $isSubscribed + ): void + { + $endpointMock = $this->getMockBuilder(SubscriberEndpoint::class) + ->disableOriginalConstructor() + ->onlyMethods(['subscribe']) + ->getMock(); + $endpointMock->expects($subscribeExpections)->method('subscribe'); + + $sut = $this->getMockBuilder(Subscriber::class) + ->setConstructorArgs([[], $endpointSet ? $endpointMock : null]) + ->onlyMethods(['isSubscribed', 'getEmailAddress']) + ->getMock(); + $sut->method('isSubscribed')->willReturn($isSubscribed); + $sut->method('getEmailAddress')->willReturn('mail@mydomain.tld'); + + $this->callMethod( + $sut, + 'resubscribe', + ['listId', 'tagid'] + ); + } + + public static function resubscribeDataProvider(): Generator + { + yield 'already subscribed, has endpoint' => [true, self::never(), true]; + yield 'unsubscribed, has endpoint' => [true, self::once(), false]; + yield 'already subscribed, no endpoint' => [false, self::never(), true]; + yield 'unsubscribed, no endpoint' => [false, self::never(), false]; + } }