AlNsTest::testInsertAfter()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
dl 11
loc 11
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\NodeAlNs;
11
use Yii;
12
13
/**
14
 * @author PaulZi <[email protected]>
15
 * @group AlNs
16
 */
17 View Code Duplication
class AlNsTest extends AutoTreeTraitTestCase
0 ignored issues
show
Duplication introduced by
This class 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...
18
{
19
    public function getModelClass()
20
    {
21
        return NodeAlNs::className();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \paulzi\autotree\...\NodeAlNs::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 NodeAlNs(['slug' => 'r']);
27
        $this->assertTrue($node->makeRoot()->save());
28
29
        $node->refresh();
30
        $this->assertEquals(null, $node->parent_id);
31
        $this->assertEquals(1,    $node->lft);
32
        $this->assertEquals(2,    $node->rgt);
33
        $this->assertEquals(0,    $node->depth);
34
    }
35
36
    public function testMakeRootUpdate()
37
    {
38
        $node = NodeAlNs::findOne(9);
0 ignored issues
show
Unused Code introduced by
The call to NodeAlNs::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...
39
        $this->assertTrue($node->makeRoot()->save());
40
41
        $node->refresh();
42
        $this->assertEquals(null, $node->parent_id);
43
        $this->assertEquals(1,    $node->lft);
44
        $this->assertEquals(8,    $node->rgt);
45
        $this->assertEquals(0,    $node->depth);
46
    }
47
48
    public function testPrependTo()
49
    {
50
        $node = new NodeAlNs(['slug' => 'new']);
51
        $this->assertTrue($node->prependTo(NodeAlNs::findOne(1))->save());
0 ignored issues
show
Bug introduced by
It seems like \paulzi\autotree\tests\models\NodeAlNs::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 NodeAlNs::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...
52
53
        $node->refresh();
54
        $this->assertEquals(1, $node->parent_id);
55
        $this->assertEquals(2, $node->lft);
56
        $this->assertEquals(3, $node->rgt);
57
        $this->assertEquals(1, $node->depth);
58
    }
59
60
    public function testPrependToAnotherTree()
61
    {
62
        $node = NodeAlNs::findOne(30);
0 ignored issues
show
Unused Code introduced by
The call to NodeAlNs::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...
63
        $this->assertTrue($node->prependTo(NodeAlNs::findOne(4))->save());
0 ignored issues
show
Bug introduced by
It seems like \paulzi\autotree\tests\models\NodeAlNs::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 NodeAlNs::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...
64
65
        $node->refresh();
66
        $this->assertEquals(4, $node->parent_id);
67
        $this->assertEquals(31, $node->lft);
68
        $this->assertEquals(40, $node->rgt);
69
        $this->assertEquals(2, $node->depth);
70
    }
71
72
    public function testAppendTo()
73
    {
74
        $node = NodeAlNs::findOne(10);
0 ignored issues
show
Unused Code introduced by
The call to NodeAlNs::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...
75
        $this->assertTrue($node->appendTo(NodeAlNs::findOne(18))->save());
0 ignored issues
show
Bug introduced by
It seems like \paulzi\autotree\tests\m...s\NodeAlNs::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 NodeAlNs::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...
76
77
        $node->refresh();
78
        $this->assertEquals(18, $node->parent_id);
79
        $this->assertEquals(23, $node->lft);
80
        $this->assertEquals(30, $node->rgt);
81
        $this->assertEquals(4, $node->depth);
82
    }
83
84
    public function testInsertBefore()
85
    {
86
        $node = new NodeAlNs(['slug' => 'new']);
87
        $this->assertTrue($node->insertBefore(NodeAlNs::findOne(22))->save());
0 ignored issues
show
Bug introduced by
It seems like \paulzi\autotree\tests\m...s\NodeAlNs::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 NodeAlNs::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...
88
89
        $node->refresh();
90
        $this->assertEquals(9, $node->parent_id);
91
        $this->assertEquals(38, $node->lft);
92
        $this->assertEquals(39, $node->rgt);
93
        $this->assertEquals(3, $node->depth);
94
    }
95
96
    public function testInsertAfter()
97
    {
98
        $node = NodeAlNs::findOne(32);
0 ignored issues
show
Unused Code introduced by
The call to NodeAlNs::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...
99
        $this->assertTrue($node->insertAfter(NodeAlNs::findOne(30))->save());
0 ignored issues
show
Bug introduced by
It seems like \paulzi\autotree\tests\m...s\NodeAlNs::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 NodeAlNs::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...
100
101
        $node->refresh();
102
        $this->assertEquals(26, $node->parent_id);
103
        $this->assertEquals(26, $node->lft);
104
        $this->assertEquals(27, $node->rgt);
105
        $this->assertEquals(1, $node->depth);
106
    }
107
108
    public function testInsertAfterAnotherTree()
109
    {
110
        $node = NodeAlNs::findOne(26);
0 ignored issues
show
Unused Code introduced by
The call to NodeAlNs::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...
111
        $this->assertTrue($node->insertAfter(NodeAlNs::findOne(21))->save());
0 ignored issues
show
Bug introduced by
It seems like \paulzi\autotree\tests\m...s\NodeAlNs::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 NodeAlNs::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...
112
113
        $node->refresh();
114
        $this->assertEquals(9, $node->parent_id);
115
        $this->assertEquals(38, $node->lft);
116
        $this->assertEquals(65, $node->rgt);
117
        $this->assertEquals(3, $node->depth);
118
    }
119
120
    public function testDelete()
121
    {
122
        $this->assertEquals(1, NodeAlNs::findOne(30)->delete());
0 ignored issues
show
Unused Code introduced by
The call to NodeAlNs::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...
123
        $this->assertEquals(null, NodeAlNs::findOne(30));
0 ignored issues
show
Unused Code introduced by
The call to NodeAlNs::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...
124
    }
125
126
    public function testDeleteWithChildren()
127
    {
128
        $this->assertEquals(10, NodeAlNs::findOne(4)->deleteWithChildren());
0 ignored issues
show
Unused Code introduced by
The call to NodeAlNs::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...
129
        $this->assertEquals(null, NodeAlNs::findOne(24));
0 ignored issues
show
Unused Code introduced by
The call to NodeAlNs::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...
130
        $this->assertEquals(15, NodeAlNs::findOne(1)->deleteWithChildren());
0 ignored issues
show
Unused Code introduced by
The call to NodeAlNs::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...
131
    }
132
}