From 57468cddf0c433c427d84895f0dfc2273f980734 Mon Sep 17 00:00:00 2001 From: Jan Iwanow Date: Wed, 4 Feb 2015 03:16:36 +1000 Subject: [PATCH] fixed removeChild method and made proper setup to test MySQL --- .../stubs/migrations/closuretable.php | 4 +- .../Generators/stubs/migrations/entity.php | 2 +- src/Franzose/ClosureTable/Models/Entity.php | 1 + tests/BaseTestCase.php | 46 +++++++++++++------ tests/EntityTestCase.php | 4 ++ ...014_01_18_162506_create_entities_table.php | 10 ++-- 6 files changed, 47 insertions(+), 20 deletions(-) diff --git a/src/Franzose/ClosureTable/Generators/stubs/migrations/closuretable.php b/src/Franzose/ClosureTable/Generators/stubs/migrations/closuretable.php index 7703290..4e4c2a9 100644 --- a/src/Franzose/ClosureTable/Generators/stubs/migrations/closuretable.php +++ b/src/Franzose/ClosureTable/Generators/stubs/migrations/closuretable.php @@ -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'); }); }); } diff --git a/src/Franzose/ClosureTable/Generators/stubs/migrations/entity.php b/src/Franzose/ClosureTable/Generators/stubs/migrations/entity.php index b32841f..d24dfb2 100644 --- a/src/Franzose/ClosureTable/Generators/stubs/migrations/entity.php +++ b/src/Franzose/ClosureTable/Generators/stubs/migrations/entity.php @@ -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'); }); }); } diff --git a/src/Franzose/ClosureTable/Models/Entity.php b/src/Franzose/ClosureTable/Models/Entity.php index 1feba8d..173b5a6 100644 --- a/src/Franzose/ClosureTable/Models/Entity.php +++ b/src/Franzose/ClosureTable/Models/Entity.php @@ -751,6 +751,7 @@ public function removeChild($position = null, $forceDelete = false) if ($this->exists) { $action = ($forceDelete === true ? 'forceDelete' : 'delete'); + $this->children($position)->$action(); } diff --git a/tests/BaseTestCase.php b/tests/BaseTestCase.php index a32e571..91e7758 100644 --- a/tests/BaseTestCase.php +++ b/tests/BaseTestCase.php @@ -1,8 +1,9 @@ app->make('artisan'); @@ -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; @@ -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); } /** diff --git a/tests/EntityTestCase.php b/tests/EntityTestCase.php index 545d91e..e459014 100644 --- a/tests/EntityTestCase.php +++ b/tests/EntityTestCase.php @@ -1,5 +1,6 @@ save(); diff --git a/tests/migrations/2014_01_18_162506_create_entities_table.php b/tests/migrations/2014_01_18_162506_create_entities_table.php index c2a4dfb..fb5da95 100644 --- a/tests/migrations/2014_01_18_162506_create_entities_table.php +++ b/tests/migrations/2014_01_18_162506_create_entities_table.php @@ -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'; }); }