Pārlūkot izejas kodu

feat(points): 添加用户积分功能并编写相关测试

- 在 UserPointEntity 中添加 add 方法,用于增加用户积分
- 创建 UserPointEntityTest 单元测试类,测试积分添加功能
- 更新 UserPointModel,将 table 属性改为 name 属性
runphp 6 mēneši atpakaļ
vecāks
revīzija
e307cc2812

+ 15 - 1
src/Entity/UserPointEntity.php

@@ -3,8 +3,22 @@ declare(strict_types=1);
 namespace SixShop\Points\Entity;
 
 use SixShop\Core\Entity\BaseEntity;
+use SixShop\Points\Model\UserPointModel;
 
 class UserPointEntity extends BaseEntity
 {
-
+    public function add(int $userID, int $point): UserPointModel
+    {
+        $userPoint = $this->where('user_id', $userID)->findOrEmpty();
+        if ($userPoint->isEmpty()) {
+            $userPoint = $this->create([
+                'user_id' => $userID,
+                'point' => $point,
+            ]);
+        } else {
+            $userPoint->inc('point', $point)->save();
+        }
+        // tood log
+        return $userPoint;
+    }
 }

+ 2 - 2
src/Model/UserPointModel.php

@@ -6,10 +6,10 @@ use think\Model;
 
 class UserPointModel extends Model
 {
-    protected function getOptions()
+    protected function getOptions(): array
     {
         return [
-            'table' => 'extension_user_point',
+            'name' => 'extension_user_point',
             'pk' => 'user_id',
         ];
     }

+ 22 - 0
test/Entity/UserPointEntityTest.php

@@ -0,0 +1,22 @@
+<?php
+declare(strict_types=1);
+namespace SixShop\Points\Entity;
+
+use PHPUnit\Framework\TestCase;
+
+class UserPointEntityTest extends TestCase
+{
+    protected UserPointEntity $userPointEntity;
+    protected function setUp(): void
+    {
+        $this->userPointEntity = app(UserPointEntity::class);
+    }
+
+    /**
+     * 测试添加积分
+     */
+    public  function testAdd()
+    {
+        $this->userPointEntity->add(1, 100);
+    }
+}