Skip to content

Commit

Permalink
testing, testing, testing
Browse files Browse the repository at this point in the history
  • Loading branch information
poing committed May 29, 2020
1 parent 204f4b6 commit 208b4b8
Show file tree
Hide file tree
Showing 51 changed files with 5,965 additions and 15 deletions.
1 change: 1 addition & 0 deletions LICENSE
@@ -1,5 +1,6 @@
The MIT License (MIT)

Copyright (c) 2020 Brian LaVallee
Copyright (c) 2013 Estanislau Trepat

Permission is hereby granted, free of charge, to any person obtaining a copy
Expand Down
18 changes: 12 additions & 6 deletions README.md
@@ -1,14 +1,20 @@
# Baum v2

[![Build Status](https://travis-ci.org/gazsp/baum.svg?branch=master)](https://travis-ci.org/gazsp/baum)
[![Coverage Status](https://coveralls.io/repos/github/gazsp/baum/badge.svg?branch=master)](https://coveralls.io/github/gazsp/baum?branch=master)
[![StyleCI](https://github.styleci.io/repos/47506280/shield?branch=master&style=flat)](https://github.styleci.io/repos/47506280)
[![Build Status](https://travis-ci.org/gazsp/baum.svg?branch=2.0.3)](https://travis-ci.org/gazsp/baum)
[![Coverage Status](https://coveralls.io/repos/github/gazsp/baum/badge.svg?branch=2.0.3)](https://coveralls.io/github/gazsp/baum?branch=2.0.3)
[![StyleCI](https://github.styleci.io/repos/47506280/shield?branch=2.0.3&style=flat)](https://github.styleci.io/repos/47506280)

## Nested Set implementation for Laravel

Baum is an implementation of the [Nested Set](http://en.wikipedia.org/wiki/Nested_set_model)
pattern for the [Laravel](http://laravel.com/) Eloquent ORM.

### Key Considerations for using a Nested Set Pattern:

1. The Nested Set pattern is appropriate where the tree element and one or two attributes are the only data.
1. The Nested Set pattern is a **poor choice** when more complex relational data exists for the elements in the tree.
1. The Nested Set pattern is **best when** you need to query a tree more frequently than you need to modify the tree.

---

[![this](https://img.shields.io/badge/warning-work%20in%20progress-red)]()
Expand All @@ -19,10 +25,11 @@ pattern for the [Laravel](http://laravel.com/) Eloquent ORM.

## Installation

Baum v2 works with Laravel 5.8 (and PHP 7.2) and above. You can add it to your project with:
Baum v2 works with Laravel 5.8 (and PHP 7.2) and above. You can add it to your own project with:
```
composer require gazsp/baum
```

## Documentation

* [About Nested Sets](#about)
Expand All @@ -45,8 +52,7 @@ Nested sets are appropriate for ordered trees (e.g. menus, commercial categories
and big trees that must be queried efficiently (e.g. threaded posts).

See the [wikipedia entry for nested sets](http://en.wikipedia.org/wiki/Nested_set_model)
for more info. Also, this is a good introductory tutorial:
[http://www.evanpetersen.com/item/nested-sets.html](http://www.evanpetersen.com/item/nested-sets.html)
for more info.

<a name="theory"></a>
## The theory behind, a TL;DR version
Expand Down
5 changes: 5 additions & 0 deletions composer.json
Expand Up @@ -14,6 +14,11 @@
"name": "Gary Pearman",
"email": "gaz@red-robot.co.uk",
"homepage": "http://red-robot.co.uk"
},
{
"name": "Brian LaVallee",
"email": "brian.lavallee@invite-comm.jp",
"homepage": "https://invite-comm.jp"
}
],
"prefer-stable": true,
Expand Down
12 changes: 11 additions & 1 deletion phpunit.xml
Expand Up @@ -16,7 +16,17 @@
<testsuite name="Basic Baum Test">
<directory>tests/Basic</directory>
</testsuite>

<testsuite name="Baum Main Test">
<file>tests/Main/Standard/CategoryColumnsTest.php</file>
<file>tests/Main/Standard/CategoryCustomEventsTest.php</file>
<file>tests/Main/Standard/CategoryHierarchyTest.php</file>
<file>tests/Main/Standard/CategoryHierarchyTest.php</file>
</testsuite>
<!--
<testsuite name="Baum Under Test">
<file>tests/Main/Standard/CategoryMovementTest.php</file>
</testsuite>
-->
</testsuites>
<filter>
<whitelist>
Expand Down
5 changes: 3 additions & 2 deletions src/Baum/SetMapper.php
Expand Up @@ -4,6 +4,7 @@

use Closure;
use Illuminate\Support\Contracts\ArrayableInterface;
use Illuminate\Support\Arr;

class SetMapper
{
Expand Down Expand Up @@ -140,14 +141,14 @@ protected function getSearchAttributes($attributes)
{
$searchable = [$this->node->getKeyName()];

return array_only($attributes, $searchable);
return Arr::only($attributes, $searchable);
}

protected function getDataAttributes($attributes)
{
$exceptions = [$this->node->getKeyName(), $this->getChildrenKeyName()];

return array_except($attributes, $exceptions);
return Arr::except($attributes, $exceptions);
}

protected function firstOrNew($attributes)
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/tree_helper.php
Expand Up @@ -15,7 +15,7 @@
function flatten_tree($tree, $only = [], &$result = [])
{
foreach ($tree as $k=>$v) {
$result[$v['id']] = $only ? array_only($v, $only) : $v;
$result[$v['id']] = $only ? Arr::only($v, $only) : $v;

if (isset($v['children'])) {
flatten_tree($v['children'], $only, $result);
Expand Down
145 changes: 145 additions & 0 deletions tests/Main/BaumTestCase.php
@@ -0,0 +1,145 @@
<?php

namespace Baum\Tests\Main;

use Orchestra\Testbench\TestCase;

class BaumTestCase extends TestCase
{
/**
* Define environment setup.
*
* @param \Illuminate\Foundation\Application $app
* @return void
*/
// protected function getEnvironmentSetUp($app)
// {
// $config = [
// 'driver' => 'sqlite',
// 'database' => ':memory:',
// 'prefix' => '',
// ];
//
// // $config = [
// // 'driver' => 'mysql',
// // 'host' => 'localhost',
// // 'username' => 'root',
// // 'password' => '',
// // 'database' => 'baum_testing'
// // ];
//
// // Setup database
// $app['config']->set('database.default', 'default');
// $app['config']->set('database.connections.default', $config);
// }

public function assertArraysAreEqual($expected, $actual, $message = '')
{
$ex = json_encode($expected, JSON_PRETTY_PRINT);
$ac = json_encode($actual, JSON_PRETTY_PRINT);

return $this->assertEquals($ex, $ac, $message);
}

public function assertNodesAreEqual($a, $b)
{
if (is_object($a)) {
$a = $a->getAttributes();
}

if (is_object($b)) {
$b = $b->getAttributes();
}

$a = Arr::only($a, ['id', 'lft', 'rgt', 'name', 'parent_id']);
$b = Arr::only($b, ['id', 'lft', 'rgt', 'name', 'parent_id']);

return $this->assertArraysAreEqual($a, $b);
}

protected function categories($name, $className = 'Category')
{
return forward_static_call_array([$className, 'where'], ['name', '=', $name])->first();
}

protected function debugQueries()
{
\DB::listen(function ($query) {
static $count = 1;
static $queries = [];

if ($count == 1) {
\Log::info('---');
}

$replace = function ($sql, $bindings) {
$needle = '?';
foreach ($bindings as $replace) {
$pos = strpos($sql, $needle);
if ($pos !== false) {
if ($replace === null) {
$replace = 'NULL';
}
$sql = substr_replace($sql, $replace, $pos, strlen($needle));
}
}

return $sql;
};

$result = $replace($query->sql, $query->bindings);

$number = $count++;

if (! isset($queries[$result])) {
$queries[$result] = 1;
} else {
$queries[$result]++;
}

$ordinal = function ($number) {
$ends = ['th', 'st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th'];

if ((($number % 100) >= 11) && (($number % 100) <= 13)) {
return $number.'th';
} else {
return $number.$ends[$number % 10];
}
};

$queryRepeatCount = '';
if ($queries[$result] > 1) {
$x = $queries[$result];
$queryRepeatCount = " - {$ordinal($x)} occurrence";
}

echo "\n/* Query $number */";
echo "\n/*".str_repeat('-', 256).'*/';
echo "\n{$result}";
// echo "\n- {$query->time}mS{$queryRepeatCount}\n";

$backtrace = debug_backtrace();
$result = [];
array_walk($backtrace, function ($a, $b) use (&$result) {
if (isset($a['file'])) {
if (strpos($a['file'], 'vendor') === false) {
if (! isset($a['class'])) {
$a['class'] = '';
}

$function = sprintf('%-50s', $a['class'].'#'.$a['function']);
$string = "$function | {$a['file']}:{$a['line']}";
array_push($result, $string);
}
}
});

$result = array_reverse($result);
echo "/*\n";
foreach ($result as $v) {
echo "$v\n";
}
echo "*/\n";
});
}
}
12 changes: 12 additions & 0 deletions tests/Main/CategoryTestCase.php
@@ -0,0 +1,12 @@
<?php

class CategoryTestCase extends BaumTestCase
{
public function setUp()
{
parent::setUp();

with(new CategoryMigrator())->up();
with(new CategorySeeder())->run();
}
}
24 changes: 24 additions & 0 deletions tests/Main/Cluster/ClusterColumnsTest.php
@@ -0,0 +1,24 @@
<?php

namespace Baum\Tests\Main\Cluster;
use Baum\Tests\Main\UnitAbstract;


class ClusterColumnsTest extends UnitAbstract
{
public function testKeyIsNonNumeric()
{
$root = Cluster::root();

$this->assertTrue(is_string($root->getKey()));
$this->assertFalse(is_numeric($root->getKey()));
}

public function testParentKeyIsNonNumeric()
{
$child1 = $this->clusters('Child 1');

$this->assertTrue(is_string($child1->getParentId()));
$this->assertFalse(is_numeric($child1->getParentId()));
}
}

0 comments on commit 208b4b8

Please sign in to comment.