Test Failed
Push — master ( 322b96...cb950d )
by Nelson
03:16
created

VersionComponentTest::testParseMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 2
dl 0
loc 15
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
/**
3
 * PHP: Nelson Martell Library file
4
 *
5
 * Copyright © 2016-2019 Nelson Martell (http://nelson6e65.github.io)
6
 *
7
 * Licensed under The MIT License (MIT)
8
 * For full copyright and license information, please see the LICENSE
9
 * Redistributions of files must retain the above copyright notice.
10
 *
11
 * @copyright 2016-2019 Nelson Martell
12
 * @link      http://nelson6e65.github.io/php_nml/
13
 * @since     v0.6.0
14
 * @license   http://www.opensource.org/licenses/mit-license.php The MIT License (MIT)
15
 * */
16
17
namespace NelsonMartell\Test\TestCase;
18
19
use NelsonMartell\Extensions\Text;
20
21
use NelsonMartell\Test\DataProviders\VersionComponentTestProvider;
22
23
use NelsonMartell\VersionComponent;
24
25
use PHPUnit\Framework\TestCase;
26
27
/**
28
 *
29
 * @author Nelson Martell <[email protected]>
30
 * @internal
31
 * */
32
class VersionComponentTest extends TestCase
33
{
34
    use VersionComponentTestProvider;
35
36
    public function getTargetClassName()
37
    {
38
        return VersionComponent::class;
39
    }
40
41
    /**
42
     * @testdox Performs conversion from compatible objects
43
     * @covers VersionComponent::parse
44
     * @dataProvider goodVersionComponentParseMethodArgumentsProvider
45
     *
46
     * @param VersionComponent $expected
47
     * @param mixed            $obj
48
     */
49
    public function testParseMethod(VersionComponent $expected, $obj) : void
50
    {
51
        $actual = VersionComponent::parse($obj);
52
53
        $message = Text::format(
54
            '{class}::{method}({obj}); // {actual}',
55
            [
56
                'class'  => VersionComponent::class,
57
                'method' => 'isValid',
58
                'obj'    => static::export($obj),
59
                'actual' => static::export($actual)
60
            ]
61
        );
62
63
        $this->assertEquals($expected, $actual, $message);
64
    }
65
66
    /**
67
     * @testdox Informs if error occurs on parsing incompatible objects
68
     * @covers VersionComponent::parse
69
     * @dataProvider badVersionComponentParseMethodArgumentsProvider
70
     * @expectedException \InvalidArgumentException
71
     *
72
     * @param mixed $obj
73
     */
74
    public function testParseMethodWithInvalidArguments($obj)
75
    {
76
        $actual = VersionComponent::parse($obj);
77
    }
78
79
    /**
80
     * @covers VersionComponent::isNull
81
     * @covers VersionComponent::isNotNull
82
     * @covers VersionComponent::isDefault
83
     * @covers VersionComponent::isNotDefault
84
     * @dataProvider nullOrDefaultStatesProvider
85
     *
86
     * @param string           $expected
87
     * @param VersionComponent $versionComponent
88
     */
89
    public function testCanCheckIfVersionComponentIsInDefaultOrNullState(
90
        string $expected,
91
        VersionComponent $versionComponent
92
    ) : void {
93
        static $format = '$versionComponent->{method}(); // {actual}';
94
95
        $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...
96
        $actuals['isNotDefault'] = $versionComponent->isNotDefault();
97
        $actuals['isNull']       = $versionComponent->isNull();
98
        $actuals['isNotNull']    = $versionComponent->isNotNull();
99
100
        $messages = [];
101
102
        foreach ($actuals as $method => $actual) {
103
            $messages[$method] = Text::format($format, ['method' => $method, 'actual' => static::export($actual)]);
104
        }
105
106
        foreach ($actuals as $method => $actual) {
107
            // Pre-tests for returning type
108
            $this->assertInternalType('boolean', $actual, $messages[$method].' # Should return a boolean #');
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\Assert::assertInternalType() has been deprecated: https://github.com/sebastianbergmann/phpunit/issues/3369 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

108
            /** @scrutinizer ignore-deprecated */ $this->assertInternalType('boolean', $actual, $messages[$method].' # Should return a boolean #');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
109
        }
110
111
        // Pre-tests for different values
112
        $this->assertNotEquals(
113
            $actuals['isDefault'],
114
            $actuals['isNotDefault'],
115
            $messages['isDefault'].PHP_EOL.$messages['isNotDefault']
116
        );
117
118
        $this->assertNotEquals(
119
            $actuals['isNull'],
120
            $actuals['isNotNull'],
121
            $messages['isNull'].PHP_EOL.$messages['isNotNull']
122
        );
123
124
125
        // Test expected
126
        if ($expected === 'default') {
127
            $this->assertTrue($actuals['isDefault'], $messages['isDefault']);
128
129
            // Can't be null AND default
130
            $this->assertNotEquals(
131
                $actuals['isNull'],
132
                $actuals['isDefault'],
133
                '#Can\'t be both, DEFAULT and NULL, at the same time'.PHP_EOL.
134
                $messages['isDefault'].PHP_EOL.
135
                $messages['isNull']
136
            );
137
        } elseif ($expected === 'null') {
138
            $this->assertTrue($actuals['isNull'], $messages['isNull']);
139
140
            // Can't be null AND default
141
            $this->assertNotEquals(
142
                $actuals['isNull'],
143
                $actuals['isDefault'],
144
                '#Can\'t be both, NULL and DEFAULT, at the same time'.PHP_EOL.
145
                $messages['isNull'].PHP_EOL.
146
                $messages['isDefault']
147
            );
148
        } else {
149
            $this->assertTrue($actuals['isNotDefault'], $messages['isNotDefault']);
150
            $this->assertTrue($actuals['isNotNull'], $messages['isNotNull']);
151
        }
152
    }
153
}
154