MixpanelEventsProducerTest.php
2 years ago
MixpanelGroupsProducerTest.php
2 years ago
MixpanelPeopleProducerTest.php
2 years ago
index.php
2 years ago
MixpanelEventsProducerTest.php
128 lines
| 1 | <?php |
| 2 | if (!defined('ABSPATH')) exit; |
| 3 | class MixpanelEventsProducerTest extends PHPUnit_Framework_TestCase { |
| 4 | protected $_instance = null; |
| 5 | protected function setUp() |
| 6 | { |
| 7 | parent::setUp(); |
| 8 | $this->_instance = new Producers_MixpanelEvents("token"); |
| 9 | } |
| 10 | protected function tearDown() |
| 11 | { |
| 12 | parent::tearDown(); |
| 13 | $this->_instance->reset(); |
| 14 | $this->_instance = null; |
| 15 | } |
| 16 | public function testTrack() { |
| 17 | $this->_instance->track("test_event", array("number" => 1)); |
| 18 | $queue = $this->_instance->getQueue(); |
| 19 | $this->assertEquals(1, count($queue)); |
| 20 | $this->assertEquals("test_event", $queue[0]['event']); |
| 21 | $this->assertEquals(1, $queue[0]['properties']['number']); |
| 22 | } |
| 23 | public function testRegister() { |
| 24 | $this->_instance->register("super_property", "super_value"); |
| 25 | $this->assertEquals("super_value", $this->_instance->getProperty("super_property")); |
| 26 | } |
| 27 | public function testRegisterAll() { |
| 28 | $this->_instance->registerAll(array("prop1" => "val1", "prop2" => "val2")); |
| 29 | $this->assertEquals("val1", $this->_instance->getProperty("prop1")); |
| 30 | $this->assertEquals("val2", $this->_instance->getProperty("prop2")); |
| 31 | } |
| 32 | public function testRegisterOnce() { |
| 33 | $this->_instance->registerOnce("prop3", "val3"); |
| 34 | $this->_instance->registerOnce("prop3", "val4"); |
| 35 | $this->assertEquals("val3", $this->_instance->getProperty("prop3")); |
| 36 | } |
| 37 | public function testRegisterAllOnce() { |
| 38 | $this->_instance->registerAllOnce(array("prop5" => "val5", "prop6" => "val6")); |
| 39 | $this->_instance->registerAllOnce(array("prop5" => "val6", "prop6" => "val7")); |
| 40 | $this->assertEquals("val5", $this->_instance->getProperty("prop5")); |
| 41 | $this->assertEquals("val6", $this->_instance->getProperty("prop6")); |
| 42 | } |
| 43 | public function unregister() { |
| 44 | $this->_instance->register("prop7", "val7"); |
| 45 | $this->_instance->register("prop8", "val8"); |
| 46 | $this->assertEquals("val7", $this->_instance->getProperty("prop7")); |
| 47 | $this->assertEquals("val8", $this->_instance->getProperty("prop8")); |
| 48 | $this->_instance->unregister("prop7"); |
| 49 | $this->assertEquals(null, $this->_instance->getProperty("prop7")); |
| 50 | $this->assertEquals("val8", $this->_instance->getProperty("prop8")); |
| 51 | } |
| 52 | public function unregisterAll() { |
| 53 | $this->_instance->registerAll(array("prop9" => "val9", "prop10" => "val10")); |
| 54 | $this->assertEquals("val9", $this->_instance->getProperty("prop9")); |
| 55 | $this->assertEquals("val10", $this->_instance->getProperty("prop10")); |
| 56 | $this->assertEquals("val11", $this->_instance->getProperty("prop11")); |
| 57 | $this->_instance->unregisterAll(array("prop9", "prop10")); |
| 58 | $this->assertEquals(null, $this->_instance->getProperty("prop9")); |
| 59 | $this->assertEquals(null, $this->_instance->getProperty("prop10")); |
| 60 | $this->assertEquals("val11", $this->_instance->getProperty("prop11")); |
| 61 | } |
| 62 | public function testCreateAlias() { |
| 63 | $distinct_id = 1; |
| 64 | $alias = 2; |
| 65 | $msg = $this->_instance->createAlias($distinct_id, $alias); |
| 66 | $this->assertEquals('$create_alias', $msg['event']); |
| 67 | $this->assertEquals($distinct_id, $msg['properties']['distinct_id']); |
| 68 | $this->assertEquals($alias, $msg['properties']['alias']); |
| 69 | } |
| 70 | public function testCreateAliasRespectsConsumerSetting() { |
| 71 | $tmp_file = __DIR__ . '/test.tmp'; |
| 72 | $this->assertFileNotExists($tmp_file); |
| 73 | $options = array('consumer' => 'file', 'file' => $tmp_file); |
| 74 | $instance = new Producers_MixpanelEvents('token', $options); |
| 75 | try { |
| 76 | $instance->createAlias(1, 2); |
| 77 | $this->assertStringEqualsFile($tmp_file, '[{"event":"$create_alias","properties":{"distinct_id":1,"alias":2,"token":"token"}}]' . PHP_EOL); |
| 78 | } catch (Exception $e) { |
| 79 | unlink($tmp_file); |
| 80 | throw $e; |
| 81 | } |
| 82 | unlink($tmp_file); |
| 83 | } |
| 84 | public function testIdentifyInvalidAnonId() { |
| 85 | $user_id = 1; |
| 86 | $anon_id = 111; |
| 87 | $this->_instance->identify($user_id, $anon_id); |
| 88 | $queue = $this->_instance->getQueue(); |
| 89 | $this->assertEquals(0, count($queue)); |
| 90 | } |
| 91 | public function testIdentifyValidAnonId() { |
| 92 | $user_id = 1; |
| 93 | $anon_id = '2c93fdf3-4fbf-4fec-baaf-136ce87c13cc'; |
| 94 | $test = $this->_instance->identify($user_id, $anon_id); |
| 95 | $queue = $this->_instance->getQueue(); |
| 96 | $this->assertEquals(1, count($queue)); |
| 97 | $this->assertEquals('$identify', $queue[0]['event']); |
| 98 | $this->assertEquals($user_id, $queue[0]['properties']['$identified_id']); |
| 99 | $this->assertEquals($anon_id, $queue[0]['properties']['$anon_id']); |
| 100 | } |
| 101 | public function testIdentifyValidAnonIdLong() { |
| 102 | $user_id = 1; |
| 103 | $anon_id = '13bbf7943e584-0885c2531-5c793977-3e8000-13bbf7943e64cf'; |
| 104 | $test = $this->_instance->identify($user_id, $anon_id); |
| 105 | $queue = $this->_instance->getQueue(); |
| 106 | $this->assertEquals(1, count($queue)); |
| 107 | $this->assertEquals('$identify', $queue[0]['event']); |
| 108 | $this->assertEquals($user_id, $queue[0]['properties']['$identified_id']); |
| 109 | $this->assertEquals($anon_id, $queue[0]['properties']['$anon_id']); |
| 110 | } |
| 111 | public function testIdentifyValidAnonIdDevice() { |
| 112 | $user_id = 1; |
| 113 | $anon_id = '$device:13bbf7943e584-0885c2531-5c793977-3e8000-13bbf7943e64cf'; |
| 114 | $test = $this->_instance->identify($user_id, $anon_id); |
| 115 | $queue = $this->_instance->getQueue(); |
| 116 | $this->assertEquals(1, count($queue)); |
| 117 | $this->assertEquals('$identify', $queue[0]['event']); |
| 118 | $this->assertEquals($user_id, $queue[0]['properties']['$identified_id']); |
| 119 | $this->assertEquals($anon_id, $queue[0]['properties']['$anon_id']); |
| 120 | } |
| 121 | public function testIdentifyNoAnonId() { |
| 122 | $user_id = 1; |
| 123 | $test = $this->_instance->identify($user_id); |
| 124 | $queue = $this->_instance->getQueue(); |
| 125 | $this->assertEquals(0, count($queue)); |
| 126 | } |
| 127 | } |
| 128 |