|
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() |
|
|
|
|
|
|
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()); |
|
|
|
|
|
|
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()); |
|
|
|
|
|
|
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
|
|
|
} |
|
|
|
|
|
|
107
|
|
|
|
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
@returnannotation as described here.