/** * @test */ public function it_serializes_and_deserializes_a_collection() { $collection = new InMemoryCollection(); $operator = new LessThan(42); $condition = new OperatorCondition('user_id', $operator); $toggle = new Toggle('toggling', array($condition)); $collection->set('some-feature', $toggle); $serializer = new InMemoryCollectionSerializer(); $serialized = $serializer->serialize($collection); $collection2 = $serializer->deserialize($serialized); $this->assertEquals($collection, $collection2); }
/** * @inheritdoc */ public function register(Application $app) { $config = $this->config; // Global context for feature toggles. You can add to this context from // your own middlewares, by extending the service, etc. $app['toggles.context'] = $app->share(function () { return new Context(); }); // The feature toggles manager which can be asked for the current state // of feature toggles. $app['toggles'] = $app->share(function () use($config) { $serializer = new InMemoryCollectionSerializer(); $collection = $serializer->deserialize($config); $toggles = new ToggleManager($collection); return $toggles; }); }
<?php require_once __DIR__ . '/../vendor/autoload.php'; use Qandidate\Toggle\Context; use Qandidate\Toggle\ToggleManager; use Qandidate\Toggle\Serializer\InMemoryCollectionSerializer; // Array value $data = array('some-feature' => array('name' => 'toggling', 'conditions' => array(array('name' => 'operator-condition', 'key' => 'user_id', 'operator' => array('name' => 'greater-than', 'value' => 41))), 'status' => 'conditionally-active')); // Create the ToggleManager $serializer = new InMemoryCollectionSerializer(); $collection = $serializer->deserialize($data); $manager = new ToggleManager($collection); // Create and check a new context for a user with id 42 $context = new Context(); $context->set('user_id', 42); var_dump($manager->active('some-feature', $context)); // true // Create and check a new context for a user with id 21 $context = new Context(); $context->set('user_id', 21); var_dump($manager->active('some-feature', $context)); // false
conditions: - name: operator-condition key: user_id operator: name: greater-than value: 41 status: conditionally-active - name: some-feature2 conditions: - name: operator-condition key: user_id operator: name: greater-than value: 42 status: conditionally-active YML; // Create the ToggleManager $array = Yaml::parse($yaml); $serializer = new InMemoryCollectionSerializer(); $collection = $serializer->deserialize($array); $manager = new ToggleManager($collection); // Create and check a new context for a user with id 42 $context = new Context(); $context->set('user_id', 42); var_dump($manager->active('some-feature', $context)); // true // Create and check a new context for a user with id 21 $context = new Context(); $context->set('user_id', 21); var_dump($manager->active('some-feature', $context)); // false