Skip to content

Commit ab17916

Browse files
authored
Merge pull request #9 from vicgonvt/master
Add tests for the Updater class
2 parents 6a0932a + cafa758 commit ab17916

File tree

10 files changed

+235
-0
lines changed

10 files changed

+235
-0
lines changed

tests/Roles.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace distinctm\LaravelDataSync\Tests;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class Roles extends Model
8+
{
9+
public $timestamps = false;
10+
11+
protected $guarded = [];
12+
13+
public function supervisor()
14+
{
15+
return $this->belongsTo(Supervisor::class);
16+
}
17+
}

tests/Supervisor.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace distinctm\LaravelDataSync\Tests;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class Supervisor extends Model
8+
{
9+
public $timestamps = false;
10+
11+
protected $guarded = [];
12+
13+
public function roles()
14+
{
15+
return $this->hasMany(Roles::class);
16+
}
17+
}

tests/TestCase.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace distinctm\LaravelDataSync\Tests;
4+
5+
use Illuminate\Database\Schema\Blueprint;
6+
use Illuminate\Support\Facades\Schema;
7+
8+
class TestCase extends \Orchestra\Testbench\TestCase
9+
{
10+
protected function getEnvironmentSetUp($app)
11+
{
12+
$app['config']->set('database.default', 'testdb');
13+
$app['config']->set('database.connections.testdb', [
14+
'driver' => 'sqlite',
15+
'database' => ':memory:'
16+
]);
17+
}
18+
19+
protected function setUp()
20+
{
21+
parent::setUp();
22+
23+
Schema::create('supervisors', function (Blueprint $table) {
24+
$table->increments('id');
25+
$table->string('name');
26+
});
27+
28+
Schema::create('roles', function (Blueprint $table) {
29+
$table->increments('id');
30+
$table->string('slug');
31+
$table->unsignedInteger('supervisor_id')->nullable();
32+
$table->string('category')->nullable();
33+
});
34+
}
35+
}

tests/Unit/UpdaterTest.php

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
namespace distinctm\LaravelDataSync\Tests;
4+
5+
use distinctm\LaravelDataSync\Tests\Fakes\UpdaterFake;
6+
use Exception;
7+
8+
class UpdaterTest extends TestCase
9+
{
10+
/** @test */
11+
public function it_adds_roles_to_the_database()
12+
{
13+
$updater = new UpdaterFake(__DIR__ . '/../test-data', 'roles');
14+
15+
$updater->run();
16+
17+
$this->assertDatabaseHas('roles', ['slug' => 'update-student-records']);
18+
$this->assertDatabaseHas('roles', ['slug' => 'borrow-ferrari']);
19+
$this->assertDatabaseHas('roles', ['slug' => 'destroy-ferrari']);
20+
}
21+
22+
/** @test */
23+
public function it_can_default_to_configuration()
24+
{
25+
config()->set('data-sync.path', __DIR__ . '/../test-data');
26+
27+
$updater = new UpdaterFake();
28+
29+
$updater->run();
30+
31+
$this->assertDatabaseHas('roles', ['slug' => 'update-student-records']);
32+
$this->assertDatabaseHas('roles', ['slug' => 'borrow-ferrari']);
33+
$this->assertDatabaseHas('roles', ['slug' => 'destroy-ferrari']);
34+
}
35+
36+
/** @test */
37+
public function it_can_update_an_existing_record()
38+
{
39+
config()->set('data-sync.path', __DIR__ . '/../test-data');
40+
(new UpdaterFake())->run();
41+
42+
config()->set('data-sync.path', __DIR__ . '/../test-data/valid');
43+
(new UpdaterFake())->run();
44+
45+
$this->assertDatabaseHas('roles', ['category' => 'changed']);
46+
$this->assertDatabaseHas('roles', ['category' => 'changed']);
47+
$this->assertDatabaseHas('roles', ['category' => 'changed']);
48+
}
49+
50+
/** @test */
51+
public function it_can_update_the_relationship()
52+
{
53+
$supervisor = Supervisor::create([
54+
'name' => 'CEO',
55+
]);
56+
57+
config()->set('data-sync.path', __DIR__ . '/../test-data/relationship', 'roles');
58+
(new UpdaterFake())->run();
59+
60+
$this->assertEquals($supervisor->id, Roles::first()->supervisor_id);
61+
$this->assertTrue($supervisor->is(Roles::first()->supervisor));
62+
}
63+
64+
/** @test */
65+
public function exception_is_thrown_if_the_directory_does_not_exists()
66+
{
67+
try {
68+
new UpdaterFake();
69+
70+
$this->fail('exception was thrown');
71+
72+
} catch (Exception $e) {
73+
$this->assertEquals('Specified sync file directory does not exist', $e->getMessage());
74+
}
75+
}
76+
77+
/** @test */
78+
public function invalid_json_throws_an_exception()
79+
{
80+
try {
81+
$updater = new UpdaterFake(__DIR__ . '/../test-data/invalid-json');
82+
$updater->run();
83+
84+
$this->fail('exception was thrown');
85+
86+
} catch (Exception $e) {
87+
$this->assertContains('No records or invalid JSON for', $e->getMessage());
88+
}
89+
90+
}
91+
92+
/** @test */
93+
public function the_json_must_contain_a_key_with_an_underscore()
94+
{
95+
try {
96+
$updater = new UpdaterFake(__DIR__ . '/../test-data/no-criteria');
97+
$updater->run();
98+
99+
$this->fail('exception was thrown');
100+
101+
} catch (Exception $e) {
102+
$this->assertEquals('No criteria/attributes detected', $e->getMessage());
103+
}
104+
105+
}
106+
}

tests/fakes/UpdaterFake.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace distinctm\LaravelDataSync\Tests\Fakes;
4+
5+
use distinctm\LaravelDataSync\Updater;
6+
7+
class UpdaterFake extends Updater
8+
{
9+
protected function getModel(string $name)
10+
{
11+
return '\\distinctm\\LaravelDataSync\\Tests\\' . studly_case(
12+
pathinfo($name, PATHINFO_FILENAME)
13+
);
14+
}
15+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[
2+
3+
]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[
2+
{
3+
"string": "update-student-records"
4+
}
5+
]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[
2+
{
3+
"_slug": "update-student-records",
4+
"category": "testing",
5+
"supervisor": {
6+
"name": "CEO"
7+
}
8+
}
9+
]

tests/test-data/roles.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[
2+
{
3+
"_slug": "update-student-records",
4+
"category": "testing"
5+
},
6+
{
7+
"_slug": "borrow-ferrari",
8+
"category": "cars"
9+
},
10+
{
11+
"_slug": "destroy-ferrari",
12+
"category": "cars"
13+
}
14+
]

tests/test-data/valid/roles.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[
2+
{
3+
"_slug": "update-student-records",
4+
"category": "changed"
5+
},
6+
{
7+
"_slug": "borrow-ferrari",
8+
"category": "changed"
9+
},
10+
{
11+
"_slug": "destroy-ferrari",
12+
"category": "changed"
13+
}
14+
]

0 commit comments

Comments
 (0)