first commit

This commit is contained in:
2024-12-17 13:43:22 +01:00
commit 8e6cd8b410
21292 changed files with 3514826 additions and 0 deletions

View File

@@ -0,0 +1,208 @@
<?php
require_once(dirname(__FILE__) . "/../lib/Segment.php");
class AnalyticsTest extends PHPUnit_Framework_TestCase {
function setUp() {
date_default_timezone_set("UTC");
Segment::init("oq0vdlg7yi", array("debug" => true));
}
function testTrack() {
$this->assertTrue(Segment::track(array(
"userId" => "john",
"event" => "Module PHP Event"
)));
}
function testGroup(){
$this->assertTrue(Segment::group(array(
"groupId" => "group-id",
"userId" => "user-id",
"traits" => array(
"plan" => "startup"
)
)));
}
function testMicrotime(){
$this->assertTrue(Segment::page(array(
"anonymousId" => "anonymous-id",
"name" => "analytics-php-microtime",
"category" => "docs",
"timestamp" => microtime(true),
"properties" => array(
"path" => "/docs/libraries/php/",
"url" => "https://segment.io/docs/libraries/php/"
)
)));
}
function testPage(){
$this->assertTrue(Segment::page(array(
"anonymousId" => "anonymous-id",
"name" => "analytics-php",
"category" => "docs",
"properties" => array(
"path" => "/docs/libraries/php/",
"url" => "https://segment.io/docs/libraries/php/"
)
)));
}
function testBasicPage(){
$this->assertTrue(Segment::page(array(
"anonymousId" => "anonymous-id"
)));
}
function testScreen(){
$this->assertTrue(Segment::screen(array(
"anonymousId" => "anonymous-id",
"name" => "2048",
"category" => "game built with php :)",
"properties" => array(
"points" => 300
)
)));
}
function testBasicScreen(){
$this->assertTrue(Segment::screen(array(
"anonymousId" => "anonymous-id"
)));
}
function testIdentify() {
$this->assertTrue(Segment::identify(array(
"userId" => "doe",
"traits" => array(
"loves_php" => false,
"birthday" => time()
)
)));
}
function testEmptyTraits() {
$this->assertTrue(Segment::identify(array(
"userId" => "empty-traits"
)));
$this->assertTrue(Segment::group(array(
"userId" => "empty-traits",
"groupId" => "empty-traits"
)));
}
function testEmptyArrayTraits() {
$this->assertTrue(Segment::identify(array(
"userId" => "empty-traits",
"traits" => array()
)));
$this->assertTrue(Segment::group(array(
"userId" => "empty-traits",
"groupId" => "empty-traits",
"traits" => array()
)));
}
function testEmptyProperties() {
$this->assertTrue(Segment::track(array(
"userId" => "user-id",
"event" => "empty-properties"
)));
$this->assertTrue(Segment::page(array(
"category" => "empty-properties",
"name" => "empty-properties",
"userId" => "user-id"
)));
}
function testEmptyArrayProperties(){
$this->assertTrue(Segment::track(array(
"userId" => "user-id",
"event" => "empty-properties",
"properties" => array()
)));
$this->assertTrue(Segment::page(array(
"category" => "empty-properties",
"name" => "empty-properties",
"userId" => "user-id",
"properties" => array()
)));
}
function testAlias() {
$this->assertTrue(Segment::alias(array(
"previousId" => "previous-id",
"userId" => "user-id"
)));
}
function testContextEmpty() {
$this->assertTrue(Segment::track(array(
"userId" => "user-id",
"event" => "Context Test",
"context" => array()
)));
}
function testContextCustom() {
$this->assertTrue(Segment::track(array(
"userId" => "user-id",
"event" => "Context Test",
"context" => array(
"active" => false
)
)));
}
function testTimestamps() {
$this->assertTrue(Segment::track(array(
"userId" => "user-id",
"event" => "integer-timestamp",
"timestamp" => (int) mktime(0, 0, 0, date('n'), 1, date('Y'))
)));
$this->assertTrue(Segment::track(array(
"userId" => "user-id",
"event" => "string-integer-timestamp",
"timestamp" => (string) mktime(0, 0, 0, date('n'), 1, date('Y'))
)));
$this->assertTrue(Segment::track(array(
"userId" => "user-id",
"event" => "iso8630-timestamp",
"timestamp" => date(DATE_ATOM, mktime(0, 0, 0, date('n'), 1, date('Y')))
)));
$this->assertTrue(Segment::track(array(
"userId" => "user-id",
"event" => "iso8601-timestamp",
"timestamp" => date(DATE_ATOM, mktime(0, 0, 0, date('n'), 1, date('Y')))
)));
$this->assertTrue(Segment::track(array(
"userId" => "user-id",
"event" => "strtotime-timestamp",
"timestamp" => strtotime('1 week ago')
)));
$this->assertTrue(Segment::track(array(
"userId" => "user-id",
"event" => "microtime-timestamp",
"timestamp" => microtime(true)
)));
$this->assertTrue(Segment::track(array(
"userId" => "user-id",
"event" => "invalid-float-timestamp",
"timestamp" => ((string) mktime(0, 0, 0, date('n'), 1, date('Y'))) . '.'
)));
}
}
?>

