Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions tests/Roles.php
Original file line number Diff line number Diff line change
@@ -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 changes: 17 additions & 0 deletions tests/Supervisor.php
Original file line number Diff line number Diff line change
@@ -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 changes: 35 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -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 changes: 106 additions & 0 deletions tests/Unit/UpdaterTest.php
Original file line number Diff line number Diff line change
@@ -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());
}

}
}
15 changes: 15 additions & 0 deletions tests/fakes/UpdaterFake.php
Original file line number Diff line number Diff line change
@@ -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 changes: 3 additions & 0 deletions tests/test-data/invalid-json/invalid.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[

]
5 changes: 5 additions & 0 deletions tests/test-data/no-criteria/no-criteria.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
{
"string": "update-student-records"
}
]
9 changes: 9 additions & 0 deletions tests/test-data/relationship/roles.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[
{
"_slug": "update-student-records",
"category": "testing",
"supervisor": {
"name": "CEO"
}
}
]
14 changes: 14 additions & 0 deletions tests/test-data/roles.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"_slug": "update-student-records",
"category": "testing"
},
{
"_slug": "borrow-ferrari",
"category": "cars"
},
{
"_slug": "destroy-ferrari",
"category": "cars"
}
]
14 changes: 14 additions & 0 deletions tests/test-data/valid/roles.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"_slug": "update-student-records",
"category": "changed"
},
{
"_slug": "borrow-ferrari",
"category": "changed"
},
{
"_slug": "destroy-ferrari",
"category": "changed"
}
]