From 578139747f7d309928d6ab164081866c3c8ae303 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Wed, 21 Sep 2016 22:02:06 +1200 Subject: [PATCH] Added custom resolver support --- src/Resolver/DataObjectLowerCamelResolver.php | 44 +++++++++++++++ src/Resolver/IResolver.php | 18 ++++++ src/TypeCreator.php | 18 ++++++ tests/Fake/DataObjectFake.php | 19 +++++++ .../DataObjectLowerCamelResolverTest.php | 56 +++++++++++++++++++ tests/TypeCreatorTest.php | 25 +++++++++ 6 files changed, 180 insertions(+) create mode 100644 src/Resolver/DataObjectLowerCamelResolver.php create mode 100644 src/Resolver/IResolver.php create mode 100644 tests/Fake/DataObjectFake.php create mode 100644 tests/Resolver/DataObjectLowerCamelResolverTest.php diff --git a/src/Resolver/DataObjectLowerCamelResolver.php b/src/Resolver/DataObjectLowerCamelResolver.php new file mode 100644 index 000000000..83e0e2827 --- /dev/null +++ b/src/Resolver/DataObjectLowerCamelResolver.php @@ -0,0 +1,44 @@ +hasMethod($info->fieldName)) { + return $object->{$info->fieldName}(); + } + + // Correct case (and getters) + if($object->hasField($info->fieldName)) { + return $object->{$info->fieldName}; + } + + // Infer casing + if($object instanceof DataObject) { + $parents = ClassInfo::ancestry($object, true); + foreach($parents as $parent) { + $fields = DataObject::database_fields($parent); + foreach($fields as $fieldName => $fieldClass) { + if(strcasecmp($fieldName, $info->fieldName) === 0) { + return $object->getField($fieldName); + } + } + } + } + } + +} diff --git a/src/Resolver/IResolver.php b/src/Resolver/IResolver.php new file mode 100644 index 000000000..233e052fd --- /dev/null +++ b/src/Resolver/IResolver.php @@ -0,0 +1,18 @@ + 99 + ]); + $resolver = new DataObjectLowerCamelResolver(); + $info = new ResolveInfo([ + 'fieldName' => 'ID' + ]); + $this->assertEquals(99, $resolver->resolve($fake, [], [], $info)); + } + + public function testResolvesDifferentCasing() + { + $fake = new DataObjectFake([ + 'ID' => 99 + ]); + $resolver = new DataObjectLowerCamelResolver(); + $info = new ResolveInfo([ + 'fieldName' => 'id' // lowercase + ]); + $this->assertEquals(99, $resolver->resolve($fake, [], [], $info)); + } + + public function testResolvesCustomGetter() + { + $fake = new DataObjectFake([]); + $resolver = new DataObjectLowerCamelResolver(); + $info = new ResolveInfo([ + 'fieldName' => 'customGetter' + ]); + $this->assertEquals('customGetterValue', $resolver->resolve($fake, [], [], $info)); + } + + public function testResolvesMethod() + { + $fake = new DataObjectFake([]); + $resolver = new DataObjectLowerCamelResolver(); + $info = new ResolveInfo([ + 'fieldName' => 'customMethod' + ]); + $this->assertEquals('customMethodValue', $resolver->resolve($fake, [], [], $info)); + } +} diff --git a/tests/TypeCreatorTest.php b/tests/TypeCreatorTest.php index 01224923d..585fade25 100644 --- a/tests/TypeCreatorTest.php +++ b/tests/TypeCreatorTest.php @@ -106,4 +106,29 @@ public function testGetFieldsUsesResolverMethod() $this->assertArrayHasKey('resolve', $fields['fieldA']); $this->assertArrayNotHasKey('resolve', $fields['fieldB']); } + + public function testGetFieldsUsesAllFieldsResolverMethod() + { + $mock = $this->getMockBuilder(TypeCreator::class) + ->setMethods(['fields','resolveField']) + ->getMock(); + $mock->method('fields')->willReturn([ + 'fieldA' => [ + 'type' => Type::string(), + ], + 'fieldB' => [ + 'type' => Type::string(), + ], + ]); + $mock->method('resolveField') + ->willReturn('resolved'); + + $fields = $mock->getFields(); + $this->assertArrayHasKey('fieldA', $fields); + $this->assertArrayHasKey('fieldB', $fields); + $this->assertArrayHasKey('resolve', $fields['fieldA']); + $this->assertArrayHasKey('resolve', $fields['fieldB']); + $this->assertEquals('resolved', $fields['fieldA']['resolve']()); + $this->assertEquals('resolved', $fields['fieldB']['resolve']()); + } }