View File

@@ -0,0 +1,122 @@
<?php
require_once(dirname(__FILE__) . "/../lib/Segment/Client.php");
class ConsumerFileTest extends PHPUnit_Framework_TestCase {
private $client;
private $filename = "/tmp/analytics.log";
function setUp() {
date_default_timezone_set("UTC");
if (file_exists($this->filename()))
unlink($this->filename());
$this->client = new Segment_Client("oq0vdlg7yi",
array("consumer" => "file",
"filename" => $this->filename));
}
function tearDown(){
if (file_exists($this->filename))
unlink($this->filename);
}
function testTrack() {
$this->assertTrue($this->client->track(array(
"userId" => "some-user",
"event" => "File PHP Event - Microtime",
"timestamp" => microtime(true),
)));
$this->checkWritten("track");
}
function testIdentify() {
$this->assertTrue($this->client->identify(array(
"userId" => "Calvin",
"traits" => array(
"loves_php" => false,
"type" => "analytics.log",
"birthday" => time()
)
)));
$this->checkWritten("identify");
}
function testGroup(){
$this->assertTrue($this->client->group(array(
"userId" => "user-id",
"groupId" => "group-id",
"traits" => array(
"type" => "consumer analytics.log test",
)
)));
}
function testPage(){
$this->assertTrue($this->client->page(array(
"userId" => "user-id",
"name" => "analytics-php",
"category" => "analytics.log",
"properties" => array(
"url" => "https://a.url/"
)
)));
}
function testScreen(){
$this->assertTrue($this->client->screen(array(
"userId" => "userId",
"name" => "grand theft auto",
"category" => "analytics.log",
"properties" => array()
)));
}
function testAlias () {
$this->assertTrue($this->client->alias(array(
"previousId" => "previous-id",
"userId" => "user-id"
)));
$this->checkWritten("alias");
}
function testSend(){
for ($i = 0; $i < 200; $i++) {
$this->client->track(array(
"userId" => "userId",
"event" => "event"
));
}
exec("php --define date.timezone=UTC send.php --secret oq0vdlg7yi --file /tmp/analytics.log", $output);
$this->assertEquals("sent 200 from 200 requests successfully", trim($output[0]));
$this->assertFalse(file_exists($this->filename()));
}
function testProductionProblems() {
# Open to a place where we should not have write access.
$client = new Segment_Client("oq0vdlg7yi",
array("consumer" => "file",
"filename" => "/dev/xxxxxxx" ));
$tracked = $client->track(array("userId" => "some-user", "event" => "my event"));
$this->assertFalse($tracked);
}
function checkWritten($type) {
exec("wc -l " . $this->filename, $output);
$out = trim($output[0]);
$this->assertEquals($out, "1 " . $this->filename);
$str = file_get_contents($this->filename);
$json = json_decode(trim($str));
$this->assertEquals($type, $json->type);
unlink($this->filename);
}
function filename(){
return '/tmp/analytics.log';
}
}
?>

View File

