|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the roukmoute/dto-tester package. |
| 5 | + * |
| 6 | + * (c) Mathias STRASSER <contact@roukmoute.fr> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace DtoTester; |
| 15 | + |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | +use Roave\BetterReflection\Reflection\ReflectionClass; |
| 18 | +use Roave\BetterReflection\Reflection\ReflectionMethod; |
| 19 | +use RuntimeException; |
| 20 | + |
| 21 | +abstract class DtoTest extends TestCase |
| 22 | +{ |
| 23 | + /** @var MapperCollection */ |
| 24 | + private $mappers; |
| 25 | + |
| 26 | + public function __construct($name = null, array $data = [], $dataName = '') |
| 27 | + { |
| 28 | + $this->mappers = MapperCollection::defaultMappers(); |
| 29 | + |
| 30 | + parent::__construct($name, $data, $dataName); |
| 31 | + } |
| 32 | + |
| 33 | + public function testGettersAndSetters() |
| 34 | + { |
| 35 | + $getterSetterMapping = []; |
| 36 | + |
| 37 | + $instance = $this->getInstance(); |
| 38 | + |
| 39 | + $this->foundMappings($instance, $getterSetterMapping); |
| 40 | + $this->assetMappings($getterSetterMapping, $instance); |
| 41 | + } |
| 42 | + |
| 43 | + abstract protected function getInstance(); |
| 44 | + |
| 45 | + private function foundMappings($instance, array &$getterSetterMapping): void |
| 46 | + { |
| 47 | + foreach (ReflectionClass::createFromInstance($instance)->getMethods() as $method) { |
| 48 | + $methodName = $method->getName(); |
| 49 | + $numberOfParameters = $method->getNumberOfParameters(); |
| 50 | + |
| 51 | + if ((mb_substr($methodName, 0, 3) === 'get' && $numberOfParameters === 0) |
| 52 | + || (mb_substr($methodName, 0, 3) === 'set' && $numberOfParameters === 1) |
| 53 | + || (mb_substr($methodName, 0, 2) === 'is' && $numberOfParameters === 0) |
| 54 | + ) { |
| 55 | + $objectName = mb_substr($methodName, mb_substr($methodName, 0, 2) === 'is' ? 2 : 3); |
| 56 | + |
| 57 | + $getterSettingPair = $this->getterSettingPair($getterSetterMapping, $objectName); |
| 58 | + |
| 59 | + if (mb_substr($methodName, 0, 3) === 'set') { |
| 60 | + $getterSettingPair->setSetter($method); |
| 61 | + } else { |
| 62 | + $getterSettingPair->setGetter($method); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + private function getterSettingPair(array &$getterSetterMapping, string $objectName): GetterSetterPair |
| 69 | + { |
| 70 | + if (isset($getterSetterMapping[$objectName])) { |
| 71 | + return $getterSetterMapping[$objectName]; |
| 72 | + } |
| 73 | + |
| 74 | + $getterSettingPair = new GetterSetterPair(); |
| 75 | + $getterSetterMapping[$objectName] = $getterSettingPair; |
| 76 | + |
| 77 | + return $getterSettingPair; |
| 78 | + } |
| 79 | + |
| 80 | + private function assetMappings(array &$getterSetterMapping, $instance): void |
| 81 | + { |
| 82 | + /** @var GetterSetterPair $pair */ |
| 83 | + foreach ($getterSetterMapping as $objectName => $pair) { |
| 84 | + $fieldName = mb_strtolower($objectName[0]) . mb_substr($objectName, 1); |
| 85 | + |
| 86 | + if ($pair->hasGetterAndSetter()) { |
| 87 | + $parameterType = $pair->setter()->getParameters()[0]->getType(); |
| 88 | + $newObject = $this->createObject($fieldName, (string) $parameterType); |
| 89 | + |
| 90 | + $pair->setter()->invoke($instance, $newObject); |
| 91 | + |
| 92 | + $this->callGetter($fieldName, $pair->getter(), $instance, $newObject); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + private function createObject(string $fieldName, string $class) |
| 98 | + { |
| 99 | + try { |
| 100 | + return $this->mappers->get($class)(); |
| 101 | + } catch (\Exception $exception) { |
| 102 | + throw new RuntimeException(sprintf('Unable to create objects for field "%s".', $fieldName)); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + private function callGetter(string $fieldName, ReflectionMethod $getter, $instance, $expected) |
| 107 | + { |
| 108 | + if ($getter->getReturnType()->isBuiltin()) { |
| 109 | + $this->assertEquals($expected, $getter->invoke($instance), sprintf('"$%s" is different', $fieldName)); |
| 110 | + } else { |
| 111 | + $this->assertSame($expected, $getter->invoke($instance), sprintf('"$%s" is different', $fieldName)); |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments