Skip to content

Commit 5c70ea6

Browse files
test: 🧪 add test
1 parent 5783137 commit 5c70ea6

File tree

10 files changed

+270
-28
lines changed

10 files changed

+270
-28
lines changed

tests/ArchTest.php

Lines changed: 0 additions & 5 deletions
This file was deleted.

tests/ExampleTest.php

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
use Illuminate\Database\Schema\Blueprint;
3+
use Illuminate\Support\Facades\Config;
4+
5+
it("add column userstamps", function (): void {
6+
testUserstampsWorkflow();
7+
});
8+
9+
it("add custom column userstamps", function (): void {
10+
Config::set("userstamps.created_by_column", "creator");
11+
Config::set("userstamps.updated_by_column", "editor");
12+
Config::set("userstamps.deleted_by_column", "destroyer");
13+
14+
testUserstampsWorkflow();
15+
});
16+
17+
function testUserstampsWorkflow(): void {
18+
$columns = getColumnNames();
19+
20+
// Test userstamps creation
21+
createTableWithUserstamps();
22+
assertUserstampsExist($columns["created"], $columns["updated"]);
23+
24+
// Test soft userstamps
25+
addSoftUserstamps();
26+
assertSoftUserstampsExist($columns["deleted"]);
27+
28+
// Test userstamps removal
29+
dropUserstamps();
30+
assertUserstampsNotExist($columns["created"], $columns["updated"]);
31+
32+
// Test soft userstamps removal
33+
dropSoftUserstamps();
34+
assertSoftUserstampsNotExist($columns["deleted"]);
35+
}
36+
37+
function getColumnNames(): array {
38+
return [
39+
"created" => Config::get("userstamps.created_by_column"),
40+
"updated" => Config::get("userstamps.updated_by_column"),
41+
"deleted" => Config::get("userstamps.deleted_by_column"),
42+
];
43+
}
44+
45+
function createTableWithUserstamps(): void {
46+
Schema::create("products", function (Blueprint $table): void {
47+
$table->increments("id");
48+
$table->userstamps();
49+
});
50+
}
51+
52+
function addSoftUserstamps(): void {
53+
Schema::table("products", function (Blueprint $table): void {
54+
$table->softUserstamps();
55+
});
56+
}
57+
58+
function dropUserstamps(): void {
59+
Schema::table("products", function (Blueprint $table): void {
60+
$table->dropUserstamps();
61+
});
62+
}
63+
64+
function dropSoftUserstamps(): void {
65+
Schema::table("products", function (Blueprint $table): void {
66+
$table->dropSoftUserstamps();
67+
});
68+
}
69+
70+
function assertUserstampsExist(string $createdColumn, string $updatedColumn): void {
71+
$columns = Schema::getColumnlisting("products");
72+
expect($columns)->toContain("{$createdColumn}_id", "{$createdColumn}_type");
73+
expect($columns)->toContain("{$updatedColumn}_id", "{$updatedColumn}_type");
74+
}
75+
76+
function assertSoftUserstampsExist(string $deletedColumn): void {
77+
$columns = Schema::getColumnlisting("products");
78+
expect($columns)->toContain("{$deletedColumn}_id", "{$deletedColumn}_type");
79+
}
80+
81+
function assertUserstampsNotExist(string $createdColumn, string $updatedColumn): void {
82+
$columns = Schema::getColumnlisting("products");
83+
expect($columns)->not->toContain("{$createdColumn}_id", "{$createdColumn}_type");
84+
expect($columns)->not->toContain("{$updatedColumn}_id", "{$updatedColumn}_type");
85+
}
86+
87+
function assertSoftUserstampsNotExist(string $deletedColumn): void {
88+
$columns = Schema::getColumnlisting("products");
89+
expect($columns)->not->toContain("{$deletedColumn}_id", "{$deletedColumn}_type");
90+
}

