From ccc553a21264926af9de42fc92cc2cef41cdca94 Mon Sep 17 00:00:00 2001 From: Tim Younger Date: Fri, 16 Dec 2016 12:27:09 -0700 Subject: [PATCH 1/2] add travis config to test PHP versions 5.5 to 7.1. each PHP version is tested with the minimum and maximum dependency versions which provides some certainty that our dependency ranges in the composer config are valid. --- .travis.yml | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 000000000..50703ad51 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,14 @@ +language: php +php: + - 5.5 + - 5.6 + - 7.0 + - 7.1 +env: + - COMPOSER_OPTS="" + - COMPOSER_OPTS="--prefer-lowest" +before_script: + - composer self-update + - composer update --no-interaction $COMPOSER_OPTS +script: + - vendor/bin/phpunit tests --coverage-text From 2f7d54645798ee271dbccad5be7e5f8903560a28 Mon Sep 17 00:00:00 2001 From: Tim Younger Date: Sat, 17 Dec 2016 23:05:41 -0700 Subject: [PATCH 2/2] add travis config to test PHP versions 5.5 to 7.1. each PHP version is tested with the minimum and maximum dependency versions which provides some certainty that our dependency ranges in the composer config are valid. update feature-generator in integration-test to return json-object with all required keys and appropriate variation-structures. run integration tests in travis. --- .travis.yml | 11 +++ integration-tests/LDDFeatureRequesterTest.php | 75 +++++++++++++++---- integration-tests/composer.json | 1 + integration-tests/php.ini | 4 + src/LaunchDarkly/ApcLDDFeatureRequester.php | 24 +++++- src/LaunchDarkly/ApcuLDDFeatureRequester.php | 32 ++++++++ 6 files changed, 129 insertions(+), 18 deletions(-) create mode 100644 integration-tests/php.ini create mode 100644 src/LaunchDarkly/ApcuLDDFeatureRequester.php diff --git a/.travis.yml b/.travis.yml index 50703ad51..087243d25 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,8 +7,19 @@ php: 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 @@ +