MixpanelBaseProducerTest.php
71 lines
| 1 | <?php |
| 2 | if (!defined('ABSPATH')) exit; |
| 3 | class MixpanelBaseProducerTest extends PHPUnit_Framework_TestCase { |
| 4 | protected $_instance = null; |
| 5 | protected $_file = null; |
| 6 | protected function setUp() { |
| 7 | parent::setUp(); |
| 8 | $this->_file = dirname(__FILE__)."/output-".time().".txt"; |
| 9 | $this->_instance = new _Producers_MixpanelBaseProducer("token", array("consumer" => "file", "debug" => true, "file" => $this->_file)); |
| 10 | } |
| 11 | protected function tearDown() { |
| 12 | parent::tearDown(); |
| 13 | $this->_instance->reset(); |
| 14 | $this->_instance = null; |
| 15 | @unlink($this->_file); |
| 16 | } |
| 17 | public function testTokenMatch() { |
| 18 | $this->assertEquals("token", $this->_instance->getToken()); |
| 19 | } |
| 20 | public function testFlush() { |
| 21 | $event1 = array("event" => "test", "properties" => array("prop1" => "val1")); |
| 22 | $event2 = array("event" => "test2", "properties" => array("prop2" => "val2")); |
| 23 | $this->_instance->enqueue($event1); |
| 24 | $this->_instance->enqueue($event2); |
| 25 | $this->_instance->flush(1); |
| 26 | $contents = file_get_contents($this->_file); |
| 27 | $this->assertEquals('[{"event":"test","properties":{"prop1":"val1"}}]'."\n". |
| 28 | '[{"event":"test2","properties":{"prop2":"val2"}}]'."\n", $contents); |
| 29 | } |
| 30 | public function testReset() { |
| 31 | $event1 = array("event" => "test", "properties" => array("prop1" => "val1")); |
| 32 | $this->_instance->enqueue($event1); |
| 33 | $this->_instance->reset(); |
| 34 | $this->assertEmpty($this->_instance->getQueue()); |
| 35 | } |
| 36 | public function testEnqueue() { |
| 37 | $this->_instance->reset(); |
| 38 | $event1 = array("event" => "test", "properties" => array("prop1" => "val1")); |
| 39 | $this->_instance->enqueue($event1); |
| 40 | $queue = $this->_instance->getQueue(); |
| 41 | $this->assertCount(1, $queue); |
| 42 | $this->assertEquals($event1, $queue[0]); |
| 43 | } |
| 44 | public function testEnqueueAll() { |
| 45 | $this->_instance->reset(); |
| 46 | $event1 = array("event" => "test", "properties" => array("prop1" => "val1")); |
| 47 | $event2 = array("event" => "test2", "properties" => array("prop1" => "val1")); |
| 48 | $events = array($event1, $event2); |
| 49 | $this->_instance->enqueueAll($events); |
| 50 | $queue = $this->_instance->getQueue(); |
| 51 | $this->assertCount(2, $queue); |
| 52 | $this->assertEquals($event1, $queue[0]); |
| 53 | $this->assertEquals($event2, $queue[1]); |
| 54 | } |
| 55 | public function testSetMaxQueueSize() { |
| 56 | $this->_instance->enqueue(array("event" => "test")); |
| 57 | $queue = $this->_instance->getQueue(); |
| 58 | $this->assertEquals(1, count($queue)); |
| 59 | $this->_instance->flush(); |
| 60 | $new_instance = new Producers_MixpanelEvents("token", array('max_queue_size' => 0)); |
| 61 | $new_instance->track("test"); |
| 62 | $queue = $new_instance->getQueue(); |
| 63 | $this->assertEquals(0, count($queue)); |
| 64 | } |
| 65 | } |
| 66 | // stub for tests |
| 67 | class _Producers_MixpanelBaseProducer extends Producers_MixpanelBaseProducer { |
| 68 | function _getEndpoint() { |
| 69 | } |
| 70 | } |
| 71 |