testCanCheckIfVersionComponentIsInDefaultOrNullState()   A
last analyzed

Complexity

Conditions 5
Paths 12

Size

Total Lines 62
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 37
nc 12
nop 2
dl 0
loc 62
rs 9.0168
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * PHP: Nelson Martell Library file
5
 *
6
 * Copyright © 2016-2021 Nelson Martell (http://nelson6e65.github.io)
7
 *
8
 * Licensed under The MIT License (MIT)
9
 * For full copyright and license information, please see the LICENSE
10
 * Redistributions of files must retain the above copyright notice.
11
 *
12
 * @copyright 2016-2021 Nelson Martell
13
 * @link      http://nelson6e65.github.io/php_nml/
14
 * @since     v0.6.0
15
 * @license   http://www.opensource.org/licenses/mit-license.php The MIT License (MIT)
16
 * */
17
18
declare(strict_types=1);
19
20
namespace NelsonMartell\Test\TestCase;
21
22
use InvalidArgumentException;
23
use NelsonMartell\Extensions\Text;
24
use NelsonMartell\Test\DataProviders\VersionComponentTestProvider;
25
use NelsonMartell\VersionComponent;
26
use PHPUnit\Framework\TestCase;
27
28
/**
29
 * @coversDefaultClass NelsonMartell\VersionComponent
30
 *
31
 * @author Nelson Martell <[email protected]>
32
 * @internal
33
 * */
34
class VersionComponentTest extends TestCase
35
{
36
    use VersionComponentTestProvider;
37
38
    public function getTargetClassName(): string
39
    {
40
        return VersionComponent::class;
41
    }
42
43
    /**
44
     * @testdox Performs conversion from compatible objects
45
     * @covers ::parse
46
     * @dataProvider goodVersionComponentParseMethodArgumentsProvider
47
     *
48
     * @param VersionComponent $expected
49
     * @param mixed            $obj
50
     */
51
    public function testParseMethod(VersionComponent $expected, $obj): void
52
    {
53
        $actual = VersionComponent::parse($obj);
54
55
        $message = Text::format(
56
            '{class}::{method}({obj}); // {actual}',
57
            [
58
                'class'  => VersionComponent::class,
59
                'method' => 'isValid',
60
                'obj'    => static::export($obj),
61
                'actual' => static::export($actual),
62
            ]
63
        );
64
65
        $this->assertEquals($expected, $actual, $message);
66
    }
67
68
    /**
69
     * @testdox Informs if error occurs on parsing incompatible objects
70
     * @covers ::parse
71
     * @dataProvider badVersionComponentParseMethodArgumentsProvider
72
     *
73
     * @param mixed $obj
74
     */
75
    public function testParseMethodWithInvalidArguments($obj): void
76
    {
77
        /** @var TestCase $this */
78
        $this->expectException(InvalidArgumentException::class);
79
        $actual = VersionComponent::parse($obj);
80
    }
81
82
    /**
83
     * @covers ::isNull
84
     * @covers ::isNotNull
85
     * @covers ::isDefault
86
     * @covers ::isNotDefault
87
     * @dataProvider nullOrDefaultStatesProvider
88
     *
89
     * @param string           $expected
90
     * @param VersionComponent $versionComponent
91
     */
92
    public function testCanCheckIfVersionComponentIsInDefaultOrNullState(
93
        string $expected,
94
        VersionComponent $versionComponent
95
    ): void {
96
        static $format = '$versionComponent->{method}(); // {actual}';
97
98
        $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...
99
        $actuals['isNotDefault'] = $versionComponent->isNotDefault();
100
        $actuals['isNull']       = $versionComponent->isNull();
101
        $actuals['isNotNull']    = $versionComponent->isNotNull();
102
103
        $messages = [];
104
105
        foreach ($actuals as $method => $actual) {
106
            $messages[$method] = Text::format($format, ['method' => $method, 'actual' => static::export($actual)]);
107
        }
108
109
        foreach ($actuals as $method => $actual) {
110
            // Pre-tests for returning type
111
            $this->assertIsBool($actual, $messages[$method] . ' # Should return a boolean #');
112
        }
113
114
        // Pre-tests for different values
115
        $this->assertNotEquals(
116
            $actuals['isDefault'],
117
            $actuals['isNotDefault'],
118
            $messages['isDefault'] . PHP_EOL . $messages['isNotDefault']
119
        );
120
121
        $this->assertNotEquals(
122
            $actuals['isNull'],
123
            $actuals['isNotNull'],
124
            $messages['isNull'] . PHP_EOL . $messages['isNotNull']
125
        );
126
127
128
        // Test expected
129
        if ($expected === 'default') {
130
            $this->assertTrue($actuals['isDefault'], $messages['isDefault']);
131
132
            // Can't be null AND default
133
            $this->assertNotEquals(
134
                $actuals['isNull'],
135
                $actuals['isDefault'],
136
                '#Can\'t be both, DEFAULT and NULL, at the same time' . PHP_EOL .
137
                $messages['isDefault'] . PHP_EOL .
138
                $messages['isNull']
139
            );
140
        } elseif ($expected === 'null') {
141
            $this->assertTrue($actuals['isNull'], $messages['isNull']);
142
143
            // Can't be null AND default
144
            $this->assertNotEquals(
145
                $actuals['isNull'],
146
                $actuals['isDefault'],
147
                '#Can\'t be both, NULL and DEFAULT, at the same time' . PHP_EOL .
148
                $messages['isNull'] . PHP_EOL .
149
                $messages['isDefault']
150
            );
151
        } else {
152
            $this->assertTrue($actuals['isNotDefault'], $messages['isNotDefault']);
153
            $this->assertTrue($actuals['isNotNull'], $messages['isNotNull']);
154
        }
155
    }
156
}
157