first commit

This commit is contained in:
2024-11-05 12:22:50 +01:00
commit e5682a3912
19641 changed files with 2948548 additions and 0 deletions

View File

@@ -0,0 +1,249 @@
<?php
require_once __DIR__ . "/../lib/Segment.php";
class AnalyticsTest extends PHPUnit_Framework_TestCase
{
public function setUp()
{
date_default_timezone_set("UTC");
Segment::init("oq0vdlg7yi", array("debug" => true));
}
public function testTrack()
{
$this->assertTrue(Segment::track(array(
"userId" => "john",
"event" => "Module PHP Event",
)));
}
public function testGroup()
{
$this->assertTrue(Segment::group(array(
"groupId" => "group-id",
"userId" => "user-id",
"traits" => array(
"plan" => "startup",
),
)));
}
public function testGroupAnonymous()
{
$this->assertTrue(Segment::group(array(
"groupId" => "group-id",
"anonymousId" => "anonymous-id",
"traits" => array(
"plan" => "startup",
),
)));
}
/**
* @expectedException \Exception
* @expectedExceptionMessage Segment::group() requires userId or anonymousId
*/
public function testGroupNoUser()
{
Segment::group(array(
"groupId" => "group-id",
"traits" => array(
"plan" => "startup",
),
));
}
public 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/",
),
)));
}
public 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/",
),
)));
}
public function testBasicPage()
{
$this->assertTrue(Segment::page(array(
"anonymousId" => "anonymous-id",
)));
}
public function testScreen()
{
$this->assertTrue(Segment::screen(array(
"anonymousId" => "anonymous-id",
"name" => "2048",
"category" => "game built with php :)",
"properties" => array(
"points" => 300
),
)));
}
public function testBasicScreen()
{
$this->assertTrue(Segment::screen(array(
"anonymousId" => "anonymous-id"
)));
}
public function testIdentify()
{
$this->assertTrue(Segment::identify(array(
"userId" => "doe",
"traits" => array(
"loves_php" => false,
"birthday" => time(),
),
)));
}
public function testEmptyTraits()
{
$this->assertTrue(Segment::identify(array(
"userId" => "empty-traits",
)));
$this->assertTrue(Segment::group(array(
"userId" => "empty-traits",
"groupId" => "empty-traits",
)));
}
public 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(),
)));
}
public 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",
)));
}
public 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(),
)));
}
public function testAlias()
{
$this->assertTrue(Segment::alias(array(
"previousId" => "previous-id",
"userId" => "user-id",
)));
}
public function testContextEmpty()
{
$this->assertTrue(Segment::track(array(
"userId" => "user-id",
"event" => "Context Test",
"context" => array(),
)));
}
public function testContextCustom()
{
$this->assertTrue(Segment::track(array(
"userId" => "user-id",
"event" => "Context Test",
"context" => array(
"active" => false,
),
)));
}
public 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,31 @@
<?php
require_once __DIR__ . '/../lib/Segment/Client.php';
class ClientTest extends PHPUnit_Framework_TestCase
{
/** @test */
public function it_uses_the_lib_curl_consumer_as_default()
{
$client = new Segment_Client('foobar', []);
$this->assertInstanceOf(Segment_Consumer_LibCurl::class, $client->getConsumer());
}
/** @test */
public function can_provide_the_consumer_configuration_as_string()
{
$client = new Segment_Client('foobar', [
'consumer' => 'fork_curl',
]);
$this->assertInstanceOf(Segment_Consumer_ForkCurl::class, $client->getConsumer());
}
/** @test */
public function can_provide_a_class_namespace_as_consumer_configuration()
{
$client = new Segment_Client('foobar', [
'consumer' => Segment_Consumer_ForkCurl::class,
]);
$this->assertInstanceOf(Segment_Consumer_ForkCurl::class, $client->getConsumer());
}
}

View File

