Completed
Push — master ( 446f2e...32b2c3 )
by Nelson
11:26
created

ConstructorMethodTester   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 75
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
getTargetClassName() 0 1 ?
A getTargetClassReflection() 0 16 4
A getTargetClassInstance() 0 4 1
A testConstructor() 0 4 1
A testConstructorWithBadArguments() 0 4 1
goodConstructorArgumentsProvider() 0 1 ?
badConstructorArgumentsProvider() 0 1 ?
1
<?php
2
/**
3
 * PHP: Nelson Martell Library file
4
 *
5
 * Content:
6
 * - Trait definition
7
 *
8
 * Copyright © 2016 Nelson Martell (http://nelson6e65.github.io)
9
 *
10
 * Licensed under The MIT License (MIT)
11
 * For full copyright and license information, please see the LICENSE
12
 * Redistributions of files must retain the above copyright notice.
13
 *
14
 * @copyright 2016 Nelson Martell
15
 * @link      http://nelson6e65.github.io/php_nml/
16
 * @since     v0.6.0
17
 * @license   http://www.opensource.org/licenses/mit-license.php The MIT License (MIT)
18
 * */
19
20
namespace NelsonMartell\Test\Helpers;
21
22
use \ReflectionClass;
23
use \ReflectionException;
24
use \BadMethodCallException;
25
use \UnexpectedValueException;
26
27
/**
28
 * Provides test methods and helpers to test class constructors.
29
 *
30
 * @author Nelson Martell <[email protected]>
31
 * */
32
trait ConstructorMethodTester
33
{
34
    /**
35
     * Gets the name of class target of this test-class.
36
     *
37
     * @return string
38
     */
39
    public abstract function getTargetClassName();
40
41
    protected $targetClassReflection = null;
42
43
44
    public function getTargetClassReflection()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
45
    {
46
        if ($this->targetClassReflection === null) {
47
            try {
48
                if (!is_string($this->getTargetClassName())) {
49
                    throw new UnexpectedValueException('``getTargetClassName()`` is not returning an string.');
50
                }
51
52
                $this->targetClassReflection = new ReflectionClass($this->getTargetClassName());
53
            } catch (ReflectionException $e) {
54
                throw new BadMethodCallException('``getTargetClassName()`` is not returning a valid class name.');
55
            }
56
        }
57
58
        return $this->targetClassReflection;
59
    }
60
61
    /**
62
     * Gets (dinamically) an instance of target class using its constructor with the (optional) arguments.
63
     * It uses the ``getTargetClassName`` return value to determinate the name of target class.
64
     *
65
     * @return mixed Instance of target class.
66
     * @throws UnexpectedValueException
67
     * @throws BadMethodCallException
68
     */
69
    public function getTargetClassInstance()
70
    {
71
        return $this->getTargetClassReflection()->newInstanceArgs(func_get_args());
72
    }
73
74
    /**
75
     * @testdox Creates new instances
76
     * @dataProvider goodConstructorArgumentsProvider
77
     */
78
    public function testConstructor()
79
    {
80
        $obj = call_user_func_array([$this, 'getTargetClassInstance'], func_get_args());
0 ignored issues
show
Unused Code introduced by
$obj is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
81
    }
82
83
    /**
84
     * @testdox Informs when error occurs on creating new instances
85
     * @dataProvider badConstructorArgumentsProvider
86
     * @expectedException \InvalidArgumentException
87
     */
88
    public function testConstructorWithBadArguments()
89
    {
90
        $obj = call_user_func_array([$this, 'getTargetClassInstance'], func_get_args());
0 ignored issues
show
Unused Code introduced by
$obj is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
91
    }
92
93
    /**
94
     * Must provide valid argument for constructor.
95
     *
96
     * @return array
97
     */
98
    public abstract function goodConstructorArgumentsProvider();
99
100
    /**
101
     * Must provide invalid argument for constructor.
102
     *
103
     * @return array
104
     */
105
    public abstract function badConstructorArgumentsProvider();
106
}
0 ignored issues
show
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
107