tests/Feature/UserstampsTest.php

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
3+
use Illuminate\Database\Schema\Blueprint;
4+
use Illuminate\Support\Facades\Config;
5+
use SanSanLabs\Userstamps\Tests\Models\Product;
6+
use SanSanLabs\Userstamps\Tests\Models\UserWithId;
7+
use SanSanLabs\Userstamps\Tests\Models\UserWithUlid;
8+
use SanSanLabs\Userstamps\Tests\Models\UserWithUuid;
9+
10+
beforeEach(function (): void {
11+
Schema::create("products", function (Blueprint $table): void {
12+
$table->id();
13+
$table->string("name");
14+
$table->timestamps();
15+
$table->softDeletes();
16+
$table->userstamps();
17+
$table->softUserstamps();
18+
});
19+
});
20+
21+
// Helper function to test userstamps behavior
22+
function testUserstampsBehavior($john, $jane, $alice, $productName): void {
23+
test()->actingAs($john);
24+
25+
$product = Product::create(["name" => $productName]);
26+
27+
// Test creation
28+
expect($product->createdBy)->not()->toBeNull();
29+
expect($product->updatedBy)->not()->toBeNull();
30+
expect($product->deletedBy)->toBeNull();
31+
expect($product->createdBy->email)->toBe($john->email);
32+
expect($product->updatedBy->email)->toBe($john->email);
33+
34+
// Test deletion
35+
test()->actingAs($jane);
36+
$product->delete();
37+
$product->refresh();
38+
39+
expect($product->deletedBy)->not()->toBeNull();
40+
expect($product->updatedBy->email)->toBe($jane->email);
41+
expect($product->deletedBy->email)->toBe($jane->email);
42+
43+
// Test restoration
44+
test()->actingAs($alice);
45+
$product->restore();
46+
$product->refresh();
47+
48+
expect($product->deletedBy)->toBeNull();
49+
expect($product->updatedBy->email)->toBe($alice->email);
50+
51+
// Test with trashed users
52+
Config::set("userstamps.with_trashed", true);
53+
$alice->delete();
54+
$alice->refresh();
55+
$product->refresh();
56+
57+
expect($product->updatedBy->email)->toBe($alice->email);
58+
}
59+
60+
it("userstamps use id", function (): void {
61+
Schema::create("user_with_ids", function (Blueprint $table): void {
62+
$table->id();
63+
$table->string("name");
64+
$table->string("email")->unique();
65+
$table->timestamps();
66+
$table->softDeletes();
67+
});
68+
69+
$users = [
70+
UserWithId::create(["name" => "John Doe", "email" => "[email protected]"]),
71+
UserWithId::create(["name" => "Jane Doe", "email" => "[email protected]"]),
72+
UserWithId::create(["name" => "Alice Doe", "email" => "[email protected]"]),
73+
];
74+
75+
testUserstampsBehavior($users[0], $users[1], $users[2], "Test Product ID");
76+
});
77+
78+
it("userstamps use ulid", function (): void {
79+
Config::set("userstamps.users_table_id_column_type", "ulid");
80+
81+
Schema::create("user_with_ulids", function (Blueprint $table): void {
82+
$table->ulid("id")->primary();
83+
$table->string("name");
84+
$table->string("email")->unique();
85+
$table->timestamps();
86+
$table->softDeletes();
87+
});
88+
89+
$users = [
90+
UserWithUlid::create(["name" => "John Doe", "email" => "[email protected]"]),
91+
UserWithUlid::create(["name" => "Jane Doe", "email" => "[email protected]"]),
92+
UserWithUlid::create(["name" => "Alice Doe", "email" => "[email protected]"]),
93+
];
94+
95+
testUserstampsBehavior($users[0], $users[1], $users[2], "Test Product ULID");
96+
});
97+
98+
it("userstamps use uuid", function (): void {
99+
Config::set("userstamps.users_table_id_column_type", "uuid");
100+
101+
Schema::create("user_with_uuids", function (Blueprint $table): void {
102+
$table->uuid("id")->primary();
103+
$table->string("name");
104+
$table->string("email")->unique();
105+
$table->timestamps();
106+
$table->softDeletes();
107+
});
108+
109+
$users = [
110+
UserWithUuid::create(["name" => "John Doe", "email" => "[email protected]"]),
111+
UserWithUuid::create(["name" => "Jane Doe", "email" => "[email protected]"]),
112+
UserWithUuid::create(["name" => "Alice Doe", "email" => "[email protected]"]),
113+
];
114+
115+
testUserstampsBehavior($users[0], $users[1], $users[2], "Test Product UUID");
116+
});

