Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
/vendor/
/doc/
/doc/
*.iml
composer.phar
.vagrant
integration-tests/vendor
integration-tests/composer.lock
4 changes: 2 additions & 2 deletions circle.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
machine:
php:
version: 5.4.10
version: 5.4.37

test:
override:
- vendor/bin/phpunit tests
- vendor/bin/phpunit tests
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@
},
"require-dev": {
"phpunit/phpunit": "4.3.*",
"phpdocumentor/phpdocumentor": "2.*"
"phpdocumentor/phpdocumentor": "2.*",
"predis/predis": "1.0.*"
},
"suggested": {
"predis/predis": "1.0.*"
},
"autoload": {
"psr-4": {
Expand Down
63 changes: 57 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 66 additions & 0 deletions integration-tests/LDDFeatureRequesterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
namespace LaunchDarkly\Tests;

require_once 'vendor/autoload.php';

use LaunchDarkly\LDClient;
use LaunchDarkly\LDUserBuilder;

class LDDFeatureRetrieverTest extends \PHPUnit_Framework_TestCase {

public function testGet() {
$redis = new \Predis\Client(array(
"scheme" => "tcp",
"host" => 'localhost',
"port" => 6379));
$client = new LDClient("BOGUS_API_KEY", array('feature_requester_class' => '\\LaunchDarkly\\LDDFeatureRequester'));
$builder = new LDUserBuilder(3);
$user = $builder->build();

$redis->del("launchdarkly:features");
$this->assertEquals("jim", $client->toggle('foo', $user, 'jim'));
$redis->hset("launchdarkly:features", 'foo', $this->gen_feature("foo", "bar"));
$this->assertEquals("bar", $client->toggle('foo', $user, 'jim'));
}

public function testGetApc() {
$redis = new \Predis\Client(array(
"scheme" => "tcp",
"host" => 'localhost',
"port" => 6379));
$client = new LDClient("BOGUS_API_KEY", array('feature_requester_class' => '\\LaunchDarkly\\ApcLDDFeatureRequester',
'apc_expiration' => 1));
$builder = new LDUserBuilder(3);
$user = $builder->build();

$redis->del("launchdarkly:features");
$this->assertEquals("jim", $client->toggle('foo', $user, 'jim'));
$redis->hset("launchdarkly:features", 'foo', $this->gen_feature("foo", "bar"));
$this->assertEquals("bar", $client->toggle('foo', $user, 'jim'));

# cached value so not updated
$redis->hset("launchdarkly:features", 'foo', $this->gen_feature("foo", "baz"));
$this->assertEquals("bar", $client->toggle('foo', $user, 'jim'));

apc_delete("launchdarkly:features.foo");
$this->assertEquals("baz", $client->toggle('foo', $user, 'jim'));
}

private function gen_feature($key, $val) {
$data = <<<EOF
{"name": "Feature $key", "key": "$key", "kind": "flag", "salt": "Zm9v", "on": true,
"variations": [{"value": "$val", "weight": 100,
"targets": [{"attribute": "key", "op": "in", "values": []}],
"userTarget": {"attribute": "key", "op": "in", "values": []}},
{"value": false, "weight": 0,
"targets": [{"attribute": "key", "op": "in", "values": []}],
"userTarget": {"attribute": "key", "op": "in", "values": []}}],
"commitDate": "2015-09-08T21:24:16.712Z",
"creationDate": "2015-09-08T21:06:16.527Z",
"version": 4}
EOF;
return $data;
}

}

7 changes: 7 additions & 0 deletions integration-tests/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
To run the tests, run:

vagrant up
vagrant ssh
cd project/integration-tests
vendor/phpunit/phpunit/phpunit LDDFeatureRequesterTest.php

51 changes: 51 additions & 0 deletions integration-tests/Vagrantfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# -*- mode: ruby -*-
# vi: set ft=ruby :

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
# All Vagrant configuration is done here. The most common configuration
# options are documented and commented below. For a complete reference,
# please see the online documentation at vagrantup.com.

# Every Vagrant virtual environment requires a box to build off of.
config.vm.box = "ubuntu/trusty64"

# The url from where the 'config.vm.box' box will be fetched if it
# doesn't already exist on the user's system.
config.vm.box_url = "https://vagrantcloud.com/ubuntu/boxes/trusty64"

config.vm.provision :shell, path: "bootstrap.sh"

# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing "localhost:8080" will access port 80 on the guest machine.
# config.vm.network :forwarded_port, guest: 80, host: 8080

# Create a private network, which allows host-only access to the machine
# using a specific IP.
# config.vm.network :private_network, ip: "192.168.33.10"

# Create a public network, which generally matched to bridged network.
# Bridged networks make the machine appear as another physical device on
# your network.
# config.vm.network :public_network

# If true, then any SSH connections made will enable agent forwarding.
# Default value: false
# config.ssh.forward_agent = true

# Share an additional folder to the guest VM. The first argument is
# the path on the host to the actual folder. The second argument is
# the path on the guest to mount the folder. And the optional third
# argument is a set of non-required options.
config.vm.synced_folder "..", "/home/vagrant/project"

config.vm.provider :virtualbox do |vb|
vb.auto_nat_dns_proxy = false
vb.customize ["modifyvm", :id, "--natdnsproxy1", "off" ]
vb.customize ["modifyvm", :id, "--natdnshostresolver1", "off" ]
vb.customize ["modifyvm", :id, "--memory", "2048"]
end
end
59 changes: 59 additions & 0 deletions integration-tests/bootstrap.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/bin/bash

# init
apt-get update 2> /dev/null

# redis
apt-get install -y redis-server 2> /dev/null

# ntp
apt-get install ntp -y 2> /dev/null
service ntp restart

# install dependencies and services
apt-get install unzip -y 2> /dev/null
apt-get install -y vim curl 2> /dev/null
apt-get install git -y 2> /dev/null

# PHP things
echo "Install PHP things"
apt-get install -y php-apc 2> /dev/null
apt-get install -y phpunit 2> /dev/null

# phpbrew stuff for 5.4
apt-get build-dep php5 2> /dev/null
apt-get install -y php5 php5-dev php-pear autoconf automake curl build-essential libxslt1-dev re2c libxml2 libxml2-dev php5-cli bison libbz2-dev libreadline-dev 2> /dev/null
apt-get install -y libfreetype6 libfreetype6-dev libpng12-0 libpng12-dev libjpeg-dev libjpeg8-dev libjpeg8 libgd-dev libgd3 libxpm4 libltdl7 libltdl-dev 2> /dev/null
apt-get install -y libssl-dev openssl 2> /dev/null
apt-get install -y gettext libgettextpo-dev libgettextpo0 2> /dev/null
apt-get install -y php5-cli 2> /dev/null
apt-get install -y libmcrypt-dev 2> /dev/null
apt-get install -y libreadline-dev 2> /dev/null

# set vim tabs
cat <<EOF > /home/vagrant/.vimrc
set tabstop=4
EOF
chown vagrant.vagrant /home/vagrant/.vimrc

su - vagrant
cd ~vagrant
pwd
curl -s -L -O https://github.com/phpbrew/phpbrew/raw/master/phpbrew
chmod +x phpbrew
sudo mv phpbrew /usr/bin/phpbrew
phpbrew init
phpbrew known --update
phpbrew update
phpbrew install 5.4.34 +default

echo "source $HOME/.phpbrew/bashrc" >> /home/vagrant/.bashrc
source $HOME/.bashrc
phpbrew switch php-5.4.34
phpbrew ext install apc
echo "apc.enable_cli = 1" >> ~/.phpbrew/php/php-5.4.34/etc/php.ini


cd /home/vagrant/project/integration-tests
curl -sS https://getcomposer.org/installer | php
php composer.phar install
29 changes: 29 additions & 0 deletions integration-tests/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "launchdarkly/launchdarkly-php-integration-tests",
"description": "Integration tests",
"homepage": "https://github.com/launchdarkly/php-client",
"license": "Apache-2.0",
"authors": [
{
"name": "LaunchDarkly <[email protected]>",
"homepage": "http://launchdarkly.com/"
}
],
"require": {
"php": ">=5.3",
"predis/predis": "1.0.*"
},
"require-dev": {
"phpunit/phpunit": "4.3.*"
},
"autoload": {
"psr-4": {
"": "../src/"
}
},
"autoload-dev": {
"psr-4": {
"": "."
}
}
}
40 changes: 40 additions & 0 deletions src/LaunchDarkly/ApcLDDFeatureRequester.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
namespace LaunchDarkly;


/**
* Feature requester from an LDD-populated redis, with APC caching
*
* @package LaunchDarkly
*/
class ApcLDDFeatureRequester extends LDDFeatureRequester {
protected $_expiration = 30;

function __construct($baseUri, $apiKey, $options) {
parent::__construct($baseUri, $apiKey, $options);

if (isset($options['apc_expiration'])) {
$this->_expiration = (int)$options['apc_expiration'];
}
}


protected function get_from_cache($key) {
$key = self::make_cache_key($key);
$enabled = apc_fetch($key);
if ($enabled === false) {
return null;
}
else {
return $enabled;
}
}

protected function store_in_cache($key, $val) {
apc_add($this->make_cache_key($key), $val, $this->_expiration);
}

private function make_cache_key($name) {
return $this->_features_key.'.'.$name;
}
}
Loading