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

IEquatableTester::testIEquatableEqualsMethod()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 15
nc 2
nop 3
dl 0
loc 27
rs 8.8571
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 as NML;
23
use NelsonMartell\Extensions\String;
24
use NelsonMartell\IEquatable;
25
26
/**
27
 * Test helper for classes implementing ``NelsonMartell\IEquatable`` interface.
28
 *
29
 * Note: Classes using this trait MUST use ConstructorMethodTester and ExporterPlugin traits too.
30
 *
31
 * @author Nelson Martell <[email protected]>
32
 * */
33
trait IEquatableTester
34
{
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 ``testIEquatableEqualsMethod(bool $expected, IEquatable $left, mixed $right)``.
41
     *
42
     * @return array
43
     */
44
    public abstract function IEquatableMethodArgumentsProvider();
45
46
47
    /**
48
     * @testdox Can check if instances are equals to other objects
49
     * @dataProvider IEquatableMethodArgumentsProvider
50
     */
51
    public function testIEquatableEqualsMethod($expected, IEquatable $left, $right)
52
    {
53
        $actual = $left->equals($right);
54
55
        $message = String::format(
56
            '$obj->{method}({right}); // Returned: {actual} ($obj: {left})',
57
            [
58
                'method' => 'equals',
59
                'left'   => static::export($left),
60
                'right'  => static::export($right),
61
                'actual' => static::export($actual)
62
            ]
63
        );
64
65
        $this->assertInternalType('boolean', $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
67
        if (!is_bool($expected)) {
68
            throw new InvalidArgumentException(String::format(
69
                '1st argument of data provider should be of "boolean" type, "{0}" given.',
70
                NML\typeof($expected)
0 ignored issues
show
Deprecated Code introduced by
The function typeof() has been deprecated with message: since v0.6.0, will be removed in v0.7.0. Use `\NelsonMartell\typeof()` instead.

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

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

Loading history...
71
            ));
72
        }
73
74
        $this->assertEquals($expected, $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...
75
76
        $this->assertTrue($left->equals($left), '[Shold be equal to itself]');
0 ignored issues
show
Bug introduced by
It seems like assertTrue() 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...
77
    }
78
79
    /**
80
     * @testdox Is compliant with ``NelsonMartell\IEquatable`` interface
81
     * @depends testIEquatableEqualsMethod
82
     */
83 View Code Duplication
    public function testIsCompliantWithIEquatableIterface()
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...
84
    {
85
        $message = String::format(
86
            '"{0}" do not implements "{1}" interface.',
87
            $this->getTargetClassName(),
88
            IEquatable::class
89
        );
90
91
        $this->assertContains(IEquatable::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...
92
    }
93
}
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...
94