Debian Codegraph @

tests/phpunit/unit/includes/FactoryArgTestTrait.php

raw symbols SCIP
1 <?php
2
3 // phpcs:disable MediaWiki.Commenting.FunctionComment.ObjectTypeHintReturn
4 // phpcs:disable MediaWiki.Commenting.FunctionComment.ObjectTypeHintParam
5
6 /**
7 * Test that a factory class correctly forwards all arguments to the class it constructs. This is
8 * useful because sometimes a class' constructor will have more arguments added, and it's easy to
9 * accidentally have the factory's constructor fall out of sync.
10 */
11 trait FactoryArgTestTrait {
12 /**
13 * @return string Name of factory class
14 */
15 abstract protected static function getFactoryClass();
16
17 /**
18 * @return string Name of instance class
19 */
20 abstract protected static function getInstanceClass();
21
22 /**
23 * @return int The number of arguments that the instance constructor receives but the factory
24 * constructor doesn't. Used for a simple argument count check. Override if this isn't zero.
25 */
26 protected static function getExtraClassArgCount() {
27 return 0;
28 }
29
30 /**
31 * Override if your factory method name is different from newInstanceClassName.
32 *
33 * @return string
34 */
35 protected function getFactoryMethodName() {
36 return 'new' . ( new ReflectionClass( $this->getInstanceClass() ) )->getShortName();
37 }
38
39 /**
40 * Override if $factory->$method( ...$args ) isn't the right way to create an instance, where
41 * $method is returned from getFactoryMethodName(), and $args is constructed by applying
42 * getMockValueForParam() to the factory method's parameters.
43 *
44 * @param object $factory
45 * @return object
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 * Override if getMockValueForParam doesn't produce suitable values for one or more of the
72 * parameters to your factory constructor or create method.
73 *
74 * @param ReflectionParameter $param One of the factory constructor's arguments
75 * @return array Empty to not override, or an array of one element which is the value to pass
76 * that will allow the object to be constructed successfully
77 */
78 protected function getOverriddenMockValueForParam( ReflectionParameter $param ) {
79 return [];
80 }
81
82 /**
83 * Override if this doesn't produce suitable values for one or more of the parameters to your
84 * factory constructor or create method.
85 *
86 * @param ReflectionParameter $param One of the factory constructor's arguments
87 * @return mixed A value to pass that will allow the object to be constructed successfully
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 // Optimistically assume a string is okay
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 * Assert that the given $instance correctly received $val as the value for parameter $name. By
118 * default, checks that the instance has some member whose value is the same as $val.
119 *
120 * @param object $instance
121 * @param string $name Name of parameter to the factory object's constructor
122 * @param mixed $val
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 * Override to return a list of constructor parameters that are not stored
137 * in the instance properties directly, so should not be verified with
138 * assertInstanceReceivedParam.
139 * @return string[]
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 }