@@ -0,0 +1,72 @@
<?php
require_once(dirname(__FILE__) . "/../lib/Segment/Client.php");
class ConsumerForkCurlTest extends PHPUnit_Framework_TestCase {
private $client;
function setUp() {
date_default_timezone_set("UTC");
$this->client = new Segment_Client("oq0vdlg7yi",
array("consumer" => "fork_curl",
"debug" => true));
}
function testTrack() {
$this->assertTrue($this->client->track(array(
"userId" => "some-user",
"event" => "PHP Fork Curl'd\" Event"
)));
}
function testIdentify() {
$this->assertTrue($this->client->identify(array(
"userId" => "user-id",
"traits" => array(
"loves_php" => false,
"type" => "consumer fork-curl test",
"birthday" => time()
)
)));
}
function testGroup(){
$this->assertTrue($this->client->group(array(
"userId" => "user-id",
"groupId" => "group-id",
"traits" => array(
"type" => "consumer fork-curl test"
)
)));
}
function testPage(){
$this->assertTrue($this->client->page(array(
"userId" => "userId",
"name" => "analytics-php",
"category" => "fork-curl",
"properties" => array(
"url" => "https://a.url/"
)
)));
}
function testScreen(){
$this->assertTrue($this->client->page(array(
"anonymousId" => "anonymous-id",
"name" => "grand theft auto",
"category" => "fork-curl",
"properties" => array()
)));
}
function testAlias() {
$this->assertTrue($this->client->alias(array(
"previousId" => "previous-id",
"userId" => "user-id"
)));
}
}
?>

View File

@@ -0,0 +1,73 @@
<?php
require_once(dirname(__FILE__) . "/../lib/Segment/Client.php");
class ConsumerLibCurlTest extends PHPUnit_Framework_TestCase {
private $client;
function setUp() {
date_default_timezone_set("UTC");
$this->client = new Segment_Client("oq0vdlg7yi",
array("consumer" => "lib_curl",
"debug" => true));
}
function testTrack() {
$this->assertTrue($this->client->track(array(
"userId" => "lib-curl-track",
"event" => "PHP Lib Curl'd\" Event"
)));
}
function testIdentify() {
$this->assertTrue($this->client->identify(array(
"userId" => "lib-curl-identify",
"traits" => array(
"loves_php" => false,
"type" => "consumer lib-curl test",
"birthday" => time()
)
)));
}
function testGroup(){
$this->assertTrue($this->client->group(array(
"userId" => "lib-curl-group",
"groupId" => "group-id",
"traits" => array(
"type" => "consumer lib-curl test"
)
)));
}
function testPage(){
$this->assertTrue($this->client->page(array(
"userId" => "lib-curl-page",
"name" => "analytics-php",
"category" => "fork-curl",
"properties" => array(
"url" => "https://a.url/"
)
)));
}
function testScreen(){
$this->assertTrue($this->client->page(array(
"anonymousId" => "lib-curl-screen",
"name" => "grand theft auto",
"category" => "fork-curl",
"properties" => array()
)));
}
function testAlias() {
$this->assertTrue($this->client->alias(array(
"previousId" => "lib-curl-alias",
"userId" => "user-id"
)));
}
}
?>

View File

