/** * @test */ public function it_should_return_false_if_the_expression_does_not_return_boolean() { $condition = new ExpressionCondition('user["tags"]', $this->language); $context = new Context(); $context->set('user', array('active' => true, 'tags' => array('symfony2', 'qandidate'))); $this->assertFalse($condition->holdsFor($context)); }
/** * @test * @dataProvider valueAvailable */ public function it_returns_whether_it_operator_holds_for_the_value_if_available($value, $expected) { $condition = new OperatorCondition('age', new GreaterThan(42)); $context = new Context(); $context->set('age', $value); $this->assertEquals($expected, $condition->holdsFor($context)); }
public function active($featureName = null, $attributes = []) { if (!$featureName) { throw new QandidateException("Feature name must not be empty"); } if (!is_array($attributes)) { throw new QandidateException("Attribute must be an instance of array"); } $this->getToggleConditions($featureName); $conditionCollection = []; foreach ($this->conditions as $condition) { $class = $this->convertStringToClass($condition->operator); if ('Qandidate\\Toggle\\Operator\\InSet' === $class) { $value = [$condition->value]; } else { $value = $condition->value; } $operator = new $class($value); $conditionCollection[] = new OperatorCondition($condition->key, $operator); } $toggle = new Toggle('toggling', $conditionCollection); $constName = $this->convertStringToConstant($this->toggle->status); if ('INACTIVE' === $constName) { $toggle->deactivate(); } else { $toggle->activate(constant('Qandidate\\Toggle\\Toggle::' . $constName)); } $this->manager->add($toggle); $context = new Context(); foreach ($attributes as $key => $value) { $context->set($key, $value); } return $this->manager->active('toggling', $context); }
/** * @return Context */ public function createContext() { $context = new Context(); $token = $this->tokenStorage->getIdentity(); if (null !== $token) { $context->set('username', $this->tokenStorage->getIdentity()); } return $context; }
<?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
<?php require_once __DIR__ . '/../vendor/autoload.php'; use Qandidate\Toggle\Context; use Qandidate\Toggle\Operator\LessThan; use Qandidate\Toggle\OperatorCondition; use Qandidate\Toggle\Toggle; use Qandidate\Toggle\ToggleCollection\InMemoryCollection; use Qandidate\Toggle\ToggleManager; // Create the ToggleManager $manager = new ToggleManager(new InMemoryCollection()); // A toggle that will be active is the user id is less than 42 $operator = new LessThan(42); $condition = new OperatorCondition('user_id', $operator); $toggle = new Toggle('toggling', array($condition)); // Add the toggle to the manager $manager->add($toggle); // Create and check a new context for a user with id 42 $context = new Context(); $context->set('user_id', 42); var_dump($manager->active('toggling', $context)); // false // Create and check a new context for a user with id 21 $context = new Context(); $context->set('user_id', 21); var_dump($manager->active('toggling', $context)); // true
private function createContext(array $properties) { $context = new Context(); foreach ($properties as $key => $value) { $context->set($key, $value); } return $context; }
/** * @test */ public function it_does_not_have_a_value_that_was_never_set() { $context = new Context(); $this->assertFalse($context->has('foo')); }
/** * @inheritdoc */ public function holdsFor(Context $context) { return $this->language->evaluate($this->expression, $context->toArray()) === true; }
<?php require_once __DIR__ . '/../vendor/autoload.php'; use Qandidate\Toggle\Context; use Qandidate\Toggle\ExpressionCondition; use Qandidate\Toggle\Toggle; use Qandidate\Toggle\ToggleCollection\InMemoryCollection; use Qandidate\Toggle\ToggleManager; use Symfony\Component\ExpressionLanguage\ExpressionLanguage; // Create the ToggleManager $manager = new ToggleManager(new InMemoryCollection()); // Create and check a new context and condition using symfony expression $context = new Context(); $context->set('user', array('active' => true, 'tags' => array('symfony2', 'qandidate'))); $context->set('product', array('price' => 30)); $language = new ExpressionLanguage(); $expression = new ExpressionCondition('user["active"] and product["price"] / 100 >= 0.2', $language); $toggle = new Toggle('sf-toggling', array($expression)); $manager->add($toggle); var_dump($manager->active('sf-toggling', $context)); // true