diff --git a/.gitignore b/.gitignore index 9fc01f2d..80f2149a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,9 @@ /vendor /build +/.idea +/.vagrant +.DS_Store +Homestead.json +Homestead.yaml composer.lock *.cache diff --git a/.php_cs b/.php_cs deleted file mode 100644 index e8c5a2a1..00000000 --- a/.php_cs +++ /dev/null @@ -1,77 +0,0 @@ -finder(DefaultFinder::create()->in(__DIR__)) - ->fixers($fixers) - ->level(FixerInterface::NONE_LEVEL) - ->setUsingCache(true); diff --git a/composer.json b/composer.json index 72f4610f..24c0b78f 100644 --- a/composer.json +++ b/composer.json @@ -23,14 +23,14 @@ "illuminate/database": "^5.2", "illuminate/events": "^5.2", "illuminate/filesystem": "^5.2", - "illuminate/support": "^5.2" + "illuminate/support": "^5.2", + "friendsofphp/php-cs-fixer": "^2.7" }, "require-dev": { "phpunit/phpunit": "4.*", "mockery/mockery": "~0.9", "d11wtq/boris": "~1.0.10", - "satooshi/php-coveralls": "dev-master", - "fabpot/php-cs-fixer": "^1.11" + "satooshi/php-coveralls": "dev-master" }, "autoload": { "psr-0": { diff --git a/phpcs.xml b/phpcs.xml new file mode 100644 index 00000000..b1beaf0a --- /dev/null +++ b/phpcs.xml @@ -0,0 +1,66 @@ + + + + + Macellan Coding Standard for Illuminate 55.x + + + vendor/* + + + + tests/* + + + + + + + + tests/* + + + tests/* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Baum/Console/BaumCommand.php b/src/Baum/Console/BaumCommand.php index 83ae90cb..b7cb83de 100644 --- a/src/Baum/Console/BaumCommand.php +++ b/src/Baum/Console/BaumCommand.php @@ -7,29 +7,30 @@ class BaumCommand extends Command { + /** * The console command name. * * @var string */ - protected $name = 'baum'; + protected $name = 'baum'; /** * The console command description. * * @var string */ - protected $description = 'Get Baum version notice.'; + protected $description = 'Get Baum version notice.'; /** * Execute the console command. * * @return void */ - public function fire() - { - $this->line('Baum version '.Baum::VERSION.''); - $this->line('A Nested Set pattern implementation for the Eloquent ORM.'); - $this->line('Copyright (c) 2013 Estanislau Trepat'); - } + public function fire() + { + $this->line('Baum version '.Baum::VERSION.''); + $this->line('A Nested Set pattern implementation for the Eloquent ORM.'); + $this->line('Copyright (c) 2013 Estanislau Trepat'); + } } diff --git a/src/Baum/Console/InstallCommand.php b/src/Baum/Console/InstallCommand.php index 6eba00cd..746ca338 100644 --- a/src/Baum/Console/InstallCommand.php +++ b/src/Baum/Console/InstallCommand.php @@ -9,46 +9,47 @@ class InstallCommand extends Command { + /** * The console command name. * * @var string */ - protected $name = 'baum:install'; + protected $name = 'baum:install'; /** * The console command description. * * @var string */ - protected $description = 'Scaffolds a new migration and model suitable for Baum.'; + protected $description = 'Scaffolds a new migration and model suitable for Baum.'; /** * Migration generator instance. * - * @var Baum\Generators\MigrationGenerator + * @var \Baum\Generators\MigrationGenerator */ - protected $migrator; + protected $migrator; /** * Model generator instance. * - * @var Baum\Generators\ModelGenerator + * @var \Baum\Generators\ModelGenerator */ - protected $modeler; + protected $modeler; /** * Create a new command instance. * * @return void */ - public function __construct(MigrationGenerator $migrator, ModelGenerator $modeler) - { - parent::__construct(); + public function __construct(MigrationGenerator $migrator, ModelGenerator $modeler) + { + parent::__construct(); - $this->migrator = $migrator; - $this->modeler = $modeler; - } + $this->migrator = $migrator; + $this->modeler = $modeler; + } /** * Execute the console command. @@ -60,68 +61,68 @@ public function __construct(MigrationGenerator $migrator, ModelGenerator $modele * * @return void */ - public function fire() - { - $name = $this->input->getArgument('name'); - $this->writeMigration($name); - $this->writeModel($name); - } + public function fire() + { + $name = $this->input->getArgument('name'); + $this->writeMigration($name); + $this->writeModel($name); + } /** * Get the command arguments. * * @return array */ - protected function getArguments() - { - return [ + protected function getArguments() + { + return [ ['name', InputArgument::REQUIRED, 'Name to use for the scaffolding of the migration and model.'], - ]; - } + ]; + } /** * Write the migration file to disk. * - * @param string $name + * @param string $name * - * @return string + * @return void */ - protected function writeMigration($name) - { - $output = pathinfo($this->migrator->create($name, $this->getMigrationsPath()), PATHINFO_FILENAME); - $this->line(" create $output"); - } + protected function writeMigration($name) + { + $output = pathinfo($this->migrator->create($name, $this->getMigrationsPath()), PATHINFO_FILENAME); + $this->line(" create $output"); + } /** * Write the model file to disk. * - * @param string $name + * @param string $name * - * @return string + * @return void */ - protected function writeModel($name) - { - $output = pathinfo($this->modeler->create($name, $this->getModelsPath()), PATHINFO_FILENAME); - $this->line(" create $output"); - } + protected function writeModel($name) + { + $output = pathinfo($this->modeler->create($name, $this->getModelsPath()), PATHINFO_FILENAME); + $this->line(" create $output"); + } /** * Get the path to the migrations directory. * * @return string */ - protected function getMigrationsPath() - { - return $this->laravel->databasePath(); - } + protected function getMigrationsPath() + { + return $this->laravel->databasePath(); + } /** * Get the path to the models directory. * * @return string */ - protected function getModelsPath() - { - return $this->laravel->basePath(); - } + protected function getModelsPath() + { + return $this->laravel->basePath(); + } } diff --git a/src/Baum/Extensions/Eloquent/Model.php b/src/Baum/Extensions/Eloquent/Model.php index 06eb7162..665cbff7 100644 --- a/src/Baum/Extensions/Eloquent/Model.php +++ b/src/Baum/Extensions/Eloquent/Model.php @@ -16,109 +16,109 @@ abstract class Model extends BaseModel * * @return \Baum\Node */ - public function reload() - { - if ($this->exists || ($this->areSoftDeletesEnabled() && $this->trashed())) { - $fresh = $this->getFreshInstance(); + public function reload() + { + if ($this->exists || ($this->areSoftDeletesEnabled() && $this->trashed())) { + $fresh = $this->getFreshInstance(); - if (is_null($fresh)) { - throw with(new ModelNotFoundException())->setModel(get_called_class()); - } + if (is_null($fresh)) { + throw with(new ModelNotFoundException())->setModel(get_called_class()); + } - $this->setRawAttributes($fresh->getAttributes(), true); + $this->setRawAttributes($fresh->getAttributes(), true); - $this->setRelations($fresh->getRelations()); + $this->setRelations($fresh->getRelations()); - $this->exists = $fresh->exists; - } else { - // Revert changes if model is not persisted - $this->attributes = $this->original; - } + $this->exists = $fresh->exists; + } else { + // Revert changes if model is not persisted + $this->attributes = $this->original; + } - return $this; - } + return $this; + } /** * Get the observable event names. * * @return array */ - public function getObservableEvents() - { - return array_merge(['moving', 'moved'], parent::getObservableEvents()); - } + public function getObservableEvents() + { + return array_merge(['moving', 'moved'], parent::getObservableEvents()); + } /** * Register a moving model event with the dispatcher. * - * @param Closure|string $callback + * @param \Closure|string $callback * * @return void */ - public static function moving($callback, $priority = 0) - { - static::registerModelEvent('moving', $callback, $priority); - } + public static function moving($callback, $priority = 0) + { + static::registerModelEvent('moving', $callback); + } - /** - * Register a moved model event with the dispatcher. - * - * @param Closure|string $callback - * - * @return void - */ - public static function moved($callback, $priority = 0) - { - static::registerModelEvent('moved', $callback, $priority); - } + /** + * Register a moved model event with the dispatcher. + * + * @param \Closure|string $callback + * + * @return void + */ + public static function moved($callback, $priority = 0) + { + static::registerModelEvent('moved', $callback); + } /** * Get a new query builder instance for the connection. * * @return \Baum\Extensions\Query\Builder */ - protected function newBaseQueryBuilder() - { - $conn = $this->getConnection(); + protected function newBaseQueryBuilder() + { + $conn = $this->getConnection(); - $grammar = $conn->getQueryGrammar(); + $grammar = $conn->getQueryGrammar(); - return new QueryBuilder($conn, $grammar, $conn->getPostProcessor()); - } + return new QueryBuilder($conn, $grammar, $conn->getPostProcessor()); + } /** * Returns a fresh instance from the database. * * @return \Baum\Node */ - protected function getFreshInstance() - { - if ($this->areSoftDeletesEnabled()) { - return static::withTrashed()->find($this->getKey()); - } + protected function getFreshInstance() + { + if ($this->areSoftDeletesEnabled()) { + return static::withTrashed()->find($this->getKey()); + } - return static::find($this->getKey()); - } + return static::find($this->getKey()); + } /** * Returns wether soft delete functionality is enabled on the model or not. * * @return bool */ - public function areSoftDeletesEnabled() - { - // To determine if there's a global soft delete scope defined we must - // first determine if there are any, to workaround a non-existent key error. - $globalScopes = $this->getGlobalScopes(); + public function areSoftDeletesEnabled() + { + // To determine if there's a global soft delete scope defined we must + // first determine if there are any, to workaround a non-existent key error. + $globalScopes = $this->getGlobalScopes(); - if (count($globalScopes) === 0) { - return false; - } + if (count($globalScopes) === 0) { + return false; + } - // Now that we're sure that the calling class has some kind of global scope - // we check for the SoftDeletingScope existance - return static::hasGlobalScope(new SoftDeletingScope()); - } + // Now that we're sure that the calling class has some kind of global scope + // we check for the SoftDeletingScope existance + return static::hasGlobalScope(new SoftDeletingScope()); + } /** * Static method which returns wether soft delete functionality is enabled @@ -126,8 +126,8 @@ public function areSoftDeletesEnabled() * * @return bool */ - public static function softDeletesEnabled() - { - return with(new static())->areSoftDeletesEnabled(); - } + public static function softDeletesEnabled() + { + return with(new static())->areSoftDeletesEnabled(); + } } diff --git a/src/Baum/Extensions/Query/Builder.php b/src/Baum/Extensions/Query/Builder.php index 6ea0b660..45e6fb39 100644 --- a/src/Baum/Extensions/Query/Builder.php +++ b/src/Baum/Extensions/Query/Builder.php @@ -14,16 +14,16 @@ class Builder extends BaseBuilder * * @return \Illuminate\Database\Query\Builder|static */ - public function reOrderBy($column, $direction = 'asc') - { - $this->orders = null; + public function reOrderBy($column, $direction = 'asc') + { + $this->orders = null; - if (!is_null($column)) { - return $this->orderBy($column, $direction); - } + if (!is_null($column)) { + return $this->orderBy($column, $direction); + } - return $this; - } + return $this; + } /** * Execute an aggregate function on the database. @@ -33,13 +33,13 @@ public function reOrderBy($column, $direction = 'asc') * * @return mixed */ - public function aggregate($function, $columns = ['*']) - { - // Postgres doesn't like ORDER BY when there's no GROUP BY clause - if (!isset($this->groups)) { - $this->reOrderBy(null); - } + public function aggregate($function, $columns = ['*']) + { + // Postgres doesn't like ORDER BY when there's no GROUP BY clause + if (!isset($this->groups)) { + $this->reOrderBy(null); + } - return parent::aggregate($function, $columns); - } + return parent::aggregate($function, $columns); + } } diff --git a/src/Baum/Generators/Generator.php b/src/Baum/Generators/Generator.php index 373370d1..64bdb863 100644 --- a/src/Baum/Generators/Generator.php +++ b/src/Baum/Generators/Generator.php @@ -6,12 +6,13 @@ abstract class Generator { + /** * The filesystem instance. * * @var \Illuminate\Filesystem\Filesystem */ - protected $files = null; + protected $files = null; /** * Create a new MigrationGenerator instance. @@ -20,88 +21,88 @@ abstract class Generator * * @return void */ - public function __construct(Filesystem $files) - { - $this->files = $files; - } + public function __construct(Filesystem $files) + { + $this->files = $files; + } /** * Get the path to the stubs. * * @return string */ - public function getStubPath() - { - return __DIR__.'/stubs'; - } + public function getStubPath() + { + return __DIR__.'/stubs'; + } /** * Get the filesystem instance. * * @return \Illuminate\Filesystem\Filesystem */ - public function getFilesystem() - { - return $this->files; - } + public function getFilesystem() + { + return $this->files; + } /** * Get the given stub by name. * - * @param string $table + * @param string $name * - * @return void + * @return string */ - protected function getStub($name) - { - if (stripos($name, '.php.stub') === false) { - $name = $name.'.php.stub'; - } + protected function getStub($name) + { + if (stripos($name, '.php.stub') === false) { + $name = $name.'.php.stub'; + } - return $this->files->get($this->getStubPath().'/'.$name); - } + return $this->files->get($this->getStubPath().'/'.$name); + } /** * Parse the provided stub and replace via the array given. * - * @param string $stub - * @param string $replacements + * @param string $stub + * @param string $replacements * * @return string */ - protected function parseStub($stub, $replacements = []) - { - $output = $stub; + protected function parseStub($stub, $replacements = []) + { + $output = $stub; - foreach ($replacements as $key => $replacement) { - $search = '{{'.$key.'}}'; - $output = str_replace($search, $replacement, $output); - } + foreach ($replacements as $key => $replacement) { + $search = '{{'.$key.'}}'; + $output = str_replace($search, $replacement, $output); + } - return $output; - } + return $output; + } /** * Inflect to a class name. * - * @param string $input + * @param string $input * * @return string */ - protected function classify($input) - { - return studly_case(str_singular($input)); - } + protected function classify($input) + { + return studly_case(str_singular($input)); + } /** * Inflect to table name. * - * @param string $input + * @param string $input * * @return string */ - protected function tableize($input) - { - return snake_case(str_plural($input)); - } + protected function tableize($input) + { + return snake_case(str_plural($input)); + } } diff --git a/src/Baum/Generators/MigrationGenerator.php b/src/Baum/Generators/MigrationGenerator.php index ce67f557..570ffb54 100644 --- a/src/Baum/Generators/MigrationGenerator.php +++ b/src/Baum/Generators/MigrationGenerator.php @@ -12,19 +12,19 @@ class MigrationGenerator extends Generator * * @return string */ - public function create($name, $path) - { - $path = $this->getPath($name, $path); + public function create($name, $path) + { + $path = $this->getPath($name, $path); - $stub = $this->getStub('migration'); + $stub = $this->getStub('migration'); - $this->files->put($path, $this->parseStub($stub, [ - 'table' => $this->tableize($name), - 'class' => $this->getMigrationClassName($name), - ])); + $this->files->put($path, $this->parseStub($stub, [ + 'table' => $this->tableize($name), + 'class' => $this->getMigrationClassName($name), + ])); - return $path; - } + return $path; + } /** * Get the migration name. @@ -33,20 +33,20 @@ public function create($name, $path) * * @return string */ - protected function getMigrationName($name) - { - return 'create_'.$this->tableize($name).'_table'; - } + protected function getMigrationName($name) + { + return 'create_'.$this->tableize($name).'_table'; + } /** * Get the name for the migration class. * * @param string $name */ - protected function getMigrationClassName($name) - { - return $this->classify($this->getMigrationName($name)); - } + protected function getMigrationClassName($name) + { + return $this->classify($this->getMigrationName($name)); + } /** * Get the full path name to the migration. @@ -56,18 +56,18 @@ protected function getMigrationClassName($name) * * @return string */ - protected function getPath($name, $path) - { - return $path.'/'.$this->getDatePrefix().'_'.$this->getMigrationName($name).'.php'; - } + protected function getPath($name, $path) + { + return $path.'/'.$this->getDatePrefix().'_'.$this->getMigrationName($name).'.php'; + } /** * Get the date prefix for the migration. * * @return int */ - protected function getDatePrefix() - { - return date('Y_m_d_His'); - } + protected function getDatePrefix() + { + return date('Y_m_d_His'); + } } diff --git a/src/Baum/Generators/ModelGenerator.php b/src/Baum/Generators/ModelGenerator.php index 99b826de..baa62f9f 100644 --- a/src/Baum/Generators/ModelGenerator.php +++ b/src/Baum/Generators/ModelGenerator.php @@ -12,19 +12,19 @@ class ModelGenerator extends Generator * * @return string */ - public function create($name, $path) - { - $path = $this->getPath($name, $path); + public function create($name, $path) + { + $path = $this->getPath($name, $path); - $stub = $this->getStub('model'); + $stub = $this->getStub('model'); - $this->files->put($path, $this->parseStub($stub, [ - 'table' => $this->tableize($name), - 'class' => $this->classify($name), - ])); + $this->files->put($path, $this->parseStub($stub, [ + 'table' => $this->tableize($name), + 'class' => $this->classify($name), + ])); - return $path; - } + return $path; + } /** * Get the full path name to the migration. @@ -34,8 +34,8 @@ public function create($name, $path) * * @return string */ - protected function getPath($name, $path) - { - return $path.'/'.$this->classify($name).'.php'; - } + protected function getPath($name, $path) + { + return $path.'/'.$this->classify($name).'.php'; + } } diff --git a/src/Baum/Move.php b/src/Baum/Move.php index 77fac237..bac9fe7b 100644 --- a/src/Baum/Move.php +++ b/src/Baum/Move.php @@ -9,120 +9,121 @@ */ class Move { - /** + + /** * Node on which the move operation will be performed. * * @var \Baum\Node */ - protected $node = null; + protected $node = null; /** * Destination node. * * @var \Baum\Node | int */ - protected $target = null; + protected $target = null; /** * Move target position, one of: child, left, right, root. * * @var string */ - protected $position = null; + protected $position = null; /** * Memoized 1st boundary. * * @var int */ - protected $_bound1 = null; + protected $_bound1 = null; /** * Memoized 2nd boundary. * * @var int */ - protected $_bound2 = null; + protected $_bound2 = null; /** * Memoized boundaries array. * * @var array */ - protected $_boundaries = null; + protected $_boundaries = null; /** * The event dispatcher instance. * * @var \Illuminate\Events\Dispatcher */ - protected static $dispatcher; + protected static $dispatcher; /** * Create a new Move class instance. * - * @param \Baum\Node $node - * @param \Baum\Node|int $target - * @param string $position + * @param \Baum\Node $node + * @param \Baum\Node|int $target + * @param string $position * - * @return void + * @return void */ - public function __construct($node, $target, $position) - { - $this->node = $node; - $this->target = $this->resolveNode($target); - $this->position = $position; + public function __construct($node, $target, $position) + { + $this->node = $node; + $this->target = $this->resolveNode($target); + $this->position = $position; - $this->setEventDispatcher($node->getEventDispatcher()); - } + $this->setEventDispatcher($node->getEventDispatcher()); + } /** * Easy static accessor for performing a move operation. * - * @param \Baum\Node $node - * @param \Baum\Node|int $target - * @param string $position + * @param \Baum\Node $node + * @param \Baum\Node|int $target + * @param string $position * * @return \Baum\Node */ - public static function to($node, $target, $position) - { - $instance = new static($node, $target, $position); + public static function to($node, $target, $position) + { + $instance = new static($node, $target, $position); - return $instance->perform(); - } + return $instance->perform(); + } /** * Perform the move operation. * * @return \Baum\Node */ - public function perform() - { - $this->guardAgainstImpossibleMove(); + public function perform() + { + $this->guardAgainstImpossibleMove(); - if ($this->fireMoveEvent('moving') === false) { - return $this->node; - } + if ($this->fireMoveEvent('moving') === false) { + return $this->node; + } - if ($this->hasChange()) { - $self = $this; + if ($this->hasChange()) { + $self = $this; - $this->node->getConnection()->transaction(function () use ($self) { - $self->updateStructure(); - }); + $this->node->getConnection()->transaction(function () use ($self) { + $self->updateStructure(); + }); - $this->target->reload(); + $this->target->reload(); - $this->node->setDepthWithSubtree(); + $this->node->setDepthWithSubtree(); - $this->node->reload(); - } + $this->node->reload(); + } - $this->fireMoveEvent('moved', false); + $this->fireMoveEvent('moved', false); - return $this->node; - } + return $this->node; + } /** * Runs the SQL query associated with the update of the indexes affected @@ -130,150 +131,156 @@ public function perform() * * @return int */ - public function updateStructure() - { - list($a, $b, $c, $d) = $this->boundaries(); + public function updateStructure() + { + list($a, $b, $c, $d) = $this->boundaries(); - // select the rows between the leftmost & the rightmost boundaries and apply a lock - $this->applyLockBetween($a, $d); + // select the rows between the leftmost & the rightmost boundaries and apply a lock + $this->applyLockBetween($a, $d); - $connection = $this->node->getConnection(); - $grammar = $connection->getQueryGrammar(); + $connection = $this->node->getConnection(); + $grammar = $connection->getQueryGrammar(); - $currentId = $this->quoteIdentifier($this->node->getKey()); - $parentId = $this->quoteIdentifier($this->parentId()); + $currentId = $this->quoteIdentifier($this->node->getKey()); + $parentId = $this->quoteIdentifier($this->parentId()); - $leftColumn = $this->node->getLeftColumnName(); - $rightColumn = $this->node->getRightColumnName(); - $parentColumn = $this->node->getParentColumnName(); + $leftColumn = $this->node->getLeftColumnName(); + $rightColumn = $this->node->getRightColumnName(); + $parentColumn = $this->node->getParentColumnName(); - $wrappedLeft = $grammar->wrap($leftColumn); - $wrappedRight = $grammar->wrap($rightColumn); - $wrappedParent = $grammar->wrap($parentColumn); - $wrappedId = $grammar->wrap($this->node->getKeyName()); + $wrappedLeft = $grammar->wrap($leftColumn); + $wrappedRight = $grammar->wrap($rightColumn); + $wrappedParent = $grammar->wrap($parentColumn); + $wrappedId = $grammar->wrap($this->node->getKeyName()); - $lftSql = "CASE + $lftSql = "CASE WHEN $wrappedLeft BETWEEN $a AND $b THEN $wrappedLeft + $d - $b WHEN $wrappedLeft BETWEEN $c AND $d THEN $wrappedLeft + $a - $c ELSE $wrappedLeft END"; - $rgtSql = "CASE + $rgtSql = "CASE WHEN $wrappedRight BETWEEN $a AND $b THEN $wrappedRight + $d - $b WHEN $wrappedRight BETWEEN $c AND $d THEN $wrappedRight + $a - $c ELSE $wrappedRight END"; - $parentSql = "CASE + $parentSql = "CASE WHEN $wrappedId = $currentId THEN $parentId ELSE $wrappedParent END"; - $updateConditions = [ - $leftColumn => $connection->raw($lftSql), - $rightColumn => $connection->raw($rgtSql), - $parentColumn => $connection->raw($parentSql), - ]; + $updateConditions = [ + $leftColumn => $connection->raw($lftSql), + $rightColumn => $connection->raw($rgtSql), + $parentColumn => $connection->raw($parentSql), + ]; - if ($this->node->timestamps) { - $updateConditions[$this->node->getUpdatedAtColumn()] = $this->node->freshTimestamp(); - } + if ($this->node->timestamps) { + $updateConditions[$this->node->getUpdatedAtColumn()] = $this->node->freshTimestamp(); + } - return $this->node + return $this->node ->newNestedSetQuery() ->where(function ($query) use ($leftColumn, $rightColumn, $a, $d) { $query->whereBetween($leftColumn, [$a, $d]) ->orWhereBetween($rightColumn, [$a, $d]); }) ->update($updateConditions); - } + } /** * Resolves suplied node. Basically returns the node unchanged if * supplied parameter is an instance of \Baum\Node. Otherwise it will try * to find the node in the database. * - * @param \Baum\node|int + * @param \Baum\node|int * - * @return \Baum\Node + * @return \Baum\Node */ - protected function resolveNode($node) - { - if ($node instanceof \Baum\Node) { - return $node->reload(); - } + protected function resolveNode($node) + { + if ($node instanceof Node) { + return $node->reload(); + } - return $this->node->newNestedSetQuery()->find($node); - } + return $this->node->newNestedSetQuery()->find($node); + } /** * Check wether the current move is possible and if not, rais an exception. * * @return void */ - protected function guardAgainstImpossibleMove() - { - if (!$this->node->exists) { - throw new MoveNotPossibleException('A new node cannot be moved.'); - } - - if (array_search($this->position, ['child', 'left', 'right', 'root']) === false) { - throw new MoveNotPossibleException("Position should be one of ['child', 'left', 'right'] but is {$this->position}."); - } - - if (!$this->promotingToRoot()) { - if (is_null($this->target)) { - if ($this->position === 'left' || $this->position === 'right') { - throw new MoveNotPossibleException("Could not resolve target node. This node cannot move any further to the {$this->position}."); - } else { - throw new MoveNotPossibleException('Could not resolve target node.'); - } - } - - if ($this->node->equals($this->target)) { - throw new MoveNotPossibleException('A node cannot be moved to itself.'); - } - - if ($this->target->insideSubtree($this->node)) { - throw new MoveNotPossibleException('A node cannot be moved to a descendant of itself (inside moved tree).'); - } - - if (!$this->node->inSameScope($this->target)) { - throw new MoveNotPossibleException('A node cannot be moved to a different scope.'); - } - } - } + protected function guardAgainstImpossibleMove() + { + if (!$this->node->exists) { + throw new MoveNotPossibleException('A new node cannot be moved.'); + } + + if (array_search($this->position, ['child', 'left', 'right', 'root']) === false) { + throw new MoveNotPossibleException( + "Position should be one of ['child', 'left', 'right'] but is {$this->position}." + ); + } + + if (!$this->promotingToRoot()) { + if (is_null($this->target)) { + if ($this->position === 'left' || $this->position === 'right') { + throw new MoveNotPossibleException( + "Could not resolve target node. This node cannot move any further to the {$this->position}." + ); + } else { + throw new MoveNotPossibleException('Could not resolve target node.'); + } + } + + if ($this->node->equals($this->target)) { + throw new MoveNotPossibleException('A node cannot be moved to itself.'); + } + + if ($this->target->insideSubtree($this->node)) { + throw new MoveNotPossibleException( + 'A node cannot be moved to a descendant of itself (inside moved tree).' + ); + } + + if (!$this->node->inSameScope($this->target)) { + throw new MoveNotPossibleException('A node cannot be moved to a different scope.'); + } + } + } /** * Computes the boundary. * * @return int */ - protected function bound1() - { - if (!is_null($this->_bound1)) { - return $this->_bound1; - } - - switch ($this->position) { - case 'child': - $this->_bound1 = $this->target->getRight(); - break; - - case 'left': - $this->_bound1 = $this->target->getLeft(); - break; - - case 'right': - $this->_bound1 = $this->target->getRight() + 1; - break; - - case 'root': - $this->_bound1 = $this->node->newNestedSetQuery()->max($this->node->getRightColumnName()) + 1; - break; - } + protected function bound1() + { + if (!is_null($this->_bound1)) { + return $this->_bound1; + } + + switch ($this->position) { + case 'child': + $this->_bound1 = $this->target->getRight(); + break; + + case 'left': + $this->_bound1 = $this->target->getLeft(); + break; - $this->_bound1 = (($this->_bound1 > $this->node->getRight()) ? $this->_bound1 - 1 : $this->_bound1); + case 'right': + $this->_bound1 = $this->target->getRight() + 1; + break; - return $this->_bound1; - } + case 'root': + $this->_bound1 = $this->node->newNestedSetQuery()->max($this->node->getRightColumnName()) + 1; + break; + } + + $this->_bound1 = (($this->_bound1 > $this->node->getRight()) ? $this->_bound1 - 1 : $this->_bound1); + + return $this->_bound1; + } /** * Computes the other boundary. @@ -281,89 +288,91 @@ protected function bound1() * * @return int */ - protected function bound2() - { - if (!is_null($this->_bound2)) { - return $this->_bound2; - } + protected function bound2() + { + if (!is_null($this->_bound2)) { + return $this->_bound2; + } - $this->_bound2 = (($this->bound1() > $this->node->getRight()) ? $this->node->getRight() + 1 : $this->node->getLeft() - 1); + $this->_bound2 = + (($this->bound1() > $this->node->getRight()) + ? $this->node->getRight() + 1 : $this->node->getLeft() - 1); - return $this->_bound2; - } + return $this->_bound2; + } /** * Computes the boundaries array. * * @return array */ - protected function boundaries() - { - if (!is_null($this->_boundaries)) { - return $this->_boundaries; - } - - // we have defined the boundaries of two non-overlapping intervals, - // so sorting puts both the intervals and their boundaries in order - $this->_boundaries = [ - $this->node->getLeft(), - $this->node->getRight(), - $this->bound1(), - $this->bound2(), - ]; - sort($this->_boundaries); - - return $this->_boundaries; - } + protected function boundaries() + { + if (!is_null($this->_boundaries)) { + return $this->_boundaries; + } + + // we have defined the boundaries of two non-overlapping intervals, + // so sorting puts both the intervals and their boundaries in order + $this->_boundaries = [ + $this->node->getLeft(), + $this->node->getRight(), + $this->bound1(), + $this->bound2(), + ]; + sort($this->_boundaries); + + return $this->_boundaries; + } /** * Computes the new parent id for the node being moved. * - * @return int + * @return mixed */ - protected function parentId() - { - switch ($this->position) { - case 'root': - return; - - case 'child': - return $this->target->getKey(); - - default: - return $this->target->getParentId(); + protected function parentId() + { + switch ($this->position) { + case 'root': + return null; + + case 'child': + return $this->target->getKey(); + + default: + return $this->target->getParentId(); + } } - } /** * Check wether there should be changes in the downward tree structure. * * @return bool */ - protected function hasChange() - { - return !($this->bound1() == $this->node->getRight() || $this->bound1() == $this->node->getLeft()); - } + protected function hasChange() + { + return !($this->bound1() == $this->node->getRight() || $this->bound1() == $this->node->getLeft()); + } /** * Check if we are promoting the provided instance to a root node. * * @return bool */ - protected function promotingToRoot() - { - return $this->position == 'root'; - } + protected function promotingToRoot() + { + return $this->position == 'root'; + } /** * Get the event dispatcher instance. * * @return \Illuminate\Events\Dispatcher */ - public static function getEventDispatcher() - { - return static::$dispatcher; - } + public static function getEventDispatcher() + { + return static::$dispatcher; + } /** * Set the event dispatcher instance. @@ -372,10 +381,10 @@ public static function getEventDispatcher() * * @return void */ - public static function setEventDispatcher(Dispatcher $dispatcher = null) - { - static::$dispatcher = $dispatcher; - } + public static function setEventDispatcher(Dispatcher $dispatcher = null) + { + static::$dispatcher = $dispatcher; + } /** * Fire the given move event for the model. @@ -385,56 +394,56 @@ public static function setEventDispatcher(Dispatcher $dispatcher = null) * * @return mixed */ - protected function fireMoveEvent($event, $halt = true) - { - if (!isset(static::$dispatcher)) { - return true; - } + protected function fireMoveEvent($event, $halt = true) + { + if (!isset(static::$dispatcher)) { + return true; + } - // Basically the same as \Illuminate\Database\Eloquent\Model->fireModelEvent - // but we relay the event into the node instance. - $event = "eloquent.{$event}: ".get_class($this->node); + // Basically the same as \Illuminate\Database\Eloquent\Model->fireModelEvent + // but we relay the event into the node instance. + $event = "eloquent.{$event}: ".get_class($this->node); - $method = $halt ? 'until' : 'fire'; + $method = $halt ? 'until' : 'fire'; - return static::$dispatcher->$method($event, $this->node); - } + return static::$dispatcher->$method($event, $this->node); + } /** * Quotes an identifier for being used in a database query. * - * @param mixed $value + * @param mixed $value * * @return string */ - protected function quoteIdentifier($value) - { - if (is_null($value)) { - return 'NULL'; - } + protected function quoteIdentifier($value) + { + if (is_null($value)) { + return 'NULL'; + } - $connection = $this->node->getConnection(); + $connection = $this->node->getConnection(); - $pdo = $connection->getPdo(); + $pdo = $connection->getPdo(); - return $pdo->quote($value); - } + return $pdo->quote($value); + } /** * Applies a lock to the rows between the supplied index boundaries. * - * @param int $lft - * @param int $rgt + * @param int $lft + * @param int $rgt * - * @return void + * @return void */ - protected function applyLockBetween($lft, $rgt) - { - $this->node->newQuery() - ->where($this->node->getLeftColumnName(), '>=', $lft) - ->where($this->node->getRightColumnName(), '<=', $rgt) - ->select($this->node->getKeyName()) - ->lockForUpdate() - ->get(); - } + protected function applyLockBetween($lft, $rgt) + { + $this->node->newQuery() + ->where($this->node->getLeftColumnName(), '>=', $lft) + ->where($this->node->getRightColumnName(), '<=', $rgt) + ->select($this->node->getKeyName()) + ->lockForUpdate() + ->get(); + } } diff --git a/src/Baum/MoveNotPossibleException.php b/src/Baum/MoveNotPossibleException.php index 8ca8cf19..11579e79 100644 --- a/src/Baum/MoveNotPossibleException.php +++ b/src/Baum/MoveNotPossibleException.php @@ -4,4 +4,5 @@ class MoveNotPossibleException extends \RuntimeException { + // } diff --git a/src/Baum/Node.php b/src/Baum/Node.php index 2ab3fddd..6b0fddf4 100644 --- a/src/Baum/Node.php +++ b/src/Baum/Node.php @@ -18,6 +18,7 @@ */ abstract class Node extends Model { + /** * Column name to store the reference to parent's node. * @@ -353,7 +354,7 @@ public function children() /** * Get a new "scoped" query builder for the Node's model. * - * @return \Illuminate\Database\Eloquent\Builder|static + * @return \Illuminate\Database\Eloquent\Builder */ public function newNestedSetQuery() { @@ -371,7 +372,7 @@ public function newNestedSetQuery() /** * Overload new Collection. * - * @param array $models + * @param array $models * * @return \Baum\Extensions\Eloquent\Collection */ @@ -383,7 +384,7 @@ public function newCollection(array $models = []) /** * Get all of the nodes from the database. * - * @param array $columns + * @param array $columns * * @return \Illuminate\Database\Eloquent\Collection|static[] */ @@ -399,7 +400,7 @@ public static function all($columns = ['*']) /** * Returns the first root node. * - * @return NestedSet + * @return \Baum\Node|null */ public static function root() { @@ -487,7 +488,7 @@ public static function rebuild() /** * Maps the provided tree structure into the database. * - * @param array|\Illuminate\Support\Contracts\ArrayableInterface + * @param array|\Illuminate\Support\Contracts\ArrayableInterface * * @return bool */ @@ -531,8 +532,8 @@ public function scopeWithoutRoot($query) /** * Provides a depth level limit for the query. * - * @param query \Illuminate\Database\Query\Builder - * @param limit integer + * @param \Illuminate\Database\Query\Builder $query + * @param int $limit * * @return \Illuminate\Database\Query\Builder */ @@ -588,7 +589,7 @@ public function isChild() /** * Returns the root node starting at the current node. * - * @return NestedSet + * @return \Baum\Node|null */ public function getRoot() { @@ -621,7 +622,7 @@ public function ancestorsAndSelf() /** * Get all the ancestor chain from the database including the current node. * - * @param array $columns + * @param array $columns * * @return \Illuminate\Database\Eloquent\Collection */ @@ -634,7 +635,7 @@ public function getAncestorsAndSelf($columns = ['*']) * Get all the ancestor chain from the database including the current node * but without the root node. * - * @param array $columns + * @param array $columns * * @return \Illuminate\Database\Eloquent\Collection */ @@ -657,7 +658,7 @@ public function ancestors() /** * Get all the ancestor chain from the database excluding the current node. * - * @param array $columns + * @param array $columns * * @return \Illuminate\Database\Eloquent\Collection */ @@ -670,7 +671,7 @@ public function getAncestors($columns = ['*']) * Get all the ancestor chain from the database excluding the current node * and the root node (from the current node's perspective). * - * @param array $columns + * @param array $columns * * @return \Illuminate\Database\Eloquent\Collection */ @@ -693,7 +694,7 @@ public function siblingsAndSelf() /** * Get all children of the parent, including self. * - * @param array $columns + * @param array $columns * * @return \Illuminate\Database\Eloquent\Collection */ @@ -715,7 +716,7 @@ public function siblings() /** * Return all children of the parent, except self. * - * @param array $columns + * @param array $columns * * @return \Illuminate\Database\Eloquent\Collection */ @@ -744,7 +745,7 @@ public function leaves() /** * Return all of its nested children which do not have children. * - * @param array $columns + * @param array $columns * * @return \Illuminate\Database\Eloquent\Collection */ @@ -774,7 +775,7 @@ public function trunks() /** * Return all of its nested children which are trunks. * - * @param array $columns + * @param array $columns * * @return \Illuminate\Database\Eloquent\Collection */ @@ -798,7 +799,7 @@ public function descendantsAndSelf() /** * Retrieve all nested children an self. * - * @param array $columns + * @param array $columns * * @return \Illuminate\Database\Eloquent\Collection */ @@ -840,7 +841,7 @@ public function descendants() /** * Retrieve all of its children & nested children. * - * @param array $columns + * @param array $columns * * @return \Illuminate\Database\Eloquent\Collection */ @@ -871,7 +872,7 @@ public function immediateDescendants() /** * Retrive all of its "immediate" descendants. * - * @param array $columns + * @param array $columns * * @return \Illuminate\Database\Eloquent\Collection */ @@ -898,7 +899,8 @@ public function getLevel() /** * Returns true if node is a direct descendant of $other. * - * @param NestedSet + * @param \Baum\Node + * * @return bool */ public function isChildOf($other) @@ -912,7 +914,7 @@ public function isChildOf($other) /** * Returns true if node is a descendant. * - * @param NestedSet + * @param \Baum\Node * * @return bool */ @@ -927,7 +929,7 @@ public function isDescendantOf($other) /** * Returns true if node is self or a descendant. * - * @param NestedSet + * @param \Baum\Node * * @return bool */ @@ -942,7 +944,7 @@ public function isSelfOrDescendantOf($other) /** * Returns true if node is an ancestor. * - * @param NestedSet + * @param \Baum\Node * * @return bool */ @@ -957,7 +959,7 @@ public function isAncestorOf($other) /** * Returns true if node is self or an ancestor. * - * @param NestedSet + * @param \Baum\Node * * @return bool */ @@ -972,7 +974,7 @@ public function isSelfOrAncestorOf($other) /** * Returns the first sibling to the left. * - * @return NestedSet + * @return \Baum\Node|null */ public function getLeftSibling() { @@ -986,7 +988,7 @@ public function getLeftSibling() /** * Returns the first sibling to the right. * - * @return NestedSet + * @return \Baum\Node|null */ public function getRightSibling() { @@ -1112,7 +1114,7 @@ public function makeRoot() /** * Equals? * - * @param \Baum\Node + * @param \Baum\Node * * @return bool */ @@ -1124,7 +1126,7 @@ public function equals($node) /** * Checkes if the given node is in the same scope as the current one. * - * @param \Baum\Node + * @param \Baum\Node * * @return bool */ @@ -1143,7 +1145,7 @@ public function inSameScope($other) * Checks wether the given node is a descendant of itself. Basically, whether * its in the subtree defined by the left and right indices. * - * @param \Baum\Node + * @param \Baum\Node * * @return bool */ @@ -1163,7 +1165,8 @@ public function insideSubtree($node) */ public function setDefaultLeftAndRight() { - $withHighestRight = $this->newNestedSetQuery()->reOrderBy($this->getRightColumnName(), 'desc')->take(1)->sharedLock()->first(); + $withHighestRight = + $this->newNestedSetQuery()->reOrderBy($this->getRightColumnName(), 'desc')->take(1)->sharedLock()->first(); $maxRgt = 0; if (!is_null($withHighestRight)) { @@ -1217,7 +1220,12 @@ public function setDepth() $this->getConnection()->transaction(function () use ($self) { $self->reload(); $level = $self->getLevel(); - $self->newNestedSetQuery()->where($self->getKeyName(), '=', $self->getKey())->update([$self->getDepthColumnName() => $level]); + + $self + ->newNestedSetQuery() + ->where($self->getKeyName(), '=', $self->getKey()) + ->update([$self->getDepthColumnName() => $level]); + $self->setAttribute($self->getDepthColumnName(), $level); }); @@ -1241,7 +1249,11 @@ public function setDepthWithSubtree() $oldDepth = !is_null($self->getDepth()) ? $self->getDepth() : 0; $newDepth = $self->getLevel(); - $self->newNestedSetQuery()->where($self->getKeyName(), '=', $self->getKey())->update([$self->getDepthColumnName() => $newDepth]); + $self + ->newNestedSetQuery() + ->where($self->getKeyName(), '=', $self->getKey()) + ->update([$self->getDepthColumnName() => $newDepth]); + $self->setAttribute($self->getDepthColumnName(), $newDepth); $diff = $newDepth - $oldDepth; @@ -1357,7 +1369,7 @@ public static function getNestedList($column, $key = null, $seperator = ' ', $sy return array_combine(array_map(function ($node) use ($key) { return $node[$key]; }, $nodes), array_map(function ($node) use ($seperator, $depthColumn, $column, $symbol) { - return str_repeat($seperator, $node[$depthColumn]) . $symbol . $node[$column]; + return str_repeat($seperator, $node[$depthColumn]).$symbol.$node[$column]; }, $nodes)); } @@ -1381,8 +1393,8 @@ public function makeTree($nodeList) * Main move method. Here we handle all node movements with the corresponding * lft/rgt index updates. * - * @param Baum\Node|int $target - * @param string $position + * @param \Baum\Node|int $target + * @param string $position * * @return \Baum\Node */ @@ -1412,8 +1424,8 @@ protected function computeLevel() /** * Return an array with the last node we could reach and its nesting level. * - * @param Baum\Node $node - * @param int $nesting + * @param \Baum\Node $node + * @param int $nesting * * @return array */ diff --git a/src/Baum/Providers/BaumServiceProvider.php b/src/Baum/Providers/BaumServiceProvider.php index 459c7cfd..18e98118 100644 --- a/src/Baum/Providers/BaumServiceProvider.php +++ b/src/Baum/Providers/BaumServiceProvider.php @@ -15,67 +15,67 @@ class BaumServiceProvider extends ServiceProvider * * @var string */ - const VERSION = '1.1.1'; + const VERSION = '1.1.1'; /** * Register the service provider. * * @return void */ - public function register() - { - $this->registerCommands(); - } + public function register() + { + $this->registerCommands(); + } /** * Register the commands. * * @return void */ - public function registerCommands() - { - $this->registerBaumCommand(); - $this->registerInstallCommand(); + public function registerCommands() + { + $this->registerBaumCommand(); + $this->registerInstallCommand(); - // Resolve the commands with Artisan by attaching the event listener to Artisan's - // startup. This allows us to use the commands from our terminal. - $this->commands('command.baum', 'command.baum.install'); - } + // Resolve the commands with Artisan by attaching the event listener to Artisan's + // startup. This allows us to use the commands from our terminal. + $this->commands('command.baum', 'command.baum.install'); + } /** * Register the 'baum' command. * * @return void */ - protected function registerBaumCommand() - { - $this->app->singleton('command.baum', function ($app) { - return new BaumCommand(); - }); - } + protected function registerBaumCommand() + { + $this->app->singleton('command.baum', function () { + return new BaumCommand(); + }); + } /** * Register the 'baum:install' command. * * @return void */ - protected function registerInstallCommand() - { - $this->app->singleton('command.baum.install', function ($app) { - $migrator = new MigrationGenerator($app['files']); - $modeler = new ModelGenerator($app['files']); + protected function registerInstallCommand() + { + $this->app->singleton('command.baum.install', function ($app) { + $migrator = new MigrationGenerator($app['files']); + $modeler = new ModelGenerator($app['files']); - return new InstallCommand($migrator, $modeler); - }); - } + return new InstallCommand($migrator, $modeler); + }); + } /** * Get the services provided by the provider. * * @return array */ - public function provides() - { - return ['command.baum', 'command.baum.install']; - } + public function provides() + { + return ['command.baum', 'command.baum.install']; + } } diff --git a/src/Baum/SetBuilder.php b/src/Baum/SetBuilder.php index 1a6764fa..506e5553 100644 --- a/src/Baum/SetBuilder.php +++ b/src/Baum/SetBuilder.php @@ -4,19 +4,20 @@ class SetBuilder { + /** * Node instance for reference. * * @var \Baum\Node */ - protected $node = null; + protected $node = null; /** * Array which will hold temporary lft, rgt index values for each scope. * * @var array */ - protected $bounds = []; + protected $bounds = []; /** * Create a new \Baum\SetBuilder class instance. @@ -25,10 +26,10 @@ class SetBuilder * * @return void */ - public function __construct($node) - { - $this->node = $node; - } + public function __construct($node) + { + $this->node = $node; + } /** * Perform the re-calculation of the left and right indexes of the whole @@ -36,126 +37,126 @@ public function __construct($node) * * @return void */ - public function rebuild() - { - // Rebuild lefts and rights for each root node and its children (recursively). - // We go by setting left (and keep track of the current left bound), then - // search for each children and recursively set the left index (while - // incrementing that index). When going back up the recursive chain we start - // setting the right indexes and saving the nodes... - $self = $this; - - $this->node->getConnection()->transaction(function () use ($self) { - foreach ($self->roots() as $root) { - $self->rebuildBounds($root, 0); - } - }); - } + public function rebuild() + { + // Rebuild lefts and rights for each root node and its children (recursively). + // We go by setting left (and keep track of the current left bound), then + // search for each children and recursively set the left index (while + // incrementing that index). When going back up the recursive chain we start + // setting the right indexes and saving the nodes... + $self = $this; + + $this->node->getConnection()->transaction(function () use ($self) { + foreach ($self->roots() as $root) { + $self->rebuildBounds($root, 0); + } + }); + } /** * Return all root nodes for the current database table appropiately sorted. * * @return Illuminate\Database\Eloquent\Collection */ - public function roots() - { - return $this->node->newQuery() - ->whereNull($this->node->getQualifiedParentColumnName()) - ->orderBy($this->node->getQualifiedLeftColumnName()) - ->orderBy($this->node->getQualifiedRightColumnName()) - ->orderBy($this->node->getQualifiedKeyName()) - ->get(); - } + public function roots() + { + return $this->node->newQuery() + ->whereNull($this->node->getQualifiedParentColumnName()) + ->orderBy($this->node->getQualifiedLeftColumnName()) + ->orderBy($this->node->getQualifiedRightColumnName()) + ->orderBy($this->node->getQualifiedKeyName()) + ->get(); + } /** * Recompute left and right index bounds for the specified node and its * children (recursive call). Fill the depth column too. */ - public function rebuildBounds($node, $depth = 0) - { - $k = $this->scopedKey($node); + public function rebuildBounds($node, $depth = 0) + { + $k = $this->scopedKey($node); - $node->setAttribute($node->getLeftColumnName(), $this->getNextBound($k)); - $node->setAttribute($node->getDepthColumnName(), $depth); + $node->setAttribute($node->getLeftColumnName(), $this->getNextBound($k)); + $node->setAttribute($node->getDepthColumnName(), $depth); - foreach ($this->children($node) as $child) { - $this->rebuildBounds($child, $depth + 1); - } + foreach ($this->children($node) as $child) { + $this->rebuildBounds($child, $depth + 1); + } - $node->setAttribute($node->getRightColumnName(), $this->getNextBound($k)); + $node->setAttribute($node->getRightColumnName(), $this->getNextBound($k)); - $node->save(); - } + $node->save(); + } /** * Return all children for the specified node. * - * @param Baum\Node $node + * @param \Baum\Node $node * * @return Illuminate\Database\Eloquent\Collection */ - public function children($node) - { - $query = $this->node->newQuery(); + public function children($node) + { + $query = $this->node->newQuery(); - $query->where($this->node->getQualifiedParentColumnName(), '=', $node->getKey()); + $query->where($this->node->getQualifiedParentColumnName(), '=', $node->getKey()); - // We must also add the scoped column values to the query to compute valid - // left and right indexes. - foreach ($this->scopedAttributes($node) as $fld => $value) { - $query->where($this->qualify($fld), '=', $value); - } + // We must also add the scoped column values to the query to compute valid + // left and right indexes. + foreach ($this->scopedAttributes($node) as $fld => $value) { + $query->where($this->qualify($fld), '=', $value); + } - $query->orderBy($this->node->getQualifiedLeftColumnName()); - $query->orderBy($this->node->getQualifiedRightColumnName()); - $query->orderBy($this->node->getQualifiedKeyName()); + $query->orderBy($this->node->getQualifiedLeftColumnName()); + $query->orderBy($this->node->getQualifiedRightColumnName()); + $query->orderBy($this->node->getQualifiedKeyName()); - return $query->get(); - } + return $query->get(); + } /** * Return an array of the scoped attributes of the supplied node. * - * @param Baum\Node $node + * @param \Baum\Node $node * * @return array */ - protected function scopedAttributes($node) - { - $keys = $this->node->getScopedColumns(); + protected function scopedAttributes($node) + { + $keys = $this->node->getScopedColumns(); - if (count($keys) == 0) { - return []; - } + if (count($keys) == 0) { + return []; + } - $values = array_map(function ($column) use ($node) { - return $node->getAttribute($column); - }, $keys); + $values = array_map(function ($column) use ($node) { + return $node->getAttribute($column); + }, $keys); - return array_combine($keys, $values); - } + return array_combine($keys, $values); + } /** * Return a string-key for the current scoped attributes. Used for index * computing when a scope is defined (acsts as an scope identifier). * - * @param Baum\Node $node + * @param \Baum\Node $node * * @return string */ - protected function scopedKey($node) - { - $attributes = $this->scopedAttributes($node); + protected function scopedKey($node) + { + $attributes = $this->scopedAttributes($node); - $output = []; + $output = []; - foreach ($attributes as $fld => $value) { - $output[] = $this->qualify($fld).'='.(is_null($value) ? 'NULL' : $value); - } + foreach ($attributes as $fld => $value) { + $output[] = $this->qualify($fld).'='.(is_null($value) ? 'NULL' : $value); + } - // NOTE: Maybe an md5 or something would be better. Should be unique though. - return implode(',', $output); - } + // NOTE: Maybe an md5 or something would be better. Should be unique though. + return implode(',', $output); + } /** * Return next index bound value for the given key (current scope identifier). @@ -164,24 +165,24 @@ protected function scopedKey($node) * * @return int */ - protected function getNextBound($key) - { - if (false === array_key_exists($key, $this->bounds)) { - $this->bounds[$key] = 0; - } + protected function getNextBound($key) + { + if (false === array_key_exists($key, $this->bounds)) { + $this->bounds[$key] = 0; + } - $this->bounds[$key] = $this->bounds[$key] + 1; + $this->bounds[$key] = $this->bounds[$key] + 1; - return $this->bounds[$key]; - } + return $this->bounds[$key]; + } /** * Get the fully qualified value for the specified column. * * @return string */ - protected function qualify($column) - { - return $this->node->getTable().'.'.$column; - } + protected function qualify($column) + { + return $this->node->getTable().'.'.$column; + } } diff --git a/src/Baum/SetMapper.php b/src/Baum/SetMapper.php index 4325b1ed..cefa625b 100644 --- a/src/Baum/SetMapper.php +++ b/src/Baum/SetMapper.php @@ -7,6 +7,7 @@ class SetMapper { + /** * Node instance for reference. * @@ -24,7 +25,7 @@ class SetMapper /** * Create a new \Baum\SetBuilder class instance. * - * @param \Baum\Node $node + * @param \Baum\Node $node * * @return void */ @@ -38,7 +39,7 @@ public function __construct($node, $childrenKeyName = 'children') /** * Maps a tree structure into the database. Unguards & wraps in transaction. * - * @param array|\Illuminate\Support\Contracts\ArrayableInterface + * @param array|\Illuminate\Support\Contracts\ArrayableInterface * * @return bool */ @@ -59,7 +60,7 @@ public function map($nodeList) * Maps a tree structure into the database without unguarding nor wrapping * inside a transaction. * - * @param array|\Illuminate\Support\Contracts\ArrayableInterface + * @param array|\Illuminate\Support\Contracts\ArrayableInterface * * @return bool */ diff --git a/src/Baum/SetValidator.php b/src/Baum/SetValidator.php index 425660e5..413f3531 100644 --- a/src/Baum/SetValidator.php +++ b/src/Baum/SetValidator.php @@ -4,12 +4,13 @@ class SetValidator { + /** * Node instance for reference. * * @var \Baum\Node */ - protected $node = null; + protected $node = null; /** * Create a new \Baum\SetValidator class instance. @@ -18,31 +19,31 @@ class SetValidator * * @return void */ - public function __construct($node) - { - $this->node = $node; - } + public function __construct($node) + { + $this->node = $node; + } /** * Determine if the validation passes. * * @return bool */ - public function passes() - { - return $this->validateBounds() && $this->validateDuplicates() && - $this->validateRoots(); - } + public function passes() + { + return $this->validateBounds() && $this->validateDuplicates() && + $this->validateRoots(); + } /** * Determine if validation fails. * * @return bool */ - public function fails() - { - return !$this->passes(); - } + public function fails() + { + return !$this->passes(); + } /** * Validates bounds of the nested tree structure. It will perform checks on @@ -51,49 +52,53 @@ public function fails() * * @return bool */ - protected function validateBounds() - { - $connection = $this->node->getConnection(); - $grammar = $connection->getQueryGrammar(); + protected function validateBounds() + { + $connection = $this->node->getConnection(); + $grammar = $connection->getQueryGrammar(); - $tableName = $this->node->getTable(); - $primaryKeyName = $this->node->getKeyName(); - $parentColumn = $this->node->getQualifiedParentColumnName(); + $tableName = $this->node->getTable(); + $primaryKeyName = $this->node->getKeyName(); + $parentColumn = $this->node->getQualifiedParentColumnName(); - $lftCol = $grammar->wrap($this->node->getLeftColumnName()); - $rgtCol = $grammar->wrap($this->node->getRightColumnName()); + $lftCol = $grammar->wrap($this->node->getLeftColumnName()); + $rgtCol = $grammar->wrap($this->node->getRightColumnName()); - $qualifiedLftCol = $grammar->wrap($this->node->getQualifiedLeftColumnName()); - $qualifiedRgtCol = $grammar->wrap($this->node->getQualifiedRightColumnName()); - $qualifiedParentCol = $grammar->wrap($this->node->getQualifiedParentColumnName()); + $qualifiedLftCol = $grammar->wrap($this->node->getQualifiedLeftColumnName()); + $qualifiedRgtCol = $grammar->wrap($this->node->getQualifiedRightColumnName()); + $qualifiedParentCol = $grammar->wrap($this->node->getQualifiedParentColumnName()); - $whereStm = "($qualifiedLftCol IS NULL OR + $whereStm = "($qualifiedLftCol IS NULL OR $qualifiedRgtCol IS NULL OR $qualifiedLftCol >= $qualifiedRgtCol OR ($qualifiedParentCol IS NOT NULL AND ($qualifiedLftCol <= parent.$lftCol OR $qualifiedRgtCol >= parent.$rgtCol)))"; - $query = $this->node->newQuery() - ->join($connection->raw($grammar->wrapTable($tableName).' AS parent'), - $parentColumn, '=', $connection->raw('parent.'.$grammar->wrap($primaryKeyName)), - 'left outer') - ->whereRaw($whereStm); - - return $query->count() == 0; - } + $query = $this->node->newQuery() + ->join( + $connection->raw($grammar->wrapTable($tableName).' AS parent'), + $parentColumn, + '=', + $connection->raw('parent.'.$grammar->wrap($primaryKeyName)), + 'left outer' + ) + ->whereRaw($whereStm); + + return $query->count() == 0; + } /** * Checks that there are no duplicates for the `lft` and `rgt` columns. * * @return bool */ - protected function validateDuplicates() - { - return - !$this->duplicatesExistForColumn($this->node->getQualifiedLeftColumnName()) && - !$this->duplicatesExistForColumn($this->node->getQualifiedRightColumnName()); - } + protected function validateDuplicates() + { + return + !$this->duplicatesExistForColumn($this->node->getQualifiedLeftColumnName()) && + !$this->duplicatesExistForColumn($this->node->getQualifiedRightColumnName()); + } /** * For each root of the whole nested set tree structure, checks that their @@ -101,18 +106,18 @@ protected function validateDuplicates() * * @return bool */ - protected function validateRoots() - { - $roots = forward_static_call([get_class($this->node), 'roots'])->get(); - - // If a scope is defined in the model we should check that the roots are - // valid *for each* value in the scope columns. - if ($this->node->isScoped()) { - return $this->validateRootsByScope($roots); - } + protected function validateRoots() + { + $roots = forward_static_call([get_class($this->node), 'roots'])->get(); - return $this->isEachRootValid($roots); - } + // If a scope is defined in the model we should check that the roots are + // valid *for each* value in the scope columns. + if ($this->node->isScoped()) { + return $this->validateRootsByScope($roots); + } + + return $this->isEachRootValid($roots); + } /** * Checks if duplicate values for the column specified exist. Takes @@ -122,31 +127,31 @@ protected function validateRoots() * * @return bool */ - protected function duplicatesExistForColumn($column) - { - $connection = $this->node->getConnection(); - $grammar = $connection->getQueryGrammar(); + protected function duplicatesExistForColumn($column) + { + $connection = $this->node->getConnection(); + $grammar = $connection->getQueryGrammar(); - $columns = array_merge($this->node->getQualifiedScopedColumns(), [$column]); + $columns = array_merge($this->node->getQualifiedScopedColumns(), [$column]); - $columnsForSelect = implode(', ', array_map(function ($col) use ($grammar) { - return $grammar->wrap($col); - }, $columns)); + $columnsForSelect = implode(', ', array_map(function ($col) use ($grammar) { + return $grammar->wrap($col); + }, $columns)); - $wrappedColumn = $grammar->wrap($column); + $wrappedColumn = $grammar->wrap($column); - $query = $this->node->newQuery() - ->select($connection->raw("$columnsForSelect, COUNT($wrappedColumn)")) - ->havingRaw("COUNT($wrappedColumn) > 1"); + $query = $this->node->newQuery() + ->select($connection->raw("$columnsForSelect, COUNT($wrappedColumn)")) + ->havingRaw("COUNT($wrappedColumn) > 1"); - foreach ($columns as $col) { - $query->groupBy($col); - } + foreach ($columns as $col) { + $query->groupBy($col); + } - $result = $query->first(); + $result = $query->first(); - return !is_null($result); - } + return !is_null($result); + } /** * Check that each root node in the list supplied satisfies that its bounds @@ -156,24 +161,24 @@ protected function duplicatesExistForColumn($column) * * @return bool */ - protected function isEachRootValid($roots) - { - $left = $right = 0; + protected function isEachRootValid($roots) + { + $left = $right = 0; - foreach ($roots as $root) { - $rootLeft = $root->getLeft(); - $rootRight = $root->getRight(); + foreach ($roots as $root) { + $rootLeft = $root->getLeft(); + $rootRight = $root->getRight(); - if (!($rootLeft > $left && $rootRight > $right)) { - return false; - } + if (!($rootLeft > $left && $rootRight > $right)) { + return false; + } - $left = $rootLeft; - $right = $rootRight; - } + $left = $rootLeft; + $right = $rootRight; + } - return true; - } + return true; + } /** * Check that each root node in the list supplied satisfies that its bounds @@ -183,18 +188,18 @@ protected function isEachRootValid($roots) * * @return bool */ - protected function validateRootsByScope($roots) - { - foreach ($this->groupRootsByScope($roots) as $scope => $groupedRoots) { - $valid = $this->isEachRootValid($groupedRoots); + protected function validateRootsByScope($roots) + { + foreach ($this->groupRootsByScope($roots) as $scope => $groupedRoots) { + $valid = $this->isEachRootValid($groupedRoots); - if (!$valid) { - return false; - } - } + if (!$valid) { + return false; + } + } - return true; - } + return true; + } /** * Given a list of root nodes, it returns an array in which the keys are the @@ -205,41 +210,41 @@ protected function validateRootsByScope($roots) * * @return array */ - protected function groupRootsByScope($roots) - { - $rootsGroupedByScope = []; + protected function groupRootsByScope($roots) + { + $rootsGroupedByScope = []; - foreach ($roots as $root) { - $key = $this->keyForScope($root); + foreach ($roots as $root) { + $key = $this->keyForScope($root); - if (!isset($rootsGroupedByScope[$key])) { - $rootsGroupedByScope[$key] = []; - } + if (!isset($rootsGroupedByScope[$key])) { + $rootsGroupedByScope[$key] = []; + } - $rootsGroupedByScope[$key][] = $root; - } + $rootsGroupedByScope[$key][] = $root; + } - return $rootsGroupedByScope; - } + return $rootsGroupedByScope; + } /** * Builds a single string for the given scope columns values. Useful for * making array keys for grouping. * - * @param Baum\Node $node + * @param \Baum\Node $node * * @return string */ - protected function keyForScope($node) - { - return implode('-', array_map(function ($column) use ($node) { - $value = $node->getAttribute($column); - - if (is_null($value)) { - return 'NULL'; - } - - return $value; - }, $node->getScopedColumns())); - } + protected function keyForScope($node) + { + return implode('-', array_map(function ($column) use ($node) { + $value = $node->getAttribute($column); + + if (is_null($value)) { + return 'NULL'; + } + + return $value; + }, $node->getScopedColumns())); + } } diff --git a/tests/models/Category.php b/tests/models/Category.php index 5b4c0c19..1bd9a62d 100644 --- a/tests/models/Category.php +++ b/tests/models/Category.php @@ -5,6 +5,7 @@ class Category extends Node { + protected $table = 'categories'; public $timestamps = false; @@ -12,21 +13,25 @@ class Category extends Node class ScopedCategory extends Category { + protected $scoped = ['company_id']; } class MultiScopedCategory extends Category { + protected $scoped = ['company_id', 'language']; } class OrderedCategory extends Category { + protected $orderColumn = 'name'; } class OrderedScopedCategory extends Category { + protected $scoped = ['company_id']; protected $orderColumn = 'name'; diff --git a/tests/models/Cluster.php b/tests/models/Cluster.php index 9f21832c..f742e3d7 100644 --- a/tests/models/Cluster.php +++ b/tests/models/Cluster.php @@ -5,6 +5,7 @@ class Cluster extends Node { + protected $table = 'clusters'; public $incrementing = false; @@ -31,28 +32,35 @@ public function ensureUuid() protected function generateUuid() { - return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', - mt_rand(0, 0xffff), mt_rand(0, 0xffff), - mt_rand(0, 0xffff), - mt_rand(0, 0x0fff) | 0x4000, - mt_rand(0, 0x3fff) | 0x8000, - mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff) - ); + return sprintf( + '%04x%04x-%04x-%04x-%04x-%04x%04x%04x', + mt_rand(0, 0xffff), + mt_rand(0, 0xffff), + mt_rand(0, 0xffff), + mt_rand(0, 0x0fff) | 0x4000, + mt_rand(0, 0x3fff) | 0x8000, + mt_rand(0, 0xffff), + mt_rand(0, 0xffff), + mt_rand(0, 0xffff) + ); } } class ScopedCluster extends Cluster { + protected $scoped = ['company_id']; } class MultiScopedCluster extends Cluster { + protected $scoped = ['company_id', 'language']; } class OrderedCluster extends Cluster { + protected $orderColumn = 'name'; } diff --git a/tests/suite/Category/CategoryCustomEventsTest.php b/tests/suite/Category/CategoryCustomEventsTest.php index 9bcf38bf..2ab14ffc 100644 --- a/tests/suite/Category/CategoryCustomEventsTest.php +++ b/tests/suite/Category/CategoryCustomEventsTest.php @@ -35,9 +35,9 @@ public function testMovementHaltsWhenReturningFalseFromMoving() $events->shouldReceive('until')->once()->with('eloquent.moving: '.get_class($unchanged), $unchanged)->andReturn(false); // Force "moving" to return false - Category::moving(function ($node) { - return false; - }); + Category::moving(function ($node) { + return false; + }); $unchanged->makeRoot(); @@ -49,7 +49,7 @@ public function testMovementHaltsWhenReturningFalseFromMoving() $this->assertEquals(7, $unchanged->getRight()); // Restore - Category::getEventDispatcher()->forget('eloquent.moving: '.get_class($unchanged)); + Category::getEventDispatcher()->forget('eloquent.moving: '.get_class($unchanged)); Category::unsetEventDispatcher(); Category::setEventDispatcher($dispatcher); diff --git a/tests/suite/Category/CategoryHierarchyTest.php b/tests/suite/Category/CategoryHierarchyTest.php index 83ba302f..705b2e86 100644 --- a/tests/suite/Category/CategoryHierarchyTest.php +++ b/tests/suite/Category/CategoryHierarchyTest.php @@ -45,7 +45,7 @@ public function testRootsStaticWithCustomOrder() $category = OrderedCategory::create(['name' => 'A new root is born']); $category->syncOriginal(); // ¿? --> This should be done already !? - $roots = OrderedCategory::roots()->get(); + $roots = OrderedCategory::roots()->get(); $this->assertCount(3, $roots); $this->assertEquals($category->getAttributes(), $roots->first()->getAttributes()); @@ -269,12 +269,12 @@ public function testGetDescendantsAndSelf() $parent = $this->categories('Root 1'); $expected = [ - $parent, - $this->categories('Child 1'), - $this->categories('Child 2'), - $this->categories('Child 2.1'), - $this->categories('Child 3'), - ]; + $parent, + $this->categories('Child 1'), + $this->categories('Child 2'), + $this->categories('Child 2.1'), + $this->categories('Child 3'), + ]; $this->assertCount(count($expected), $parent->getDescendantsAndSelf()); @@ -290,49 +290,49 @@ public function testGetDescendantsAndSelfWithLimit() $this->assertEquals([$parent], $parent->getDescendantsAndSelf(0)->all()); $this->assertEquals([ - $parent, - $this->categories('Child 1'), - $this->categories('Child 2'), - $this->categories('Child 3'), - ], $parent->getDescendantsAndSelf(1)->all()); + $parent, + $this->categories('Child 1'), + $this->categories('Child 2'), + $this->categories('Child 3'), + ], $parent->getDescendantsAndSelf(1)->all()); $this->assertEquals([ - $parent, - $this->categories('Child 1'), - $this->categories('Child 2'), - $this->categories('Child 2.1'), - $this->categories('Child 3'), - ], $parent->getDescendantsAndSelf(2)->all()); + $parent, + $this->categories('Child 1'), + $this->categories('Child 2'), + $this->categories('Child 2.1'), + $this->categories('Child 3'), + ], $parent->getDescendantsAndSelf(2)->all()); $this->assertEquals([ - $parent, - $this->categories('Child 1'), - $this->categories('Child 2'), - $this->categories('Child 2.1'), - $this->categories('Child 2.1.1'), - $this->categories('Child 3'), - ], $parent->getDescendantsAndSelf(3)->all()); + $parent, + $this->categories('Child 1'), + $this->categories('Child 2'), + $this->categories('Child 2.1'), + $this->categories('Child 2.1.1'), + $this->categories('Child 3'), + ], $parent->getDescendantsAndSelf(3)->all()); $this->assertEquals([ - $parent, - $this->categories('Child 1'), - $this->categories('Child 2'), - $this->categories('Child 2.1'), - $this->categories('Child 2.1.1'), - $this->categories('Child 2.1.1.1'), - $this->categories('Child 3'), - ], $parent->getDescendantsAndSelf(4)->all()); + $parent, + $this->categories('Child 1'), + $this->categories('Child 2'), + $this->categories('Child 2.1'), + $this->categories('Child 2.1.1'), + $this->categories('Child 2.1.1.1'), + $this->categories('Child 3'), + ], $parent->getDescendantsAndSelf(4)->all()); $this->assertEquals([ - $parent, - $this->categories('Child 1'), - $this->categories('Child 2'), - $this->categories('Child 2.1'), - $this->categories('Child 2.1.1'), - $this->categories('Child 2.1.1.1'), - $this->categories('Child 2.1.1.1.1'), - $this->categories('Child 3'), - ], $parent->getDescendantsAndSelf(10)->all()); + $parent, + $this->categories('Child 1'), + $this->categories('Child 2'), + $this->categories('Child 2.1'), + $this->categories('Child 2.1.1'), + $this->categories('Child 2.1.1.1'), + $this->categories('Child 2.1.1.1.1'), + $this->categories('Child 3'), + ], $parent->getDescendantsAndSelf(10)->all()); } public function testGetDescendants() @@ -340,11 +340,11 @@ public function testGetDescendants() $parent = $this->categories('Root 1'); $expected = [ - $this->categories('Child 1'), - $this->categories('Child 2'), - $this->categories('Child 2.1'), - $this->categories('Child 3'), - ]; + $this->categories('Child 1'), + $this->categories('Child 2'), + $this->categories('Child 2.1'), + $this->categories('Child 3'), + ]; $this->assertCount(count($expected), $parent->getDescendants()); @@ -360,54 +360,54 @@ public function testGetDescendantsWithLimit() $this->assertEmpty($parent->getDescendants(0)->all()); $this->assertEquals([ - $this->categories('Child 1'), - $this->categories('Child 2'), - $this->categories('Child 3'), - ], $parent->getDescendants(1)->all()); + $this->categories('Child 1'), + $this->categories('Child 2'), + $this->categories('Child 3'), + ], $parent->getDescendants(1)->all()); $this->assertEquals([ - $this->categories('Child 1'), - $this->categories('Child 2'), - $this->categories('Child 2.1'), - $this->categories('Child 3'), - ], $parent->getDescendants(2)->all()); + $this->categories('Child 1'), + $this->categories('Child 2'), + $this->categories('Child 2.1'), + $this->categories('Child 3'), + ], $parent->getDescendants(2)->all()); $this->assertEquals([ - $this->categories('Child 1'), - $this->categories('Child 2'), - $this->categories('Child 2.1'), - $this->categories('Child 2.1.1'), - $this->categories('Child 3'), - ], $parent->getDescendants(3)->all()); + $this->categories('Child 1'), + $this->categories('Child 2'), + $this->categories('Child 2.1'), + $this->categories('Child 2.1.1'), + $this->categories('Child 3'), + ], $parent->getDescendants(3)->all()); $this->assertEquals([ - $this->categories('Child 1'), - $this->categories('Child 2'), - $this->categories('Child 2.1'), - $this->categories('Child 2.1.1'), - $this->categories('Child 2.1.1.1'), - $this->categories('Child 3'), - ], $parent->getDescendants(4)->all()); + $this->categories('Child 1'), + $this->categories('Child 2'), + $this->categories('Child 2.1'), + $this->categories('Child 2.1.1'), + $this->categories('Child 2.1.1.1'), + $this->categories('Child 3'), + ], $parent->getDescendants(4)->all()); $this->assertEquals([ - $this->categories('Child 1'), - $this->categories('Child 2'), - $this->categories('Child 2.1'), - $this->categories('Child 2.1.1'), - $this->categories('Child 2.1.1.1'), - $this->categories('Child 2.1.1.1.1'), - $this->categories('Child 3'), - ], $parent->getDescendants(5)->all()); + $this->categories('Child 1'), + $this->categories('Child 2'), + $this->categories('Child 2.1'), + $this->categories('Child 2.1.1'), + $this->categories('Child 2.1.1.1'), + $this->categories('Child 2.1.1.1.1'), + $this->categories('Child 3'), + ], $parent->getDescendants(5)->all()); $this->assertEquals([ - $this->categories('Child 1'), - $this->categories('Child 2'), - $this->categories('Child 2.1'), - $this->categories('Child 2.1.1'), - $this->categories('Child 2.1.1.1'), - $this->categories('Child 2.1.1.1.1'), - $this->categories('Child 3'), - ], $parent->getDescendants(10)->all()); + $this->categories('Child 1'), + $this->categories('Child 2'), + $this->categories('Child 2.1'), + $this->categories('Child 2.1.1'), + $this->categories('Child 2.1.1.1'), + $this->categories('Child 2.1.1.1.1'), + $this->categories('Child 3'), + ], $parent->getDescendants(10)->all()); } public function testDescendantsRecursesChildren() @@ -417,7 +417,7 @@ public function testDescendantsRecursesChildren() $c = Category::create(['name' => 'C']); // a > b > c - $b->makeChildOf($a); + $b->makeChildOf($a); $c->makeChildOf($b); $a->reload(); @@ -590,7 +590,7 @@ public function testToHierarchyReturnsHierarchicalData() public function testToHierarchyNestsCorrectly() { // Prune all categories - Category::query()->delete(); + Category::query()->delete(); // Build a sample tree structure: // @@ -610,7 +610,7 @@ public function testToHierarchyNestsCorrectly() // |- C.2 // - D // - $a = Category::create(['name' => 'A']); + $a = Category::create(['name' => 'A']); $b = Category::create(['name' => 'B']); $c = Category::create(['name' => 'C']); $d = Category::create(['name' => 'D']); @@ -651,9 +651,9 @@ public function testToHierarchyNestsCorrectly() $this->assertTrue(Category::isValidNestedSet()); // Build expectations (expected trees/subtrees) - $expectedWholeTree = [ - 'A' => ['A.1' => null, 'A.2' => null], - 'B' => [ + $expectedWholeTree = [ + 'A' => ['A.1' => null, 'A.2' => null], + 'B' => [ 'B.1' => null, 'B.2' => [ 'B.2.1' => null, @@ -661,15 +661,15 @@ public function testToHierarchyNestsCorrectly() 'B.2.3' => null, ], 'B.3' => null, - ], - 'C' => ['C.1' => null, 'C.2' => null], - 'D' => null, - ]; + ], + 'C' => ['C.1' => null, 'C.2' => null], + 'D' => null, + ]; - $expectedSubtreeA = ['A' => ['A.1' => null, 'A.2' => null]]; + $expectedSubtreeA = ['A' => ['A.1' => null, 'A.2' => null]]; $expectedSubtreeB = [ - 'B' => [ + 'B' => [ 'B.1' => null, 'B.2' => [ 'B.2.1' => null, @@ -677,15 +677,15 @@ public function testToHierarchyNestsCorrectly() 'B.2.3' => null, ], 'B.3' => null, - ], - ]; + ], + ]; $expectedSubtreeC = ['C.1' => null, 'C.2' => null]; $expectedSubtreeD = ['D' => null]; // Perform assertions - $wholeTree = hmap(Category::all()->toHierarchy()->toArray()); + $wholeTree = hmap(Category::all()->toHierarchy()->toArray()); $this->assertArraysAreEqual($expectedWholeTree, $wholeTree); $subtreeA = hmap($this->categories('A')->getDescendantsAndSelf()->toHierarchy()->toArray()); @@ -714,11 +714,11 @@ public function testToHierarchyNestsCorrectlyNotSequential() $this->assertTrue(Category::isValidNestedSet()); $expected = [ - 'Child 1' => [ + 'Child 1' => [ 'Child 1.1' => null, 'Child 1.2' => null, - ], - ]; + ], + ]; $parent->reload(); $this->assertArraysAreEqual($expected, hmap($parent->getDescendantsAndSelf()->toHierarchy()->toArray())); @@ -729,23 +729,23 @@ public function testToHierarchyNestsCorrectlyWithOrder() with(new OrderedCategorySeeder())->run(); $expectedWhole = [ - 'Root A' => null, - 'Root Z' => [ + 'Root A' => null, + 'Root Z' => [ 'Child A' => null, 'Child C' => null, 'Child G' => ['Child G.1' => null], - ], - ]; + ], + ]; $this->assertArraysAreEqual($expectedWhole, hmap(OrderedCategory::all()->toHierarchy()->toArray())); $expectedSubtreeZ = [ - 'Root Z' => [ + 'Root Z' => [ 'Child A' => null, 'Child C' => null, 'Child G' => ['Child G.1' => null], - ], - ]; + ], + ]; $this->assertArraysAreEqual($expectedSubtreeZ, hmap($this->categories('Root Z', 'OrderedCategory')->getDescendantsAndSelf()->toHierarchy()->toArray())); } @@ -755,13 +755,13 @@ public function testGetNestedList() $nestedList = Category::getNestedList('name', 'id', $seperator); $expected = [ - 1 => str_repeat($seperator, 0).'Root 1', - 2 => str_repeat($seperator, 1).'Child 1', - 3 => str_repeat($seperator, 1).'Child 2', - 4 => str_repeat($seperator, 2).'Child 2.1', - 5 => str_repeat($seperator, 1).'Child 3', - 6 => str_repeat($seperator, 0).'Root 2', - ]; + 1 => str_repeat($seperator, 0).'Root 1', + 2 => str_repeat($seperator, 1).'Child 1', + 3 => str_repeat($seperator, 1).'Child 2', + 4 => str_repeat($seperator, 2).'Child 2.1', + 5 => str_repeat($seperator, 1).'Child 3', + 6 => str_repeat($seperator, 0).'Root 2', + ]; $this->assertArraysAreEqual($expected, $nestedList); } @@ -773,13 +773,13 @@ public function testGetNestedListSymbol() $nestedList = Category::getNestedList('name', 'id', $seperator, $symbol); $expected = [ - 1 => str_repeat($seperator, 0).$symbol.'Root 1', - 2 => str_repeat($seperator, 1).$symbol.'Child 1', - 3 => str_repeat($seperator, 1).$symbol.'Child 2', - 4 => str_repeat($seperator, 2).$symbol.'Child 2.1', - 5 => str_repeat($seperator, 1).$symbol.'Child 3', - 6 => str_repeat($seperator, 0).$symbol.'Root 2', - ]; + 1 => str_repeat($seperator, 0).$symbol.'Root 1', + 2 => str_repeat($seperator, 1).$symbol.'Child 1', + 3 => str_repeat($seperator, 1).$symbol.'Child 2', + 4 => str_repeat($seperator, 2).$symbol.'Child 2.1', + 5 => str_repeat($seperator, 1).$symbol.'Child 3', + 6 => str_repeat($seperator, 0).$symbol.'Root 2', + ]; $this->assertArraysAreEqual($expected, $nestedList); } diff --git a/tests/suite/Category/CategoryMovementTest.php b/tests/suite/Category/CategoryMovementTest.php index 997cc568..a4477a20 100644 --- a/tests/suite/Category/CategoryMovementTest.php +++ b/tests/suite/Category/CategoryMovementTest.php @@ -12,7 +12,7 @@ public function testMoveLeft() } /** - * @expectedException Baum\MoveNotPossibleException + * @expectedException \Baum\MoveNotPossibleException */ public function testMoveLeftRaisesAnExceptionWhenNotPossible() { @@ -58,7 +58,7 @@ public function testMoveToLeftOf() } /** - * @expectedException Baum\MoveNotPossibleException + * @expectedException \Baum\MoveNotPossibleException */ public function testMoveToLeftOfRaisesAnExceptionWhenNotPossible() { @@ -99,7 +99,7 @@ public function testMoveRight() } /** - * @expectedException Baum\MoveNotPossibleException + * @expectedException \Baum\MoveNotPossibleException */ public function testMoveRightRaisesAnExceptionWhenNotPossible() { @@ -147,7 +147,7 @@ public function testMoveToRightOf() } /** - * @expectedException Baum\MoveNotPossibleException + * @expectedException \Baum\MoveNotPossibleException */ public function testMoveToRightOfRaisesAnExceptionWhenNotPossible() { @@ -462,7 +462,7 @@ public function testMakeLastChildOfSwappingRootsWithSubtrees() } /** - * @expectedException Baum\MoveNotPossibleException + * @expectedException \Baum\MoveNotPossibleException */ public function testUnpersistedNodeCannotBeMoved() { @@ -472,7 +472,7 @@ public function testUnpersistedNodeCannotBeMoved() } /** - * @expectedException Baum\MoveNotPossibleException + * @expectedException \Baum\MoveNotPossibleException */ public function testUnpersistedNodeCannotBeMadeChild() { @@ -482,7 +482,7 @@ public function testUnpersistedNodeCannotBeMadeChild() } /** - * @expectedException Baum\MoveNotPossibleException + * @expectedException \Baum\MoveNotPossibleException */ public function testNodesCannotBeMovedToItself() { @@ -492,7 +492,7 @@ public function testNodesCannotBeMovedToItself() } /** - * @expectedException Baum\MoveNotPossibleException + * @expectedException \Baum\MoveNotPossibleException */ public function testNodesCannotBeMadeChildOfThemselves() { @@ -502,7 +502,7 @@ public function testNodesCannotBeMadeChildOfThemselves() } /** - * @expectedException Baum\MoveNotPossibleException + * @expectedException \Baum\MoveNotPossibleException */ public function testNodesCannotBeMovedToDescendantsOfThemselves() { diff --git a/tests/suite/Category/CategoryRelationsTest.php b/tests/suite/Category/CategoryRelationsTest.php index 085961e8..a885ff74 100644 --- a/tests/suite/Category/CategoryRelationsTest.php +++ b/tests/suite/Category/CategoryRelationsTest.php @@ -98,7 +98,7 @@ public function testChildrenRelationObeysDefaultOrdering() $this->assertEquals($expected, $children); // Swap 2 nodes & re-test - Category::query()->where('id', '=', 2)->update(['lft' => 8, 'rgt' => 9]); + Category::query()->where('id', '=', 2)->update(['lft' => 8, 'rgt' => 9]); Category::query()->where('id', '=', 5)->update(['lft' => 2, 'rgt' => 3]); $children = $this->categories('Root 1')->children()->get()->all(); diff --git a/tests/suite/Category/CategoryScopedTest.php b/tests/suite/Category/CategoryScopedTest.php index 5506a418..dd2bee4d 100644 --- a/tests/suite/Category/CategoryScopedTest.php +++ b/tests/suite/Category/CategoryScopedTest.php @@ -155,9 +155,9 @@ public function testSimpleSubtreeMovements() $root3->reload(); $expected = [ - $this->categories('Child 5', 'ScopedCategory'), - $this->categories('Child 5.1', 'ScopedCategory'), - ]; + $this->categories('Child 5', 'ScopedCategory'), + $this->categories('Child 5.1', 'ScopedCategory'), + ]; $this->assertEquals($expected, $root3->getDescendants()->all()); } @@ -175,12 +175,12 @@ public function testFullSubtreeMovements() $root3->reload(); $expected = [ - $this->categories('Root 2', 'ScopedCategory'), - $this->categories('Child 4', 'ScopedCategory'), - $this->categories('Child 5', 'ScopedCategory'), - $this->categories('Child 5.1', 'ScopedCategory'), - $this->categories('Child 6', 'ScopedCategory'), - ]; + $this->categories('Root 2', 'ScopedCategory'), + $this->categories('Child 4', 'ScopedCategory'), + $this->categories('Child 5', 'ScopedCategory'), + $this->categories('Child 5.1', 'ScopedCategory'), + $this->categories('Child 6', 'ScopedCategory'), + ]; $this->assertEquals($expected, $root3->getDescendants()->all()); } @@ -211,9 +211,9 @@ public function testSimpleSubtreeMovementsMultiple() $root2->reload(); $expected = [ - $this->categories('Hijo 2', 'MultiScopedCategory'), - $this->categories('Hijo 2.1', 'MultiScopedCategory'), - ]; + $this->categories('Hijo 2', 'MultiScopedCategory'), + $this->categories('Hijo 2.1', 'MultiScopedCategory'), + ]; $this->assertEquals($expected, $root2->getDescendants()->all()); } @@ -229,12 +229,12 @@ public function testFullSubtreeMovementsMultiple() $root2->reload(); $expected = [ - $this->categories('Raiz 1', 'MultiScopedCategory'), - $this->categories('Hijo 1', 'MultiScopedCategory'), - $this->categories('Hijo 2', 'MultiScopedCategory'), - $this->categories('Hijo 2.1', 'MultiScopedCategory'), - $this->categories('Hijo 3', 'MultiScopedCategory'), - ]; + $this->categories('Raiz 1', 'MultiScopedCategory'), + $this->categories('Hijo 1', 'MultiScopedCategory'), + $this->categories('Hijo 2', 'MultiScopedCategory'), + $this->categories('Hijo 2.1', 'MultiScopedCategory'), + $this->categories('Hijo 3', 'MultiScopedCategory'), + ]; $this->assertEquals($expected, $root2->getDescendants()->all()); } @@ -243,95 +243,95 @@ public function testToHierarchyNestsCorrectlyWithScopedOrder() with(new OrderedScopedCategorySeeder())->run(); $expectedWhole1 = [ - 'Root 1' => [ + 'Root 1' => [ 'Child 1' => null, 'Child 2' => [ 'Child 2.1' => null, ], 'Child 3' => null, - ], - ]; + ], + ]; $expectedWhole2 = [ - 'Root 2' => [ + 'Root 2' => [ 'Child 4' => null, 'Child 5' => [ 'Child 5.1' => null, ], 'Child 6' => null, - ], - ]; + ], + ]; $this->assertArraysAreEqual($expectedWhole1, hmap(OrderedScopedCategory::where('company_id', 1)->get()->toHierarchy()->toArray())); $this->assertArraysAreEqual($expectedWhole2, hmap(OrderedScopedCategory::where('company_id', 2)->get()->toHierarchy()->toArray())); } /** - * @expectedException Baum\MoveNotPossibleException + * @expectedException \Baum\MoveNotPossibleException */ - public function testNodesCannotMoveBetweenScopes() - { - $child4 = $this->categories('Child 4', 'ScopedCategory'); - $root1 = $this->categories('Root 1', 'ScopedCategory'); + public function testNodesCannotMoveBetweenScopes() + { + $child4 = $this->categories('Child 4', 'ScopedCategory'); + $root1 = $this->categories('Root 1', 'ScopedCategory'); - $child4->makeChildOf($root1); - } + $child4->makeChildOf($root1); + } /** - * @expectedException Baum\MoveNotPossibleException + * @expectedException \Baum\MoveNotPossibleException */ - public function testNodesCannotMoveBetweenScopesMultiple() - { - $root1 = $this->categories('Root 1', 'MultiScopedCategory'); - $child4 = $this->categories('Child 4', 'MultiScopedCategory'); + public function testNodesCannotMoveBetweenScopesMultiple() + { + $root1 = $this->categories('Root 1', 'MultiScopedCategory'); + $child4 = $this->categories('Child 4', 'MultiScopedCategory'); - $child4->makeChildOf($root1); - } + $child4->makeChildOf($root1); + } /** - * @expectedException Baum\MoveNotPossibleException + * @expectedException \Baum\MoveNotPossibleException */ - public function testNodesCannotMoveBetweenScopesMultiple2() - { - $root1 = $this->categories('Racine 1', 'MultiScopedCategory'); - $child2 = $this->categories('Hijo 2', 'MultiScopedCategory'); + public function testNodesCannotMoveBetweenScopesMultiple2() + { + $root1 = $this->categories('Racine 1', 'MultiScopedCategory'); + $child2 = $this->categories('Hijo 2', 'MultiScopedCategory'); - $child2->makeChildOf($root1); - } + $child2->makeChildOf($root1); + } // TODO: Moving nodes between scopes is problematic ATM. Fix it or find a work-around. - public function testMoveNodeBetweenScopes() - { - $this->markTestSkipped(); + public function testMoveNodeBetweenScopes() + { + $this->markTestSkipped(); - // $root1 = Menu::create(array('caption' => 'TL1', 'site_id' => 1, 'language' => 'en')); - // $child11 = Menu::create(array('caption' => 'C11', 'site_id' => 1, 'language' => 'en')); - // $child12 = Menu::create(array('caption' => 'C12', 'site_id' => 1, 'language' => 'en')); + // $root1 = Menu::create(array('caption' => 'TL1', 'site_id' => 1, 'language' => 'en')); + // $child11 = Menu::create(array('caption' => 'C11', 'site_id' => 1, 'language' => 'en')); + // $child12 = Menu::create(array('caption' => 'C12', 'site_id' => 1, 'language' => 'en')); - // $this->assertTrue(Menu::isValidNestedSet()); + // $this->assertTrue(Menu::isValidNestedSet()); - // $child11->makeChildOf($root1); - // $child12->makeChildOf($root1); + // $child11->makeChildOf($root1); + // $child12->makeChildOf($root1); - // $this->assertTrue(Menu::isValidNestedSet()); + // $this->assertTrue(Menu::isValidNestedSet()); - // $root2 = Menu::create(array('caption' => 'TL2', 'site_id' => 2, 'language' => 'en')); - // $child21 = Menu::create(array('caption' => 'C21', 'site_id' => 2, 'language' => 'en')); - // $child22 = Menu::create(array('caption' => 'C22', 'site_id' => 2, 'language' => 'en')); - // $child21->makeChildOf($root2); - // $child22->makeChildOf($root2); + // $root2 = Menu::create(array('caption' => 'TL2', 'site_id' => 2, 'language' => 'en')); + // $child21 = Menu::create(array('caption' => 'C21', 'site_id' => 2, 'language' => 'en')); + // $child22 = Menu::create(array('caption' => 'C22', 'site_id' => 2, 'language' => 'en')); + // $child21->makeChildOf($root2); + // $child22->makeChildOf($root2); - // $this->assertTrue(Menu::isValidNestedSet()); + // $this->assertTrue(Menu::isValidNestedSet()); - // $child11->update(array('site_id' => 2)); - // $child11->makeChildOf($root2); + // $child11->update(array('site_id' => 2)); + // $child11->makeChildOf($root2); - // $this->assertTrue(Menu::isValidNestedSet()); + // $this->assertTrue(Menu::isValidNestedSet()); - // $expected = array($this->menus('C12')); - // $this->assertEquals($expected, $root1->children()->get()->all()); + // $expected = array($this->menus('C12')); + // $this->assertEquals($expected, $root1->children()->get()->all()); - // $expected = array($this->menus('C21'), $this->menus('C22'), $this->menus('C11')); - // $this->assertEquals($expected, $root2->children()->get()->all()); - } + // $expected = array($this->menus('C21'), $this->menus('C22'), $this->menus('C11')); + // $this->assertEquals($expected, $root2->children()->get()->all()); + } } diff --git a/tests/suite/Category/CategorySoftDeletesTest.php b/tests/suite/Category/CategorySoftDeletesTest.php index 2b8f9b89..f9ca434f 100644 --- a/tests/suite/Category/CategorySoftDeletesTest.php +++ b/tests/suite/Category/CategorySoftDeletesTest.php @@ -37,9 +37,9 @@ public function testDeleteMaintainsTreeValidWithSubtrees() $this->assertTrue(SoftCategory::isValidNestedSet()); $expected = [ - $this->categories('Child 1', 'SoftCategory'), - $this->categories('Child 3', 'SoftCategory'), - ]; + $this->categories('Child 1', 'SoftCategory'), + $this->categories('Child 3', 'SoftCategory'), + ]; $this->assertEquals($expected, $this->categories('Root 1', 'SoftCategory')->getDescendants()->all()); } @@ -52,10 +52,10 @@ public function testDeleteShiftsIndexes() $this->assertTrue(SoftCategory::isValidNestedSet()); $expected = [ - $this->categories('Child 2', 'SoftCategory'), - $this->categories('Child 2.1', 'SoftCategory'), - $this->categories('Child 3', 'SoftCategory'), - ]; + $this->categories('Child 2', 'SoftCategory'), + $this->categories('Child 2.1', 'SoftCategory'), + $this->categories('Child 3', 'SoftCategory'), + ]; $this->assertEquals($expected, $this->categories('Root 1', 'SoftCategory')->getDescendants()->all()); $this->assertEquals(1, $this->categories('Root 1', 'SoftCategory')->getLeft()); @@ -80,9 +80,9 @@ public function testDeleteShiftsIndexesSubtree() $this->assertTrue(SoftCategory::isValidNestedSet()); $expected = [ - $this->categories('Child 1', 'SoftCategory'), - $this->categories('Child 3', 'SoftCategory'), - ]; + $this->categories('Child 1', 'SoftCategory'), + $this->categories('Child 3', 'SoftCategory'), + ]; $this->assertEquals($expected, $this->categories('Root 1', 'SoftCategory')->getDescendants()->all()); $this->assertEquals(1, $this->categories('Root 1', 'SoftCategory')->getLeft()); @@ -138,11 +138,11 @@ public function testRestoreMaintainsTreeValidWithSubtrees() $this->assertTrue(SoftCategory::isValidNestedSet()); $expected = [ - $this->categories('Child 1', 'SoftCategory'), - $this->categories('Child 2', 'SoftCategory'), - $this->categories('Child 2.1', 'SoftCategory'), - $this->categories('Child 3', 'SoftCategory'), - ]; + $this->categories('Child 1', 'SoftCategory'), + $this->categories('Child 2', 'SoftCategory'), + $this->categories('Child 2.1', 'SoftCategory'), + $this->categories('Child 3', 'SoftCategory'), + ]; $this->assertEquals($expected, $this->categories('Root 1', 'SoftCategory')->getDescendants()->all()); } @@ -159,11 +159,11 @@ public function testRestoreUnshiftsIndexes() $this->assertTrue(SoftCategory::isValidNestedSet()); $expected = [ - $this->categories('Child 1', 'SoftCategory'), - $this->categories('Child 2', 'SoftCategory'), - $this->categories('Child 2.1', 'SoftCategory'), - $this->categories('Child 3', 'SoftCategory'), - ]; + $this->categories('Child 1', 'SoftCategory'), + $this->categories('Child 2', 'SoftCategory'), + $this->categories('Child 2.1', 'SoftCategory'), + $this->categories('Child 3', 'SoftCategory'), + ]; $this->assertEquals($expected, $this->categories('Root 1', 'SoftCategory')->getDescendants()->all()); $this->assertEquals(1, $this->categories('Root 1', 'SoftCategory')->getLeft()); @@ -194,11 +194,11 @@ public function testRestoreUnshiftsIndexesSubtree() $this->assertTrue(SoftCategory::isValidNestedSet()); $expected = [ - $this->categories('Child 1', 'SoftCategory'), - $this->categories('Child 2', 'SoftCategory'), - $this->categories('Child 2.1', 'SoftCategory'), - $this->categories('Child 3', 'SoftCategory'), - ]; + $this->categories('Child 1', 'SoftCategory'), + $this->categories('Child 2', 'SoftCategory'), + $this->categories('Child 2.1', 'SoftCategory'), + $this->categories('Child 3', 'SoftCategory'), + ]; $this->assertEquals($expected, $this->categories('Root 1', 'SoftCategory')->getDescendants()->all()); $this->assertEquals(1, $this->categories('Root 1', 'SoftCategory')->getLeft()); @@ -229,11 +229,11 @@ public function testRestoreUnshiftsIndexesFullSubtree() $this->assertTrue(SoftCategory::isValidNestedSet()); $expected = [ - $this->categories('Child 1', 'SoftCategory'), - $this->categories('Child 2', 'SoftCategory'), - $this->categories('Child 2.1', 'SoftCategory'), - $this->categories('Child 3', 'SoftCategory'), - ]; + $this->categories('Child 1', 'SoftCategory'), + $this->categories('Child 2', 'SoftCategory'), + $this->categories('Child 2.1', 'SoftCategory'), + $this->categories('Child 3', 'SoftCategory'), + ]; $this->assertEquals($expected, $this->categories('Root 1', 'SoftCategory')->getDescendants()->all()); $this->assertEquals(1, $this->categories('Root 1', 'SoftCategory')->getLeft()); diff --git a/tests/suite/Category/CategoryTreeMapperTest.php b/tests/suite/Category/CategoryTreeMapperTest.php index 69acd0df..0a12a84b 100644 --- a/tests/suite/Category/CategoryTreeMapperTest.php +++ b/tests/suite/Category/CategoryTreeMapperTest.php @@ -12,18 +12,18 @@ public function setUp() public function testBuildTree() { $tree = [ - ['id' => 1, 'name' => 'A'], - ['id' => 2, 'name' => 'B'], - ['id' => 3, 'name' => 'C', 'children' => [ + ['id' => 1, 'name' => 'A'], + ['id' => 2, 'name' => 'B'], + ['id' => 3, 'name' => 'C', 'children' => [ ['id' => 4, 'name' => 'C.1', 'children' => [ ['id' => 5, 'name' => 'C.1.1'], ['id' => 6, 'name' => 'C.1.2'], ]], ['id' => 7, 'name' => 'C.2'], ['id' => 8, 'name' => 'C.3'], - ]], - ['id' => 9, 'name' => 'D'], - ]; + ]], + ['id' => 9, 'name' => 'D'], + ]; $this->assertTrue(Category::buildTree($tree)); $this->assertTrue(Category::isValidNestedSet()); @@ -35,32 +35,32 @@ public function testBuildTree() public function testBuildTreePrunesAndInserts() { $tree = [ - ['id' => 1, 'name' => 'A'], - ['id' => 2, 'name' => 'B'], - ['id' => 3, 'name' => 'C', 'children' => [ + ['id' => 1, 'name' => 'A'], + ['id' => 2, 'name' => 'B'], + ['id' => 3, 'name' => 'C', 'children' => [ ['id' => 4, 'name' => 'C.1', 'children' => [ ['id' => 5, 'name' => 'C.1.1'], ['id' => 6, 'name' => 'C.1.2'], ]], ['id' => 7, 'name' => 'C.2'], ['id' => 8, 'name' => 'C.3'], - ]], - ['id' => 9, 'name' => 'D'], - ]; + ]], + ['id' => 9, 'name' => 'D'], + ]; $this->assertTrue(Category::buildTree($tree)); $this->assertTrue(Category::isValidNestedSet()); // Postgres fix - if (DB::connection()->getDriverName() === 'pgsql') { - $tablePrefix = DB::connection()->getTablePrefix(); - $sequenceName = $tablePrefix.'categories_id_seq'; - DB::connection()->statement('ALTER SEQUENCE '.$sequenceName.' RESTART WITH 10'); - } + if (DB::connection()->getDriverName() === 'pgsql') { + $tablePrefix = DB::connection()->getTablePrefix(); + $sequenceName = $tablePrefix.'categories_id_seq'; + DB::connection()->statement('ALTER SEQUENCE '.$sequenceName.' RESTART WITH 10'); + } $updated = [ - ['id' => 1, 'name' => 'A'], - ['id' => 2, 'name' => 'B'], - ['id' => 3, 'name' => 'C', 'children' => [ + ['id' => 1, 'name' => 'A'], + ['id' => 2, 'name' => 'B'], + ['id' => 3, 'name' => 'C', 'children' => [ ['id' => 4, 'name' => 'C.1', 'children' => [ ['id' => 5, 'name' => 'C.1.1'], ['id' => 6, 'name' => 'C.1.2'], @@ -69,16 +69,16 @@ public function testBuildTreePrunesAndInserts() ['name' => 'C.2.1'], ['name' => 'C.2.2'], ]], - ]], - ['id' => 9, 'name' => 'D'], - ]; + ]], + ['id' => 9, 'name' => 'D'], + ]; $this->assertTrue(Category::buildTree($updated)); $this->assertTrue(Category::isValidNestedSet()); $expected = [ - ['id' => 1, 'name' => 'A'], - ['id' => 2, 'name' => 'B'], - ['id' => 3, 'name' => 'C', 'children' => [ + ['id' => 1, 'name' => 'A'], + ['id' => 2, 'name' => 'B'], + ['id' => 3, 'name' => 'C', 'children' => [ ['id' => 4, 'name' => 'C.1', 'children' => [ ['id' => 5, 'name' => 'C.1.1'], ['id' => 6, 'name' => 'C.1.2'], @@ -87,9 +87,9 @@ public function testBuildTreePrunesAndInserts() ['id' => 10, 'name' => 'C.2.1'], ['id' => 11, 'name' => 'C.2.2'], ]], - ]], - ['id' => 9, 'name' => 'D'], - ]; + ]], + ['id' => 9, 'name' => 'D'], + ]; $hierarchy = Category::all()->toHierarchy()->toArray(); $this->assertArraysAreEqual($expected, array_ints_keys(hmap($hierarchy, ['id', 'name']))); @@ -98,26 +98,26 @@ public function testBuildTreePrunesAndInserts() public function testBuildTreeMoveNodes() { // Create a tree - $updated = [ - ['id' => 1, 'name' => 'A'], - ['id' => 2, 'name' => 'B'], - ['id' => 3, 'name' => 'C', 'children' => [ + $updated = [ + ['id' => 1, 'name' => 'A'], + ['id' => 2, 'name' => 'B'], + ['id' => 3, 'name' => 'C', 'children' => [ ['id' => 4, 'name' => 'C.1', 'children' => [ ['id' => 5, 'name' => 'C.1.1'], ['id' => 6, 'name' => 'C.1.2'], ]], ['id' => 7, 'name' => 'C.2'], - ]], - ['id' => 9, 'name' => 'D'], - ]; + ]], + ['id' => 9, 'name' => 'D'], + ]; $this->assertTrue(Category::buildTree($updated)); $this->assertTrue(Category::isValidNestedSet()); // Add some nodes - $updated = [ - ['id' => 1, 'name' => 'A'], - ['id' => 2, 'name' => 'B'], - ['id' => 3, 'name' => 'C', 'children' => [ + $updated = [ + ['id' => 1, 'name' => 'A'], + ['id' => 2, 'name' => 'B'], + ['id' => 3, 'name' => 'C', 'children' => [ ['id' => 4, 'name' => 'C.1', 'children' => [ ['id' => 5, 'name' => 'C.1.1'], ['id' => 6, 'name' => 'C.1.2'], @@ -126,29 +126,29 @@ public function testBuildTreeMoveNodes() ['name' => 'C.2.1'], ['name' => 'C.2.2'], ]], - ]], - ['id' => 9, 'name' => 'D'], - ]; + ]], + ['id' => 9, 'name' => 'D'], + ]; $this->assertTrue(Category::buildTree($updated)); $this->assertTrue(Category::isValidNestedSet()); // Move node 7 to be child of node 2 - $updated = [ - ['id' => 1, 'name' => 'A'], - ['id' => 2, 'name' => 'B', 'children' => [ + $updated = [ + ['id' => 1, 'name' => 'A'], + ['id' => 2, 'name' => 'B', 'children' => [ ['id' => 7, 'name' => 'C.2', 'children' => [ ['id' => 10, 'name' => 'C.2.1'], ['id' => 11, 'name' => 'C.2.2'], ]], - ]], - ['id' => 3, 'name' => 'C', 'children' => [ + ]], + ['id' => 3, 'name' => 'C', 'children' => [ ['id' => 4, 'name' => 'C.1', 'children' => [ ['id' => 5, 'name' => 'C.1.1'], ['id' => 6, 'name' => 'C.1.2'], ]], - ]], - ['id' => 9, 'name' => 'D'], - ]; + ]], + ['id' => 9, 'name' => 'D'], + ]; $this->assertTrue(Category::buildTree($updated)); $this->assertTrue(Category::isValidNestedSet()); @@ -163,35 +163,35 @@ public function testMakeTree() $parent = Category::find(3); $subtree = [ - ['id' => 4, 'name' => 'Child 2.1'], - ['name' => 'Child 2.2'], - ['name' => 'Child 2.3', 'children' => [ + ['id' => 4, 'name' => 'Child 2.1'], + ['name' => 'Child 2.2'], + ['name' => 'Child 2.3', 'children' => [ ['name' => 'Child 2.3.1', 'children' => [ ['name' => 'Child 2.3.1.1'], ['name' => 'Child 2.3.1.1'], ]], ['name' => 'Child 2.3.2'], ['name' => 'Child 2.3.3'], - ]], - ['name' => 'Child 2.4'], - ]; + ]], + ['name' => 'Child 2.4'], + ]; $this->assertTrue($parent->makeTree($subtree)); $this->assertTrue(Category::isValidNestedSet()); $expected = [ - ['id' => 4, 'name' => 'Child 2.1'], - ['id' => 7, 'name' => 'Child 2.2'], - ['id' => 8, 'name' => 'Child 2.3', 'children' => [ + ['id' => 4, 'name' => 'Child 2.1'], + ['id' => 7, 'name' => 'Child 2.2'], + ['id' => 8, 'name' => 'Child 2.3', 'children' => [ ['id' => 9, 'name' => 'Child 2.3.1', 'children' => [ ['id' => 10, 'name' => 'Child 2.3.1.1'], ['id' => 11, 'name' => 'Child 2.3.1.1'], ]], ['id' => 12, 'name' => 'Child 2.3.2'], ['id' => 13, 'name' => 'Child 2.3.3'], - ]], - ['id' => 14, 'name' => 'Child 2.4'], - ]; + ]], + ['id' => 14, 'name' => 'Child 2.4'], + ]; $hierarchy = $parent->reload()->getDescendants()->toHierarchy()->toArray(); $this->assertArraysAreEqual($expected, array_ints_keys(hmap($hierarchy, ['id', 'name']))); @@ -204,69 +204,69 @@ public function testMakeTreePrunesAndInserts() $parent = Category::find(3); $subtree = [ - ['id' => 4, 'name' => 'Child 2.1'], - ['name' => 'Child 2.2'], - ['name' => 'Child 2.3', 'children' => [ + ['id' => 4, 'name' => 'Child 2.1'], + ['name' => 'Child 2.2'], + ['name' => 'Child 2.3', 'children' => [ ['name' => 'Child 2.3.1', 'children' => [ ['name' => 'Child 2.3.1.1'], ['name' => 'Child 2.3.1.1'], ]], ['name' => 'Child 2.3.2'], ['name' => 'Child 2.3.3'], - ]], - ['name' => 'Child 2.4'], - ]; + ]], + ['name' => 'Child 2.4'], + ]; $this->assertTrue($parent->makeTree($subtree)); $this->assertTrue(Category::isValidNestedSet()); $expected = [ - ['id' => 4, 'name' => 'Child 2.1'], - ['id' => 7, 'name' => 'Child 2.2'], - ['id' => 8, 'name' => 'Child 2.3', 'children' => [ + ['id' => 4, 'name' => 'Child 2.1'], + ['id' => 7, 'name' => 'Child 2.2'], + ['id' => 8, 'name' => 'Child 2.3', 'children' => [ ['id' => 9, 'name' => 'Child 2.3.1', 'children' => [ ['id' => 10, 'name' => 'Child 2.3.1.1'], ['id' => 11, 'name' => 'Child 2.3.1.1'], ]], ['id' => 12, 'name' => 'Child 2.3.2'], ['id' => 13, 'name' => 'Child 2.3.3'], - ]], - ['id' => 14, 'name' => 'Child 2.4'], - ]; + ]], + ['id' => 14, 'name' => 'Child 2.4'], + ]; $hierarchy = $parent->reload()->getDescendants()->toHierarchy()->toArray(); $this->assertArraysAreEqual($expected, array_ints_keys(hmap($hierarchy, ['id', 'name']))); $modified = [ - ['id' => 7, 'name' => 'Child 2.2'], - ['id' => 8, 'name' => 'Child 2.3'], - ['id' => 14, 'name' => 'Child 2.4'], - ['name' => 'Child 2.5', 'children' => [ + ['id' => 7, 'name' => 'Child 2.2'], + ['id' => 8, 'name' => 'Child 2.3'], + ['id' => 14, 'name' => 'Child 2.4'], + ['name' => 'Child 2.5', 'children' => [ ['name' => 'Child 2.5.1', 'children' => [ ['name' => 'Child 2.5.1.1'], ['name' => 'Child 2.5.1.1'], ]], ['name' => 'Child 2.5.2'], ['name' => 'Child 2.5.3'], - ]], - ]; + ]], + ]; $this->assertTrue($parent->makeTree($modified)); $this->assertTrue(Category::isValidNestedSet()); $expected = [ - ['id' => 7, 'name' => 'Child 2.2'], - ['id' => 8, 'name' => 'Child 2.3'], - ['id' => 14, 'name' => 'Child 2.4'], - ['id' => 15, 'name' => 'Child 2.5', 'children' => [ + ['id' => 7, 'name' => 'Child 2.2'], + ['id' => 8, 'name' => 'Child 2.3'], + ['id' => 14, 'name' => 'Child 2.4'], + ['id' => 15, 'name' => 'Child 2.5', 'children' => [ ['id' => 16, 'name' => 'Child 2.5.1', 'children' => [ ['id' => 17, 'name' => 'Child 2.5.1.1'], ['id' => 18, 'name' => 'Child 2.5.1.1'], ]], ['id' => 19, 'name' => 'Child 2.5.2'], ['id' => 20, 'name' => 'Child 2.5.3'], - ]], - ]; + ]], + ]; $hierarchy = $parent->reload()->getDescendants()->toHierarchy()->toArray(); $this->assertArraysAreEqual($expected, array_ints_keys(hmap($hierarchy, ['id', 'name']))); diff --git a/tests/suite/Category/CategoryTreeRebuildingTest.php b/tests/suite/Category/CategoryTreeRebuildingTest.php index 2922c673..089400c1 100644 --- a/tests/suite/Category/CategoryTreeRebuildingTest.php +++ b/tests/suite/Category/CategoryTreeRebuildingTest.php @@ -68,9 +68,9 @@ public function testRebuildWithScope() $this->assertEquals($root->getAttributes(), $this->categories('A', 'MultiScopedCategory')->getAttributes()); // Compare attributes, not objects - $expected = array_map(function ($item) { - return $item->getAttributes(); - }, [$child1, $child2]); + $expected = array_map(function ($item) { + return $item->getAttributes(); + }, [$child1, $child2]); $children = $this->categories('A', 'MultiScopedCategory')->children()->get()->all(); $children = array_map(function ($item) { diff --git a/tests/suite/Category/CategoryTreeValidationTest.php b/tests/suite/Category/CategoryTreeValidationTest.php index d42a2769..3954b0cf 100644 --- a/tests/suite/Category/CategoryTreeValidationTest.php +++ b/tests/suite/Category/CategoryTreeValidationTest.php @@ -64,7 +64,7 @@ public function testTreeIsNotValidWithOverlappingRoots() $this->assertTrue(Category::isValidNestedSet()); // Force Root 2 to overlap with Root 1 - $root = $this->categories('Root 2'); + $root = $this->categories('Root 2'); $root->lft = 0; $root->save(); diff --git a/tests/suite/Cluster/ClusterHierarchyTest.php b/tests/suite/Cluster/ClusterHierarchyTest.php index 33d2edfb..03e35be1 100644 --- a/tests/suite/Cluster/ClusterHierarchyTest.php +++ b/tests/suite/Cluster/ClusterHierarchyTest.php @@ -37,7 +37,7 @@ public function testRootsStaticWithCustomOrder() $cluster = OrderedCluster::create(['name' => 'A new root is born']); $cluster->syncOriginal(); // ¿? --> This should be done already !? - $roots = OrderedCluster::roots()->get(); + $roots = OrderedCluster::roots()->get(); $this->assertCount(3, $roots); $this->assertEquals($cluster->getAttributes(), $roots->first()->getAttributes()); @@ -123,10 +123,10 @@ public function testGetLeavesInIteration() $node = $this->clusters('Root 1'); $expectedIds = [ - '5d7ce1fd-6151-46d3-a5b3-0ebb9988dc57', - '3315a297-af87-4ad3-9fa5-19785407573d', - '054476d2-6830-4014-a181-4de010ef7114', - ]; + '5d7ce1fd-6151-46d3-a5b3-0ebb9988dc57', + '3315a297-af87-4ad3-9fa5-19785407573d', + '054476d2-6830-4014-a181-4de010ef7114', + ]; foreach ($node->getLeaves() as $i => $leaf) { $this->assertEquals($expectedIds[$i], $leaf->getKey()); @@ -267,12 +267,12 @@ public function testGetDescendantsAndSelf() $parent = $this->clusters('Root 1'); $expected = [ - $parent, - $this->clusters('Child 1'), - $this->clusters('Child 2'), - $this->clusters('Child 2.1'), - $this->clusters('Child 3'), - ]; + $parent, + $this->clusters('Child 1'), + $this->clusters('Child 2'), + $this->clusters('Child 2.1'), + $this->clusters('Child 3'), + ]; $this->assertCount(count($expected), $parent->getDescendantsAndSelf()); @@ -288,49 +288,49 @@ public function testGetDescendantsAndSelfWithLimit() $this->assertEquals([$parent], $parent->getDescendantsAndSelf(0)->all()); $this->assertEquals([ - $parent, - $this->clusters('Child 1'), - $this->clusters('Child 2'), - $this->clusters('Child 3'), - ], $parent->getDescendantsAndSelf(1)->all()); + $parent, + $this->clusters('Child 1'), + $this->clusters('Child 2'), + $this->clusters('Child 3'), + ], $parent->getDescendantsAndSelf(1)->all()); $this->assertEquals([ - $parent, - $this->clusters('Child 1'), - $this->clusters('Child 2'), - $this->clusters('Child 2.1'), - $this->clusters('Child 3'), - ], $parent->getDescendantsAndSelf(2)->all()); + $parent, + $this->clusters('Child 1'), + $this->clusters('Child 2'), + $this->clusters('Child 2.1'), + $this->clusters('Child 3'), + ], $parent->getDescendantsAndSelf(2)->all()); $this->assertEquals([ - $parent, - $this->clusters('Child 1'), - $this->clusters('Child 2'), - $this->clusters('Child 2.1'), - $this->clusters('Child 2.1.1'), - $this->clusters('Child 3'), - ], $parent->getDescendantsAndSelf(3)->all()); + $parent, + $this->clusters('Child 1'), + $this->clusters('Child 2'), + $this->clusters('Child 2.1'), + $this->clusters('Child 2.1.1'), + $this->clusters('Child 3'), + ], $parent->getDescendantsAndSelf(3)->all()); $this->assertEquals([ - $parent, - $this->clusters('Child 1'), - $this->clusters('Child 2'), - $this->clusters('Child 2.1'), - $this->clusters('Child 2.1.1'), - $this->clusters('Child 2.1.1.1'), - $this->clusters('Child 3'), - ], $parent->getDescendantsAndSelf(4)->all()); + $parent, + $this->clusters('Child 1'), + $this->clusters('Child 2'), + $this->clusters('Child 2.1'), + $this->clusters('Child 2.1.1'), + $this->clusters('Child 2.1.1.1'), + $this->clusters('Child 3'), + ], $parent->getDescendantsAndSelf(4)->all()); $this->assertEquals([ - $parent, - $this->clusters('Child 1'), - $this->clusters('Child 2'), - $this->clusters('Child 2.1'), - $this->clusters('Child 2.1.1'), - $this->clusters('Child 2.1.1.1'), - $this->clusters('Child 2.1.1.1.1'), - $this->clusters('Child 3'), - ], $parent->getDescendantsAndSelf(10)->all()); + $parent, + $this->clusters('Child 1'), + $this->clusters('Child 2'), + $this->clusters('Child 2.1'), + $this->clusters('Child 2.1.1'), + $this->clusters('Child 2.1.1.1'), + $this->clusters('Child 2.1.1.1.1'), + $this->clusters('Child 3'), + ], $parent->getDescendantsAndSelf(10)->all()); } public function testGetDescendants() @@ -338,11 +338,11 @@ public function testGetDescendants() $parent = $this->clusters('Root 1'); $expected = [ - $this->clusters('Child 1'), - $this->clusters('Child 2'), - $this->clusters('Child 2.1'), - $this->clusters('Child 3'), - ]; + $this->clusters('Child 1'), + $this->clusters('Child 2'), + $this->clusters('Child 2.1'), + $this->clusters('Child 3'), + ]; $this->assertCount(count($expected), $parent->getDescendants()); @@ -358,54 +358,54 @@ public function testGetDescendantsWithLimit() $this->assertEmpty($parent->getDescendants(0)->all()); $this->assertEquals([ - $this->clusters('Child 1'), - $this->clusters('Child 2'), - $this->clusters('Child 3'), - ], $parent->getDescendants(1)->all()); + $this->clusters('Child 1'), + $this->clusters('Child 2'), + $this->clusters('Child 3'), + ], $parent->getDescendants(1)->all()); $this->assertEquals([ - $this->clusters('Child 1'), - $this->clusters('Child 2'), - $this->clusters('Child 2.1'), - $this->clusters('Child 3'), - ], $parent->getDescendants(2)->all()); + $this->clusters('Child 1'), + $this->clusters('Child 2'), + $this->clusters('Child 2.1'), + $this->clusters('Child 3'), + ], $parent->getDescendants(2)->all()); $this->assertEquals([ - $this->clusters('Child 1'), - $this->clusters('Child 2'), - $this->clusters('Child 2.1'), - $this->clusters('Child 2.1.1'), - $this->clusters('Child 3'), - ], $parent->getDescendants(3)->all()); + $this->clusters('Child 1'), + $this->clusters('Child 2'), + $this->clusters('Child 2.1'), + $this->clusters('Child 2.1.1'), + $this->clusters('Child 3'), + ], $parent->getDescendants(3)->all()); $this->assertEquals([ - $this->clusters('Child 1'), - $this->clusters('Child 2'), - $this->clusters('Child 2.1'), - $this->clusters('Child 2.1.1'), - $this->clusters('Child 2.1.1.1'), - $this->clusters('Child 3'), - ], $parent->getDescendants(4)->all()); + $this->clusters('Child 1'), + $this->clusters('Child 2'), + $this->clusters('Child 2.1'), + $this->clusters('Child 2.1.1'), + $this->clusters('Child 2.1.1.1'), + $this->clusters('Child 3'), + ], $parent->getDescendants(4)->all()); $this->assertEquals([ - $this->clusters('Child 1'), - $this->clusters('Child 2'), - $this->clusters('Child 2.1'), - $this->clusters('Child 2.1.1'), - $this->clusters('Child 2.1.1.1'), - $this->clusters('Child 2.1.1.1.1'), - $this->clusters('Child 3'), - ], $parent->getDescendants(5)->all()); + $this->clusters('Child 1'), + $this->clusters('Child 2'), + $this->clusters('Child 2.1'), + $this->clusters('Child 2.1.1'), + $this->clusters('Child 2.1.1.1'), + $this->clusters('Child 2.1.1.1.1'), + $this->clusters('Child 3'), + ], $parent->getDescendants(5)->all()); $this->assertEquals([ - $this->clusters('Child 1'), - $this->clusters('Child 2'), - $this->clusters('Child 2.1'), - $this->clusters('Child 2.1.1'), - $this->clusters('Child 2.1.1.1'), - $this->clusters('Child 2.1.1.1.1'), - $this->clusters('Child 3'), - ], $parent->getDescendants(10)->all()); + $this->clusters('Child 1'), + $this->clusters('Child 2'), + $this->clusters('Child 2.1'), + $this->clusters('Child 2.1.1'), + $this->clusters('Child 2.1.1.1'), + $this->clusters('Child 2.1.1.1.1'), + $this->clusters('Child 3'), + ], $parent->getDescendants(10)->all()); } public function testDescendantsRecursesChildren() @@ -415,7 +415,7 @@ public function testDescendantsRecursesChildren() $c = Cluster::create(['name' => 'C']); // a > b > c - $b->makeChildOf($a); + $b->makeChildOf($a); $c->makeChildOf($b); $a->reload(); @@ -458,14 +458,14 @@ public function testIsAncestorOf() $this->assertFalse($this->clusters('Child 1')->isAncestorOf($this->clusters('Child 1'))); } - public function testIsChildOf() - { + public function testIsChildOf() + { $this->assertTrue($this->clusters('Child 1')->isChildOf($this->clusters('Root 1'))); $this->assertTrue($this->clusters('Child 2')->isChildOf($this->clusters('Root 1'))); $this->assertTrue($this->clusters('Child 2.1')->isChildOf($this->clusters('Child 2'))); $this->assertFalse($this->clusters('Child 2.1')->isChildOf($this->clusters('Root 1'))); $this->assertFalse($this->clusters('Child 2.1')->isChildOf($this->clusters('Child 1'))); - } + } public function testIsSelfOrDescendantOf() { @@ -597,7 +597,7 @@ public function testToHierarchyReturnsHierarchicalData() public function testToHierarchyNestsCorrectly() { // Prune all categories - Cluster::query()->delete(); + Cluster::query()->delete(); // Build a sample tree structure: // @@ -617,7 +617,7 @@ public function testToHierarchyNestsCorrectly() // |- C.2 // - D // - $a = Cluster::create(['name' => 'A']); + $a = Cluster::create(['name' => 'A']); $b = Cluster::create(['name' => 'B']); $c = Cluster::create(['name' => 'C']); $d = Cluster::create(['name' => 'D']); @@ -658,9 +658,9 @@ public function testToHierarchyNestsCorrectly() $this->assertTrue(Cluster::isValidNestedSet()); // Build expectations (expected trees/subtrees) - $expectedWholeTree = [ - 'A' => ['A.1' => null, 'A.2' => null], - 'B' => [ + $expectedWholeTree = [ + 'A' => ['A.1' => null, 'A.2' => null], + 'B' => [ 'B.1' => null, 'B.2' => [ 'B.2.1' => null, @@ -668,15 +668,15 @@ public function testToHierarchyNestsCorrectly() 'B.2.3' => null, ], 'B.3' => null, - ], - 'C' => ['C.1' => null, 'C.2' => null], - 'D' => null, - ]; + ], + 'C' => ['C.1' => null, 'C.2' => null], + 'D' => null, + ]; - $expectedSubtreeA = ['A' => ['A.1' => null, 'A.2' => null]]; + $expectedSubtreeA = ['A' => ['A.1' => null, 'A.2' => null]]; $expectedSubtreeB = [ - 'B' => [ + 'B' => [ 'B.1' => null, 'B.2' => [ 'B.2.1' => null, @@ -684,15 +684,15 @@ public function testToHierarchyNestsCorrectly() 'B.2.3' => null, ], 'B.3' => null, - ], - ]; + ], + ]; $expectedSubtreeC = ['C.1' => null, 'C.2' => null]; $expectedSubtreeD = ['D' => null]; // Perform assertions - $wholeTree = hmap(Cluster::all()->toHierarchy()->toArray()); + $wholeTree = hmap(Cluster::all()->toHierarchy()->toArray()); $this->assertArraysAreEqual($expectedWholeTree, $wholeTree); $subtreeA = hmap($this->clusters('A')->getDescendantsAndSelf()->toHierarchy()->toArray()); @@ -721,11 +721,11 @@ public function testToHierarchyNestsCorrectlyNotSequential() $this->assertTrue(Cluster::isValidNestedSet()); $expected = [ - 'Child 1' => [ + 'Child 1' => [ 'Child 1.1' => null, 'Child 1.2' => null, - ], - ]; + ], + ]; $parent->reload(); $this->assertArraysAreEqual($expected, hmap($parent->getDescendantsAndSelf()->toHierarchy()->toArray())); @@ -736,22 +736,22 @@ public function testToHierarchyNestsCorrectlyWithOrder() with(new OrderedClusterSeeder())->run(); $expectedWhole = [ - 'Root A' => null, - 'Root Z' => [ + 'Root A' => null, + 'Root Z' => [ 'Child A' => null, 'Child C' => null, 'Child G' => ['Child G.1' => null], - ], - ]; + ], + ]; $this->assertArraysAreEqual($expectedWhole, hmap(OrderedCluster::all()->toHierarchy()->toArray())); $expectedSubtreeZ = [ - 'Root Z' => [ + 'Root Z' => [ 'Child A' => null, 'Child C' => null, 'Child G' => ['Child G.1' => null], - ], - ]; + ], + ]; $this->assertArraysAreEqual($expectedSubtreeZ, hmap($this->clusters('Root Z', 'OrderedCluster')->getDescendantsAndSelf()->toHierarchy()->toArray())); } @@ -761,13 +761,13 @@ public function testGetNestedList() $nestedList = Cluster::getNestedList('name', 'id', $seperator); $expected = [ - '7461d8f5-2ea9-4788-99c4-9d0244f0bfb1' => str_repeat($seperator, 0).'Root 1', - '5d7ce1fd-6151-46d3-a5b3-0ebb9988dc57' => str_repeat($seperator, 1).'Child 1', - '07c1fc8c-53b5-4fe7-b9c4-e09f266a455c' => str_repeat($seperator, 1).'Child 2', - '3315a297-af87-4ad3-9fa5-19785407573d' => str_repeat($seperator, 2).'Child 2.1', - '054476d2-6830-4014-a181-4de010ef7114' => str_repeat($seperator, 1).'Child 3', - '3bb62314-9e1e-49c6-a5cb-17a9ab9b1b9a' => str_repeat($seperator, 0).'Root 2', - ]; + '7461d8f5-2ea9-4788-99c4-9d0244f0bfb1' => str_repeat($seperator, 0).'Root 1', + '5d7ce1fd-6151-46d3-a5b3-0ebb9988dc57' => str_repeat($seperator, 1).'Child 1', + '07c1fc8c-53b5-4fe7-b9c4-e09f266a455c' => str_repeat($seperator, 1).'Child 2', + '3315a297-af87-4ad3-9fa5-19785407573d' => str_repeat($seperator, 2).'Child 2.1', + '054476d2-6830-4014-a181-4de010ef7114' => str_repeat($seperator, 1).'Child 3', + '3bb62314-9e1e-49c6-a5cb-17a9ab9b1b9a' => str_repeat($seperator, 0).'Root 2', + ]; $this->assertArraysAreEqual($expected, $nestedList); } @@ -779,13 +779,13 @@ public function testGetNestedListSymbol() $nestedList = Cluster::getNestedList('name', 'id', $seperator, $symbol); $expected = [ - '7461d8f5-2ea9-4788-99c4-9d0244f0bfb1' => str_repeat($seperator, 0).$symbol.'Root 1', - '5d7ce1fd-6151-46d3-a5b3-0ebb9988dc57' => str_repeat($seperator, 1).$symbol.'Child 1', - '07c1fc8c-53b5-4fe7-b9c4-e09f266a455c' => str_repeat($seperator, 1).$symbol.'Child 2', - '3315a297-af87-4ad3-9fa5-19785407573d' => str_repeat($seperator, 2).$symbol.'Child 2.1', - '054476d2-6830-4014-a181-4de010ef7114' => str_repeat($seperator, 1).$symbol.'Child 3', - '3bb62314-9e1e-49c6-a5cb-17a9ab9b1b9a' => str_repeat($seperator, 0).$symbol.'Root 2', - ]; + '7461d8f5-2ea9-4788-99c4-9d0244f0bfb1' => str_repeat($seperator, 0).$symbol.'Root 1', + '5d7ce1fd-6151-46d3-a5b3-0ebb9988dc57' => str_repeat($seperator, 1).$symbol.'Child 1', + '07c1fc8c-53b5-4fe7-b9c4-e09f266a455c' => str_repeat($seperator, 1).$symbol.'Child 2', + '3315a297-af87-4ad3-9fa5-19785407573d' => str_repeat($seperator, 2).$symbol.'Child 2.1', + '054476d2-6830-4014-a181-4de010ef7114' => str_repeat($seperator, 1).$symbol.'Child 3', + '3bb62314-9e1e-49c6-a5cb-17a9ab9b1b9a' => str_repeat($seperator, 0).$symbol.'Root 2', + ]; $this->assertArraysAreEqual($expected, $nestedList); } diff --git a/tests/suite/Cluster/ClusterMovementTest.php b/tests/suite/Cluster/ClusterMovementTest.php index eaa58523..fdebe848 100644 --- a/tests/suite/Cluster/ClusterMovementTest.php +++ b/tests/suite/Cluster/ClusterMovementTest.php @@ -14,15 +14,15 @@ public function testMoveLeft() } /** - * @expectedException Baum\MoveNotPossibleException + * @expectedException \Baum\MoveNotPossibleException */ - public function testMoveLeftRaisesAnExceptionWhenNotPossible() - { - $node = $this->clusters('Child 2'); + public function testMoveLeftRaisesAnExceptionWhenNotPossible() + { + $node = $this->clusters('Child 2'); - $node->moveLeft(); - $node->moveLeft(); - } + $node->moveLeft(); + $node->moveLeft(); + } public function testMoveLeftDoesNotChangeDepth() { @@ -62,12 +62,12 @@ public function testMoveToLeftOf() } /** - * @expectedException Baum\MoveNotPossibleException + * @expectedException \Baum\MoveNotPossibleException */ - public function testMoveToLeftOfRaisesAnExceptionWhenNotPossible() - { - $this->clusters('Child 1')->moveToLeftOf($this->clusters('Child 1')->getLeftSibling()); - } + public function testMoveToLeftOfRaisesAnExceptionWhenNotPossible() + { + $this->clusters('Child 1')->moveToLeftOf($this->clusters('Child 1')->getLeftSibling()); + } public function testMoveToLeftOfDoesNotChangeDepth() { @@ -107,15 +107,15 @@ public function testMoveRight() } /** - * @expectedException Baum\MoveNotPossibleException + * @expectedException \Baum\MoveNotPossibleException */ - public function testMoveRightRaisesAnExceptionWhenNotPossible() - { - $node = $this->clusters('Child 2'); + public function testMoveRightRaisesAnExceptionWhenNotPossible() + { + $node = $this->clusters('Child 2'); - $node->moveRight(); - $node->moveRight(); - } + $node->moveRight(); + $node->moveRight(); + } public function testMoveRightDoesNotChangeDepth() { @@ -155,12 +155,12 @@ public function testMoveToRightOf() } /** - * @expectedException Baum\MoveNotPossibleException + * @expectedException \Baum\MoveNotPossibleException */ - public function testMoveToRightOfRaisesAnExceptionWhenNotPossible() - { - $this->clusters('Child 3')->moveToRightOf($this->clusters('Child 3')->getRightSibling()); - } + public function testMoveToRightOfRaisesAnExceptionWhenNotPossible() + { + $this->clusters('Child 3')->moveToRightOf($this->clusters('Child 3')->getRightSibling()); + } public function testMoveToRightOfDoesNotChangeDepth() { @@ -472,54 +472,54 @@ public function testMakeLastChildOfSwappingRootsWithSubtrees() } /** - * @expectedException Baum\MoveNotPossibleException + * @expectedException \Baum\MoveNotPossibleException */ - public function testUnpersistedNodeCannotBeMoved() - { - $unpersisted = new Cluster(['name' => 'Unpersisted']); + public function testUnpersistedNodeCannotBeMoved() + { + $unpersisted = new Cluster(['name' => 'Unpersisted']); - $unpersisted->moveToRightOf($this->clusters('Root 1')); - } + $unpersisted->moveToRightOf($this->clusters('Root 1')); + } /** - * @expectedException Baum\MoveNotPossibleException + * @expectedException \Baum\MoveNotPossibleException */ - public function testUnpersistedNodeCannotBeMadeChild() - { - $unpersisted = new Cluster(['name' => 'Unpersisted']); + public function testUnpersistedNodeCannotBeMadeChild() + { + $unpersisted = new Cluster(['name' => 'Unpersisted']); - $unpersisted->makeChildOf($this->clusters('Root 1')); - } + $unpersisted->makeChildOf($this->clusters('Root 1')); + } /** - * @expectedException Baum\MoveNotPossibleException + * @expectedException \Baum\MoveNotPossibleException */ - public function testNodesCannotBeMovedToItself() - { - $node = $this->clusters('Child 1'); + public function testNodesCannotBeMovedToItself() + { + $node = $this->clusters('Child 1'); - $node->moveToRightOf($node); - } + $node->moveToRightOf($node); + } /** - * @expectedException Baum\MoveNotPossibleException + * @expectedException \Baum\MoveNotPossibleException */ - public function testNodesCannotBeMadeChildOfThemselves() - { - $node = $this->clusters('Child 1'); + public function testNodesCannotBeMadeChildOfThemselves() + { + $node = $this->clusters('Child 1'); - $node->makeChildOf($node); - } + $node->makeChildOf($node); + } /** - * @expectedException Baum\MoveNotPossibleException + * @expectedException \Baum\MoveNotPossibleException */ - public function testNodesCannotBeMovedToDescendantsOfThemselves() - { - $node = $this->clusters('Root 1'); + public function testNodesCannotBeMovedToDescendantsOfThemselves() + { + $node = $this->clusters('Root 1'); - $node->makeChildOf($this->clusters('Child 2.1')); - } + $node->makeChildOf($this->clusters('Child 2.1')); + } public function testDepthIsUpdatedWhenMadeChild() { @@ -529,7 +529,7 @@ public function testDepthIsUpdatedWhenMadeChild() $d = Cluster::create(['name' => 'D']); // a > b > c > d - $b->makeChildOf($a); + $b->makeChildOf($a); $c->makeChildOf($b); $d->makeChildOf($c); @@ -552,7 +552,7 @@ public function testDepthIsUpdatedOnDescendantsWhenParentMoves() $d = Cluster::create(['name' => 'D']); // a > b > c > d - $b->makeChildOf($a); + $b->makeChildOf($a); $c->makeChildOf($b); $d->makeChildOf($c); diff --git a/tests/suite/NodeModelExtensionsTest.php b/tests/suite/NodeModelExtensionsTest.php index d0a92d15..93e6098d 100644 --- a/tests/suite/NodeModelExtensionsTest.php +++ b/tests/suite/NodeModelExtensionsTest.php @@ -127,19 +127,19 @@ public function testReloadResetsChangesOnDeletedNodes() /** * @expectedException Illuminate\Database\Eloquent\ModelNotFoundException */ - public function testReloadThrowsExceptionIfNodeCannotBeLocated() - { - $node = Category::create(['name' => 'Some node']); - $this->assertNotNull($node->getKey()); - - $node->delete(); - $this->assertNull($this->categories('Some node')); - $this->assertFalse($node->exists); - - // Fake persisted state, reload & expect failure - $node->exists = true; - $node->reload(); - } + public function testReloadThrowsExceptionIfNodeCannotBeLocated() + { + $node = Category::create(['name' => 'Some node']); + $this->assertNotNull($node->getKey()); + + $node->delete(); + $this->assertNull($this->categories('Some node')); + $this->assertFalse($node->exists); + + // Fake persisted state, reload & expect failure + $node->exists = true; + $node->reload(); + } public function testNewNestedSetQueryUsesInternalBuilder() { diff --git a/tests/suite/support.php b/tests/suite/support.php index a34b2b31..e45de417 100644 --- a/tests/suite/support.php +++ b/tests/suite/support.php @@ -2,62 +2,62 @@ if (!function_exists('hmap')) { - /** - * Simple function which aids in converting the tree hierarchy into something - * more easily testable... - * - * @param array $nodes - * - * @return array - */ - function hmap(array $nodes, $preserve = null) - { - $output = []; - - foreach ($nodes as $node) { - if (is_null($preserve)) { - $output[$node['name']] = empty($node['children']) ? null : hmap($node['children']); - } else { - $preserve = is_string($preserve) ? [$preserve] : $preserve; - - $current = array_only($node, $preserve); - if (array_key_exists('children', $node)) { - $children = $node['children']; - - if (count($children) > 0) { - $current['children'] = hmap($children, $preserve); - } - } - - $output[] = $current; - } - } - - return $output; - } + /** + * Simple function which aids in converting the tree hierarchy into something + * more easily testable... + * + * @param array $nodes + * + * @return array + */ + function hmap(array $nodes, $preserve = null) + { + $output = []; + + foreach ($nodes as $node) { + if (is_null($preserve)) { + $output[$node['name']] = empty($node['children']) ? null : hmap($node['children']); + } else { + $preserve = is_string($preserve) ? [$preserve] : $preserve; + + $current = array_only($node, $preserve); + if (array_key_exists('children', $node)) { + $children = $node['children']; + + if (count($children) > 0) { + $current['children'] = hmap($children, $preserve); + } + } + + $output[] = $current; + } + } + + return $output; + } } if (!function_exists('array_ints_keys')) { - /** - * Cast provided keys's values into ints. This is to wrestle with PDO driver - * inconsistencies. - * - * @param array $input - * @param mixed $keys - * - * @return array - */ - function array_ints_keys(array $input, $keys = 'id') - { - $keys = is_string($keys) ? [$keys] : $keys; - - array_walk_recursive($input, function (&$value, $key) use ($keys) { - if (array_search($key, $keys) !== false) { - $value = (int) $value; - } - }); - - return $input; - } + /** + * Cast provided keys's values into ints. This is to wrestle with PDO driver + * inconsistencies. + * + * @param array $input + * @param mixed $keys + * + * @return array + */ + function array_ints_keys(array $input, $keys = 'id') + { + $keys = is_string($keys) ? [$keys] : $keys; + + array_walk_recursive($input, function (&$value, $key) use ($keys) { + if (array_search($key, $keys) !== false) { + $value = (int) $value; + } + }); + + return $input; + } }