/** * this custom instantiator dumps the given spec */ public function instantiate(Fixture $fixture) { $class = $fixture->getClass(); $newObj = new $class(); $newObj->uuid = uniqid(); return $newObj; }
/** * {@inheritDoc} */ public function set(Fixture $fixture, $object, $property, $value) { if (!method_exists($object, $fixture->getCustomSetter())) { throw new \RuntimeException('Setter ' . $fixture->getCustomSetter() . ' not found in object'); } $customSetter = $fixture->getCustomSetter()->getValue(); $object->{$customSetter}($property, $value); }
/** * {@inheritDoc} */ public function instantiate(Fixture $fixture) { $tablename = substr($fixture->getClass(), 6); $r = new TableRecord($tablename); if ($this->auto_uuid_column) { $uuid = (string) Uuid::uuid4(); $r->setR_uuid($uuid); } return $r; }
/** * {@inheritDoc} */ public function instantiate(Fixture $fixture) { $class = $fixture->getClass(); $constructorMethod = $fixture->getConstructorMethod(); $constructorArgs = $fixture->getConstructorArgs(); $reflClass = new \ReflectionClass($class); $constructorArgs = $this->processor->process($constructorArgs, array(), $fixture->getValueForCurrent()); foreach ($constructorArgs as $index => $value) { $constructorArgs[$index] = $this->typeHintChecker->check($class, $constructorMethod, $value, $index); } if ($constructorMethod === '__construct') { $instance = $reflClass->newInstanceArgs($constructorArgs); } else { $instance = forward_static_call_array(array($class, $constructorMethod), $constructorArgs); if (!$instance instanceof $class) { throw new \UnexpectedValueException("The static constructor '{$constructorMethod}' for object '{$fixture}' returned an object that is not an instance of '{$class}'"); } } return $instance; }
/** * ensures that the property generated for the given fixture is a unique property * * @param Fixture $fixture * @param PropertyDefinition $property * @return mixed */ protected function generateUnique(Fixture $fixture, PropertyDefinition $property) { $class = $fixture->getClass(); $key = $property->getName(); $i = $uniqueTriesLimit = 128; do { // process values $value = $this->processor->process($property, $fixture->getSetProperties(), $fixture->getValueForCurrent()); if (is_object($value)) { $valHash = spl_object_hash($value); } elseif (is_array($value)) { $valHash = hash('md4', serialize($value)); } else { $valHash = $value; } } while (--$i > 0 && isset($this->uniqueValues[$class . $key][$valHash])); if (isset($this->uniqueValues[$class . $key][$valHash])) { throw new \RuntimeException("Couldn't generate random unique value for {$class}: {$key} in {$uniqueTriesLimit} tries."); } $this->uniqueValues[$class . $key][$valHash] = true; return $value; }
/** * {@inheritDoc} */ public function instantiate(Fixture $fixture) { $reflClass = new \ReflectionClass($fixture->getClass()); return $reflClass->newInstanceWithoutConstructor(); }
/** * extends this fixture by the given template * * @param Fixture $template */ public function extendTemplate(Fixture $template) { if (!$template->isTemplate()) { throw new \InvalidArgumentException('Argument must be a template, not just a fixture.'); } foreach ($template->properties as $property) { if (!isset($this->spec[$property->getName()])) { $this->addProperty($property->getName(), $property->getValue()); } } }
/** * {@inheritDoc} */ public function instantiate(Fixture $fixture) { // unserialize hack for php <5.4 return unserialize(sprintf('O:%d:"%s":0:{}', strlen($fixture->getClass()), $fixture->getClass())); }
/** * {@inheritDoc} */ public function instantiate(Fixture $fixture) { $class = $fixture->getClass(); return new $class(); }
public function canSet(Fixture $fixture, $object, $property, $value) { return preg_match('/Contact/', $fixture->getClass()); }
public function testGetCustomSetterWillReturnTheCustomSetterValue() { $setFixture = new Fixture(self::USER, 'user', array('__set' => 'setterFunc'), null); $noSetFixture = new Fixture(self::USER, 'user', array(), null); $this->assertEquals('setterFunc', $setFixture->getCustomSetter()); $this->assertNull($noSetFixture->getCustomSetter()); }