Skip to content

Commit 747a7df

Browse files
committed
improve code base and add more tests
1 parent 445fb4d commit 747a7df

File tree

3 files changed

+41
-11
lines changed

3 files changed

+41
-11
lines changed

src/Api/General/Contact.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ private function updateContact(string $contactIdOrEmail, UpdateContact $contact)
107107
{
108108
return $this->handleResponse(
109109
$this->httpPut(
110-
path: $this->getBasePath() . '/' . $contactIdOrEmail,
110+
path: $this->getBasePath() . '/' . urlencode($contactIdOrEmail),
111111
body: ['contact' => $contact->toArray()]
112112
)
113113
);
@@ -122,7 +122,7 @@ private function updateContact(string $contactIdOrEmail, UpdateContact $contact)
122122
private function deleteContact(string $idOrEmail): ResponseInterface
123123
{
124124
return $this->handleResponse(
125-
$this->httpDelete($this->getBasePath() . '/' . $idOrEmail)
125+
$this->httpDelete($this->getBasePath() . '/' . urlencode($idOrEmail))
126126
);
127127
}
128128

src/DTO/Request/Contact/UpdateContact.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,15 @@ public function getUnsubscribed(): ?bool
5555

5656
public function toArray(): array
5757
{
58-
return [
59-
'email' => $this->getEmail(),
60-
'fields' => $this->getFields(),
61-
'list_ids_included' => $this->getListIdsIncluded(),
62-
'list_ids_excluded' => $this->getListIdsExcluded(),
63-
'unsubscribed' => $this->getUnsubscribed(),
64-
];
58+
return array_filter(
59+
[
60+
'email' => $this->getEmail(),
61+
'fields' => $this->getFields(),
62+
'list_ids_included' => $this->getListIdsIncluded(),
63+
'list_ids_excluded' => $this->getListIdsExcluded(),
64+
'unsubscribed' => $this->getUnsubscribed(),
65+
],
66+
fn($value) => $value !== null
67+
);
6568
}
6669
}

tests/Api/General/ContactTest.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public function testUpdateContactByEmail(): void
117117
$this->contact->expects($this->once())
118118
->method('httpPut')
119119
->with(
120-
AbstractApi::DEFAULT_HOST . '/api/accounts/' . self::FAKE_ACCOUNT_ID . '/contacts/' . $contactEmail,
120+
AbstractApi::DEFAULT_HOST . '/api/accounts/' . self::FAKE_ACCOUNT_ID . '/contacts/' . urlencode($contactEmail),
121121
[],
122122
['contact' => $contactDTO->toArray()]
123123
)
@@ -132,6 +132,33 @@ public function testUpdateContactByEmail(): void
132132
$this->assertEquals($contactEmail, $responseData['email']);
133133
}
134134

135+
public function testUpdateContactExcludesNullUnsubscribed(): void
136+
{
137+
$contactEmail = 'test@example.com';
138+
$contactDTO = new UpdateContact($contactEmail, ['last_name' => 'Smith'], [3], [1, 2]);
139+
140+
$this->contact->expects($this->once())
141+
->method('httpPut')
142+
->with(
143+
AbstractApi::DEFAULT_HOST . '/api/accounts/' . self::FAKE_ACCOUNT_ID . '/contacts/' . urlencode($contactEmail),
144+
[],
145+
$this->callback(function (array $payload) {
146+
$this->assertArrayHasKey('contact', $payload);
147+
$this->assertArrayNotHasKey('unsubscribed', $payload['contact']);
148+
return true;
149+
})
150+
)
151+
->willReturn(
152+
new Response(200, ['Content-Type' => 'application/json'], json_encode($this->getExpectedUpdateContactData()))
153+
);
154+
155+
$response = $this->contact->updateContactByEmail($contactEmail, $contactDTO);
156+
$responseData = ResponseHelper::toArray($response)['data'];
157+
158+
$this->assertInstanceOf(Response::class, $response);
159+
$this->assertEquals('unsubscribed', $responseData['status']);
160+
}
161+
135162
public function testDeleteContactById(): void
136163
{
137164
$contactId = '019706a8-9612-77be-8586-4f26816b467a';
@@ -151,7 +178,7 @@ public function testDeleteContactByEmail(): void
151178
$contactEmail = 'test@example.com';
152179
$this->contact->expects($this->once())
153180
->method('httpDelete')
154-
->with(AbstractApi::DEFAULT_HOST . '/api/accounts/' . self::FAKE_ACCOUNT_ID . '/contacts/' . $contactEmail)
181+
->with(AbstractApi::DEFAULT_HOST . '/api/accounts/' . self::FAKE_ACCOUNT_ID . '/contacts/' . urlencode($contactEmail))
155182
->willReturn(new Response(204));
156183

157184
$response = $this->contact->deleteContactByEmail($contactEmail);

0 commit comments

Comments
 (0)