diff --git a/tests/Roles.php b/tests/Roles.php new file mode 100644 index 0000000..2a876aa --- /dev/null +++ b/tests/Roles.php @@ -0,0 +1,17 @@ +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 new file mode 100644 index 0000000..9dfd32a --- /dev/null +++ b/tests/TestCase.php @@ -0,0 +1,35 @@ +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(); + }); + } +} \ No newline at end of file diff --git a/tests/Unit/UpdaterTest.php b/tests/Unit/UpdaterTest.php new file mode 100644 index 0000000..74cdcab --- /dev/null +++ b/tests/Unit/UpdaterTest.php @@ -0,0 +1,106 @@ +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()); + } + + } +} \ 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 @@ +