Completed
Push — master ( a2c643...e562f7 )
by Nelson
04:00
created

VersionComponentTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 59
dl 0
loc 144
rs 10
c 0
b 0
f 0
wmc 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testPerformsImplicitConversionToString() 0 6 1
A testParseMethod() 0 15 1
A getTargetClassName() 0 3 1
A testCanCheckIfVersionComponentIsInDefaultOrNullState() 0 60 5
A testPerformsConversionToString() 0 14 1
A testParseMethodWithInvalidArguments() 0 3 1
1
<?php
2
/**
3
 * PHP: Nelson Martell Library file
4
 *
5
 * Content:
6
 * - Test case for: [NelsonMartell] Version
7
 *
8
 * Copyright © 2016-2017 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-2017 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\Extensions\Text;
23
use NelsonMartell\Test\DataProviders\VersionComponentTestProvider;
24
use NelsonMartell\VersionComponent;
25
use PHPUnit\Framework\TestCase;
26
use \InvalidArgumentException;
27
28
/**
29
 *
30
 * @author Nelson Martell <[email protected]>
31
 * @internal
32
 * */
33
class VersionComponentTest extends TestCase
34
{
35
    use VersionComponentTestProvider;
36
37
    public function getTargetClassName()
38
    {
39
        return VersionComponent::class;
40
    }
41
42
    /**
43
     * @testdox Performs conversion from compatible objects
44
     * @coverage VersionComponent::parse
45
     * @dataProvider goodVersionComponentParseMethodArgumentsProvider
46
     */
47
    public function testParseMethod(VersionComponent $expected, $obj)
48
    {
49
        $actual = VersionComponent::parse($obj);
50
51
        $message = Text::format(
52
            '{class}::{method}({obj}); // {actual}',
53
            [
54
                'class'  => VersionComponent::class,
55
                'method' => 'isValid',
56
                'obj'    => static::export($obj),
57
                'actual' => static::export($actual)
58
            ]
59
        );
60
61
        $this->assertEquals($expected, $actual, $message);
62
    }
63
64
    /**
65
     * @testdox Informs if error occurs on parsing incompatible objects
66
     * @coverage VersionComponent::parse
67
     * @dataProvider badVersionComponentParseMethodArgumentsProvider
68
     * @expectedException \InvalidArgumentException
69
     */
70
    public function testParseMethodWithInvalidArguments($obj)
71
    {
72
        $actual = VersionComponent::parse($obj);
1 ignored issue
show
Unused Code introduced by
The assignment to $actual is dead and can be removed.
Loading history...
73
    }
74
75
    /**
76
     * @coverage VersionComponent::__toString
77
     * @coverage VersionComponent::toString
78
     * @dataProvider versionComponentToStringMethodArgumentsProvider
79
     */
80
    public function testPerformsConversionToString($expected, VersionComponent $versionComponent)
81
    {
82
        $actual = $versionComponent->toString();
83
84
        $message = Text::format(
85
            '$versionComponent->{method}(); // {actual}',
86
            [
87
                'method' => 'toString',
88
                'actual' => static::export($actual)
89
            ]
90
        );
91
92
        $this->assertInternalType('string', $actual, $message.' # Should return a string #');
93
        $this->assertEquals($expected, $actual, $message);
94
    }
95
96
    /**
97
     * @coverage VersionComponent::__toString
98
     * @coverage VersionComponent::toString
99
     * @depends testPerformsConversionToString
100
     * @dataProvider versionComponentToStringMethodArgumentsProvider
101
     */
102
    public function testPerformsImplicitConversionToString($str, VersionComponent $obj)
103
    {
104
        $expected = "<VersionComponent>$str</VersionComponent>";
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $str instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
105
        $actual   = "<VersionComponent>$obj</VersionComponent>";
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $obj instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
106
107
        $this->assertEquals($expected, $actual);
108
    }
109
110
    /**
111
     * @coverage VersionComponent::isNull
112
     * @coverage VersionComponent::isNotNull
113
     * @coverage VersionComponent::isDefault
114
     * @coverage VersionComponent::isNotDefault
115
     * @dataProvider nullOrDefaultStatesProvider
116
     */
117
    public function testCanCheckIfVersionComponentIsInDefaultOrNullState($expected, VersionComponent $versionComponent)
1 ignored issue
show
Coding Style introduced by
This line exceeds maximum limit of 100 characters; contains 119 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
118
    {
119
        static $format = '$versionComponent->{method}(); // {actual}';
120
121
        $actuals['isDefault']    = $versionComponent->isDefault();
0 ignored issues
show
Comprehensibility Best Practice introduced by
$actuals was never initialized. Although not strictly required by PHP, it is generally a good practice to add $actuals = array(); before regardless.
Loading history...
122
        $actuals['isNotDefault'] = $versionComponent->isNotDefault();
123
        $actuals['isNull']       = $versionComponent->isNull();
124
        $actuals['isNotNull']    = $versionComponent->isNotNull();
125
126
        $messages = [];
127
128
        foreach ($actuals as $method => $actual) {
129
            $messages[$method] = Text::format($format, ['method' => $method, 'actual' => static::export($actual)]);
1 ignored issue
show
Coding Style introduced by
This line exceeds maximum limit of 100 characters; contains 115 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
130
        }
131
132
        foreach ($actuals as $method => $actual) {
133
            // Pre-tests for returning type
134
            $this->assertInternalType('boolean', $actual, $messages[$method].' # Should return a boolean #');
1 ignored issue
show
Coding Style introduced by
This line exceeds maximum limit of 100 characters; contains 109 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
135
        }
136
137
        // Pre-tests for different values
138
        $this->assertNotEquals(
139
            $actuals['isDefault'],
140
            $actuals['isNotDefault'],
141
            $messages['isDefault'].PHP_EOL.$messages['isNotDefault']
142
        );
143
144
        $this->assertNotEquals(
145
            $actuals['isNull'],
146
            $actuals['isNotNull'],
147
            $messages['isNull'].PHP_EOL.$messages['isNotNull']
148
        );
149
150
151
        // Test expected
152
        if ($expected === 'default') {
153
            $this->assertTrue($actuals['isDefault'], $messages['isDefault']);
154
155
            // Can't be null AND default
156
            $this->assertNotEquals(
157
                $actuals['isNull'],
158
                $actuals['isDefault'],
159
                '#Can\'t be both, DEFAULT and NULL, at the same time'.PHP_EOL.
160
                $messages['isDefault'].PHP_EOL.
161
                $messages['isNull']
162
            );
163
        } elseif ($expected === 'null') {
164
            $this->assertTrue($actuals['isNull'], $messages['isNull']);
165
166
            // Can't be null AND default
167
            $this->assertNotEquals(
168
                $actuals['isNull'],
169
                $actuals['isDefault'],
170
                '#Can\'t be both, NULL and DEFAULT, at the same time'.PHP_EOL.
171
                $messages['isNull'].PHP_EOL.
172
                $messages['isDefault']
173
            );
174
        } else {
175
            $this->assertTrue($actuals['isNotDefault'], $messages['isNotDefault']);
176
            $this->assertTrue($actuals['isNotNull'], $messages['isNotNull']);
177
        }
178
    }
179
}
180