-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
211 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit backupGlobals="false" backupStaticAttributes="false" bootstrap="vendor/autoload.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false"> | ||
<coverage> | ||
<include> | ||
<directory suffix=".php">src/</directory> | ||
</include> | ||
</coverage> | ||
<testsuites> | ||
<testsuite name="Package Test Suite"> | ||
<directory suffix=".php">./tests/</directory> | ||
</testsuite> | ||
</testsuites> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
|
||
namespace nhalstead\Transferable\Tests; | ||
|
||
use Illuminate\Support\Facades\DB; | ||
use Illuminate\Database\Eloquent\Model; | ||
use nhalstead\Transferable\Exceptions\DanglingRelationships; | ||
use nhalstead\Transferable\Interfaces\NoDanglingRelationships; | ||
use nhalstead\Transferable\Traits\TransferableRelationship; | ||
|
||
class AggregateTest extends TestCase | ||
{ | ||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
DB::table('genres')->insert([ | ||
["name" => "Fiction"], | ||
["name" => "Science Fiction"], | ||
["name" => "Action Adventure"], | ||
]); | ||
|
||
DB::table('books')->insert([ | ||
["title" => "Catching Fire", "genre_id" => 1], | ||
["title" => "The Hunger Games", "genre_id" => 1], | ||
["title" => "Nineteen Eighty-Four", "genre_id" => 2], | ||
["title" => "The Martian", "genre_id" => 2], | ||
["title" => "Treasure Island", "genre_id" => 3], | ||
["title" => "Hatchet", "genre_id" => 3], | ||
]); | ||
} | ||
|
||
public function testCheckDangling() | ||
{ | ||
$all = Genre::all(); | ||
|
||
foreach($all as $genre) { | ||
$this->assertEquals(2, $genre->countTransferable()); | ||
} | ||
} | ||
|
||
public function testRejectDeleteWhileContainsDangling() | ||
{ | ||
|
||
$category = Genre::find(2); | ||
|
||
$this->assertEquals(2, $category->books()->count()); | ||
$this->expectException(DanglingRelationships::class); | ||
$category->delete(); | ||
} | ||
|
||
public function testDeleteAfterTransferDanglingDryRun() | ||
{ | ||
$newCategory = Genre::find(1); | ||
$category = Genre::find(2); | ||
|
||
$this->assertEquals(2, $category->countTransferable()); | ||
|
||
$changed = $category->transferTo($newCategory, true); | ||
$this->assertEquals(2, $changed); | ||
|
||
$this->assertEquals(4, $newCategory->countTransferable()); | ||
$this->assertEquals(2, $category->countTransferable()); | ||
} | ||
|
||
public function testDeleteAfterTransferDangling() | ||
{ | ||
$newCategory = Genre::find(1); | ||
$category = Genre::find(2); | ||
|
||
$this->assertEquals(2, $category->countTransferable()); | ||
|
||
$changed = $category->transferTo($newCategory); | ||
$this->assertEquals(2, $changed); | ||
|
||
$this->assertEquals(4, $newCategory->countTransferable()); | ||
$this->assertEquals(0, $category->countTransferable()); | ||
|
||
$category->delete(); | ||
} | ||
|
||
} | ||
|
||
class Book extends Model | ||
{ | ||
public function genre() | ||
{ | ||
return $this->belongsTo(Genre::class); | ||
} | ||
} | ||
|
||
class Genre extends Model implements NoDanglingRelationships | ||
{ | ||
use TransferableRelationship; | ||
|
||
protected $transferable = [ | ||
"books" | ||
]; | ||
|
||
public function books() | ||
{ | ||
return $this->hasMany(Book::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace nhalstead\Transferable\Tests; | ||
|
||
use Orchestra\Testbench\TestCase as BaseTestCase; | ||
|
||
abstract class TestCase extends BaseTestCase | ||
{ | ||
public function setUp() : void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->loadMigrationsFrom(__DIR__ . '/database/migrations'); | ||
|
||
$this->artisan('migrate'); | ||
} | ||
|
||
protected function getEnvironmentSetUp($app) | ||
{ | ||
$app['config']->set('database.default', 'testbench'); | ||
|
||
$app['config']->set('database.connections.testbench', [ | ||
'driver' => 'sqlite', | ||
'database' => ':memory:', | ||
'prefix' => '', | ||
]); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
tests/database/migrations/2020_10_11_000000_create_genres_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\Schema; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Database\Migrations\Migration; | ||
|
||
class CreateGenresTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create("genres", function (Blueprint $table) { | ||
$table->increments("id"); | ||
$table->string("name"); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::dropIfExists("genres"); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
tests/database/migrations/2020_10_12_000000_create_books_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\Schema; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Database\Migrations\Migration; | ||
|
||
class CreateBooksTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create("books", function (Blueprint $table) { | ||
$table->increments("id"); | ||
$table->string("title"); | ||
$table->integer("genre_id")->unsigned(); | ||
$table->foreign("genre_id")->references("id")->on("genres"); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::dropIfExists("books"); | ||
} | ||
} |