| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
trait FactoryArgTestTrait { |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
abstract protected static function getFactoryClass(); |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
abstract protected static function getInstanceClass(); |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
protected static function getExtraClassArgCount() { |
| 27 |
return 0; |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
protected function getFactoryMethodName() { |
| 36 |
return 'new' . ( new ReflectionClass( $this->getInstanceClass() ) )->getShortName(); |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
protected function createInstanceFromFactory( $factory ) { |
| 48 |
$methodName = $this->getFactoryMethodName(); |
| 49 |
$methodObj = new ReflectionMethod( $factory, $methodName ); |
| 50 |
$mocks = []; |
| 51 |
foreach ( $methodObj->getParameters() as $param ) { |
| 52 |
$mocks[] = $this->getMockValueForParam( $param ); |
| 53 |
} |
| 54 |
|
| 55 |
return $factory->$methodName( ...$mocks ); |
| 56 |
} |
| 57 |
|
| 58 |
public function testConstructorArgNum() { |
| 59 |
$factoryClass = static::getFactoryClass(); |
| 60 |
$instanceClass = static::getInstanceClass(); |
| 61 |
$factoryConstructor = new ReflectionMethod( $factoryClass, '__construct' ); |
| 62 |
$instanceConstructor = new ReflectionMethod( $instanceClass, '__construct' ); |
| 63 |
$this->assertSame( |
| 64 |
$instanceConstructor->getNumberOfParameters() - static::getExtraClassArgCount(), |
| 65 |
$factoryConstructor->getNumberOfParameters(), |
| 66 |
"$instanceClass and $factoryClass constructors have an inconsistent number of " . |
| 67 |
' parameters. Did you add a parameter to one and not the other?' ); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
protected function getOverriddenMockValueForParam( ReflectionParameter $param ) { |
| 79 |
return []; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
protected function getMockValueForParam( ReflectionParameter $param ) { |
| 90 |
$overridden = $this->getOverriddenMockValueForParam( $param ); |
| 91 |
if ( $overridden ) { |
| 92 |
return $overridden[0]; |
| 93 |
} |
| 94 |
|
| 95 |
$pos = $param->getPosition(); |
| 96 |
|
| 97 |
$type = $param->getType(); |
| 98 |
if ( !$type || $type->getName() === 'string' ) { |
| 99 |
|
| 100 |
return "some unlikely string $pos"; |
| 101 |
} |
| 102 |
|
| 103 |
$type = $type->getName(); |
| 104 |
|
| 105 |
if ( $type === 'array' || $type === 'iterable' ) { |
| 106 |
return [ "some unlikely string $pos" ]; |
| 107 |
} |
| 108 |
|
| 109 |
if ( class_exists( $type ) || interface_exists( $type ) ) { |
| 110 |
return $this->createMock( $type ); |
| 111 |
} |
| 112 |
|
| 113 |
$this->fail( "Unrecognized parameter type $type" ); |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
protected function assertInstanceReceivedParam( $instance, $name, $val ) { |
| 125 |
foreach ( ( new ReflectionObject( $instance ) )->getProperties() as $prop ) { |
| 126 |
if ( $prop->getValue( $instance ) === $val ) { |
| 127 |
$this->assertTrue( true ); |
| 128 |
return; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
$this->fail( "Param $name not received by " . static::getInstanceClass() ); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
protected function getIgnoredParamNames() { |
| 142 |
return [ 'hookContainer' ]; |
| 143 |
} |
| 144 |
|
| 145 |
public function testAllArgumentsWerePassed() { |
| 146 |
$factoryClass = static::getFactoryClass(); |
| 147 |
|
| 148 |
$factoryConstructor = new ReflectionMethod( $factoryClass, '__construct' ); |
| 149 |
$mocks = []; |
| 150 |
foreach ( $factoryConstructor->getParameters() as $param ) { |
| 151 |
$mocks[$param->getName()] = $this->getMockValueForParam( $param ); |
| 152 |
} |
| 153 |
|
| 154 |
$instance = |
| 155 |
$this->createInstanceFromFactory( new $factoryClass( ...array_values( $mocks ) ) ); |
| 156 |
|
| 157 |
foreach ( $mocks as $name => $mock ) { |
| 158 |
if ( in_array( $name, $this->getIgnoredParamNames() ) ) { |
| 159 |
continue; |
| 160 |
} |
| 161 |
$this->assertInstanceReceivedParam( $instance, $name, $mock ); |
| 162 |
} |
| 163 |
} |
| 164 |
} |