Exemplo n.º 1
0
 /**
  * Test the behavior of the proxy when it is cloned :
  * All states must be cloned
  * DI Container must be cloned
  * Registered states must be cloned
  * Active states must be cloned
  * The cloned proxy must has a new unique id.
  */
 public function testCloning()
 {
     $this->initializeProxy('state1', true);
     $obj = new \stdClass();
     $obj->foo = 'bar';
     $clonedProxy = clone $this->proxy;
     //States must be independently
     $this->assertEquals(array('state1', 'state2', 'state3'), $this->proxy->listAvailableStates());
     $this->assertEquals(array('state1'), $this->proxy->listEnabledStates());
     $this->assertEquals(array('state1', 'state2', 'state3'), $clonedProxy->listAvailableStates());
     $this->assertEquals(array('state1'), $clonedProxy->listEnabledStates());
     //List must perform independently
     $clonedProxy->switchState('state2');
     $clonedProxy->unregisterState('state3');
     $this->assertEquals(array('state1', 'state2', 'state3'), $this->proxy->listAvailableStates());
     $this->assertEquals(array('state1'), $this->proxy->listEnabledStates());
     $this->assertEquals(array('state1', 'state2'), $clonedProxy->listAvailableStates());
     $this->assertEquals(array('state2'), $clonedProxy->listEnabledStates());
 }
Exemplo n.º 2
0
 /**
  * To initialize a proxy object with its states. States are fetched by the finder of this stated class.
  *
  * @param ProxyInterface $proxyObject
  * @param string         $stateName
  *
  * @return FactoryInterface
  *
  * @throws Exception\StateNotFound if the $stateName was not found for this stated class
  */
 public function startup(ProxyInterface $proxyObject, string $stateName = null) : FactoryInterface
 {
     //Get all states available
     $statesList = $this->listStatesByClasses();
     //Check if the require state is available
     if (null !== $stateName && !isset($statesList[$stateName])) {
         throw new Exception\StateNotFound(\sprintf('Error, the state "%s" was not found in this stated class', $stateName));
     }
     //Get the main finder of this stated class, to compare it with finders of parents classes
     $mainFinder = $this->getFinder();
     //Load each state into proxy
     foreach ($statesList as $loadingStateName => $finderLoader) {
         //Create new state object by the finder
         $stateObject = $this->buildState($loadingStateName, $finderLoader, $finderLoader !== $mainFinder);
         //Add the state in the proxy
         $proxyObject->registerState($loadingStateName, $stateObject);
     }
     //Switch to required state
     if (null !== $stateName) {
         $proxyObject->switchState($stateName);
     } elseif (isset($statesList[ProxyInterface::DEFAULT_STATE_NAME])) {
         //No required stated name, check if the default state is available and load it
         $proxyObject->switchState(ProxyInterface::DEFAULT_STATE_NAME);
     }
     return $this;
 }