From cafa758a99aba5e7a87903d64a090ccd2bff27dc Mon Sep 17 00:00:00 2001 From: Victor Gonzalez Date: Mon, 28 Jan 2019 11:30:00 -0500 Subject: [PATCH] Updater class tests --- tests/Roles.php | 7 ++ tests/Supervisor.php | 17 +++ tests/TestCase.php | 20 ++++ tests/Unit/UpdaterTest.php | 106 +++++++++++++++---- tests/fakes/UpdaterFake.php | 15 +++ tests/test-data/invalid-json/invalid.json | 3 + tests/test-data/no-criteria/no-criteria.json | 5 + tests/test-data/relationship/roles.json | 9 ++ tests/test-data/roles.json | 9 +- tests/test-data/valid/roles.json | 14 +++ 10 files changed, 184 insertions(+), 21 deletions(-) create mode 100644 tests/Supervisor.php create mode 100644 tests/fakes/UpdaterFake.php create mode 100644 tests/test-data/invalid-json/invalid.json create mode 100644 tests/test-data/no-criteria/no-criteria.json create mode 100644 tests/test-data/relationship/roles.json create mode 100644 tests/test-data/valid/roles.json diff --git a/tests/Roles.php b/tests/Roles.php index a3e58a7..2a876aa 100644 --- a/tests/Roles.php +++ b/tests/Roles.php @@ -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); + } } \ No newline at end of file diff --git a/tests/Supervisor.php b/tests/Supervisor.php new file mode 100644 index 0000000..1272458 --- /dev/null +++ b/tests/Supervisor.php @@ -0,0 +1,17 @@ +hasMany(Roles::class); + } +} \ No newline at end of file diff --git a/tests/TestCase.php b/tests/TestCase.php index 0a69158..9dfd32a 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -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(); + }); + } } \ No newline at end of file diff --git a/tests/Unit/UpdaterTest.php b/tests/Unit/UpdaterTest.php index 7a86df5..74cdcab 100644 --- a/tests/Unit/UpdaterTest.php +++ b/tests/Unit/UpdaterTest.php @@ -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()); + } + } } \ No newline at end of file diff --git a/tests/fakes/UpdaterFake.php b/tests/fakes/UpdaterFake.php new file mode 100644 index 0000000..070616d --- /dev/null +++ b/tests/fakes/UpdaterFake.php @@ -0,0 +1,15 @@ +