generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add some test cases related to supported database column types, and e…
…loquent attribute castings.
- Loading branch information
1 parent
420bf40
commit 746fd61
Showing
24 changed files
with
1,785 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RichanFongdasen\Turso\Enums; | ||
|
||
use PDO; | ||
|
||
enum PdoParam: int | ||
{ | ||
case NULL = PDO::PARAM_NULL; | ||
case BOOL = PDO::PARAM_BOOL; | ||
case INT = PDO::PARAM_INT; | ||
case STR = PDO::PARAM_STR; | ||
case LOB = PDO::PARAM_LOB; | ||
|
||
public static function fromValue(mixed $value): static | ||
{ | ||
return match (gettype($value)) { | ||
'boolean' => self::BOOL, | ||
'double', 'integer' => self::INT, | ||
'resource' => self::LOB, | ||
'NULL' => self::NULL, | ||
default => self::STR, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RichanFongdasen\Turso\Enums; | ||
|
||
/** | ||
* Turso data type enumeration. | ||
* Ref: https://github.com/tursodatabase/libsql/blob/main/docs/HRANA_3_SPEC.md#values. | ||
*/ | ||
enum TursoType: string | ||
{ | ||
case NULL = 'null'; | ||
case INTEGER = 'integer'; | ||
case FLOAT = 'float'; | ||
case TEXT = 'text'; | ||
case BLOB = 'blob'; | ||
|
||
public static function fromValue(mixed $value): static | ||
{ | ||
$result = match (gettype($value)) { | ||
'NULL' => self::NULL, | ||
'boolean', 'integer' => self::INTEGER, | ||
'double', 'float' => self::FLOAT, | ||
'string' => self::fromString($value), | ||
|
||
default => null, | ||
}; | ||
|
||
return ($result !== null) | ||
? $result | ||
: self::TEXT; | ||
} | ||
|
||
public static function fromString(string $value): self | ||
{ | ||
if (! ctype_print($value) || ! mb_check_encoding($value, 'UTF-8')) { | ||
return self::BLOB; | ||
} | ||
|
||
return self::TEXT; | ||
} | ||
|
||
public function bind(mixed $value): array | ||
{ | ||
return match ($this) { | ||
self::NULL => [ | ||
'type' => $this->value, | ||
'value' => 'null', | ||
], | ||
self::FLOAT => [ | ||
'type' => $this->value, | ||
'value' => $value, | ||
], | ||
self::BLOB => [ | ||
'type' => $this->value, | ||
'base64' => base64_encode(base64_encode($value)), | ||
], | ||
|
||
default => [ | ||
'type' => $this->value, | ||
'value' => (string) $value, | ||
], | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\DB; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
beforeEach(function () { | ||
Schema::create('boolean_table', function ($table) { | ||
$table->id(); | ||
$table->boolean('confirmed'); | ||
}); | ||
}); | ||
|
||
afterEach(function () { | ||
Schema::dropIfExists('boolean_table'); | ||
}); | ||
|
||
test('it can insert a new boolean data, and the value will be saved as an integer', function () { | ||
$result = DB::table('boolean_table')->insert([ | ||
'confirmed' => true, | ||
]); | ||
|
||
$newData = DB::table('boolean_table')->first(); | ||
|
||
expect($result)->toBeTrue() | ||
->and(DB::table('boolean_table')->count())->toBe(1) | ||
->and($newData->confirmed)->toBe(1); | ||
})->group('BooleanDataTest', 'DataTypes', 'FeatureTest'); | ||
|
||
test('it can update an existing boolean data, and the retrieved value will be an integer', function () { | ||
DB::table('boolean_table')->insert([ | ||
'confirmed' => true, | ||
]); | ||
|
||
$result = DB::table('boolean_table')->update([ | ||
'confirmed' => false, | ||
]); | ||
|
||
$updatedData = DB::table('boolean_table')->first(); | ||
|
||
expect($result)->toBe(1) | ||
->and($updatedData->confirmed)->toBe(0); | ||
})->group('BooleanDataTest', 'DataTypes', 'FeatureTest'); | ||
|
||
test('it can find the saved record', function () { | ||
DB::table('boolean_table')->insert([ | ||
'confirmed' => true, | ||
]); | ||
DB::table('boolean_table')->insert([ | ||
'confirmed' => false, | ||
]); | ||
|
||
$found = DB::table('boolean_table')->where('confirmed', false)->first(); | ||
|
||
expect($found->id)->toBe(2) | ||
->and($found->confirmed)->toBe(0); | ||
})->group('BooleanDataTest', 'DataTypes', 'FeatureTest'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\DB; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
beforeEach(function () { | ||
Schema::create('date_table', function ($table) { | ||
$table->id(); | ||
$table->date('started_at'); | ||
}); | ||
}); | ||
|
||
afterEach(function () { | ||
Schema::dropIfExists('date_table'); | ||
}); | ||
|
||
test('it can insert a new date data', function () { | ||
$date = '2021-01-01'; | ||
|
||
$result = DB::table('date_table')->insert([ | ||
'started_at' => $date, | ||
]); | ||
|
||
$newData = DB::table('date_table')->first(); | ||
|
||
expect($result)->toBeTrue() | ||
->and(DB::table('date_table')->count())->toBe(1) | ||
->and($newData->started_at)->toBe($date); | ||
})->group('DateDataTest', 'DataTypes', 'FeatureTest'); | ||
|
||
test('it can update an existing date data', function () { | ||
$date = '2021-01-01'; | ||
|
||
DB::table('date_table')->insert([ | ||
'started_at' => $date, | ||
]); | ||
|
||
$newDate = '2021-02-01'; | ||
|
||
$result = DB::table('date_table')->update([ | ||
'started_at' => $newDate, | ||
]); | ||
|
||
$updatedData = DB::table('date_table')->first(); | ||
|
||
expect($result)->toBe(1) | ||
->and($updatedData->started_at)->toBe($newDate); | ||
})->group('DateDataTest', 'DataTypes', 'FeatureTest'); | ||
|
||
test('it can find the saved record', function () { | ||
$date = '2021-01-01'; | ||
|
||
DB::table('date_table')->insert([ | ||
'started_at' => '2021-02-01', | ||
]); | ||
DB::table('date_table')->insert([ | ||
'started_at' => $date, | ||
]); | ||
|
||
$found = DB::table('date_table')->where('started_at', $date)->first(); | ||
|
||
expect($found->id)->toBe(2) | ||
->and($found->started_at)->toBe($date); | ||
})->group('DateDataTest', 'DataTypes', 'FeatureTest'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\DB; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
beforeEach(function () { | ||
Schema::create('datetime_table', function ($table) { | ||
$table->id(); | ||
$table->dateTime('published_at'); | ||
$table->timestamps(); | ||
}); | ||
}); | ||
|
||
afterEach(function () { | ||
Schema::dropIfExists('datetime_table'); | ||
}); | ||
|
||
test('it can insert a new datetime data', function () { | ||
$publishedAt = now(); | ||
|
||
$result = DB::table('datetime_table')->insert([ | ||
'published_at' => $publishedAt, | ||
]); | ||
|
||
$newData = DB::table('datetime_table')->first(); | ||
|
||
expect($result)->toBeTrue() | ||
->and(DB::table('datetime_table')->count())->toBe(1) | ||
->and($newData->published_at)->toBe($publishedAt->format('Y-m-d H:i:s')); | ||
})->group('DateTimeDataTest', 'DataTypes', 'FeatureTest'); | ||
|
||
test('it can update an existing datetime data', function () { | ||
$publishedAt = now(); | ||
|
||
DB::table('datetime_table')->insert([ | ||
'published_at' => $publishedAt, | ||
]); | ||
|
||
$newPublishedAt = now()->subDay(); | ||
|
||
$result = DB::table('datetime_table')->update([ | ||
'published_at' => $newPublishedAt, | ||
]); | ||
|
||
$updatedData = DB::table('datetime_table')->first(); | ||
|
||
expect($result)->toBe(1) | ||
->and($updatedData->published_at)->toBe($newPublishedAt->format('Y-m-d H:i:s')); | ||
})->group('DateTimeDataTest', 'DataTypes', 'FeatureTest'); | ||
|
||
test('it can find the saved record', function () { | ||
$publishedAt = now(); | ||
|
||
DB::table('datetime_table')->insert([ | ||
'published_at' => now()->subDay(), | ||
]); | ||
DB::table('datetime_table')->insert([ | ||
'published_at' => $publishedAt, | ||
]); | ||
|
||
$found = DB::table('datetime_table')->where('published_at', '>=', $publishedAt->format('Y-m-d H:i:s'))->first(); | ||
|
||
expect($found->id)->toBe(2) | ||
->and($found->published_at)->toBe($publishedAt->format('Y-m-d H:i:s')); | ||
})->group('DateTimeDataTest', 'DataTypes', 'FeatureTest'); |
Oops, something went wrong.