|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Hypervel\Tests\Core\Database\Eloquent\Concerns; |
| 6 | + |
| 7 | +use Hyperf\Database\Model\Booted; |
| 8 | +use Hyperf\Database\Model\TraitInitializers; |
| 9 | +use Hypervel\Database\Eloquent\Attributes\Boot; |
| 10 | +use Hypervel\Database\Eloquent\Attributes\Initialize; |
| 11 | +use Hypervel\Database\Eloquent\Model; |
| 12 | +use Hypervel\Testbench\TestCase; |
| 13 | + |
| 14 | +/** |
| 15 | + * @internal |
| 16 | + * @coversNothing |
| 17 | + */ |
| 18 | +class HasBootableTraitsTest extends TestCase |
| 19 | +{ |
| 20 | + protected function setUp(): void |
| 21 | + { |
| 22 | + parent::setUp(); |
| 23 | + |
| 24 | + // Reset model booted state so each test starts fresh |
| 25 | + Booted::$container = []; |
| 26 | + TraitInitializers::$container = []; |
| 27 | + |
| 28 | + // Reset static state before each test |
| 29 | + BootableTraitsTestModel::$bootCalled = false; |
| 30 | + BootableTraitsTestModel::$conventionalBootCalled = false; |
| 31 | + BootableTraitsTestModel::$initializeCalled = false; |
| 32 | + BootableTraitsTestModel::$conventionalInitializeCalled = false; |
| 33 | + BootableTraitsTestModel::$bootCallCount = 0; |
| 34 | + BootableTraitsTestModel::$initializeCallCount = 0; |
| 35 | + } |
| 36 | + |
| 37 | + protected function tearDown(): void |
| 38 | + { |
| 39 | + // Reset model booted state |
| 40 | + Booted::$container = []; |
| 41 | + TraitInitializers::$container = []; |
| 42 | + |
| 43 | + // Reset static state after each test |
| 44 | + BootableTraitsTestModel::$bootCalled = false; |
| 45 | + BootableTraitsTestModel::$conventionalBootCalled = false; |
| 46 | + BootableTraitsTestModel::$initializeCalled = false; |
| 47 | + BootableTraitsTestModel::$conventionalInitializeCalled = false; |
| 48 | + BootableTraitsTestModel::$bootCallCount = 0; |
| 49 | + BootableTraitsTestModel::$initializeCallCount = 0; |
| 50 | + |
| 51 | + parent::tearDown(); |
| 52 | + } |
| 53 | + |
| 54 | + public function testBootAttributeCallsStaticMethodDuringBoot(): void |
| 55 | + { |
| 56 | + $this->assertFalse(BootableTraitsTestModel::$bootCalled); |
| 57 | + |
| 58 | + // Creating a model triggers boot |
| 59 | + new BootableTraitsTestModel(); |
| 60 | + |
| 61 | + $this->assertTrue(BootableTraitsTestModel::$bootCalled); |
| 62 | + } |
| 63 | + |
| 64 | + public function testConventionalBootMethodStillWorks(): void |
| 65 | + { |
| 66 | + $this->assertFalse(BootableTraitsTestModel::$conventionalBootCalled); |
| 67 | + |
| 68 | + new BootableTraitsTestModel(); |
| 69 | + |
| 70 | + $this->assertTrue(BootableTraitsTestModel::$conventionalBootCalled); |
| 71 | + } |
| 72 | + |
| 73 | + public function testInitializeAttributeAddsMethodToInitializers(): void |
| 74 | + { |
| 75 | + $this->assertFalse(BootableTraitsTestModel::$initializeCalled); |
| 76 | + |
| 77 | + // Creating a model triggers initialize |
| 78 | + new BootableTraitsTestModel(); |
| 79 | + |
| 80 | + $this->assertTrue(BootableTraitsTestModel::$initializeCalled); |
| 81 | + } |
| 82 | + |
| 83 | + public function testConventionalInitializeMethodStillWorks(): void |
| 84 | + { |
| 85 | + $this->assertFalse(BootableTraitsTestModel::$conventionalInitializeCalled); |
| 86 | + |
| 87 | + new BootableTraitsTestModel(); |
| 88 | + |
| 89 | + $this->assertTrue(BootableTraitsTestModel::$conventionalInitializeCalled); |
| 90 | + } |
| 91 | + |
| 92 | + public function testBothAttributeAndConventionalMethodsWorkTogether(): void |
| 93 | + { |
| 94 | + $this->assertFalse(BootableTraitsTestModel::$bootCalled); |
| 95 | + $this->assertFalse(BootableTraitsTestModel::$conventionalBootCalled); |
| 96 | + $this->assertFalse(BootableTraitsTestModel::$initializeCalled); |
| 97 | + $this->assertFalse(BootableTraitsTestModel::$conventionalInitializeCalled); |
| 98 | + |
| 99 | + new BootableTraitsTestModel(); |
| 100 | + |
| 101 | + $this->assertTrue(BootableTraitsTestModel::$bootCalled); |
| 102 | + $this->assertTrue(BootableTraitsTestModel::$conventionalBootCalled); |
| 103 | + $this->assertTrue(BootableTraitsTestModel::$initializeCalled); |
| 104 | + $this->assertTrue(BootableTraitsTestModel::$conventionalInitializeCalled); |
| 105 | + } |
| 106 | + |
| 107 | + public function testBootMethodIsOnlyCalledOnce(): void |
| 108 | + { |
| 109 | + BootableTraitsTestModel::$bootCallCount = 0; |
| 110 | + |
| 111 | + new BootableTraitsTestModel(); |
| 112 | + new BootableTraitsTestModel(); |
| 113 | + new BootableTraitsTestModel(); |
| 114 | + |
| 115 | + // Boot should only be called once regardless of how many instances |
| 116 | + $this->assertSame(1, BootableTraitsTestModel::$bootCallCount); |
| 117 | + } |
| 118 | + |
| 119 | + public function testInitializeMethodIsCalledForEachInstance(): void |
| 120 | + { |
| 121 | + BootableTraitsTestModel::$initializeCallCount = 0; |
| 122 | + |
| 123 | + new BootableTraitsTestModel(); |
| 124 | + new BootableTraitsTestModel(); |
| 125 | + new BootableTraitsTestModel(); |
| 126 | + |
| 127 | + // Initialize should be called for each instance |
| 128 | + $this->assertSame(3, BootableTraitsTestModel::$initializeCallCount); |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +// Test trait with #[Boot] attribute method |
| 133 | +trait HasCustomBootMethod |
| 134 | +{ |
| 135 | + #[Boot] |
| 136 | + public static function customBootMethod(): void |
| 137 | + { |
| 138 | + static::$bootCalled = true; |
| 139 | + ++static::$bootCallCount; |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +// Test trait with conventional boot method |
| 144 | +trait HasConventionalBootMethod |
| 145 | +{ |
| 146 | + public static function bootHasConventionalBootMethod(): void |
| 147 | + { |
| 148 | + static::$conventionalBootCalled = true; |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +// Test trait with #[Initialize] attribute method |
| 153 | +trait HasCustomInitializeMethod |
| 154 | +{ |
| 155 | + #[Initialize] |
| 156 | + public function customInitializeMethod(): void |
| 157 | + { |
| 158 | + static::$initializeCalled = true; |
| 159 | + ++static::$initializeCallCount; |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +// Test trait with conventional initialize method |
| 164 | +trait HasConventionalInitializeMethod |
| 165 | +{ |
| 166 | + public function initializeHasConventionalInitializeMethod(): void |
| 167 | + { |
| 168 | + static::$conventionalInitializeCalled = true; |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +class BootableTraitsTestModel extends Model |
| 173 | +{ |
| 174 | + use HasCustomBootMethod; |
| 175 | + use HasConventionalBootMethod; |
| 176 | + use HasCustomInitializeMethod; |
| 177 | + use HasConventionalInitializeMethod; |
| 178 | + |
| 179 | + public static bool $bootCalled = false; |
| 180 | + |
| 181 | + public static bool $conventionalBootCalled = false; |
| 182 | + |
| 183 | + public static bool $initializeCalled = false; |
| 184 | + |
| 185 | + public static bool $conventionalInitializeCalled = false; |
| 186 | + |
| 187 | + public static int $bootCallCount = 0; |
| 188 | + |
| 189 | + public static int $initializeCallCount = 0; |
| 190 | + |
| 191 | + protected ?string $table = 'test_models'; |
| 192 | +} |
0 commit comments