AlMpTest   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 2
dl 0
loc 108
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getModelClass() 0 4 1
A testMakeRootInsert() 0 10 1
A testMakeRootUpdate() 0 10 1
A testPrependTo() 0 10 1
A testPrependToAnotherTree() 0 10 1
A testAppendTo() 0 10 1
A testInsertBefore() 0 10 1
A testInsertAfter() 0 10 1
A testInsertAfterAnotherTree() 0 10 1
A testDelete() 0 5 1
A testDeleteWithChildren() 0 6 1
1
<?php
2
/**
3
 * @link https://github.com/paulzi/yii2-auto-tree
4
 * @copyright Copyright (c) 2015 PaulZi <[email protected]>
5
 * @license MIT (https://github.com/paulzi/yii2-auto-tree/blob/master/LICENSE)
6
 */
7
8
namespace paulzi\autotree\tests;
9
10
use paulzi\autotree\tests\models\NodeAlMp;
11
use Yii;
12
13
/**
14
 * @author PaulZi <[email protected]>
15
 * @group AlMp
16
 */
17
class AlMpTest extends AutoTreeTraitTestCase
18
{
19
    public function getModelClass()
20
    {
21
        return NodeAlMp::className();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \paulzi\autotree\...\NodeAlMp::className(); (string) is incompatible with the return type of the parent method paulzi\autotree\tests\Au...TestCase::getModelClass of type yii\db\BaseActiveRecord|null.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
22
    }
23
24
    public function testMakeRootInsert()
25
    {
26
        $node = new NodeAlMp(['slug' => 'r']);
27
        $this->assertTrue($node->makeRoot()->save());
28
29
        $node->refresh();
30
        $this->assertEquals(null, $node->parent_id);
31
        $this->assertEquals(0,    $node->sort);
32
        $this->assertEquals(0,    $node->depth);
33
    }
34
35
    public function testMakeRootUpdate()
36
    {
37
        $node = NodeAlMp::findOne(9);
0 ignored issues
show
Unused Code introduced by
The call to NodeAlMp::findOne() has too many arguments starting with 9.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
38
        $this->assertTrue($node->makeRoot()->save());
39
40
        $node->refresh();
41
        $this->assertEquals(null, $node->parent_id);
42
        $this->assertEquals(0,    $node->sort);
43
        $this->assertEquals(0,    $node->depth);
44
    }
45
46
    public function testPrependTo()
47
    {
48
        $node = new NodeAlMp(['slug' => 'new']);
49
        $this->assertTrue($node->prependTo(NodeAlMp::findOne(1))->save());
0 ignored issues
show
Bug introduced by
It seems like \paulzi\autotree\tests\models\NodeAlMp::findOne(1) can be null; however, prependTo() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
Unused Code introduced by
The call to NodeAlMp::findOne() has too many arguments starting with 1.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
50
51
        $node->refresh();
52
        $this->assertEquals(1,    $node->parent_id);
53
        $this->assertEquals(-101, $node->sort);
54
        $this->assertEquals(1,    $node->depth);
55
    }
56
57
    public function testPrependToAnotherTree()
58
    {
59
        $node = NodeAlMp::findOne(30);
0 ignored issues
show
Unused Code introduced by
The call to NodeAlMp::findOne() has too many arguments starting with 30.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
60
        $this->assertTrue($node->prependTo(NodeAlMp::findOne(4))->save());
0 ignored issues
show
Bug introduced by
It seems like \paulzi\autotree\tests\models\NodeAlMp::findOne(4) can be null; however, prependTo() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
Unused Code introduced by
The call to NodeAlMp::findOne() has too many arguments starting with 4.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
61
62
        $node->refresh();
63
        $this->assertEquals(4,    $node->parent_id);
64
        $this->assertEquals(-100, $node->sort);
65
        $this->assertEquals(2,    $node->depth);
66
    }
67
68
    public function testAppendTo()
69
    {
70
        $node = NodeAlMp::findOne(10);
0 ignored issues
show
Unused Code introduced by
The call to NodeAlMp::findOne() has too many arguments starting with 10.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
71
        $this->assertTrue($node->appendTo(NodeAlMp::findOne(18))->save());
0 ignored issues
show
Bug introduced by
It seems like \paulzi\autotree\tests\m...s\NodeAlMp::findOne(18) can be null; however, appendTo() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
Unused Code introduced by
The call to NodeAlMp::findOne() has too many arguments starting with 18.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
72
73
        $node->refresh();
74
        $this->assertEquals(18, $node->parent_id);
75
        $this->assertEquals(0, $node->sort);
76
        $this->assertEquals(4, $node->depth);
77
    }
78
79
    public function testInsertBefore()
80
    {
81
        $node = new NodeAlMp(['slug' => 'new']);
82
        $this->assertTrue($node->insertBefore(NodeAlMp::findOne(22))->save());
0 ignored issues
show
Bug introduced by
It seems like \paulzi\autotree\tests\m...s\NodeAlMp::findOne(22) can be null; however, insertBefore() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
Unused Code introduced by
The call to NodeAlMp::findOne() has too many arguments starting with 22.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
83
84
        $node->refresh();
85
        $this->assertEquals(9, $node->parent_id);
86
        $this->assertEquals(3, $node->sort);
87
        $this->assertEquals(3, $node->depth);
88
    }
89
90
    public function testInsertAfter()
91
    {
92
        $node = NodeAlMp::findOne(32);
0 ignored issues
show
Unused Code introduced by
The call to NodeAlMp::findOne() has too many arguments starting with 32.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
93
        $this->assertTrue($node->insertAfter(NodeAlMp::findOne(30))->save());
0 ignored issues
show
Bug introduced by
It seems like \paulzi\autotree\tests\m...s\NodeAlMp::findOne(30) can be null; however, insertAfter() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
Unused Code introduced by
The call to NodeAlMp::findOne() has too many arguments starting with 30.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
94
95
        $node->refresh();
96
        $this->assertEquals(26, $node->parent_id);
97
        $this->assertEquals(3, $node->sort);
98
        $this->assertEquals(1, $node->depth);
99
    }
100
101
    public function testInsertAfterAnotherTree()
102
    {
103
        $node = NodeAlMp::findOne(26);
0 ignored issues
show
Unused Code introduced by
The call to NodeAlMp::findOne() has too many arguments starting with 26.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
104
        $this->assertTrue($node->insertAfter(NodeAlMp::findOne(21))->save());
0 ignored issues
show
Bug introduced by
It seems like \paulzi\autotree\tests\m...s\NodeAlMp::findOne(21) can be null; however, insertAfter() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
Unused Code introduced by
The call to NodeAlMp::findOne() has too many arguments starting with 21.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
105
106
        $node->refresh();
107
        $this->assertEquals(9, $node->parent_id);
108
        $this->assertEquals(3, $node->sort);
109
        $this->assertEquals(3, $node->depth);
110
    }
111
112
    public function testDelete()
113
    {
114
        $this->assertEquals(1, NodeAlMp::findOne(30)->delete());
0 ignored issues
show
Unused Code introduced by
The call to NodeAlMp::findOne() has too many arguments starting with 30.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
115
        $this->assertEquals(null, NodeAlMp::findOne(30));
0 ignored issues
show
Unused Code introduced by
The call to NodeAlMp::findOne() has too many arguments starting with 30.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
116
    }
117
118
    public function testDeleteWithChildren()
119
    {
120
        $this->assertEquals(10, NodeAlMp::findOne(4)->deleteWithChildren());
0 ignored issues
show
Unused Code introduced by
The call to NodeAlMp::findOne() has too many arguments starting with 4.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
121
        $this->assertEquals(null, NodeAlMp::findOne(24));
0 ignored issues
show
Unused Code introduced by
The call to NodeAlMp::findOne() has too many arguments starting with 24.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
122
        $this->assertEquals(15, NodeAlMp::findOne(1)->deleteWithChildren());
0 ignored issues
show
Unused Code introduced by
The call to NodeAlMp::findOne() has too many arguments starting with 1.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
123
    }
124
}