Ejemplo n.º 1
0
 public function testGetVariantChooser()
 {
     // Arrange
     $bag = new Bag($this->test, $this->participationFilter, $this->variantChooser);
     // Act
     $chooser = $bag->getVariantChooser();
     // Assert
     $this->assertInstanceOf(ChooserInterface::class, $chooser);
 }
Ejemplo n.º 2
0
 /**
  * Process the test bag
  *
  * @param Bag $bag
  *
  * @return bool true if the variant got executed, false otherwise
  */
 private function handleTestBag(Bag $bag)
 {
     $test = $bag->getTest();
     $isParticipating = $this->participationManager->participates($test->getIdentifier());
     $testParticipation = $this->participationManager->getParticipatingVariant($test->getIdentifier());
     // Check if the user is marked as "do not participate".
     if ($isParticipating && null === $testParticipation) {
         $this->dispatcher->dispatch('phpab.participation.blocked', [$this, $bag]);
         return;
     }
     // When the user does not participate at the test, let him participate.
     if (!$isParticipating && !$bag->getParticipationFilter()->shouldParticipate()) {
         // The user should not participate so let's set participation
         // to null so he will not participate in the future, too.
         $this->dispatcher->dispatch('phpab.participation.block', [$this, $bag]);
         $this->participationManager->participate($test->getIdentifier(), null);
         return;
     }
     // Let's try to recover a previously stored Variant
     if ($isParticipating && $testParticipation !== null) {
         $variant = $bag->getTest()->getVariant($testParticipation);
         // If we managed to identify a Variant by a previously stored participation, do its magic again.
         if ($variant instanceof VariantInterface) {
             $this->activateVariant($bag, $variant);
             return;
         }
     }
     // Choose a variant for later usage. If the user should participate this one will be used
     $chosen = $bag->getVariantChooser()->chooseVariant($test->getVariants());
     // Check if user participation should be blocked. Or maybe the variant does not exists anymore?
     if (null === $chosen || !$test->getVariant($chosen->getIdentifier())) {
         $this->dispatcher->dispatch('phpab.participation.variant_missing', [$this, $bag]);
         $this->participationManager->participate($test->getIdentifier(), null);
         return;
     }
     // Store the chosen variant so he will not switch between different states
     $this->participationManager->participate($test->getIdentifier(), $chosen->getIdentifier());
     $this->activateVariant($bag, $chosen);
 }