|
| 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 | +}); |
0 commit comments