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

PrivateClassMethodTrait   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
c 1
b 0
f 0
lcom 0
cbo 0
dl 0
loc 41
rs 10
ccs 10
cts 10
cp 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getPrivateClassMethod() 0 7 1
A getPrivateClassProperty() 0 7 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