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

testIComparableCompareToMethod()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 35
Code Lines 23

Duplication

Lines 20
Ratio 57.14 %

Importance

Changes 0
Metric Value
cc 4
eloc 23
nc 4
nop 3
dl 20
loc 35
rs 8.5806
c 0
b 0
f 0
1
<?php
2
/**
3
 * PHP: Nelson Martell Library file
4
 *
5
 * Content:
6
 * - Trait definition
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\Helpers;
21
22
use NelsonMartell\Extensions\String;
23
use NelsonMartell\IComparable;
24
25
/**
26
 * Test helper for classes implementing ``NelsonMartell\IComparable`` interface.
27
 *
28
 * Note: Classes using this trait MUST use ConstructorMethodTester and ExporterPlugin traits too.
29
 *
30
 * @author Nelson Martell <[email protected]>
31
 * */
32
trait IComparableTester
33
{
34
    public abstract function getTargetClassInstance(); // use ConstructorMethodTester;
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
35
    public abstract function getTargetClassName(); // use ConstructorMethodTester;
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
36
    public abstract function getTargetClassReflection(); // use ConstructorMethodTester;
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
37
    public abstract function export($obj, $depth = 2, $short = false); // use plugin/ExporterPlugin;
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
38
39
    /**
40
     * Datasets for ``testIComparableCompareToMethod(integer|null $expected, IComparable $left, mixed $right)``.
41
     *
42
     * @return array
43
     */
44
    public abstract function IComparableCompareToMethodArgumentsProvider();
45
46
    /**
47
     * @testdox Can compare relative position with other objects
48
     * @dataProvider IComparableCompareToMethodArgumentsProvider
49
     */
50
    public function testIComparableCompareToMethod($expected, IComparable $left, $right)
51
    {
52
        $actual = $left->compareTo($right);
53
54
        $message = String::format(
55
            '$obj->{method}({right}); // Returned: {actual} ($obj: {left})',
56
            [
57
                'method' => 'compareTo',
58
                'left'   => static::export($left),
59
                'right'  => static::export($right),
60
                'actual' => static::export($actual)
61
            ]
62
        );
63
64 View Code Duplication
        if ($expected === 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
65
            $this->assertInternalType('integer', $actual, $message);
0 ignored issues
show
Bug introduced by
It seems like assertInternalType() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
66
            $this->assertEquals(0, $actual, $message);
0 ignored issues
show
Bug introduced by
It seems like assertEquals() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
67
        } else {
68
            if ($expected === null) {
69
                $this->assertNull($actual, $message);
0 ignored issues
show
Bug introduced by
It seems like assertNull() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
70
            } else {
71
                $major = $minor = 0;
72
73
                if ($expected < 0) {
74
                    $minor = $actual;
75
                } else {
76
                    $major = $actual;
77
                }
78
79
                $this->assertInternalType('integer', $actual, $message);
0 ignored issues
show
Bug introduced by
It seems like assertInternalType() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
80
                $this->assertGreaterThan($minor, $major, $message);
0 ignored issues
show
Bug introduced by
It seems like assertGreaterThan() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
81
                $this->assertLessThan($major, $minor, $message);
0 ignored issues
show
Bug introduced by
It seems like assertLessThan() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
82
            }
83
        }
84
    }
85
86
    /**
87
     * @testdox Is compliant with ``NelsonMartell\IComparable`` interface
88
     * @depends testIComparableCompareToMethod
89
     */
90 View Code Duplication
    public function testIsCompliantWithIComparableIterface()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
91
    {
92
        $message = String::format(
93
            '"{0}" do not implements "{1}" interface.',
94
            $this->getTargetClassName(),
95
            IComparable::class
96
        );
97
98
        $this->assertContains(IComparable::class, $this->getTargetClassReflection()->getInterfaceNames(), $message);
0 ignored issues
show
Bug introduced by
It seems like assertContains() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
99
    }
100
}
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...
101