Skip to content

Commit 1f814c1

Browse files
dmvdbruggejbwyme
authored andcommitted
Make createAlias adhere to the consumer config
Including test
1 parent d65f1e7 commit 1f814c1

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

lib/Producers/MixpanelEvents.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,22 @@ public function createAlias($original_id, $new_id) {
142142
"properties" => array("distinct_id" => $original_id, "alias" => $new_id, "token" => $this->_token)
143143
);
144144

145-
$options = array_merge($this->_options, array("endpoint" => $this->_getEndpoint(), "fork" => false));
146-
$curlConsumer = new ConsumerStrategies_CurlConsumer($options);
147-
$success = $curlConsumer->persist(array($msg));
145+
// Save the current fork/async options
146+
$old_fork = isset($this->_options['fork']) ? $this->_options['fork'] : false;
147+
$old_async = isset($this->_options['async']) ? $this->_options['async'] : false;
148+
149+
// Override fork/async to make the new consumer synchronous
150+
$this->_options['fork'] = false;
151+
$this->_options['async'] = false;
152+
153+
// The name is ambiguous, but this creates a new consumer with current $this->_options
154+
$consumer = $this->_getConsumer();
155+
$success = $consumer->persist(array($msg));
156+
157+
// Restore the original fork/async settings
158+
$this->_options['fork'] = $old_fork;
159+
$this->_options['async'] = $old_async;
160+
148161
if (!$success) {
149162
error_log("Creating Mixpanel Alias (original id: $original_id, new id: $new_id) failed");
150163
throw new Exception("Tried to create an alias but the call was not successful");
@@ -161,4 +174,4 @@ public function createAlias($original_id, $new_id) {
161174
function _getEndpoint() {
162175
return $this->_options['events_endpoint'];
163176
}
164-
}
177+
}

test/Producers/MixpanelEventsProducerTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,19 @@ public function testCreateAlias() {
8383
$this->assertEquals($original_id, $msg['properties']['distinct_id']);
8484
$this->assertEquals($new_id, $msg['properties']['alias']);
8585
}
86+
87+
public function testCreateAliasRespectsConsumerSetting() {
88+
$tmp_file = __DIR__ . '/test.tmp';
89+
$this->assertFileNotExists($tmp_file);
90+
91+
$options = array('consumer' => 'file', 'file' => $tmp_file);
92+
$instance = new Producers_MixpanelEvents('token', $options);
93+
94+
try {
95+
$instance->createAlias(1, 2);
96+
$this->assertStringEqualsFile($tmp_file, '[{"event":"$create_alias","properties":{"distinct_id":1,"alias":2,"token":"token"}}]' . PHP_EOL);
97+
} finally {
98+
unlink($tmp_file);
99+
}
100+
}
86101
}

0 commit comments

Comments
 (0)