From fe319b2733a0968ee9ba25aa3b4594046f12a58e Mon Sep 17 00:00:00 2001 From: Daniel Seifert Date: Sat, 4 Jan 2025 22:39:02 +0100 Subject: [PATCH] subscription entity get a endpoint object, so it can persist modified properties --- src/Entities/Subscription.php | 26 ++++++++++++++ .../Resources/SubscriptionProcessTest.php | 36 +++++++++---------- 2 files changed, 44 insertions(+), 18 deletions(-) diff --git a/src/Entities/Subscription.php b/src/Entities/Subscription.php index fbf55ae..7c75649 100644 --- a/src/Entities/Subscription.php +++ b/src/Entities/Subscription.php @@ -15,11 +15,20 @@ namespace D3\KlicktippPhpClient\Entities; +use D3\KlicktippPhpClient\Exceptions\BaseException; use D3\KlicktippPhpClient\Resources\SubscriptionProcess; use Doctrine\Common\Collections\ArrayCollection; class Subscription extends ArrayCollection { + private ?SubscriptionProcess $endpoint; + + public function __construct(array $elements = [], ?SubscriptionProcess $endpoint = null) + { + $this->endpoint = $endpoint; + parent::__construct($elements); + } + public function getListId(): string { return $this->get(SubscriptionProcess::LISTID) ?? ''; @@ -30,6 +39,11 @@ class Subscription extends ArrayCollection return $this->get(SubscriptionProcess::NAME) ?? ''; } + public function setName(string $name): void + { + $this->set(SubscriptionProcess::NAME, $name); + } + public function getPendingUrl(): string { return $this->get(SubscriptionProcess::PENDINGURL) ?? ''; @@ -59,4 +73,16 @@ class Subscription extends ArrayCollection { return $this->get(SubscriptionProcess::USE_CHANGE_EMAIL); } + + /** + * @return null|bool + * @throws BaseException + */ + public function persist(): ?bool + { + return $this->endpoint?->update( + $this->getListId(), + $this->getName() + ); + } } diff --git a/tests/integration/Resources/SubscriptionProcessTest.php b/tests/integration/Resources/SubscriptionProcessTest.php index 8d84019..3eb56da 100644 --- a/tests/integration/Resources/SubscriptionProcessTest.php +++ b/tests/integration/Resources/SubscriptionProcessTest.php @@ -73,7 +73,7 @@ class SubscriptionProcessTest extends IntegrationTestCase * @covers \D3\KlicktippPhpClient\Resources\SubscriptionProcess::getEntity * @dataProvider getDataProvider */ - public function testGet(ResponseInterface $response, ?Subscription $expected, bool $expectException = false) + public function testGet(ResponseInterface $response, ?array $expected, bool $expectException = false) { $sut = new SubscriptionProcess($this->getConnectionMock($response)); @@ -81,14 +81,14 @@ class SubscriptionProcessTest extends IntegrationTestCase $this->expectException(BaseException::class); } - $this->assertEquals( - $expected, - $this->callMethod( - $sut, - 'getEntity', - ['470370'] - ) + $return = $this->callMethod( + $sut, + 'getEntity', + ['470370'] ); + + $this->assertInstanceOf(Subscription::class, $return); + $this->assertSame($expected, $return->toArray()); } public static function getDataProvider(): Generator @@ -101,7 +101,7 @@ class SubscriptionProcessTest extends IntegrationTestCase "'.SubscriptionProcess::USE_SINGLE_OPTIN.'": false, "'.SubscriptionProcess::RESEND_CONFIRMATION_EMAIL.'": false, "'.SubscriptionProcess::USE_CHANGE_EMAIL.'": false - }'), new Subscription([ + }'), [ SubscriptionProcess::LISTID => "470370", SubscriptionProcess::NAME => "name 1", SubscriptionProcess::PENDINGURL => "", @@ -109,7 +109,7 @@ class SubscriptionProcessTest extends IntegrationTestCase SubscriptionProcess::USE_SINGLE_OPTIN => false, SubscriptionProcess::RESEND_CONFIRMATION_EMAIL => false, SubscriptionProcess::USE_CHANGE_EMAIL => false, - ])]; + ]]; yield 'unknown id' => [new Response(404, [], '["Kein Opt-In-Prozess mit dieser ID."]'), null, true]; yield 'access denied' => [new Response(403, [], '["API Zugriff verweigert"]'), null, true]; } @@ -121,7 +121,7 @@ class SubscriptionProcessTest extends IntegrationTestCase * @covers \D3\KlicktippPhpClient\Resources\SubscriptionProcess::create * @dataProvider getDataProvider */ - public function testCreate(ResponseInterface $response, ?Subscription $expected, bool $expectException = false) + public function testCreate(ResponseInterface $response, ?array $expected, bool $expectException = false) { $sut = new SubscriptionProcess($this->getConnectionMock($response)); @@ -129,14 +129,14 @@ class SubscriptionProcessTest extends IntegrationTestCase $this->expectException(BaseException::class); } - $this->assertEquals( - $expected, - $this->callMethod( - $sut, - 'create', - ['newName'] - ) + $return = $this->callMethod( + $sut, + 'create', + ['newName'] ); + + $this->assertInstanceOf(Subscription::class, $return); + $this->assertSame($expected, $return->toArray()); } /**