tests/Models/Product.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace SanSanLabs\Userstamps\Tests\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Database\Eloquent\SoftDeletes;
7+
use SanSanLabs\Userstamps\Concerns\HasUserstamps;
8+
9+
class Product extends Model {
10+
use HasUserstamps;
11+
use SoftDeletes;
12+
13+
/**
14+
* The attributes that are mass assignable.
15+
*
16+
* @var array
17+
*/
18+
protected $fillable = ["name"];
19+
}

tests/Models/UserWithId.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace SanSanLabs\Userstamps\Tests\Models;
4+
5+
use Illuminate\Database\Eloquent\SoftDeletes;
6+
use Illuminate\Foundation\Auth\User as Authenticatable;
7+
8+
class UserWithId extends Authenticatable {
9+
use SoftDeletes;
10+
11+
protected $fillable = ["name", "email"];
12+
}

tests/Models/UserWithUlid.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace SanSanLabs\Userstamps\Tests\Models;
4+
5+
use Illuminate\Database\Eloquent\Concerns\HasUlids;
6+
use Illuminate\Database\Eloquent\SoftDeletes;
7+
use Illuminate\Foundation\Auth\User as Authenticatable;
8+
9+
class UserWithUlid extends Authenticatable {
10+
use HasUlids;
11+
use SoftDeletes;
12+
13+
protected $fillable = ["name", "email"];
14+
}

tests/Models/UserWithUuid.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace SanSanLabs\Userstamps\Tests\Models;
4+
5+
use Illuminate\Database\Eloquent\Concerns\HasUuids;
6+
use Illuminate\Database\Eloquent\SoftDeletes;
7+
use Illuminate\Foundation\Auth\User as Authenticatable;
8+
9+
class UserWithUuid extends Authenticatable {
10+
use HasUuids;
11+
use SoftDeletes;
12+
13+
protected $fillable = ["name", "email"];
14+
}

tests/Pest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
22

3-
use VendorName\Skeleton\Tests\TestCase;
3+
use SanSanLabs\Userstamps\Tests\TestCase;
44

55
uses(TestCase::class)->in(__DIR__);

tests/TestCase.php

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,16 @@
11
<?php
22

3-
namespace VendorName\Skeleton\Tests;
3+
namespace SanSanLabs\Userstamps\Tests;
44

5-
use Illuminate\Database\Eloquent\Factories\Factory;
65
use Orchestra\Testbench\TestCase as Orchestra;
7-
use VendorName\Skeleton\SkeletonServiceProvider;
6+
use SanSanLabs\Userstamps\UserstampsServiceProvider;
87

98
class TestCase extends Orchestra {
109
protected function setUp(): void {
1110
parent::setUp();
12-
13-
Factory::guessFactoryNamesUsing(fn(string $modelName) => "VendorName\\Skeleton\\Database\\Factories\\" . class_basename($modelName) . "Factory");
14-
}
15-
16-
protected function getPackageProviders($app) {
17-
return [SkeletonServiceProvider::class];
1811
}
1912

20-
public function getEnvironmentSetUp($app) {
21-
config()->set("database.default", "testing");
22-
23-
/*
24-
foreach (\Illuminate\Support\Facades\File::allFiles(__DIR__ . '/database/migrations') as $migration) {
25-
(include $migration->getRealPath())->up();
26-
}
27-
*/
13+
protected function getPackageProviders($app): array {
14+
return [UserstampsServiceProvider::class];
2815
}
2916
}

0 commit comments

Comments
 (0)