Skip to content

Commit

Permalink
fixed removeChild method and made proper setup to test MySQL
Browse files Browse the repository at this point in the history
  • Loading branch information
franzose committed Feb 3, 2015
1 parent 4e9e621 commit 57468cd
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public function up()
$t->integer('descendant', false, true);
$t->integer('depth', false, true);

$t->foreign('ancestor')->references('id')->on('{{entity_table}}');
$t->foreign('descendant')->references('id')->on('{{entity_table}}');
$t->foreign('ancestor')->references('id')->on('{{entity_table}}')->onDelete('cascade');
$t->foreign('descendant')->references('id')->on('{{entity_table}}')->onDelete('cascade');
});
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function up()
$t->integer('real_depth', false, true);
$t->softDeletes();

$t->foreign('parent_id')->references('id')->on('{{entity_table}}');
$t->foreign('parent_id')->references('id')->on('{{entity_table}}')->onDelete('set null');
});
});
}
Expand Down
1 change: 1 addition & 0 deletions src/Franzose/ClosureTable/Models/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,7 @@ public function removeChild($position = null, $forceDelete = false)
if ($this->exists)
{
$action = ($forceDelete === true ? 'forceDelete' : 'delete');

$this->children($position)->$action();
}

Expand Down
46 changes: 33 additions & 13 deletions tests/BaseTestCase.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php namespace Franzose\ClosureTable\Tests;

use \Orchestra\Testbench\TestCase;
use \Mockery;

use DB;
use Event;
use Orchestra\Testbench\TestCase;
use Mockery;
use Franzose\ClosureTable\Models\Entity;

/**
Expand All @@ -13,7 +14,7 @@ abstract class BaseTestCase extends TestCase {
use \Way\Tests\ModelHelpers;

public static $debug = false;
public static $sqlite_in_memory = true;
public static $sqlite_in_memory = false;

public function setUp()
{
Expand All @@ -24,9 +25,9 @@ public function setUp()

if (!static::$sqlite_in_memory)
{
\DB::statement('DROP TABLE IF EXISTS entities;');
\DB::statement('DROP TABLE IF EXISTS entities_closure');
\DB::statement('DROP TABLE IF EXISTS migrations');
DB::statement('DROP TABLE IF EXISTS entities_closure');
DB::statement('DROP TABLE IF EXISTS entities;');
DB::statement('DROP TABLE IF EXISTS migrations');
}

$artisan = $this->app->make('artisan');
Expand All @@ -42,7 +43,7 @@ public function setUp()
if (static::$debug)
{
Entity::$debug = true;
\Event::listen('illuminate.query', function($sql, $bindings, $time){
Event::listen('illuminate.query', function($sql, $bindings, $time){
$sql = str_replace(array('%', '?'), array('%%', '%s'), $sql);
$full_sql = vsprintf($sql, $bindings);
echo PHP_EOL.'- BEGIN QUERY -'.PHP_EOL.$full_sql.PHP_EOL.'- END QUERY -'.PHP_EOL;
Expand All @@ -64,11 +65,30 @@ protected function getEnvironmentSetUp($app)
$app['path.base'] = __DIR__ . '/../src';

$app['config']->set('database.default', 'closuretable');
$app['config']->set('database.connections.closuretable', array(
'driver' => 'sqlite',
'database' => static::$sqlite_in_memory ? ':memory:' : __DIR__.'/../test.sqlite',
'prefix' => '',
));

if (static::$sqlite_in_memory)
{
$options = [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
];
}
else
{
$options = [
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'closure-table-test',
'username' => 'root',
'password' => '',
'prefix' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
];
}

$app['config']->set('database.connections.closuretable', $options);
}

/**
Expand Down
4 changes: 4 additions & 0 deletions tests/EntityTestCase.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace Franzose\ClosureTable\Tests;

use DB;
use Franzose\ClosureTable\Models\ClosureTable;
use Mockery;
use Franzose\ClosureTable\Models\Entity;
Expand Down Expand Up @@ -81,7 +82,10 @@ public function testIsRoot()

public function testCreate()
{
DB::statement("SET foreign_key_checks=0");
ClosureTable::truncate();
Entity::truncate();
DB::statement("SET foreign_key_checks=1");

$entity1 = new Entity;
$entity1->save();
Expand Down
10 changes: 6 additions & 4 deletions tests/migrations/2014_01_18_162506_create_entities_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ public function up()
Schema::create('entities', function(Blueprint $table)
{
$table->increments('id');
$table->integer('parent_id')->nullable();
$table->unsignedInteger('parent_id')->nullable();
$table->string('title')->default('The Title');
$table->text('excerpt')->default('The excerpt');
$table->longText('body')->default('The content');
$table->text('excerpt');
$table->longText('body');
$table->integer('position', false, true);
$table->integer('real_depth', false, true);
$table->softDeletes();

$table->foreign('parent_id')->references('id')->on('entities');
$table->foreign('parent_id')->references('id')->on('entities')->onDelete('set null');

$table->engine = 'InnoDB';
});
}

Expand Down

0 comments on commit 57468cd

Please sign in to comment.