|
|
@@ -0,0 +1,133 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App\Module\Game\Tests;
|
|
|
+
|
|
|
+use App\Module\Game\Hooks\TestHook;
|
|
|
+use App\Module\Game\Models\Test;
|
|
|
+use Illuminate\Foundation\Testing\DatabaseTransactions;
|
|
|
+use Tests\TestCase;
|
|
|
+
|
|
|
+class TestHookTest extends TestCase
|
|
|
+{
|
|
|
+ use DatabaseTransactions;
|
|
|
+
|
|
|
+ protected TestHook $hook;
|
|
|
+
|
|
|
+ public function setUp(): void
|
|
|
+ {
|
|
|
+ parent::setUp();
|
|
|
+ $this->hook = app(TestHook::class);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 测试创建并通知
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function test_create_and_notify(): void
|
|
|
+ {
|
|
|
+ $data = [
|
|
|
+ 'name' => '测试数据',
|
|
|
+ 'code' => 'TEST001',
|
|
|
+ 'description' => '测试描述'
|
|
|
+ ];
|
|
|
+
|
|
|
+ $result = $this->hook->createAndNotify($data);
|
|
|
+
|
|
|
+ $this->assertIsArray($result);
|
|
|
+ $this->assertEquals('测试数据创建成功', $result['title']);
|
|
|
+ $this->assertStringContainsString($data['name'], $result['content']);
|
|
|
+ $this->assertStringContainsString($data['code'], $result['content']);
|
|
|
+ $this->assertArrayHasKey('test_id', $result['data']);
|
|
|
+ $this->assertEquals($data['name'], $result['data']['name']);
|
|
|
+ $this->assertEquals($data['code'], $result['data']['code']);
|
|
|
+
|
|
|
+ // 验证数据是否真的创建了
|
|
|
+ $test = Test::find($result['data']['test_id']);
|
|
|
+ $this->assertNotNull($test);
|
|
|
+ $this->assertEquals($data['name'], $test->name);
|
|
|
+ $this->assertEquals($data['code'], $test->code);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 测试更新并通知
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function test_update_and_notify(): void
|
|
|
+ {
|
|
|
+ // 创建测试数据
|
|
|
+ $test = Test::factory()->create();
|
|
|
+
|
|
|
+ $data = [
|
|
|
+ 'name' => '更新数据',
|
|
|
+ 'code' => 'TEST002'
|
|
|
+ ];
|
|
|
+
|
|
|
+ $result = $this->hook->updateAndNotify($test->id, $data);
|
|
|
+
|
|
|
+ $this->assertIsArray($result);
|
|
|
+ $this->assertEquals('测试数据更新成功', $result['title']);
|
|
|
+ $this->assertStringContainsString($data['name'], $result['content']);
|
|
|
+ $this->assertStringContainsString($data['code'], $result['content']);
|
|
|
+ $this->assertEquals($test->id, $result['data']['test_id']);
|
|
|
+ $this->assertEquals($data['name'], $result['data']['name']);
|
|
|
+ $this->assertEquals($data['code'], $result['data']['code']);
|
|
|
+
|
|
|
+ // 验证数据是否真的更新了
|
|
|
+ $test->refresh();
|
|
|
+ $this->assertEquals($data['name'], $test->name);
|
|
|
+ $this->assertEquals($data['code'], $test->code);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 测试更新不存在的数据
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function test_update_non_existent(): void
|
|
|
+ {
|
|
|
+ $result = $this->hook->updateAndNotify(999, [
|
|
|
+ 'name' => '更新数据',
|
|
|
+ 'code' => 'TEST002'
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $this->assertNull($result);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 测试删除并通知
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function test_delete_and_notify(): void
|
|
|
+ {
|
|
|
+ // 创建测试数据
|
|
|
+ $test = Test::factory()->create();
|
|
|
+
|
|
|
+ $result = $this->hook->deleteAndNotify($test->id);
|
|
|
+
|
|
|
+ $this->assertIsArray($result);
|
|
|
+ $this->assertEquals('测试数据删除成功', $result['title']);
|
|
|
+ $this->assertStringContainsString($test->name, $result['content']);
|
|
|
+ $this->assertStringContainsString($test->code, $result['content']);
|
|
|
+ $this->assertEquals($test->id, $result['data']['test_id']);
|
|
|
+ $this->assertEquals($test->name, $result['data']['name']);
|
|
|
+ $this->assertEquals($test->code, $result['data']['code']);
|
|
|
+
|
|
|
+ // 验证数据是否真的删除了
|
|
|
+ $this->assertNull(Test::find($test->id));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 测试删除不存在的数据
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function test_delete_non_existent(): void
|
|
|
+ {
|
|
|
+ $result = $this->hook->deleteAndNotify(999);
|
|
|
+
|
|
|
+ $this->assertNull($result);
|
|
|
+ }
|
|
|
+}
|