@@ -0,0 +1,166 @@
<?php
require_once __DIR__ . "/../lib/Segment/Client.php";
class ConsumerFileTest extends PHPUnit_Framework_TestCase
{
private $client;
private $filename = "/tmp/analytics.log";
public 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,
)
);
}
public function tearDown()
{
if (file_exists($this->filename)) {
unlink($this->filename);
}
}
public function testTrack()
{
$this->assertTrue($this->client->track(array(
"userId" => "some-user",
"event" => "File PHP Event - Microtime",
"timestamp" => microtime(true),
)));
$this->checkWritten("track");
}
public function testIdentify()
{
$this->assertTrue($this->client->identify(array(
"userId" => "Calvin",
"traits" => array(
"loves_php" => false,
"type" => "analytics.log",
"birthday" => time(),
),
)));
$this->checkWritten("identify");
}
public function testGroup()
{
$this->assertTrue($this->client->group(array(
"userId" => "user-id",
"groupId" => "group-id",
"traits" => array(
"type" => "consumer analytics.log test",
),
)));
}
public function testPage()
{
$this->assertTrue($this->client->page(array(
"userId" => "user-id",
"name" => "analytics-php",
"category" => "analytics.log",
"properties" => array(
"url" => "https://a.url/",
),
)));
}
public function testScreen()
{
$this->assertTrue($this->client->screen(array(
"userId" => "userId",
"name" => "grand theft auto",
"category" => "analytics.log",
"properties" => array(),
)));
}
public function testAlias()
{
$this->assertTrue($this->client->alias(array(
"previousId" => "previous-id",
"userId" => "user-id",
)));
$this->checkWritten("alias");
}
public 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->assertSame("sent 200 from 200 requests successfully", trim($output[0]));
$this->assertFileNotExists($this->filename());
}
public 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);
}
public function testFileSecurityCustom() {
$client = new Segment_Client(
"testsecret",
array(
"consumer" => "file",
"filename" => $this->filename,
"filepermissions" => 0700
)
);
$tracked = $client->track(array("userId" => "some_user", "event" => "File PHP Event"));
$this->assertEquals(0700, (fileperms($this->filename) & 0777));
}
public function testFileSecurityDefaults() {
$client = new Segment_Client(
"testsecret",
array(
"consumer" => "file",
"filename" => $this->filename
)
);
$tracked = $client->track(array("userId" => "some_user", "event" => "File PHP Event"));
$this->assertEquals(0777, (fileperms($this->filename) & 0777));
}
public function checkWritten($type)
{
exec("wc -l " . $this->filename, $output);
$out = trim($output[0]);
$this->assertSame($out, "1 " . $this->filename);
$str = file_get_contents($this->filename);
$json = json_decode(trim($str));
$this->assertSame($type, $json->type);
unlink($this->filename);
}
public function filename()
{
return '/tmp/analytics.log';
}
}

View File

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

View File

