コード例 #1
0
 /**
  * Creates an instance of the target class.
  *
  */
 public function setupTargetInstance()
 {
     if (!property_exists($this, 'target')) {
         return;
     }
     $spec = $this->target;
     if (is_string($spec)) {
         $this->target = new $spec();
         return;
     }
     $testNameParts = explode(' ', $this->getName());
     $testName = array_shift($testNameParts);
     $testSet = trim(array_pop($testNameParts), '"');
     $testNameKey = '@' . $testName;
     $testNameSetKey = $testNameKey . '|' . $testSet;
     $testSpec = isset($spec[$testNameKey]) ? $spec[$testNameKey] : (isset($spec[$testNameSetKey]) ? $spec[$testNameSetKey] : null);
     if (!is_array($spec) || false === $testSpec) {
         $this->target = null;
         return;
     }
     if (null !== $testSpec) {
         if (is_string($testSpec)) {
             if (class_exists($testSpec)) {
                 $testSpec = [$testSpec];
             } else {
                 $this->target = $this->{$testSpec}();
                 return;
             }
         }
         /* Override specs for specific test */
         foreach ($testSpec as $key => $value) {
             $spec[$key] = $value;
         }
     }
     if (isset($spec[0])) {
         $spec['class'] = $spec[0];
     }
     if (!isset($spec['args'])) {
         $spec['args'] = isset($spec[1]) ? $spec[1] : [];
     }
     if (isset($spec['method'])) {
         $this->target = $this->{$spec['method']}();
         return;
     }
     if (is_string($spec['args'])) {
         $spec['args'] = $this->{$spec['args']}();
     }
     if (isset($spec['mock'])) {
         $this->target = $this->_setupTarget_setupMock($spec['class'], $spec['args'], $spec['mock']);
         return;
     }
     $this->target = InstanceCreator::newClass($spec['class'], $spec['args']);
 }
コード例 #2
0
 /**
  * @testdox      Allows setting and/or getting object property values.
  * @dataProvider propertiesProvider
  */
 public function testSetterAndGetter($name, $spec)
 {
     $errTmpl = __METHOD__ . ': ' . get_class($this);
     if (!property_exists($this, 'target') || !is_object($this->target) && !isset($spec['target'])) {
         throw new \PHPUnit_Framework_Exception($errTmpl . ' must define the property "target" and the value must be an object.');
     }
     if (!is_array($spec)) {
         $spec = ['value' => $spec];
     }
     if (isset($spec['@value'])) {
         $spec['value'] = InstanceCreator::fromSpec($spec['@value'], InstanceCreator::FORCE_INSTANTIATION);
     }
     /* Value could be 'null', so we need to use array_key_exists here. */
     if (!array_key_exists('value', $spec)) {
         if (!array_key_exists('default', $spec)) {
             throw new \PHPUnit_Framework_Exception($errTmpl . ': Specification must contain the key "value" or "default".');
         }
         $spec['value'] = null;
         $spec['ignore_getter'] = true;
         $spec['ignore_setter'] = true;
     }
     if (isset($spec['target'])) {
         $this->target = is_object($spec['target']) ? $spec['target'] : InstanceCreator::fromSpec($spec['target'], InstanceCreator::FORCE_INSTANTIATION);
     }
     if (isset($spec['@default'])) {
         $spec['default'] = InstanceCreator::fromSpec($spec['@default'], InstanceCreator::FORCE_INSTANTIATION);
     } else {
         if (isset($spec['default@'])) {
             $spec['default'] = '@' . $spec['default@'];
         }
     }
     if (is_string($spec['value']) && 0 === strpos($spec['value'], '@')) {
         $spec['value'] = InstanceCreator::newClass($spec['value']);
     }
     $this->_setterGetter_triggerHook('pre', $spec);
     $getterMethod = isset($spec['getter_method']) ? str_replace('*', $name, $spec['getter_method']) : "get{$name}";
     $setterMethod = isset($spec['setter_method']) ? str_replace('*', $name, $spec['setter_method']) : "set{$name}";
     $getterArgs = isset($spec['getter_args']) ? $spec['getter_args'] : false;
     $setterArgs = isset($spec['setter_args']) ? $spec['setter_args'] : false;
     if (isset($spec['setter_exception'])) {
         $this->_setterGetter_assertSetterGetterException($setterMethod, $spec['setter_exception'], $spec['value'], $setterArgs);
         return;
     }
     if (isset($spec['default'])) {
         $defaultGetterArgs = isset($spec['default_args']) ? $spec['default_args'] : $getterArgs;
         $assert = isset($spec['default_assert']) ? $spec['default_assert'] : null;
         $this->_setterGetter_assertGetterValue($getterMethod, $spec['default'], $defaultGetterArgs, $assert, true);
     }
     if (!isset($spec['ignore_setter']) || !$spec['ignore_setter']) {
         $assert = isset($spec['setter_assert']) ? $spec['setter_assert'] : null;
         $this->_setterGetter_assertSetterValue($setterMethod, $spec['value'], $setterArgs, $assert, array_key_exists('setter_value', $spec) ? $spec['setter_value'] : '__FLUENT_INTERFACE__');
     }
     if (isset($spec['getter_exception'])) {
         $this->_setterGetter_assertSetterGetterException($getterMethod, $spec['getter_exception'], '__GETTER_EXCEPTION__', $getterArgs);
         return;
     }
     if (isset($spec['expect_property'])) {
         $assert = isset($spec['property_assert']) ? $spec['property_assert'] : null;
         $this->_setterGetter_assertPropertyValue($name, $spec['expect_property'], $assert);
     } else {
         if (!isset($spec['ignore_getter']) || !$spec['ignore_getter']) {
             $assert = isset($spec['getter_assert']) ? $spec['getter_assert'] : null;
             $this->_setterGetter_assertGetterValue($getterMethod, isset($spec['expect']) ? $spec['expect'] : $spec['value'], $getterArgs, $assert);
         }
     }
     $this->_setterGetter_triggerHook('post', $spec);
 }