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