Skip to content

Commit 0949787

Browse files
committed
Increased code coverage
1 parent 3161dab commit 0949787

File tree

4 files changed

+58
-23
lines changed

4 files changed

+58
-23
lines changed

composer.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
},
2323

2424
"autoload": {
25-
"psr-0": {
26-
"Kalnoy\\Nestedset": "src/"
27-
}
28-
}
25+
"psr-0": { "Kalnoy\\Nestedset": "src/" }
26+
},
27+
28+
"minimum-stability": "stable"
2929
}

phpunit.xml

+6
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,10 @@
1515
<directory suffix=".php">./tests/</directory>
1616
</testsuite>
1717
</testsuites>
18+
19+
<filter>
20+
<whitelist>
21+
<directory>./src</directory>
22+
</whitelist>
23+
</filter>
1824
</phpunit>

src/Kalnoy/Nestedset/Node.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -955,7 +955,7 @@ public function getAncestors(array $columns = array('*'))
955955
*
956956
* @param array $columns
957957
*
958-
* @return Collection
958+
* @return Collection|Node[]
959959
*/
960960
public function getDescendants(array $columns = array('*'))
961961
{
@@ -967,7 +967,7 @@ public function getDescendants(array $columns = array('*'))
967967
*
968968
* @param array $columns
969969
*
970-
* @return Collection
970+
* @return Collection|Node[]
971971
*/
972972
public function getSiblings(array $columns = array('*'))
973973
{
@@ -979,7 +979,7 @@ public function getSiblings(array $columns = array('*'))
979979
*
980980
* @param array $columns
981981
*
982-
* @return Collection
982+
* @return Collection|Node[]
983983
*/
984984
public function getNextSiblings(array $columns = array('*'))
985985
{
@@ -991,7 +991,7 @@ public function getNextSiblings(array $columns = array('*'))
991991
*
992992
* @param array $columns
993993
*
994-
* @return Collection
994+
* @return Collection|Node[]
995995
*/
996996
public function getPrevSiblings(array $columns = array('*'))
997997
{

tests/NodeTest.php

+44-15
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ public function assertNodeRecievesValidValues($node)
8585
);
8686
}
8787

88+
/**
89+
* @param $name
90+
*
91+
* @return \Kalnoy\Nestedset\Node
92+
*/
8893
public function findCategory($name)
8994
{
9095
return Category::whereName($name)->first();
@@ -93,6 +98,7 @@ public function findCategory($name)
9398
public function testTreeNotBroken()
9499
{
95100
$this->assertTreeNotBroken();
101+
$this->assertFalse(Category::isBroken());
96102
}
97103

98104
public function nodeValues($node)
@@ -123,9 +129,11 @@ public function testRecievesValidValuesWhenAppendedTo()
123129

124130
$root->append($node);
125131

132+
$this->assertTrue($node->hasMoved());
126133
$this->assertEquals($accepted, $this->nodeValues($node));
127134
$this->assertTreeNotBroken();
128135
$this->assertFalse($node->isDirty());
136+
$this->assertTrue($node->isDescendantOf($root));
129137
}
130138

131139
public function testRecievesValidValuesWhenPrependedTo()
@@ -134,8 +142,10 @@ public function testRecievesValidValuesWhenPrependedTo()
134142
$node = new Category([ 'name' => 'test' ]);
135143
$root->prepend($node);
136144

145+
$this->assertTrue($node->hasMoved());
137146
$this->assertEquals(array($root->_lft + 1, $root->_lft + 2, $root->id), $this->nodeValues($node));
138147
$this->assertTreeNotBroken();
148+
$this->assertTrue($node->isDescendantOf($root));
139149
}
140150

141151
public function testRecievesValidValuesWhenInsertedAfter()
@@ -144,6 +154,7 @@ public function testRecievesValidValuesWhenInsertedAfter()
144154
$node = new Category([ 'name' => 'test' ]);
145155
$node->after($target)->save();
146156

157+
$this->assertTrue($node->hasMoved());
147158
$this->assertEquals(array($target->_rgt + 1, $target->_rgt + 2, $target->parent->id), $this->nodeValues($node));
148159
$this->assertTreeNotBroken();
149160
$this->assertFalse($node->isDirty());
@@ -155,6 +166,7 @@ public function testRecievesValidValuesWhenInsertedBefore()
155166
$node = new Category([ 'name' => 'test' ]);
156167
$node->before($target)->save();
157168

169+
$this->assertTrue($node->hasMoved());
158170
$this->assertEquals(array($target->_lft, $target->_lft + 1, $target->parent->id), $this->nodeValues($node));
159171
$this->assertTreeNotBroken();
160172
}
@@ -166,6 +178,7 @@ public function testCategoryMovesDown()
166178

