diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 000000000..087243d25 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,25 @@ +language: php +php: + - 5.5 + - 5.6 + - 7.0 + - 7.1 +env: + - COMPOSER_OPTS="" + - COMPOSER_OPTS="--prefer-lowest" +services: + - redis-server +before_install: + - if [[ $TRAVIS_PHP_VERSION != 'hhvm' ]] ; then pecl channel-update pecl.php.net; fi; + - if [[ $TRAVIS_PHP_VERSION != 'hhvm' && $TRAVIS_PHP_VERSION != 7.* ]]; then pecl install riak-beta; fi; + - if [[ $TRAVIS_PHP_VERSION = '5.5' ]] ; then echo yes | pecl install apcu-4.0.10; fi; + - if [[ $TRAVIS_PHP_VERSION = '5.6' ]] ; then echo yes | pecl install apcu-4.0.10; fi; + - if [[ $TRAVIS_PHP_VERSION = 7.* ]] ; then pecl config-set preferred_state beta; echo yes | pecl install apcu; fi; + - if [[ $TRAVIS_PHP_VERSION != 'hhvm' ]]; then phpenv config-add ./integration-tests/php.ini; fi; +before_script: + - composer self-update + - composer update --no-interaction $COMPOSER_OPTS +script: + - vendor/bin/phpunit tests --coverage-text + - ln -s vendor integration-tests/vendor + - vendor/bin/phpunit integration-tests/LDDFeatureRequesterTest.php diff --git a/integration-tests/LDDFeatureRequesterTest.php b/integration-tests/LDDFeatureRequesterTest.php index 602b87717..a2a8e1865 100644 --- a/integration-tests/LDDFeatureRequesterTest.php +++ b/integration-tests/LDDFeatureRequesterTest.php @@ -3,6 +3,8 @@ require_once 'vendor/autoload.php'; +use LaunchDarkly\ApcLDDFeatureRequester; +use LaunchDarkly\ApcuLDDFeatureRequester; use LaunchDarkly\LDClient; use LaunchDarkly\LDUserBuilder; @@ -28,8 +30,10 @@ public function testGetApc() { "scheme" => "tcp", "host" => 'localhost', "port" => 6379)); - $client = new LDClient("BOGUS_API_KEY", array('feature_requester_class' => '\\LaunchDarkly\\ApcLDDFeatureRequester', - 'apc_expiration' => 1)); + $client = new LDClient("BOGUS_API_KEY", [ + 'feature_requester_class' => extension_loaded('apcu') ? ApcuLDDFeatureRequester::class : ApcLDDFeatureRequester::class, + 'apc_expiration' => 1, + ]); $builder = new LDUserBuilder(3); $user = $builder->build(); @@ -42,24 +46,63 @@ public function testGetApc() { $redis->hset("launchdarkly:features", 'foo', $this->gen_feature("foo", "baz")); $this->assertEquals("bar", $client->variation('foo', $user, 'jim')); - apc_delete("launchdarkly:features.foo"); + if (extension_loaded('apcu')) { + \apcu_delete("launchdarkly:features.foo"); + } else { + \apc_delete("launchdarkly:features.foo"); + } $this->assertEquals("baz", $client->variation('foo', $user, 'jim')); } private function gen_feature($key, $val) { - $data = << 'Feature ' . $val, + 'key' => $key, + 'kind' => 'flag', + 'salt' => 'Zm9v', + 'on' => true, + 'variations' => [ + $val, + false, + ], + 'commitDate' => '2015-09-08T21:24:16.712Z', + 'creationDate' => '2015-09-08T21:06:16.527Z', + 'version' => 4, + 'prerequisites' => [], + 'targets' => [ + [ + 'values' => [ + $val, + ], + 'variation' => 0, + ], + [ + 'values' => [ + false, + ], + 'variation' => 1, + ], + ], + 'rules' => [], + 'fallthrough' => [ + 'rollout' => [ + 'variations' => [ + [ + 'variation' => 0, + 'weight' => 95000, + ], + [ + 'variation' => 1, + 'weight' => 5000, + ], + ], + ], + ], + 'offVariation' => null, + 'deleted' => false, + ]; + + return \json_encode($data); } } diff --git a/integration-tests/composer.json b/integration-tests/composer.json index e0cd69628..0ee1f605e 100644 --- a/integration-tests/composer.json +++ b/integration-tests/composer.json @@ -10,6 +10,7 @@ } ], "require": { + "monolog/monolog": "1.21.0", "php": ">=5.3", "predis/predis": "1.0.*" }, diff --git a/integration-tests/php.ini b/integration-tests/php.ini new file mode 100644 index 000000000..230dbf858 --- /dev/null +++ b/integration-tests/php.ini @@ -0,0 +1,4 @@ +extension="redis.so" + +apc.enabled=1 +apc.enable_cli=1 diff --git a/src/LaunchDarkly/ApcLDDFeatureRequester.php b/src/LaunchDarkly/ApcLDDFeatureRequester.php index 9ad8e7f82..308dc196a 100644 --- a/src/LaunchDarkly/ApcLDDFeatureRequester.php +++ b/src/LaunchDarkly/ApcLDDFeatureRequester.php @@ -18,10 +18,19 @@ function __construct($baseUri, $apiKey, $options) { } } + /** + * @param $key + * @param $success + * @return mixed + */ + protected function fetch($key, &$success = null) + { + return \apc_fetch($key, $success); + } protected function get_from_cache($key) { $key = self::make_cache_key($key); - $enabled = apc_fetch($key); + $enabled = $this->fetch($key); if ($enabled === false) { return null; } @@ -30,8 +39,19 @@ protected function get_from_cache($key) { } } + /** + * @param $key + * @param $var + * @param int $ttl + * @return mixed + */ + protected function add($key, $var, $ttl = 0) + { + return \apc_add($key, $var, $ttl); + } + protected function store_in_cache($key, $val) { - apc_add($this->make_cache_key($key), $val, $this->_expiration); + $this->add($this->make_cache_key($key), $val, $this->_expiration); } private function make_cache_key($name) { diff --git a/src/LaunchDarkly/ApcuLDDFeatureRequester.php b/src/LaunchDarkly/ApcuLDDFeatureRequester.php new file mode 100644 index 000000000..7b9562050 --- /dev/null +++ b/src/LaunchDarkly/ApcuLDDFeatureRequester.php @@ -0,0 +1,32 @@ +