Merge pull request #9 from vicgonvt/master

Add tests for the Updater class
This commit is contained in:
Jani Gyllenberg
2019-01-28 11:49:11 -05:00
committed by GitHub
10 changed files with 235 additions and 0 deletions

17
tests/Roles.php Normal file
View File

@@ -0,0 +1,17 @@
<?php
namespace distinctm\LaravelDataSync\Tests;
use Illuminate\Database\Eloquent\Model;
class Roles extends Model
{
public $timestamps = false;
protected $guarded = [];
public function supervisor()
{
return $this->belongsTo(Supervisor::class);
}
}

17
tests/Supervisor.php Normal file
View File

@@ -0,0 +1,17 @@
<?php
namespace distinctm\LaravelDataSync\Tests;
use Illuminate\Database\Eloquent\Model;
class Supervisor extends Model
{
public $timestamps = false;
protected $guarded = [];
public function roles()
{
return $this->hasMany(Roles::class);
}
}

35
tests/TestCase.php Normal file
View File

@@ -0,0 +1,35 @@
<?php
namespace distinctm\LaravelDataSync\Tests;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class TestCase extends \Orchestra\Testbench\TestCase
{
protected function getEnvironmentSetUp($app)
{
$app['config']->set('database.default', 'testdb');
$app['config']->set('database.connections.testdb', [
'driver' => 'sqlite',
'database' => ':memory:'
]);
}
protected function setUp()
{
parent::setUp();
Schema::create('supervisors', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
});
Schema::create('roles', function (Blueprint $table) {
$table->increments('id');
$table->string('slug');
$table->unsignedInteger('supervisor_id')->nullable();
$table->string('category')->nullable();
});
}
}

106
tests/Unit/UpdaterTest.php Normal file
View File

@@ -0,0 +1,106 @@
<?php
namespace distinctm\LaravelDataSync\Tests;
use distinctm\LaravelDataSync\Tests\Fakes\UpdaterFake;
use Exception;
class UpdaterTest extends TestCase
{
/** @test */
public function it_adds_roles_to_the_database()
{
$updater = new UpdaterFake(__DIR__ . '/../test-data', 'roles');
$updater->run();
$this->assertDatabaseHas('roles', ['slug' => 'update-student-records']);
$this->assertDatabaseHas('roles', ['slug' => 'borrow-ferrari']);
$this->assertDatabaseHas('roles', ['slug' => 'destroy-ferrari']);
}
/** @test */
public function it_can_default_to_configuration()
{
config()->set('data-sync.path', __DIR__ . '/../test-data');
$updater = new UpdaterFake();
$updater->run();
$this->assertDatabaseHas('roles', ['slug' => 'update-student-records']);
$this->assertDatabaseHas('roles', ['slug' => 'borrow-ferrari']);
$this->assertDatabaseHas('roles', ['slug' => 'destroy-ferrari']);
}
/** @test */
public function it_can_update_an_existing_record()
{
config()->set('data-sync.path', __DIR__ . '/../test-data');
(new UpdaterFake())->run();
config()->set('data-sync.path', __DIR__ . '/../test-data/valid');
(new UpdaterFake())->run();
$this->assertDatabaseHas('roles', ['category' => 'changed']);
$this->assertDatabaseHas('roles', ['category' => 'changed']);
$this->assertDatabaseHas('roles', ['category' => 'changed']);
}
/** @test */
public function it_can_update_the_relationship()
{
$supervisor = Supervisor::create([
'name' => 'CEO',
]);
config()->set('data-sync.path', __DIR__ . '/../test-data/relationship', 'roles');
(new UpdaterFake())->run();
$this->assertEquals($supervisor->id, Roles::first()->supervisor_id);
$this->assertTrue($supervisor->is(Roles::first()->supervisor));
}
/** @test */
public function exception_is_thrown_if_the_directory_does_not_exists()
{
try {
new UpdaterFake();
$this->fail('exception was thrown');
} catch (Exception $e) {
$this->assertEquals('Specified sync file directory does not exist', $e->getMessage());
}
}
/** @test */
public function invalid_json_throws_an_exception()
{
try {
$updater = new UpdaterFake(__DIR__ . '/../test-data/invalid-json');
$updater->run();
$this->fail('exception was thrown');
} catch (Exception $e) {
$this->assertContains('No records or invalid JSON for', $e->getMessage());
}
}
/** @test */
public function the_json_must_contain_a_key_with_an_underscore()
{
try {
$updater = new UpdaterFake(__DIR__ . '/../test-data/no-criteria');
$updater->run();
$this->fail('exception was thrown');
} catch (Exception $e) {
$this->assertEquals('No criteria/attributes detected', $e->getMessage());
}
}
}

View File

@@ -0,0 +1,15 @@
<?php
namespace distinctm\LaravelDataSync\Tests\Fakes;
use distinctm\LaravelDataSync\Updater;
class UpdaterFake extends Updater
{
protected function getModel(string $name)
{
return '\\distinctm\\LaravelDataSync\\Tests\\' . studly_case(
pathinfo($name, PATHINFO_FILENAME)
);
}
}

View File

@@ -0,0 +1,3 @@
[
]

View File

@@ -0,0 +1,5 @@
[
{
"string": "update-student-records"
}
]

View File

@@ -0,0 +1,9 @@
[
{
"_slug": "update-student-records",
"category": "testing",
"supervisor": {
"name": "CEO"
}
}
]

View File

@@ -0,0 +1,14 @@
[
{
"_slug": "update-student-records",
"category": "testing"
},
{
"_slug": "borrow-ferrari",
"category": "cars"
},
{
"_slug": "destroy-ferrari",
"category": "cars"
}
]

View File

@@ -0,0 +1,14 @@
[
{
"_slug": "update-student-records",
"category": "changed"
},
{
"_slug": "borrow-ferrari",
"category": "changed"
},
{
"_slug": "destroy-ferrari",
"category": "changed"
}
]