From 880608ed642462011a4a9ee35dbf3a4c93ddd0a6 Mon Sep 17 00:00:00 2001 From: Daniel Seifert Date: Thu, 2 Jan 2025 23:51:20 +0100 Subject: [PATCH] remove unneccessary convert method --- src/Resources/Model.php | 10 --- src/Resources/Subscriber.php | 71 ++++++++++--------- .../integration/Resources/SubscriberTest.php | 46 ++++++------ 3 files changed, 61 insertions(+), 66 deletions(-) diff --git a/src/Resources/Model.php b/src/Resources/Model.php index b388892..d8e3e29 100644 --- a/src/Resources/Model.php +++ b/src/Resources/Model.php @@ -25,14 +25,4 @@ abstract class Model { $this->connection = $connection; } - - protected function convertDataArrayToUrlParameters(array $data): array - { - $urlFields = []; - foreach (array_filter($data) as $key => $value) { - $urlFields['fields['.$key.']'] = $value; - } - - return $urlFields; - } } diff --git a/src/Resources/Subscriber.php b/src/Resources/Subscriber.php index 0c7e068..3ce9b1d 100644 --- a/src/Resources/Subscriber.php +++ b/src/Resources/Subscriber.php @@ -82,16 +82,15 @@ class Subscriber extends Model 'subscriber.json', [ RequestOptions::FORM_PARAMS => array_filter( - array_merge( - $this->convertDataArrayToUrlParameters($fields ?? []), - [ - 'email' => trim($mailAddress), - 'listid' => trim($listId ?? ''), - 'tagid' => trim($tagId ?? ''), - 'fields' => array_filter($fields ?? []), - 'smsnumber' => trim($smsNumber ?? ''), - ] - ) + [ + 'email' => trim($mailAddress), + 'listid' => trim($listId ?? ''), + 'tagid' => trim($tagId ?? ''), + 'fields' => array_filter( + array_map('trim', $fields ?? []) + ), + 'smsnumber' => trim($smsNumber ?? ''), + ] ), ] ) @@ -127,12 +126,14 @@ class Subscriber extends Model 'POST', 'subscriber/tag.json', [ - RequestOptions::FORM_PARAMS => [ - 'email' => trim($mailAddress), - 'tagids' => implode(',', array_filter( - array_map('trim', $tagIds) - )), - ], + RequestOptions::FORM_PARAMS => array_filter( + [ + 'email' => trim($mailAddress), + 'tagids' => array_filter( + array_map('trim', $tagIds) + ), + ] + ), ] ) ); @@ -189,13 +190,13 @@ class Subscriber extends Model 'subscriber/'.urlencode(trim($subscriberId)).'.json', [ RequestOptions::FORM_PARAMS => array_filter( - array_merge( - $this->convertDataArrayToUrlParameters($fields ?? []), - [ - 'newemail' => trim($newEmail ?? ''), - 'newsmsnumber' => trim($newSmsNumber ?? ''), - ] - ) + [ + 'newemail' => trim($newEmail ?? ''), + 'newsmsnumber' => trim($newSmsNumber ?? ''), + 'fields' => array_filter( + array_map('trim', $fields ?? []) + ), + ] ), ] ) @@ -219,22 +220,26 @@ class Subscriber extends Model /** * @throws BaseException */ - public function signin(string $apikey, string $emailAddress, ?array $fields = null, ?string $smsNumber = null): string - { + public function signin( + string $apikey, + string $emailAddress, + ?array $fields = null, + ?string $smsNumber = null + ): string { return current( $this->connection->requestAndParse( 'POST', 'subscriber/signin.json', [ RequestOptions::FORM_PARAMS => array_filter( - array_merge( - $this->convertDataArrayToUrlParameters($fields ?? []), - [ - 'apikey' => trim($apikey), - 'email' => trim($emailAddress), - 'smsnumber' => trim($smsNumber ?? ''), - ] - ) + [ + 'apikey' => trim($apikey), + 'email' => trim($emailAddress), + 'smsnumber' => trim($smsNumber ?? ''), + 'fields' => array_filter( + array_map('trim', $fields ?? []) + ), + ] ), ] ) diff --git a/tests/integration/Resources/SubscriberTest.php b/tests/integration/Resources/SubscriberTest.php index 587e680..b3a4fe6 100644 --- a/tests/integration/Resources/SubscriberTest.php +++ b/tests/integration/Resources/SubscriberTest.php @@ -240,9 +240,16 @@ class SubscriberTest extends IntegrationTestCase $this->callMethod( $sut, 'subscribe', - ['testsubscriber@mydomain.com'] + ['testsubscriber@mydomain.com', '1234567', '2345678', ['field1' => 'abcd', 'field2' => 'efgh']] ) ); + + /** @var RequestInterface $request */ + $request = current($this->getHistoryContainer())['request']; + $requestFormParams = $request->getBody()->getContents(); + $this->assertMatchesRegularExpression('/fields%5Bfield1%5D=abcd/m', $requestFormParams); + $this->assertMatchesRegularExpression('/fields%5Bfield2%5D=efgh/m', $requestFormParams); + $this->assertMatchesRegularExpression('/email=testsubscriber%40mydomain.com/m', $requestFormParams); } public static function subscribeDataProvider(): Generator @@ -342,6 +349,13 @@ class SubscriberTest extends IntegrationTestCase ['testsubscriber@mydomain.com', ['2354758', ' 2354858']] ) ); + + /** @var RequestInterface $request */ + $request = current($this->getHistoryContainer())['request']; + $requestFormParams = $request->getBody()->getContents(); + $this->assertMatchesRegularExpression('/tagids%5B0%5D=2354758/m', $requestFormParams); + $this->assertMatchesRegularExpression('/tagids%5B1%5D=2354858/m', $requestFormParams); + $this->assertMatchesRegularExpression('/email=testsubscriber%40mydomain.com/m', $requestFormParams); } public static function tagDataProvider(): Generator @@ -508,9 +522,16 @@ class SubscriberTest extends IntegrationTestCase $this->callMethod( $sut, 'signin', - ['7gefzp8255z8z8469', 'subsriber@mydomain.com'] + ['7gefzp8255z8z8469', 'subsriber@mydomain.com', ['field1' => 'abcd', 'field2' => 'efgh']] ) ); + + /** @var RequestInterface $request */ + $request = current($this->getHistoryContainer())['request']; + $requestFormParams = $request->getBody()->getContents(); + $this->assertMatchesRegularExpression('/fields%5Bfield1%5D=abcd/m', $requestFormParams); + $this->assertMatchesRegularExpression('/fields%5Bfield2%5D=efgh/m', $requestFormParams); + $this->assertMatchesRegularExpression('/apikey=7gefzp8255z8z8469/m', $requestFormParams); } public static function signinDataProvider(): Generator @@ -670,25 +691,4 @@ class SubscriberTest extends IntegrationTestCase ['myMailAddress'] ); } - - /** - * @test - * @throws ReflectionException - * @covers \D3\KlicktippPhpClient\Resources\Model::convertDataArrayToUrlParameters - */ - public function testConvertDataArrayToUrlParameters() - { - $sut = $this->getMockBuilder(Subscriber::class) - ->disableOriginalConstructor() - ->getMock(); - - $this->assertSame( - ['fields[field1]' => 'value1'], - $this->callMethod( - $sut, - 'convertDataArrayToUrlParameters', - [['field1' => 'value1']] - ) - ); - } }