@@ -0,0 +1,125 @@
<?php
require_once __DIR__ . "/../lib/Segment/Client.php";
class ConsumerLibCurlTest extends PHPUnit_Framework_TestCase
{
private $client;
public function setUp()
{
date_default_timezone_set("UTC");
$this->client = new Segment_Client(
"oq0vdlg7yi",
array(
"consumer" => "lib_curl",
"debug" => true,
)
);
}
public function testTrack()
{
$this->assertTrue($this->client->track(array(
"userId" => "lib-curl-track",
"event" => "PHP Lib Curl'd\" Event",
)));
}
public function testIdentify()
{
$this->assertTrue($this->client->identify(array(
"userId" => "lib-curl-identify",
"traits" => array(
"loves_php" => false,
"type" => "consumer lib-curl test",
"birthday" => time(),
),
)));
}
public function testGroup()
{
$this->assertTrue($this->client->group(array(
"userId" => "lib-curl-group",
"groupId" => "group-id",
"traits" => array(
"type" => "consumer lib-curl test",
),
)));
}
public function testPage()
{
$this->assertTrue($this->client->page(array(
"userId" => "lib-curl-page",
"name" => "analytics-php",
"category" => "fork-curl",
"properties" => array(
"url" => "https://a.url/",
),
)));
}
public function testScreen()
{
$this->assertTrue($this->client->page(array(
"anonymousId" => "lib-curl-screen",
"name" => "grand theft auto",
"category" => "fork-curl",
"properties" => array(),
)));
}
public function testAlias()
{
$this->assertTrue($this->client->alias(array(
"previousId" => "lib-curl-alias",
"userId" => "user-id",
)));
}
public function testRequestCompression() {
$options = array(
"compress_request" => true,
"consumer" => "lib_curl",
"error_handler" => function ($errno, $errmsg) {
throw new \RuntimeException($errmsg, $errno);
},
);
$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();
}
public function testLargeMessageSizeError()
{
$options = array(
"debug" => true,
"consumer" => "lib_curl",
);
$client = new Segment_Client("testlargesize", $options);
$big_property = "";
for ($i = 0; $i < 32 * 1024; ++$i) {
$big_property .= "a";
}
$this->assertFalse(
$client->track(
array(
"userId" => "some-user",
"event" => "Super Large PHP Event",
"properties" => array("big_property" => $big_property),
)
) && $client->flush()
);
$client->__destruct();
}
}

View File

@@ -0,0 +1,220 @@
<?php
require_once __DIR__ . "/../lib/Segment/Client.php";
class ConsumerSocketTest extends PHPUnit_Framework_TestCase
{
private $client;
public function setUp()
{
date_default_timezone_set("UTC");
$this->client = new Segment_Client(
"oq0vdlg7yi",
array("consumer" => "socket")
);
}
public function testTrack()
{
$this->assertTrue($this->client->track(array(
"userId" => "some-user",
"event" => "Socket PHP Event",
)));
}
public function testIdentify()
{
$this->assertTrue($this->client->identify(array(
"userId" => "Calvin",
"traits" => array(
"loves_php" => false,
"birthday" => time(),
),
)));
}
public function testGroup()
{
$this->assertTrue($this->client->group(array(
"userId" => "user-id",
"groupId" => "group-id",
"traits" => array(
"type" => "consumer socket test",
),
)));
}
public function testPage()
{
$this->assertTrue($this->client->page(array(
"userId" => "user-id",
"name" => "analytics-php",
"category" => "socket",
"properties" => array(
"url" => "https://a.url/",
),
)));
}
public function testScreen()
{
$this->assertTrue($this->client->screen(array(
"anonymousId" => "anonymousId",
"name" => "grand theft auto",
"category" => "socket",
"properties" => array(),
)));
}
public function testAlias()
{
$this->assertTrue($this->client->alias(array(
"previousId" => "some-socket",
"userId" => "new-socket",
)));
}
public 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();
}
public 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();
}
public function testDebugProblems()
{
$options = array(
"debug" => true,
"consumer" => "socket",
"error_handler" => function ($errno, $errmsg) {
if (400 != $errno) {
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();
}
public 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();
}
public function testLargeMessageSizeError()
{
$options = array(
"debug" => true,
"consumer" => "socket",
);
$client = new Segment_Client("testlargesize", $options);
$big_property = "";
for ($i = 0; $i < 32 * 1024; ++$i) {
$big_property .= "a";
}
$this->assertFalse(
$client->track(
array(
"userId" => "some-user",
"event" => "Super Large PHP Event",
"properties" => array("big_property" => $big_property),
)
) && $client->flush()
);
$client->__destruct();
}
/**
* @expectedException \RuntimeException
*/
public 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();
}
public function testRequestCompression() {
$options = array(
"compress_request" => true,
"consumer" => "socket",
"error_handler" => function ($errno, $errmsg) {
throw new \RuntimeException($errmsg, $errno);
},
);
$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();
}
}

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"}