@@ -0,0 +1,154 @@
<?php
require_once(dirname(__FILE__) . "/../lib/Segment/Client.php");
class ConsumerSocketTest extends PHPUnit_Framework_TestCase {
private $client;
function setUp() {
date_default_timezone_set("UTC");
$this->client = new Segment_Client("oq0vdlg7yi",
array("consumer" => "socket"));
}
function testTrack() {
$this->assertTrue($this->client->track(array(
"userId" => "some-user",
"event" => "Socket PHP Event"
)));
}
function testIdentify() {
$this->assertTrue($this->client->identify(array(
"userId" => "Calvin",
"traits" => array(
"loves_php" => false,
"birthday" => time()
)
)));
}
function testGroup(){
$this->assertTrue($this->client->group(array(
"userId" => "user-id",
"groupId" => "group-id",
"traits" => array(
"type" => "consumer socket test"
)
)));
}
function testPage(){
$this->assertTrue($this->client->page(array(
"userId" => "user-id",
"name" => "analytics-php",
"category" => "socket",
"properties" => array(
"url" => "https://a.url/"
)
)));
}
function testScreen(){
$this->assertTrue($this->client->screen(array(
"anonymousId" => "anonymousId",
"name" => "grand theft auto",
"category" => "socket",
"properties" => array()
)));
}
function testAlias() {
$this->assertTrue($this->client->alias(array(
"previousId" => "some-socket",
"userId" => "new-socket"
)));
}
function testShortTimeout() {
$client = new Segment_Client("oq0vdlg7yi",
array( "timeout" => 0.01,
"consumer" => "socket" ));
$this->assertTrue($client->track(array(
"userId" => "some-user",
"event" => "Socket PHP Event"
)));
$this->assertTrue($client->identify(array(
"userId" => "some-user",
"traits" => array()
)));
$client->__destruct();
}
function testProductionProblems() {
$client = new Segment_Client("x", array(
"consumer" => "socket",
"error_handler" => function () { throw new Exception("Was called"); }));
# Shouldn't error out without debug on.
$client->track(array("user_id" => "some-user", "event" => "Production Problems"));
$client->__destruct();
}
function testDebugProblems() {
$options = array(
"debug" => true,
"consumer" => "socket",
"error_handler" => function ($errno, $errmsg) {
if ($errno != 400)
throw new Exception("Response is not 400"); }
);
$client = new Segment_Client("x", $options);
# Should error out with debug on.
$client->track(array("user_id" => "some-user", "event" => "Socket PHP Event"));
$client->__destruct();
}
function testLargeMessage () {
$options = array(
"debug" => true,
"consumer" => "socket"
);
$client = new Segment_Client("testsecret", $options);
$big_property = "";
for ($i = 0; $i < 10000; $i++) {
$big_property .= "a";
}
$this->assertTrue($client->track(array(
"userId" => "some-user",
"event" => "Super Large PHP Event",
"properties" => array("big_property" => $big_property)
)));
$client->__destruct();
}
/**
* @expectedException \RuntimeException
*/
function testConnectionError() {
$client = new Segment_Client("x", array(
"consumer" => "socket",
"host" => "api.segment.ioooooo",
"error_handler" => function ($errno, $errmsg) {
throw new \RuntimeException($errmsg, $errno);
},
));
$client->track(array("user_id" => "some-user", "event" => "Event"));
$client->__destruct();
}
}
?>

View File

@@ -0,0 +1,6 @@
{"userId":"some-user","event":"File PHP Event","context":{"library":{"name":"analytics-php","version":"1.0.0"}},"timestamp":"2014-05-13T16:19:17+00:00","messageId":"f8c9cda1-f21b-4d40-8198-085eaa99dedb","type":"track"}
{"userId":"Calvin","traits":{"loves_php":false,"type":"analytics.log","birthday":1399997957},"context":{"library":{"name":"analytics-php","version":"1.0.0"}},"timestamp":"2014-05-13T16:19:17+00:00","messageId":"fe37073d-40fa-41e1-b826-8659fe74a199","type":"identify"}
{"userId":"user-id","groupId":"group-id","traits":{"type":"consumer analytics.log test"},"context":{"library":{"name":"analytics-php","version":"1.0.0"}},"timestamp":"2014-05-13T16:19:17+00:00","messageId":"8e6bb92a-7347-424a-94d6-a4234617b070","type":"group"}
{"userId":"user-id","name":"analytics-php","category":"analytics.log","properties":{"url":"https:\/\/a.url\/"},"context":{"library":{"name":"analytics-php","version":"1.0.0"}},"timestamp":"2014-05-13T16:19:17+00:00","messageId":"b3db73cd-337b-4b50-8967-f653f9183e02","type":"page"}
{"userId":"userId","name":"grand theft auto","category":"analytics.log","properties":[],"context":{"library":{"name":"analytics-php","version":"1.0.0"}},"timestamp":"2014-05-13T16:19:17+00:00","messageId":"2527e1a0-e631-47b1-872e-f575f0722547","type":"page"}
{"previousId":"previous-id","userId":"user-id","context":{"library":{"name":"analytics-php","version":"1.0.0"}},"timestamp":"2014-05-13T16:19:17+00:00","messageId":"5f883f43-c15c-49c6-b4e6-59d5cb6d657b","type":"alias"}

View File

@@ -0,0 +1,11 @@
<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Location: ../");
exit;