Bump php to ^8.0, add tooling, linting, fixes

This commit is contained in:
Ismo Vuorinen
2024-12-11 12:58:22 +02:00
parent e12085be2c
commit aec5ec026a
30 changed files with 4467 additions and 2018 deletions

View File

@@ -3,6 +3,7 @@
namespace nullthoughts\LaravelDataSync\Tests;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Roles extends Model
{
@@ -10,7 +11,7 @@ class Roles extends Model
protected $guarded = [];
public function supervisor()
public function supervisor(): BelongsTo
{
return $this->belongsTo(Supervisor::class);
}

View File

@@ -3,6 +3,7 @@
namespace nullthoughts\LaravelDataSync\Tests;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
class Supervisor extends Model
{
@@ -10,7 +11,7 @@ class Supervisor extends Model
protected $guarded = [];
public function roles()
public function roles(): HasMany
{
return $this->hasMany(Roles::class);
}

View File

@@ -7,25 +7,30 @@ use Illuminate\Support\Facades\Schema;
class TestCase extends \Orchestra\Testbench\TestCase
{
protected function getEnvironmentSetUp($app)
/** @psalm-suppress PropertyNotSetInConstructor */
public string $testDataPath;
protected function getEnvironmentSetUp($app): void
{
$app['config']->set('database.default', 'testdb');
$app['config']->set('database.connections.testdb', [
'driver' => 'sqlite',
'driver' => 'sqlite',
'database' => ':memory:',
]);
$this->testDataPath = __DIR__.'/test-data';
}
protected function setUp(): void
{
parent::setUp();
Schema::create('supervisors', function (Blueprint $table) {
Schema::create('supervisors', static function (Blueprint $table): void {
$table->increments('id');
$table->string('name');
});
Schema::create('roles', function (Blueprint $table) {
Schema::create('roles', static function (Blueprint $table): void {
$table->increments('id');
$table->string('slug');
$table->unsignedInteger('supervisor_id')->nullable();

View File

@@ -1,12 +1,15 @@
<?php
namespace nullthoughts\LaravelDataSync\Tests;
namespace nullthoughts\LaravelDataSync\Tests\Unit;
use Exception;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
use nullthoughts\LaravelDataSync\Exceptions\ErrorUpdatingModelException;
use nullthoughts\LaravelDataSync\Tests\fakes\UpdaterFake;
use Exception;
use nullthoughts\LaravelDataSync\Tests\Fakes\UpdaterFake;
use nullthoughts\LaravelDataSync\Tests\Roles;
use nullthoughts\LaravelDataSync\Tests\Supervisor;
use nullthoughts\LaravelDataSync\Tests\TestCase;
class UpdaterRemoteTest extends TestCase
{
@@ -81,9 +84,10 @@ class UpdaterRemoteTest extends TestCase
/**
* @test
*
* @group current
*/
public function exception_is_thrown_if_the_directory_does_not_exists()
public function exception_is_thrown_if_the_directory_does_not_exists(): void
{
try {
new UpdaterFake(null, null, true, 's3');
@@ -95,7 +99,7 @@ class UpdaterRemoteTest extends TestCase
}
/** @test */
public function invalid_json_throws_an_exception_in_remote()
public function invalid_json_throws_an_exception_in_remote(): void
{
try {
$updater = new UpdaterFake('test-data/invalid-json', null, true, 's3');
@@ -108,7 +112,7 @@ class UpdaterRemoteTest extends TestCase
}
/** @test */
public function the_json_must_contain_a_key_with_an_underscore_in_remote()
public function the_json_must_contain_a_key_with_an_underscore_in_remote(): void
{
try {
$updater = new UpdaterFake('test-data/no-criteria', null, true, 's3');
@@ -121,7 +125,7 @@ class UpdaterRemoteTest extends TestCase
}
/** @test */
public function order_of_imports_can_be_defined_in_config_in_remote()
public function order_of_imports_can_be_defined_in_config_in_remote(): void
{
config()->set('data-sync.order', [
'Supervisor',
@@ -136,7 +140,7 @@ class UpdaterRemoteTest extends TestCase
}
/** @test */
public function exception_is_thrown_if_imports_are_in_incorrect_order_in_remote()
public function exception_is_thrown_if_imports_are_in_incorrect_order_in_remote(): void
{
config()->set('data-sync.order', [
'Roles',
@@ -150,7 +154,7 @@ class UpdaterRemoteTest extends TestCase
}
/** @test */
public function it_ignores_non_json_files_in_remote()
public function it_ignores_non_json_files_in_remote(): void
{
$updater = new UpdaterFake('test-data/not-json', null, true, 's3');
$updater->run();

View File

@@ -1,17 +1,20 @@
<?php
namespace nullthoughts\LaravelDataSync\Tests;
namespace nullthoughts\LaravelDataSync\Tests\Unit;
use Exception;
use nullthoughts\LaravelDataSync\Exceptions\ErrorUpdatingModelException;
use nullthoughts\LaravelDataSync\Tests\fakes\UpdaterFake;
use Exception;
use nullthoughts\LaravelDataSync\Tests\Roles;
use nullthoughts\LaravelDataSync\Tests\Supervisor;
use nullthoughts\LaravelDataSync\Tests\TestCase;
class UpdaterTest extends TestCase
{
/** @test */
public function it_adds_roles_to_the_database()
public function it_adds_roles_to_the_database(): void
{
$updater = new UpdaterFake(__DIR__.'/../test-data', 'roles');
$updater = new UpdaterFake($this->testDataPath, 'roles');
$updater->run();
@@ -23,9 +26,9 @@ class UpdaterTest extends TestCase
/** @test */
public function it_can_default_to_configuration()
{
config()->set('data-sync.path', __DIR__.'/../test-data');
config()->set('data-sync.path', $this->testDataPath);
$updater = new UpdaterFake();
$updater = new UpdaterFake;
$updater->run();
@@ -37,11 +40,11 @@ class UpdaterTest extends TestCase
/** @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', $this->testDataPath);
(new UpdaterFake)->run();
config()->set('data-sync.path', __DIR__.'/../test-data/valid');
(new UpdaterFake())->run();
config()->set('data-sync.path', $this->testDataPath.'/valid');
(new UpdaterFake)->run();
$this->assertDatabaseHas('roles', ['category' => 'changed']);
$this->assertDatabaseHas('roles', ['category' => 'changed']);
@@ -49,24 +52,24 @@ class UpdaterTest extends TestCase
}
/** @test */
public function it_can_update_the_relationship()
public function it_can_update_the_relationship(): void
{
$supervisor = Supervisor::create([
'name' => 'CEO',
]);
config()->set('data-sync.path', __DIR__.'/../test-data/relationship', 'roles');
(new UpdaterFake())->run();
config()->set('data-sync.path', $this->testDataPath.'/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()
public function exception_is_thrown_if_the_directory_does_not_exists(): void
{
try {
new UpdaterFake();
new UpdaterFake;
$this->fail('exception was thrown');
} catch (Exception $e) {
@@ -75,10 +78,10 @@ class UpdaterTest extends TestCase
}
/** @test */
public function invalid_json_throws_an_exception()
public function invalid_json_throws_an_exception(): void
{
try {
$updater = new UpdaterFake(__DIR__.'/../test-data/invalid-json');
$updater = new UpdaterFake($this->testDataPath.'/invalid-json');
$updater->run();
$this->fail('exception was thrown');
@@ -88,10 +91,10 @@ class UpdaterTest extends TestCase
}
/** @test */
public function the_json_must_contain_a_key_with_an_underscore()
public function the_json_must_contain_a_key_with_an_underscore(): void
{
try {
$updater = new UpdaterFake(__DIR__.'/../test-data/no-criteria');
$updater = new UpdaterFake($this->testDataPath.'/no-criteria');
$updater->run();
$this->fail('exception was thrown');
@@ -101,22 +104,21 @@ class UpdaterTest extends TestCase
}
/** @test */
public function order_of_imports_can_be_defined_in_config()
public function order_of_imports_can_be_defined_in_config(): void
{
config()->set('data-sync.order', [
'Supervisor',
'Roles',
]);
$updater = new UpdaterFake(__DIR__.'/../test-data/ordered');
$updater = new UpdaterFake($this->testDataPath.'/ordered');
$updater->run();
$this->assertDatabaseHas('roles', ['slug' => 'update-student-records']);
$this->assertDatabaseHas('supervisors', ['name' => 'CEO']);
}
/** @test */
public function exception_is_thrown_if_imports_are_in_incorrect_order()
public function exception_is_thrown_if_imports_are_in_incorrect_order(): void
{
config()->set('data-sync.order', [
'Roles',
@@ -125,14 +127,14 @@ class UpdaterTest extends TestCase
$this->expectException(ErrorUpdatingModelException::class);
$updater = new UpdaterFake(__DIR__.'/../test-data/ordered');
$updater = new UpdaterFake($this->testDataPath.'/ordered');
$updater->run();
}
/** @test */
public function it_ignores_non_json_files()
public function it_ignores_non_json_files(): void
{
$updater = new UpdaterFake(__DIR__.'/../test-data/not-json');
$updater = new UpdaterFake($this->testDataPath.'/not-json');
$updater->run();
$this->assertDatabaseMissing('roles', ['slug' => 'update-student-records']);

View File

@@ -2,15 +2,15 @@
namespace nullthoughts\LaravelDataSync\Tests\Fakes;
use nullthoughts\LaravelDataSync\Updater;
use Illuminate\Support\Str;
use nullthoughts\LaravelDataSync\Updater;
class UpdaterFake extends Updater
{
protected function getModel(string $name)
{
return '\\nullthoughts\\LaravelDataSync\\Tests\\'.Str::studly(
pathinfo($name, PATHINFO_FILENAME)
);
pathinfo($name, PATHINFO_FILENAME)
);
}
}
}

View File

@@ -1,3 +1 @@
[
]
[]

View File

@@ -2,4 +2,4 @@
{
"string": "update-student-records"
}
]
]

View File

@@ -6,4 +6,4 @@
"name": "CEO"
}
}
]
]

View File

@@ -6,4 +6,4 @@
"name": "CEO"
}
}
]
]

View File

@@ -2,4 +2,4 @@
{
"_name": "CEO"
}
]
]

View File

@@ -6,4 +6,4 @@
"name": "CEO"
}
}
]
]

View File

@@ -11,4 +11,4 @@
"_slug": "destroy-ferrari",
"category": "cars"
}
]
]

View File

@@ -11,4 +11,4 @@
"_slug": "destroy-ferrari",
"category": "changed"
}
]
]