Skip to content

Commit

Permalink
Make createAlias adhere to the consumer config
Browse files Browse the repository at this point in the history
Including test
  • Loading branch information
dmvdbrugge authored and jbwyme committed Jul 8, 2019
1 parent d65f1e7 commit 1f814c1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
21 changes: 17 additions & 4 deletions lib/Producers/MixpanelEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,22 @@ public function createAlias($original_id, $new_id) {
"properties" => array("distinct_id" => $original_id, "alias" => $new_id, "token" => $this->_token)
);

$options = array_merge($this->_options, array("endpoint" => $this->_getEndpoint(), "fork" => false));
$curlConsumer = new ConsumerStrategies_CurlConsumer($options);
$success = $curlConsumer->persist(array($msg));
// Save the current fork/async options
$old_fork = isset($this->_options['fork']) ? $this->_options['fork'] : false;
$old_async = isset($this->_options['async']) ? $this->_options['async'] : false;

// Override fork/async to make the new consumer synchronous
$this->_options['fork'] = false;
$this->_options['async'] = false;

// The name is ambiguous, but this creates a new consumer with current $this->_options
$consumer = $this->_getConsumer();
$success = $consumer->persist(array($msg));

// Restore the original fork/async settings
$this->_options['fork'] = $old_fork;
$this->_options['async'] = $old_async;

if (!$success) {
error_log("Creating Mixpanel Alias (original id: $original_id, new id: $new_id) failed");
throw new Exception("Tried to create an alias but the call was not successful");
Expand All @@ -161,4 +174,4 @@ public function createAlias($original_id, $new_id) {
function _getEndpoint() {
return $this->_options['events_endpoint'];
}
}
}
15 changes: 15 additions & 0 deletions test/Producers/MixpanelEventsProducerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,19 @@ public function testCreateAlias() {
$this->assertEquals($original_id, $msg['properties']['distinct_id']);
$this->assertEquals($new_id, $msg['properties']['alias']);
}

public function testCreateAliasRespectsConsumerSetting() {
$tmp_file = __DIR__ . '/test.tmp';
$this->assertFileNotExists($tmp_file);

$options = array('consumer' => 'file', 'file' => $tmp_file);
$instance = new Producers_MixpanelEvents('token', $options);

try {
$instance->createAlias(1, 2);
$this->assertStringEqualsFile($tmp_file, '[{"event":"$create_alias","properties":{"distinct_id":1,"alias":2,"token":"token"}}]' . PHP_EOL);
} finally {
unlink($tmp_file);
}
}
}

0 comments on commit 1f814c1

Please sign in to comment.