167179
$target->append($node);
168180

181+
$this->assertTrue($node->hasMoved());
169182
$this->assertNodeRecievesValidValues($node);
170183
$this->assertTreeNotBroken();
171184
}
@@ -177,6 +190,7 @@ public function testCategoryMovesUp()
177190

178191
$target->append($node);
179192

193+
$this->assertTrue($node->hasMoved());
180194
$this->assertTreeNotBroken();
181195
$this->assertNodeRecievesValidValues($node);
182196
}
@@ -221,12 +235,18 @@ public function testGetsAncestorsDirect()
221235
$this->assertEquals(array(1, 5, 7), $path);
222236
}
223237

224-
public function testDescendantsQueried()
238+
public function testDescendants()
225239
{
226240
$node = $this->findCategory('mobile');
227241
$descendants = $node->descendants()->lists('name');
242+
$expected = array('nokia', 'samsung', 'galaxy', 'sony');
243+
244+
$this->assertEquals($expected, $descendants);
245+
246+
$descendants = $node->getDescendants()->lists('name');
228247

229-
$this->assertEquals(array('nokia', 'samsung', 'galaxy', 'sony'), $descendants);
248+
$this->assertEquals(count($descendants), $node->getDescendantCount());
249+
$this->assertEquals($expected, $descendants);
230250
}
231251

232252
public function testWithDepthWorks()
@@ -256,6 +276,13 @@ public function testParentIdAttributeAccessorAppendsNode()
256276
$node->save();
257277

258278
$this->assertEquals(5, $node->parent_id);
279+
$this->assertEquals(5, $node->getParentId());
280+
281+
$node->parent_id = null;
282+
$node->save();
283+
284+
$this->assertEquals(null, $node->parent_id);
285+
$this->assertTrue($node->isRoot());
259286
}
260287

261288
/**
@@ -292,28 +319,30 @@ public function testFailsToSaveNodeUntilParentIsSaved()
292319
$node->appendTo($parent)->save();
293320
}
294321

295-
public function testGetsSiblings()
322+
public function testSiblings()
296323
{
297324
$node = $this->findCategory('samsung');
298325
$siblings = $node->siblings()->lists('id');
326+
$next = $node->nextSiblings()->lists('id');
327+
$prev = $node->prevSiblings()->lists('id');
299328

300329
$this->assertEquals(array(6, 9), $siblings);
301-
}
330+
$this->assertEquals(array(9), $next);
331+
$this->assertEquals(array(6), $prev);
302332

303-
public function testGetsNextSiblings()
304-
{
305-
$node = $this->findCategory('samsung');
306-
$siblings = $node->nextSiblings()->lists('id');
333+
$siblings = $node->getSiblings()->lists('id');
334+
$next = $node->getNextSiblings()->lists('id');
335+
$prev = $node->getPrevSiblings()->lists('id');
307336

308-
$this->assertEquals(array(9), $siblings);
309-
}
337+
$this->assertEquals(array(6, 9), $siblings);
338+
$this->assertEquals(array(9), $next);
339+
$this->assertEquals(array(6), $prev);
310340

311-
public function testGetsPrevSiblings()
312-
{
313-
$node = $this->findCategory('samsung');
314-
$siblings = $node->prevSiblings()->lists('id');
341+
$next = $node->getNextSibling();
342+
$prev = $node->getPrevSibling();
315343

316-
$this->assertEquals(array(6), $siblings);
344+
$this->assertEquals(9, $next->id);
345+
$this->assertEquals(6, $prev->id);
317346
}
318347

319348
public function testFetchesReversed()

0 commit comments

Comments
 (0)