testIComparableCompareToMethod()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 33
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 22
c 0
b 0
f 0
nc 4
nop 3
dl 0
loc 33
rs 9.568
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\Helpers;
21
22
use ReflectionClass;
23
use NelsonMartell\Extensions\Text;
24
use NelsonMartell\IComparable;
25
use PHPUnit\Framework\TestCase;
26
27
/**
28
 * Test helper for classes implementing ``NelsonMartell\IComparable`` interface.
29
 *
30
 * Note: Classes using this trait MUST use ConstructorMethodTester and ExporterPlugin traits too.
31
 *
32
 * @author Nelson Martell <[email protected]>
33
 * @since  0.6.0
34
 * */
35
trait IComparableTester
36
{
37
    /**
38
     * @return string
39
     *
40
     * @see ConstructorMethodTester
41
     */
42
    abstract public function getTargetClassName(): string;
43
44
    /**
45
     * @param mixed $obj
46
     * @param int   $depth
47
     * @param bool  $short
48
     *
49
     * @return string
50
     *
51
     * @see ExporterPlugin
52
     */
53
    abstract public static function export($obj, int $depth = 2, bool $short = false): string;
54
55
    /**
56
     * Datasets for ``testIComparableCompareToMethod(integer|null $expected, IComparable $left, mixed $right)``.
57
     *
58
     * @return array
59
     */
60
    abstract public function IComparableCompareToMethodArgumentsProvider(): array;
61
62
    /**
63
     * @testdox Can compare relative position with other objects
64
     * @dataProvider IComparableCompareToMethodArgumentsProvider
65
     *
66
     * @param int|null    $expected
67
     * @param IComparable $left
68
     * @param mixed       $right
69
     *
70
     * @see IComparable::compareTo()
71
     */
72
    public function testIComparableCompareToMethod($expected, IComparable $left, $right): void
73
    {
74
        $actual = $left->compareTo($right);
75
76
        $message = Text::format(
77
            '$obj->{method}({right}); // Returned: {actual} ($obj: {left})',
78
            [
79
                'method' => 'compareTo',
80
                'left'   => static::export($left),
81
                'right'  => static::export($right),
82
                'actual' => static::export($actual),
83
            ]
84
        );
85
86
        /** @var TestCase $this */
87
        if ($expected === 0) {
88
            $this->assertIsInt($actual, $message);
89
            $this->assertEquals(0, $actual, $message);
90
        } else {
91
            if ($expected === null) {
92
                $this->assertNull($actual, $message);
93
            } else {
94
                $major = $minor = 0;
95
96
                if ($expected < 0) {
97
                    $minor = $actual;
98
                } else {
99
                    $major = $actual;
100
                }
101
102
                $this->assertIsInt($actual, $message);
103
                $this->assertGreaterThan($minor, $major, $message);
104
                $this->assertLessThan($major, $minor, $message);
105
            }
106
        }
107
    }
108
109
    /**
110
     * @testdox Is compliant with ``NelsonMartell\IComparable`` interface
111
     * @depends testIComparableCompareToMethod
112
     */
113
    public function testIsCompliantWithIComparableIterface()
114
    {
115
        $message = Text::format(
116
            '"{0}" do not implements "{1}" interface.',
117
            $this->getTargetClassName(),
118
            IComparable::class
119
        );
120
121
        $classReflection = new ReflectionClass($this->getTargetClassName());
122
123
        /** @var TestCase $this */
124
        $this->assertContains(IComparable::class, $classReflection->getInterfaceNames(), $message);
125
    }
126
}
127