Skip to content

Commit 9e7fc3d

Browse files
Add failing test
Addresses #10
1 parent 28bc74a commit 9e7fc3d

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

tests/CascadeSoftDeletesIntegrationTest.php

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
<?php
22

3-
use Illuminate\Database\Capsule\Manager;
4-
use Illuminate\Events\Dispatcher;
53
use Illuminate\Container\Container;
4+
use Illuminate\Database\Capsule\Manager;
65
use Illuminate\Database\Eloquent\Model;
76
use Illuminate\Database\Eloquent\SoftDeletes;
7+
use Illuminate\Events\Dispatcher;
88

99
class CascadeSoftDeletesIntegrationTest extends PHPUnit_Framework_TestCase
1010
{
1111
public static function setupBeforeClass()
1212
{
1313
$manager = new Manager();
1414
$manager->addConnection([
15-
'driver' => 'sqlite',
15+
'driver' => 'sqlite',
1616
'database' => ':memory:',
1717
]);
1818

@@ -43,6 +43,13 @@ public static function setupBeforeClass()
4343
$table->string('body');
4444
$table->timestamps();
4545
});
46+
47+
$manager->schema()->create('post_types', function ($table) {
48+
$table->increments('id');
49+
$table->integer('post_id')->unsigned()->nullable();
50+
$table->string('label');
51+
$table->timestamps();
52+
});
4653
}
4754

4855

@@ -201,6 +208,22 @@ public function it_handles_grandchildren()
201208
}
202209
}
203210

211+
/** @test */
212+
public function it_cascades_a_has_one_relationship()
213+
{
214+
$post = Tests\Entities\Post::create([
215+
'title' => 'Cascae a has one relationship',
216+
'body' => 'This is how you cascade a has one relationship',
217+
]);
218+
219+
$type = new Tests\Entities\PostType(['label' => 'Test']);
220+
221+
$post->postType()->save($type);
222+
223+
$post->delete();
224+
$this->assertCount(0, Tests\Entities\Author::withTrashed()->where('id', $type->id)->get());
225+
}
226+
204227
/**
205228
* Attach some dummy posts (w/ comments) to the given author.
206229
*
@@ -241,5 +264,4 @@ private function attachCommentsToPost($post)
241264

242265
return $post;
243266
}
244-
245267
}

tests/Entities/Post.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,17 @@ class Post extends Model
1212

1313
public $dates = ['deleted_at'];
1414

15-
protected $cascadeDeletes = ['comments'];
15+
protected $cascadeDeletes = ['comments', 'postType'];
1616

1717
protected $fillable = ['title', 'body'];
1818

1919
public function comments()
2020
{
2121
return $this->hasMany('Tests\Entities\Comment');
2222
}
23+
24+
public function postType()
25+
{
26+
return $this->hasOne('Tests\Entities\PostType');
27+
}
2328
}

tests/Entities/PostType.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Tests\Entities;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class PostType extends Model
8+
{
9+
protected $fillable = ['label'];
10+
11+
public function post()
12+
{
13+
return $this->belongsTo(Post::class);
14+
}
15+
}

0 commit comments

Comments
 (0)