Skip to content

Commit 273e99a

Browse files
committed
Filter empty associative arrays to nulls when passing event metadata
1 parent a0a1bd9 commit 273e99a

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

src/Intercom/IntercomBasicAuthClient.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,13 @@ public static function factory($config = [])
3434

3535
return $client;
3636
}
37+
38+
public static function filterEmptyList($possiblyEmpty)
39+
{
40+
if (empty($possiblyEmpty)) {
41+
return NULL;
42+
} else {
43+
return $possiblyEmpty;
44+
}
45+
}
3746
}

src/Intercom/Service/config/intercom_public_event.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@
2424
"metadata": {
2525
"location": "json",
2626
"required": false,
27-
"type": "array"
27+
"type": "array",
28+
"filters": [
29+
{
30+
"method": "Intercom\\IntercomBasicAuthClient::filterEmptyList",
31+
"args": ["@value"]
32+
}
33+
]
2834
},
2935
"user_id": {
3036
"location": "json",

tests/Intercom/Resources/EventTest.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,35 @@ public function testCreateEvent()
1313
$this->assertRequestJson(['created_at' => 1401970113, 'event_name' => 'invited-friend']);
1414
}
1515

16+
/*
17+
* Empty associative arrays get mapped to [] in PHP
18+
* Check that we filter them out to nulls, which are valid on the server
19+
*/
20+
public function testNoMetadata()
21+
{
22+
$this->setMockResponse($this->client, 'Event/Event.txt');
23+
$this->client->createEvent(['metadata' => [], 'created_at' => 1401970113, 'event_name' => 'invited-friend']);
24+
25+
$this->assertRequest('POST', '/events');
26+
$body = $this->getOnlyMockedRequest()->getBody()->__toString();
27+
$json = json_decode($body);
28+
$this->assertEquals(NULL, $json->metadata);
29+
}
30+
31+
/*
32+
* Check that our array filtering doesn't interfere with valid metadata
33+
*/
34+
public function testMetadata()
35+
{
36+
$this->setMockResponse($this->client, 'Event/Event.txt');
37+
$this->client->createEvent(['metadata' => ['foo' => 'bar'], 'created_at' => 1401970113, 'event_name' => 'invited-friend']);
38+
39+
$this->assertRequest('POST', '/events');
40+
$body = $this->getOnlyMockedRequest()->getBody()->__toString();
41+
$json = json_decode($body);
42+
$this->assertEquals('bar', $json->metadata->foo);
43+
}
44+
1645
/**
1746
* @expectedException \Guzzle\Service\Exception\ValidationException
1847
*/
@@ -36,4 +65,4 @@ public function testCreateEventNoEventName()
3665
{
3766
$this->client->createEvent(['created_at' => 1401970113]);
3867
}
39-
}
68+
}

0 commit comments

Comments
 (0)