1- <?php
1+ <?php declare (strict_types = 1 );
22
33namespace Spamer \DependencyMocker ;
44
5- use Nette ;
6- use Mockery ;
75
86class Mocker
97{
10- /** @var array */
8+ /**
9+ * @var array
10+ */
1111 public static $ bannedClasses = [];
1212
13- /** @var \ReflectionClass */
13+ /**
14+ * @var \ReflectionClass
15+ */
1416 private static $ reflectedClass ;
1517
16- /** @var Mockery\Mock */
17- private static $ mockedClass ;
18-
1918 /**
20- * @param string $className
21- * @return \ReflectionClass|\stdClass
19+ * @var \Mockery\Mock
2220 */
23- public static function mockClassDependencies ($ className )
21+ private static $ mockedClass ;
22+
23+
24+ public static function mockClassDependencies (
25+ string $ className
26+ ) : \Mockery \MockInterface
2427 {
2528 self ::$ reflectedClass = new \ReflectionClass ($ className );
26- self ::$ mockedClass = Mockery::mock ($ className );
29+ self ::$ mockedClass = \ Mockery::mock ($ className );
2730
2831 self ::mockInjectedMethods ($ className );
2932 self ::mockInjectedProperties ();
@@ -33,32 +36,31 @@ public static function mockClassDependencies($className)
3336 }
3437
3538
36- /**
37- * @param string $className
38- */
39- private static function mockInjectedMethods ($ className )
39+ private static function mockInjectedMethods (
40+ string $ className
41+ ) : void
4042 {
4143 foreach (self ::$ reflectedClass ->getMethods () as $ method ) {
42- if (substr ($ method ->getName (), 0 , 6 ) === ' inject ' ) {
44+ if (\strpos ($ method ->getName (), ' inject ' ) === 0 ) {
4345 self ::mockDependenciesFromMethod ($ className , $ method ->getName ());
4446 }
4547 }
4648 }
4749
4850
49- private static function mockInjectedProperties ()
51+ private static function mockInjectedProperties () : void
5052 {
5153 /** @var \ReflectionProperty $property */
5254 foreach (self ::$ reflectedClass ->getProperties () as $ property ) {
5355 if (
54- Nette \ DI \PhpReflection ::parseAnnotation ($ property , 'inject ' ) !== NULL
56+ ReflectionHelper ::parseAnnotation ($ property , 'inject ' ) !== NULL
5557 ||
56- Nette \ DI \PhpReflection ::parseAnnotation ($ property , 'autowire ' ) !== NULL
58+ ReflectionHelper ::parseAnnotation ($ property , 'autowire ' ) !== NULL
5759 ) {
58- if ($ mockedParameterClass = Nette \ DI \PhpReflection ::parseAnnotation ($ property , 'var ' )) {
59- $ mockedParameterClass = Nette \ DI \PhpReflection ::expandClassName (
60+ if ($ mockedParameterClass = ReflectionHelper ::parseAnnotation ($ property , 'var ' )) {
61+ $ mockedParameterClass = ReflectionHelper ::expandClassName (
6062 $ mockedParameterClass ,
61- Nette \ DI \PhpReflection ::getDeclaringClass ($ property )
63+ ReflectionHelper ::getDeclaringClass ($ property )
6264 );
6365 }
6466 self ::setProperty ($ mockedParameterClass , $ property );
@@ -67,22 +69,18 @@ private static function mockInjectedProperties()
6769 }
6870
6971
70- /**
71- * @param string $className
72- */
73- private static function mockConstructorDependencies ($ className )
72+ private static function mockConstructorDependencies (string $ className ) : void
7473 {
7574 if (method_exists ($ className , '__construct ' )) {
7675 self ::mockDependenciesFromMethod ($ className , '__construct ' );
7776 }
7877 }
7978
8079
81- /**
82- * @param string $className
83- * @param string $methodName
84- */
85- private static function mockDependenciesFromMethod ($ className , $ methodName )
80+ private static function mockDependenciesFromMethod (
81+ string $ className ,
82+ string $ methodName
83+ ) : void
8684 {
8785 $ reflectionMethod = new \ReflectionMethod ($ className , $ methodName );
8886 $ parameters = $ reflectionMethod ->getParameters ();
@@ -97,10 +95,9 @@ private static function mockDependenciesFromMethod($className, $methodName)
9795 }
9896
9997
100- /**
101- * @param array $bannedClasses
102- */
103- public static function setBannedClasses ($ bannedClasses )
98+ public static function setBannedClasses (
99+ array $ bannedClasses
100+ ) : void
104101 {
105102 self ::$ bannedClasses = $ bannedClasses ;
106103 }
@@ -109,11 +106,18 @@ public static function setBannedClasses($bannedClasses)
109106 /**
110107 * @param string $className
111108 * @param \ReflectionParameter|\ReflectionProperty $class
109+ * @throws \ReflectionException
112110 */
113- private static function setProperty ($ className , $ class )
111+ private static function setProperty (
112+ string $ className ,
113+ $ class
114+ ) : void
114115 {
115- if ( ! in_array ($ className , self ::$ bannedClasses ) && $ class ->getDeclaringClass ()->hasProperty ($ class ->getName ())) {
116- $ mockedParameter = Mockery::mock ($ className );
116+ if (
117+ ! in_array ($ className , self ::$ bannedClasses , TRUE )
118+ && $ class ->getDeclaringClass ()->hasProperty ($ class ->getName ())
119+ ) {
120+ $ mockedParameter = \Mockery::mock ($ className );
117121 $ property = new \ReflectionProperty ($ class ->getDeclaringClass ()->getName (), $ class ->getName ());
118122 $ property ->setAccessible (TRUE );
119123 $ property ->setValue (self ::$ mockedClass , $ mockedParameter );
@@ -126,6 +130,7 @@ private static function setProperty($className, $class)
126130 * @param string $property
127131 * @param object $object
128132 * @return mixed
133+ * @throws \ReflectionException
129134 */
130135 public static function getProperty ($ class , $ property , $ object )
131136 {
@@ -142,6 +147,7 @@ public static function getProperty($class, $property, $object)
142147 * @param string $method
143148 * @param array $arguments
144149 * @return mixed
150+ * @throws \ReflectionException
145151 */
146152 public static function callPrivateFunction ($ object , $ method , $ arguments = [])
147153 {
0 commit comments