public function getConstraints()
 {
     // evaluate any configured constraints
     $constraints = $this->owner->config()->get('constraints');
     $allConstraints = array();
     if (count($constraints)) {
         foreach ($constraints as $fieldName => $info) {
             // if we've only given one constraint, it'll be a string, so make it an array
             if (is_string($info)) {
                 $info = array($info);
             }
             foreach ($info as $constraintClass => $config) {
                 // allow plain classes, without arrays
                 if (is_numeric($constraintClass)) {
                     $constraintClass = $config;
                     $config = array();
                 }
                 if (!is_array($config)) {
                     parse_str($config, $arr);
                     $config = $arr;
                 }
                 if (!class_exists($constraintClass)) {
                     $constraintClass = 'SilverStripeAustralia\\Constraints\\Constraints\\' . $constraintClass;
                 }
                 $constraint = $this->injector->create($constraintClass, $this->owner, $fieldName, $config);
                 $allConstraints[] = $constraint;
             }
         }
     }
     return $allConstraints;
 }
 /**
  * @test
  * @profile
  * @ignore(ignoreUntilFixed)
  */
 public function mockFactory()
 {
     $mockException = Mock_Factory::mock('Components\\Test_Exception', array('test/unit/case/mock', 'Mocked Exception.'));
     $mockExceptionDefault = Mock_Factory::mock('Components\\Test_Exception');
     $mockRunner = Mock_Factory::mock('Components\\Test_Runner');
     $mockListener = Mock_Factory::mock('Components\\Test_Listener');
     $mockListener->when('onInitialize')->doReturn(true);
     $mockListener->when('onExecute')->doReturn(true);
     $mockListener->when('onTerminate')->doNothing();
     assertTrue($mockListener->onExecute($mockRunner));
     assertTrue($mockListener->onInitialize($mockRunner));
     $mockLL->onTerminate($mockRunner);
     assertEquals('test/unit/case/mock', $mockException->getNamespace());
     assertEquals('Mocked Exception.', $mockException->getMessage());
     assertEquals('test/exception', $mockExceptionDefault->getNamespace());
     assertEquals('Test exception.', $mockExceptionDefault->getMessage());
     $mockBindingModule = Mock_Factory::mock('Components\\Binding_Module');
     $mockBindingModule->when('bind')->doAnswer(function (Binding_Module $self_, $type_) {
         echo "Bound {$type_}\r\n";
         return $self_->bind($type_);
     });
     $mockBindingModule->when('configure')->doAnswer(function (Binding_Module $self_) {
         $self_->bind('Components\\Test_Runner')->toInstance(Test_Runner::get());
         $self_->bind(Integer::TYPE)->toInstance(22)->named('boundInteger');
     });
     $injector = Injector::create($mockBindingModule);
     assertSame(Test_Runner::get(), $injector->resolveInstance('Components\\Test_Runner'));
     assertEquals(22, $injector->resolveInstance(Integer::TYPE, 'boundInteger'));
 }
 public function testCreateConfiggedObjectWithCustomConstructorArgs()
 {
     // need to make sure that even if the config defines some constructor params,
     // that we take our passed in constructor args instead
     $injector = new Injector(array('locator' => 'InjectorTestConfigLocator'));
     $item = $injector->create('ConfigConstructor', 'othervalue');
     $this->assertEquals($item->property, 'othervalue');
 }
Example #4
0
 public function testSameNamedSingeltonPrototype()
 {
     $injector = new Injector();
     // get a singleton object
     $object = $injector->get('NeedsBothCirculars');
     $object->var = 'One';
     $again = $injector->get('NeedsBothCirculars');
     $this->assertEquals($again->var, 'One');
     // create a NEW instance object
     $new = $injector->create('NeedsBothCirculars');
     $this->assertNull($new->var);
     // this will trigger a problem below
     $new->var = 'Two';
     $again = $injector->get('NeedsBothCirculars');
     $this->assertEquals($again->var, 'One');
 }
Example #5
0
 public function testSimpleSingleton()
 {
     $injector = new Injector();
     $one = $injector->create('CircularOne');
     $two = $injector->create('CircularOne');
     $this->assertFalse($one === $two);
     $one = $injector->get('CircularTwo');
     $two = $injector->get('CircularTwo');
     $this->assertTrue($one === $two);
 }
 /**
  * Retrieve a cached fragment in the context of this page. 
  * 
  * @param string $name
  * @return string
  */
 public function CachedFragment($name)
 {
     $item = $this->injector->create('CachedFragment', $this->owner, $name);
     return $item;
 }
 /**
  * @return \Components\Injector
  */
 public function getInjector()
 {
     if (null === $this->m_injector) {
         $this->m_injector = Injector::create($this->getBindingModule());
     }
     return $this->m_injector;
 }