Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/Contracts/NestedSetCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,32 @@ public function toTree($root = false): NestedSetCollection;
* @return NestedSetCollection<Tmodel>
*/
public function toFlatTree($root = false): NestedSetCollection;

/**
* Count the number of items in the collection.
*
* @return int
*/
public function count(): int;

/**
* Get the values of a given key.
*
* @param string|int|array<array-key, string>|null $value
* @param string|null $key
*
* @return \Illuminate\Support\Collection<array-key, mixed>
*/
public function pluck($value, $key = null);

/**
* Run a map over each of the items.
*
* @template TMapValue
*
* @param callable(NodeModel, int): TMapValue $callback
*
* @return \Illuminate\Support\Collection<int, TMapValue>
*/
public function map(callable $callback);
}
19 changes: 12 additions & 7 deletions tests/NodeTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
<?php

namespace tests;

use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Database\Schema\Blueprint;
use Kalnoy\Nestedset\NestedSet;
use PHPUnit\Framework\TestCase;
use tests\models\Category;

class NodeTest extends PHPUnit\Framework\TestCase
class NodeTest extends TestCase
{
public static function setUpBeforeClass(): void
{
Expand All @@ -13,7 +18,7 @@ public static function setUpBeforeClass(): void

Capsule::disableQueryLog();

$schema->create('categories', function (Illuminate\Database\Schema\Blueprint $table) {
$schema->create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->softDeletes();
Expand Down Expand Up @@ -225,7 +230,7 @@ public function testCategoryMovesUp()

public function testFailsToInsertIntoChild()
{
$this->expectException(Exception::class);
$this->expectException(\Exception::class);

$node = $this->findCategory('notebooks');
$target = $node->children()->first();
Expand All @@ -235,7 +240,7 @@ public function testFailsToInsertIntoChild()

public function testFailsToAppendIntoItself()
{
$this->expectException(Exception::class);
$this->expectException(\Exception::class);

$node = $this->findCategory('notebooks');

Expand All @@ -244,7 +249,7 @@ public function testFailsToAppendIntoItself()

public function testFailsToPrependIntoItself()
{
$this->expectException(Exception::class);
$this->expectException(\Exception::class);

$node = $this->findCategory('notebooks');

Expand Down Expand Up @@ -339,7 +344,7 @@ public function testParentIdAttributeAccessorAppendsNode()

public function testFailsToSaveNodeUntilNotInserted()
{
$this->expectException(Exception::class);
$this->expectException(\Exception::class);

$node = new Category();
$node->save();
Expand Down Expand Up @@ -405,7 +410,7 @@ public function testSoftDeletedNodeisDeletedWhenParentIsDeleted()

public function testFailsToSaveNodeUntilParentIsSaved()
{
$this->expectException(Exception::class);
$this->expectException(\Exception::class);

$node = new Category(['title' => 'Node']);
$parent = new Category(['title' => 'Parent']);
Expand Down
14 changes: 8 additions & 6 deletions tests/ScopedNodeTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?php

namespace tests;

use Illuminate\Database\Capsule\Manager as Capsule;
use Kalnoy\Nestedset\NestedSet;
use PHPUnit\Framework\TestCase;
use tests\models\MenuItem;

include __DIR__ . '/models/MenuItem.php';

class ScopedNodeTest extends PHPUnit\Framework\TestCase
class ScopedNodeTest extends TestCase
{
public static function setUpBeforeClass(): void
{
Expand All @@ -15,7 +17,7 @@ public static function setUpBeforeClass(): void

Capsule::disableQueryLog();

$schema->create('menu_items', function (Illuminate\Database\Schema\Blueprint $table) {
$schema->create('menu_items', function (\Illuminate\Database\Schema\Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('menu_id');
$table->string('title')->nullable();
Expand Down Expand Up @@ -204,7 +206,7 @@ protected function assertOtherScopeNotAffected()

public function testAppendingToAnotherScopeFails()
{
$this->expectException(LogicException::class);
$this->expectException(\LogicException::class);

$a = MenuItem::find(1);
$b = MenuItem::find(3);
Expand All @@ -214,7 +216,7 @@ public function testAppendingToAnotherScopeFails()

public function testInsertingBeforeAnotherScopeFails()
{
$this->expectException(LogicException::class);
$this->expectException(\LogicException::class);

$a = MenuItem::find(1);
$b = MenuItem::find(3);
Expand Down
2 changes: 2 additions & 0 deletions tests/models/Category.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace tests\models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Kalnoy\Nestedset\Contracts\Node;
Expand Down
2 changes: 2 additions & 0 deletions tests/models/DuplicateCategory.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace tests\models;

use Illuminate\Database\Eloquent\Model;
use Kalnoy\Nestedset\Contracts\Node;
use Kalnoy\Nestedset\NodeTrait;
Expand Down
2 changes: 2 additions & 0 deletions tests/models/MenuItem.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace tests\models;

use Illuminate\Database\Eloquent\Model;
use Kalnoy\Nestedset\Contracts\Node;
use Kalnoy\Nestedset\NodeTrait;
Expand Down