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

PropertiesHandlerTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 56
rs 10
c 0
b 0
f 0
wmc 5
lcom 0
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetAccesibleProperties() 0 5 1
A testSetAccesibleProperties() 0 13 2
A testUnaccesibleProperties() 0 12 2
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;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 13 spaces but found 1 space

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

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
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;
0 ignored issues
show
Unused Code introduced by
$tmp 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...
84
        } else {
85
            $obj->$propertyName = $value;
86
        }
87
88
        $this->assertNull($actual);
89
    }
90
}
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...
91