public function testBatchingFlushesQueueAutomatically() { $events = [['event' => 'foo', 'properties' => ['bar' => 'baz']], ['event' => 'foo', 'properties' => ['bar' => 'baz']]]; foreach ($events as $event) { $this->client->track($event); } }
/** * Tests that the correct log file is used */ public function testLogFile() { $this->client->track(['userId' => 123, 'event' => 'foo', 'properties' => ['bar' => 'baz']]); $this->assertTrue(file_exists(sys_get_temp_dir() . "/segment-io.log")); $file = sys_get_temp_dir() . "/segment-io-test2.log"; $this->client = new Client(['write_key' => 123, 'max_queue_size' => 1, 'batching' => 'file', 'log_file' => sys_get_temp_dir() . "/segment-io-test2.log"]); $this->client->track(['userId' => 123, 'event' => 'foo', 'properties' => ['bar' => 'baz']]); $this->assertTrue(file_exists(sys_get_temp_dir() . "/segment-io-test2.log")); }
/** * Testing the Client::loadServiceDescription($filepath, $version) method * * Should fail on invalid types or file paths */ public function testLoadingServiceDescription() { // Test with a valid Service Description $description = $this->client->loadServiceDescription(__DIR__ . '/../src/Description/segment.io.%s.php', $this->client->getConfig('version')); $this->assertInstanceOf('GuzzleHttp\\Command\\Guzzle\\Description', $description); // Test with a valid file and bad response $this->setExpectedException('InvalidArgumentException', 'Invalid Service Description!'); $this->client->loadServiceDescription(__DIR__ . '/Description/invalid.service.description.%s.php', 'v1'); // Test with a invalid file path $this->setExpectedException('InvalidArgumentException', 'Invalid Service Description!'); $this->client->loadServiceDescription('foo.%.bar', 'v1'); }
/** * Creates a SegmentIO\Client * * @param string $key The Segment.io API Write Key * @param boolean $debug Whether or not to use a MockAdapater * * @return Client */ private function createClient($key, $debug = false) { $client = new Client(['write_key' => $key, 'batching' => false]); if ($debug) { $stream = Stream::factory(json_encode(['success' => true])); $mock = new Mock([new Response(200, [], $stream)]); $client->getEmitter()->attach($mock); } return $client; }
/** * Flushes the queue by batching Import operations * * @return boolean */ public function flush() { if (empty($this->queue)) { return false; } $operations = array_chunk($this->queue, $this->batchSize); foreach ($operations as $batch) { $this->client->import(['batch' => $batch]); } return true; }