Skip to content

Commit cafa758

Browse files
committed
Updater class tests
1 parent 02867f5 commit cafa758

File tree

10 files changed

+180
-17
lines changed

10 files changed

+180
-17
lines changed

tests/Roles.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,12 @@
66

77
class Roles extends Model
88
{
9+
public $timestamps = false;
910

11+
protected $guarded = [];
12+
13+
public function supervisor()
14+
{
15+
return $this->belongsTo(Supervisor::class);
16+
}
1017
}

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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace distinctm\LaravelDataSync\Tests;
44

5+
use Illuminate\Database\Schema\Blueprint;
6+
use Illuminate\Support\Facades\Schema;
7+
58
class TestCase extends \Orchestra\Testbench\TestCase
69
{
710
protected function getEnvironmentSetUp($app)
@@ -12,4 +15,21 @@ protected function getEnvironmentSetUp($app)
1215
'database' => ':memory:'
1316
]);
1417
}
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+
}
1535
}

tests/Unit/UpdaterTest.php

Lines changed: 84 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,105 @@
22

33
namespace distinctm\LaravelDataSync\Tests;
44

5-
use distinctm\LaravelDataSync\Updater;
5+
use distinctm\LaravelDataSync\Tests\Fakes\UpdaterFake;
6+
use Exception;
67

78
class UpdaterTest extends TestCase
89
{
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+
964
/** @test */
1065
public function exception_is_thrown_if_the_directory_does_not_exists()
1166
{
12-
$this->expectException(\Exception::class);
67+
try {
68+
new UpdaterFake();
1369

14-
new Updater();
70+
$this->fail('exception was thrown');
71+
72+
} catch (Exception $e) {
73+
$this->assertEquals('Specified sync file directory does not exist', $e->getMessage());
74+
}
1575
}
1676

1777
/** @test */
18-
public function experiment()
78+
public function invalid_json_throws_an_exception()
1979
{
20-
$updater = new UpdaterFake(__DIR__ . '/../test-data', 'roles');
80+
try {
81+
$updater = new UpdaterFake(__DIR__ . '/../test-data/invalid-json');
82+
$updater->run();
2183

22-
\DB::enableQueryLog();
23-
$updater->run();
24-
\DB::disableQueryLog();
84+
$this->fail('exception was thrown');
85+
86+
} catch (Exception $e) {
87+
$this->assertContains('No records or invalid JSON for', $e->getMessage());
88+
}
2589

26-
dd(\DB::getQueryLog());
2790
}
28-
}
2991

30-
class UpdaterFake extends Updater
31-
{
32-
protected function getModel(string $name)
92+
/** @test */
93+
public function the_json_must_contain_a_key_with_an_underscore()
3394
{
34-
return Roles::class;
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+
35105
}
36106
}

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: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
[
22
{
3-
"_slug": "update-student-records"
3+
"_slug": "update-student-records",
4+
"category": "testing"
45
},
56
{
6-
"_slug": "borrow-ferrari"
7+
"_slug": "borrow-ferrari",
8+
"category": "cars"
79
},
810
{
9-
"_slug": "destroy-ferrari"
11+
"_slug": "destroy-ferrari",
12+
"category": "cars"
1013
}
1114
]

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)