Skip to content

Commit

Permalink
Added Tests and Migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
nhalstead committed Oct 12, 2020
1 parent 7ce390f commit d3e2ba8
Show file tree
Hide file tree
Showing 5 changed files with 211 additions and 0 deletions.
13 changes: 13 additions & 0 deletions phpunit.xml.dist
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>
104 changes: 104 additions & 0 deletions tests/AggregateTest.php
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);
}
}
28 changes: 28 additions & 0 deletions tests/TestCase.php
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' => '',
]);
}
}
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 tests/database/migrations/2020_10_12_000000_create_books_table.php
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");
}
}

0 comments on commit d3e2ba8

Please sign in to comment.