From 02867f5be733f29951e226face3f73a3a64130a7 Mon Sep 17 00:00:00 2001 From: Victor Gonzalez Date: Mon, 28 Jan 2019 10:02:14 -0500 Subject: [PATCH 1/2] wip tests --- tests/Roles.php | 10 ++++++++++ tests/TestCase.php | 15 +++++++++++++++ tests/Unit/UpdaterTest.php | 36 ++++++++++++++++++++++++++++++++++++ tests/test-data/roles.json | 11 +++++++++++ 4 files changed, 72 insertions(+) create mode 100644 tests/Roles.php create mode 100644 tests/TestCase.php create mode 100644 tests/Unit/UpdaterTest.php create mode 100644 tests/test-data/roles.json diff --git a/tests/Roles.php b/tests/Roles.php new file mode 100644 index 0000000..a3e58a7 --- /dev/null +++ b/tests/Roles.php @@ -0,0 +1,10 @@ +set('database.default', 'testdb'); + $app['config']->set('database.connections.testdb', [ + 'driver' => 'sqlite', + 'database' => ':memory:' + ]); + } +} \ No newline at end of file diff --git a/tests/Unit/UpdaterTest.php b/tests/Unit/UpdaterTest.php new file mode 100644 index 0000000..7a86df5 --- /dev/null +++ b/tests/Unit/UpdaterTest.php @@ -0,0 +1,36 @@ +expectException(\Exception::class); + + new Updater(); + } + + /** @test */ + public function experiment() + { + $updater = new UpdaterFake(__DIR__ . '/../test-data', 'roles'); + + \DB::enableQueryLog(); + $updater->run(); + \DB::disableQueryLog(); + + dd(\DB::getQueryLog()); + } +} + +class UpdaterFake extends Updater +{ + protected function getModel(string $name) + { + return Roles::class; + } +} \ No newline at end of file diff --git a/tests/test-data/roles.json b/tests/test-data/roles.json new file mode 100644 index 0000000..c5f860f --- /dev/null +++ b/tests/test-data/roles.json @@ -0,0 +1,11 @@ +[ + { + "_slug": "update-student-records" + }, + { + "_slug": "borrow-ferrari" + }, + { + "_slug": "destroy-ferrari" + } +] \ No newline at end of file From cafa758a99aba5e7a87903d64a090ccd2bff27dc Mon Sep 17 00:00:00 2001 From: Victor Gonzalez Date: Mon, 28 Jan 2019 11:30:00 -0500 Subject: [PATCH 2/2] Updater class tests --- tests/Roles.php | 7 ++ tests/Supervisor.php | 17 ++++ tests/TestCase.php | 20 ++++ tests/Unit/UpdaterTest.php | 98 +++++++++++++++++--- 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, 180 insertions(+), 17 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 @@ 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 @@ protected function getEnvironmentSetUp($app) '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 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() { - $this->expectException(\Exception::class); + try { + new UpdaterFake(); - new Updater(); + $this->fail('exception was thrown'); + + } catch (Exception $e) { + $this->assertEquals('Specified sync file directory does not exist', $e->getMessage()); + } } /** @test */ - public function experiment() + public function invalid_json_throws_an_exception() { - $updater = new UpdaterFake(__DIR__ . '/../test-data', 'roles'); + try { + $updater = new UpdaterFake(__DIR__ . '/../test-data/invalid-json'); + $updater->run(); - \DB::enableQueryLog(); - $updater->run(); - \DB::disableQueryLog(); + $this->fail('exception was thrown'); + + } catch (Exception $e) { + $this->assertContains('No records or invalid JSON for', $e->getMessage()); + } - dd(\DB::getQueryLog()); } -} -class UpdaterFake extends Updater -{ - protected function getModel(string $name) + /** @test */ + public function the_json_must_contain_a_key_with_an_underscore() { - return Roles::class; + 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 @@ +