Completed
Pull Request — develop (#640)
by Narcotic
08:00 queued 01:02
created

PrivateClassMethodTrait::getPrivateClassProperty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
ccs 5
cts 5
cp 1
cc 1
eloc 5
nc 1
nop 2
crap 1
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 20
    public function getPrivateClassMethod($className, $methodName)
30
    {
31 20
        $reflector = new \ReflectionClass($className);
32 20
        $method = $reflector->getMethod($methodName);
33 20
        $method->setAccessible(true);
34 20
        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 4
    public function getPrivateClassProperty($className, $propertyName)
47
    {
48 4
        $reflector = new \ReflectionClass($className);
49 4
        $prop = $reflector->getProperty($propertyName);
50 4
        $prop->setAccessible(true);
51 4
        return $prop;
52
    }
53
}
54