public function testWithersReturnNewModifiedInstance() { $name = 'username'; $definition = new Property($name, 'foo'); $newDefinition = $definition->withValue(new \stdClass()); $this->assertEquals(new Property($name, 'foo'), $definition); $this->assertEquals(new Property($name, new \stdClass()), $newDefinition); }
/** * {@inheritdoc} * * @throws NoSuchPropertyException * @throws InaccessiblePropertyException * @throws InvalidArgumentException When the typehint does not match for example * @throws HydrationException */ public function hydrate(ObjectInterface $object, Property $property, GenerationContext $context) : ObjectInterface { $instance = $object->getInstance(); try { $this->propertyAccessor->setValue($instance, $property->getName(), $property->getValue()); } catch (SymfonyNoSuchPropertyException $exception) { throw HydrationExceptionFactory::createForCouldNotHydrateObjectWithProperty($object, $property, 0, $exception); } catch (SymfonyAccessException $exception) { throw HydrationExceptionFactory::createForInaccessibleProperty($object, $property, 0, $exception); } catch (SymfonyInvalidArgumentException $exception) { throw HydrationExceptionFactory::createForInvalidProperty($object, $property, 0, $exception); } catch (SymfonyPropertyAccessException $exception) { throw HydrationExceptionFactory::create($object, $property, 0, $exception); } return new SimpleObject($object->getId(), $instance); }
public static function createForCouldNotHydrateObjectWithProperty(ObjectInterface $object, Property $property, int $code = 0, \Throwable $previous = null) : NoSuchPropertyException { return new NoSuchPropertyException(sprintf('Could not hydrate the property "%s" of the object "%s" (class: %s).', $property->getName(), $object->getId(), get_class($object->getInstance())), $code, $previous); }
public function testResolvesAllPropertyValues() { $object = new SimpleObject('dummy', new \stdClass()); $set = ResolvedFixtureSetFactory::create(null, $fixtures = (new FixtureBag())->with($fixture = new SimpleFixture('dummy', \stdClass::class, new SpecificationBag(null, (new PropertyBag())->with($username = new Property('username', $usernameValue = new FakeValue()))->with($group = new Property('group', $groupValue = new FakeValue())), new MethodCallBag())))); $context = new GenerationContext(); $context->markIsResolvingFixture('foo'); $resolverProphecy = $this->prophesize(ValueResolverInterface::class); $setAfterFirstResolution = ResolvedFixtureSetFactory::create(new ParameterBag(['iteration' => 1]), $fixtures); $resolverProphecy->resolve($usernameValue, $fixture, $set, ['_instances' => $set->getObjects()->toArray()], $context)->willReturn(new ResolvedValueWithFixtureSet('Bob', $setAfterFirstResolution)); $setAfterSecondResolution = ResolvedFixtureSetFactory::create(new ParameterBag(['iteration' => 2]), $fixtures); $resolverProphecy->resolve($groupValue, $fixture, $setAfterFirstResolution, ['_instances' => $set->getObjects()->toArray(), 'username' => 'Bob'], $context)->willReturn(new ResolvedValueWithFixtureSet('Badass', $setAfterSecondResolution)); /** @var ValueResolverInterface $resolver */ $resolver = $resolverProphecy->reveal(); $hydratorProphecy = $this->prophesize(PropertyHydratorInterface::class); $newInstance = new \stdClass(); $newInstance->username = '******'; $newObject = $object->withInstance($newInstance); $hydratorProphecy->hydrate($object, $username->withValue('Bob'), $context)->willReturn($newObject); $secondNewInstance = clone $newInstance; $secondNewInstance->group = 'Badass'; $secondNewObject = $object->withInstance($secondNewInstance); $hydratorProphecy->hydrate($newObject, $group->withValue('Badass'), $context)->willReturn($secondNewObject); /** @var PropertyHydratorInterface $hydrator */ $hydrator = $hydratorProphecy->reveal(); $expected = new ResolvedFixtureSet(new ParameterBag(['iteration' => 2]), $fixtures, new ObjectBag(['dummy' => $secondNewObject])); $hydrator = new SimpleHydrator($hydrator, $resolver); $actual = $hydrator->hydrate($object, $set, $context); $this->assertEquals($expected, $actual); }