1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PHP: Nelson Martell Library file |
4
|
|
|
* |
5
|
|
|
* Content: |
6
|
|
|
* - Test case for: NelsonMartell\PropertiesHandler |
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\TestCase; |
21
|
|
|
|
22
|
|
|
use NelsonMartell as NML; |
23
|
|
|
use NelsonMartell\Type; |
24
|
|
|
use NelsonMartell\Extensions\String; |
25
|
|
|
use NelsonMartell\Test\DataProviders\PropertiesHandlerTestProvider; |
26
|
|
|
use \PHPUnit_Framework_TestCase as TestCase; |
27
|
|
|
use \InvalidArgumentException; |
28
|
|
|
use \BadMethodCallException; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* |
32
|
|
|
* @author Nelson Martell <[email protected]> |
33
|
|
|
* @internal |
34
|
|
|
* */ |
35
|
|
|
class PropertiesHandlerTest extends TestCase |
36
|
|
|
{ |
37
|
|
|
use PropertiesHandlerTestProvider; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @testdox Uses getters and setters methods in order to access and set values to class properties |
41
|
|
|
* @dataProvider getAccesiblePropertiesProvider |
42
|
|
|
*/ |
43
|
|
|
public function testGetAccesibleProperties($expected, $obj, $propertyName) |
44
|
|
|
{ |
45
|
|
|
$actual = $obj->$propertyName; |
46
|
|
|
$this->assertEquals($expected, $actual); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @testdox Can set properties with setters. |
51
|
|
|
* @param mixed|null $expected |
52
|
|
|
* @param mixed $obj |
53
|
|
|
* @param string $propertyName |
54
|
|
|
* @param mixed $value |
55
|
|
|
* |
56
|
|
|
* @return void |
57
|
|
|
* @dataProvider setAccesiblePropertiesProvider |
58
|
|
|
*/ |
59
|
|
|
public function testSetAccesibleProperties($expected, $obj, $propertyName, $value = null) |
60
|
|
|
{ |
61
|
|
|
$actual = null; |
62
|
|
|
if ($expected === null) { |
63
|
|
|
// Check not throws any error |
64
|
|
|
$obj->$propertyName = $value; |
65
|
|
|
} else { |
66
|
|
|
$obj->$propertyName = $value; |
67
|
|
|
$actual = $obj->$propertyName; |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$this->assertEquals($expected, $actual); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @testdox Informs when trying to access inexistent or inaccesible properties (catchable exception) |
75
|
|
|
* @expectedException BadMethodCallException |
76
|
|
|
* @dataProvider unaccesiblePropertiesProvider |
77
|
|
|
*/ |
78
|
|
|
public function testUnaccesibleProperties($obj, $propertyName, $value = null) |
79
|
|
|
{ |
80
|
|
|
$actual = null; |
81
|
|
|
|
82
|
|
|
if ($value === null) { |
83
|
|
|
$tmp = $obj->$propertyName; |
|
|
|
|
84
|
|
|
} else { |
85
|
|
|
$obj->$propertyName = $value; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$this->assertNull($actual); |
89
|
|
|
} |
90
|
|
|
} |
|
|
|
|
91
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.