Completed
Push — master ( decfed...00ae50 )
by Narcotic
61:33
created

PrivateClassMethodTrait::getPrivateClassMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
1
<?php
2
/**
3
 * trait for test functions
4
 */
5
6
namespace Graviton\TestBundle\Test;
7
8
/**
9
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
10
 * @license  https://opensource.org/licenses/MIT MIT License
11
 * @link     http://swisscom.ch
12
 */
13
trait PrivateClassMethodTrait
14
{
15
16
    /**
17
     * Testing private methods for a class.
18
     *
19
     * $class = new YourClass(); or service...
20
     * $method = $this->getPrivateClassMethod(get_class($class), 'getPrivateFunction');
21
     * $result = $method->invokeArgs( $this->activityManager, [argument1, argument2, ...]);
22
     *
23
     * @param string $className  String name for class, full namespace.
24
     * @param string $methodName Method name to be used
25
     *
26
     * @return \ReflectionMethod
27
     * @throws \ReflectionException
28
     */
29
    public function getPrivateClassMethod($className, $methodName)
30
    {
31
        $reflector = new \ReflectionClass($className);
32
        $method = $reflector->getMethod($methodName);
33
        $method->setAccessible(true);
34
        return $method;
35
    }
36
37
    /**
38
     * provide a protected property of a given object.
39
     *
40
     * @param string $className    String name for class, full namespace.
41
     * @param string $propertyName property name
42
     *
43
     * @return \ReflectionProperty
44
     * @throws \ReflectionException
45
     */
46
    public function getPrivateClassProperty($className, $propertyName)
47
    {
48
        $reflector = new \ReflectionClass($className);
49
        $prop = $reflector->getProperty($propertyName);
50
        $prop->setAccessible(true);
51
        return $prop;
52
    }
53
}
54