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(); |
|
|
|
|
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); |
|
|
|
|
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()); |
|
|
|
|
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); |
|
|
|
|
60
|
|
|
$this->assertTrue($node->prependTo(NodeAlMp::findOne(4))->save()); |
|
|
|
|
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); |
|
|
|
|
71
|
|
|
$this->assertTrue($node->appendTo(NodeAlMp::findOne(18))->save()); |
|
|
|
|
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()); |
|
|
|
|
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); |
|
|
|
|
93
|
|
|
$this->assertTrue($node->insertAfter(NodeAlMp::findOne(30))->save()); |
|
|
|
|
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); |
|
|
|
|
104
|
|
|
$this->assertTrue($node->insertAfter(NodeAlMp::findOne(21))->save()); |
|
|
|
|
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()); |
|
|
|
|
115
|
|
|
$this->assertEquals(null, NodeAlMp::findOne(30)); |
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function testDeleteWithChildren() |
119
|
|
|
{ |
120
|
|
|
$this->assertEquals(10, NodeAlMp::findOne(4)->deleteWithChildren()); |
|
|
|
|
121
|
|
|
$this->assertEquals(null, NodeAlMp::findOne(24)); |
|
|
|
|
122
|
|
|
$this->assertEquals(15, NodeAlMp::findOne(1)->deleteWithChildren()); |
|
|
|
|
123
|
|
|
} |
124
|
|
|
} |
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:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.