mirror of
https://github.com/nullthoughts/laravel-data-sync.git
synced 2026-01-26 03:34:02 +00:00
Updater class tests
This commit is contained in:
@@ -6,5 +6,12 @@ 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
17
tests/Supervisor.php
Normal 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);
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,9 @@
|
||||
|
||||
namespace distinctm\LaravelDataSync\Tests;
|
||||
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class TestCase extends \Orchestra\Testbench\TestCase
|
||||
{
|
||||
protected function getEnvironmentSetUp($app)
|
||||
@@ -12,4 +15,21 @@ class TestCase extends \Orchestra\Testbench\TestCase
|
||||
'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();
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -2,35 +2,105 @@
|
||||
|
||||
namespace distinctm\LaravelDataSync\Tests;
|
||||
|
||||
use distinctm\LaravelDataSync\Updater;
|
||||
use distinctm\LaravelDataSync\Tests\Fakes\UpdaterFake;
|
||||
use Exception;
|
||||
|
||||
class UpdaterTest extends TestCase
|
||||
{
|
||||
/** @test */
|
||||
public function exception_is_thrown_if_the_directory_does_not_exists()
|
||||
{
|
||||
$this->expectException(\Exception::class);
|
||||
|
||||
new Updater();
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function experiment()
|
||||
public function it_adds_roles_to_the_database()
|
||||
{
|
||||
$updater = new UpdaterFake(__DIR__ . '/../test-data', 'roles');
|
||||
|
||||
\DB::enableQueryLog();
|
||||
$updater->run();
|
||||
\DB::disableQueryLog();
|
||||
|
||||
dd(\DB::getQueryLog());
|
||||
$this->assertDatabaseHas('roles', ['slug' => 'update-student-records']);
|
||||
$this->assertDatabaseHas('roles', ['slug' => 'borrow-ferrari']);
|
||||
$this->assertDatabaseHas('roles', ['slug' => 'destroy-ferrari']);
|
||||
}
|
||||
}
|
||||
|
||||
class UpdaterFake extends Updater
|
||||
{
|
||||
protected function getModel(string $name)
|
||||
/** @test */
|
||||
public function it_can_default_to_configuration()
|
||||
{
|
||||
return Roles::class;
|
||||
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());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
15
tests/fakes/UpdaterFake.php
Normal file
15
tests/fakes/UpdaterFake.php
Normal 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)
|
||||
);
|
||||
}
|
||||
}
|
||||
3
tests/test-data/invalid-json/invalid.json
Normal file
3
tests/test-data/invalid-json/invalid.json
Normal file
@@ -0,0 +1,3 @@
|
||||
[
|
||||
|
||||
]
|
||||
5
tests/test-data/no-criteria/no-criteria.json
Normal file
5
tests/test-data/no-criteria/no-criteria.json
Normal file
@@ -0,0 +1,5 @@
|
||||
[
|
||||
{
|
||||
"string": "update-student-records"
|
||||
}
|
||||
]
|
||||
9
tests/test-data/relationship/roles.json
Normal file
9
tests/test-data/relationship/roles.json
Normal file
@@ -0,0 +1,9 @@
|
||||
[
|
||||
{
|
||||
"_slug": "update-student-records",
|
||||
"category": "testing",
|
||||
"supervisor": {
|
||||
"name": "CEO"
|
||||
}
|
||||
}
|
||||
]
|
||||
@@ -1,11 +1,14 @@
|
||||
[
|
||||
{
|
||||
"_slug": "update-student-records"
|
||||
"_slug": "update-student-records",
|
||||
"category": "testing"
|
||||
},
|
||||
{
|
||||
"_slug": "borrow-ferrari"
|
||||
"_slug": "borrow-ferrari",
|
||||
"category": "cars"
|
||||
},
|
||||
{
|
||||
"_slug": "destroy-ferrari"
|
||||
"_slug": "destroy-ferrari",
|
||||
"category": "cars"
|
||||
}
|
||||
]
|
||||
14
tests/test-data/valid/roles.json
Normal file
14
tests/test-data/valid/roles.json
Normal file
@@ -0,0 +1,14 @@
|
||||
[
|
||||
{
|
||||
"_slug": "update-student-records",
|
||||
"category": "changed"
|
||||
},
|
||||
{
|
||||
"_slug": "borrow-ferrari",
|
||||
"category": "changed"
|
||||
},
|
||||
{
|
||||
"_slug": "destroy-ferrari",
|
||||
"category": "changed"
|
||||
}
|
||||
]
|
||||
Reference in New Issue
Block a user