소스 검색

定义了测试用例的执行bat文件
写了CacheService的调用方法和相应测试

LD 5 년 전
부모
커밋
676fab056a

+ 9 - 1
app/Http/Controllers/TestController.php

@@ -737,7 +737,15 @@ where (commodities.owner_id,commodity_barcodes.code) in (select commodities.owne
     {
         /** @var CacheService $cacheService */
         $cacheService = app('CacheService');
-        dd(config('cache.expireTime.authorities'));
+        $authorities=$cacheService->getOrExecute('userxx',function (){
+            return Authority::with('roles')->get();
+        });
+        dd($authorities);
+    }
+    public function output2()
+    {
+        dump(Cache::get('aa'));
+        Cache::put('aa', '2223', 5);
     }
     public function usage()
     {

+ 11 - 5
app/Providers/AuthServiceProvider.php

@@ -4,6 +4,7 @@ namespace App\Providers;
 
 use App\Authority;
 use App\Services\AuthorityService;
+use App\Services\CacheService;
 use App\User;
 use Illuminate\Support\Facades\Auth;
 use Illuminate\Support\Facades\Cache;
@@ -33,6 +34,8 @@ class AuthServiceProvider extends ServiceProvider
 
         $isSuperAdmin = null;
         if(!Schema::hasTable('users')){return;}
+        /** @var CacheService $cacheService */
+        $cacheService = app('CacheService');
         Gate::before(function ($user)use(&$isSuperAdmin) {
             if($isSuperAdmin===null){
                 $isSuperAdmin=$user->isSuperAdmin();
@@ -44,17 +47,20 @@ class AuthServiceProvider extends ServiceProvider
             }
         });
         if(!Schema::hasTable('authorities')){return;}
-        /** @var AuthorityService $authorityService */
-        $authorityService=app('AuthorityService');
-        $authorities = $authorityService->getAll();
+        $authorities=$cacheService->getOrExecute('authorities',function (){
+            return Authority::with('roles')->get();
+        });
         foreach($authorities as $authority) {
-            Gate::define($authority->name, function($user) use ($authority) {
+            Gate::define($authority->name, function($user) use ($authority,$cacheService) {
                 if(Cache::get('isSuperAdmin')){
                     if($authority['permission']=='允许'){
                         return true;
                     }
                 }
-                return $user->hasRole($authority->roles);
+                $authorityRoles=$cacheService->getOrExecute("authority{$authority['id']}->roles",function ()use($authority){
+                    return $authority->roles;
+                });
+                return $user->hasRole($authorityRoles);
             });
         }
     }

+ 26 - 26
app/Services/AuthorityService.php

@@ -11,30 +11,30 @@ use Illuminate\Support\Facades\Cache;
 
 class AuthorityService
 {
-    /**
-     * @return Authority[]
-     */
-    function getAll(){
-        $authorities = Cache::get('authorities');
-        if(!$authorities){
-            $authorities = Authority::with('roles')->get();
-            Cache::put('authorities', $authorities, config('cache.expirations.authorities'));
-        }
-        return $authorities;
-    }
-
-    /**
-     * @param Authority $authority
-     * @return Role[]
-     * @throws Exception
-     */
-    function getRoles(Authority $authority){
-        if(!$authority['id']??'') throw new Exception('User对象或id不能为空');
-        $roles = Cache::get('authorityGetRoles'.$authority['id']);
-        if(!$roles){
-            $roles = Authority::with('roles')->get();
-            Cache::put('authorities', $roles, config('cache.expirations.authorities'));
-        }
-        return $roles;
-    }
+//    /**
+//     * @return Authority[]
+//     */
+//    function getAll(){
+//        $authorities = Cache::get('authorities');
+//        if(!$authorities){
+//            $authorities = Authority::with('roles')->get();
+//            Cache::put('authorities', $authorities, config('cache.expirations.authorities'));
+//        }
+//        return $authorities;
+//    }
+//
+//    /**
+//     * @param Authority $authority
+//     * @return Role[]
+//     * @throws Exception
+//     */
+//    function getRoles(Authority $authority){
+//        if(!$authority['id']??'') throw new Exception('User对象或id不能为空');
+//        $roles = Cache::get('authorityGetRoles'.$authority['id']);
+//        if(!$roles){
+//            $roles = Authority::with('roles')->get();
+//            Cache::put('authorities', $roles, config('cache.expirations.authorities'));
+//        }
+//        return $roles;
+//    }
 }

+ 1 - 0
app/Services/CacheService.php

@@ -11,6 +11,7 @@ class CacheService
     function getOrExecute(String $key, $func){
         $results = Cache::get($key);
         if(!$results){
+            if(!$func||gettype($func)!='object') throw new \Exception('执行函数类型错误');
             $results = $func();
             if(!$results)return null;
             Cache::put($key, $results, config('cache.expirations.default'));

+ 5 - 5
app/Services/InventoryCompareService.php

@@ -106,10 +106,10 @@ class InventoryCompareService
             $wmsInventoryCompareZp=$query->where('属性仓',$custom_location)->where('产品编码',$sku)->where('质量状态','ZP')->first();
             $wmsInventoryCompareCc=$query->where('属性仓',$custom_location)->where('产品编码',$sku)->where('质量状态','CC')->first();
             $wmsInventoryCompareDj=$query->where('属性仓',$custom_location)->where('产品编码',$sku)->where('质量状态','DJ')->first();
-            //$unknownQualityStatus=$query->where('属性仓',$custom_location)->where('产品编码',$sku)->whereNotIn('质量状态',['DJ','CC','ZP']);
-            //if (!$wmsInventoryCompareZp&&!$wmsInventoryCompareCc&&!$wmsInventoryCompareDj&&$unknownQualityStatus->isEmpty()){
-                //$inventoryCompare=$this->createInventoryCompare_underImport($sku, $custom_location, $amount, $owner_id,$owner_name);
-            //}
+            $unknownQualityStatus=$query->where('属性仓',$custom_location)->where('产品编码',$sku)->whereNotIn('质量状态',['DJ','CC','ZP']);
+            if (!$wmsInventoryCompareZp&&!$wmsInventoryCompareCc&&!$wmsInventoryCompareDj&&$unknownQualityStatus->isEmpty()){
+                $this->createInventoryCompare_underImport($sku, $custom_location, $amount, $owner_id,$owner_name);
+            }
             $creatingMissionCode = $this->getCreatingMissionCode($owner_name);
             $commodityId=Commodity::where('sku',$sku)->where('owner_id',$owner_id)->value('id');
             if ($wmsInventoryCompareZp||$wmsInventoryCompareDj) {
@@ -140,7 +140,7 @@ class InventoryCompareService
                 ];
                 array_push($wasInventoryCompares,$wasInventoryCompareCC);
             }
-            //if($unknownQualityStatus->isNotEmpty())return null;
+            if($unknownQualityStatus->isNotEmpty())return null;
         }
         $inventoryCompares=DB::table('inventory_compares')->insert($wasInventoryCompares);
         if (!$inventoryCompares)return null;

+ 0 - 1
config/cache.php

@@ -6,7 +6,6 @@ return [
 
     'expirations'=>[
         'default'=>10,
-        'authorities'=>10,
     ],
     /*
     |--------------------------------------------------------------------------

+ 0 - 1
phpunit-Current.bat

@@ -1 +0,0 @@
-vendor\bin\phpunit.bat --testsuite=Current

+ 0 - 1
phpunit-Inventory.bat

@@ -1 +0,0 @@
-vendor\bin\phpunit.bat --testsuite=Inventory

+ 0 - 1
phpunit-OrderIssue.bat

@@ -1 +0,0 @@
-vendor\bin\phpunit.bat --testsuite=OrderIssues

+ 0 - 1
phpunit-Unit.bat

@@ -1 +0,0 @@
-vendor\bin\phpunit.bat --testsuite=Unit

+ 1 - 0
phpunit.bat

@@ -0,0 +1 @@
+vendor\bin\phpunit.bat --testsuite=%1

+ 3 - 6
phpunit.xml

@@ -12,18 +12,15 @@
         <testsuite name="Unit">
             <directory suffix="Test.php">./tests/Unit</directory>
         </testsuite>
-        <testsuite name="Current">
-            <directory suffix="Test.php">./tests/Current</directory>
-        </testsuite>
-        <testsuite name="Feature">
-            <directory suffix="Test.php">./tests/Feature</directory>
-        </testsuite>
         <testsuite name="Inventory">
             <directory suffix="Test.php">./tests/Inventory</directory>
         </testsuite>
         <testsuite name="OrderIssues">
             <directory suffix="Test.php">./tests/OrderIssues</directory>
         </testsuite>
+        <testsuite name="Services">
+            <directory suffix="Test.php">./tests/Services</directory>
+        </testsuite>
     </testsuites>
     <filter>
         <whitelist processUncoveredFilesFromWhitelist="true">

+ 4 - 4
public/t.php

@@ -1,6 +1,6 @@
 <?php
 
-$a = '11';
-echo $a;
-$a = '\\' . $a . '\\';
-echo trim($a, '\\');
+$a=function(){
+
+};
+echo gettype($a);

+ 0 - 20
tests/Feature/FromTest.php

@@ -1,20 +0,0 @@
-<?php
-
-namespace Tests\Feature;
-
-use Tests\TestCase;
-use Illuminate\Foundation\Testing\WithFaker;
-use Illuminate\Foundation\Testing\RefreshDatabase;
-
-class FromTest extends TestCase
-{
-    /**
-     * A basic feature test example.
-     *
-     * @return void
-     */
-    public function testDelete(){
-        $response=$this->get('/waybill');
-        $response->assertStatus(302);
-    }
-}

+ 0 - 62
tests/Feature/WaybillTest.php

@@ -1,62 +0,0 @@
-<?php
-
-namespace Tests\Feature;
-
-use App\WaybillPriceModel;
-use App\User;
-use App\Waybill;
-use App\WaybillPayoff;
-use Illuminate\Support\Facades\Auth;
-use Illuminate\Support\Facades\Request;
-use phpDocumentor\Reflection\Types\This;
-use Tests\TestCase;
-use Illuminate\Foundation\Testing\WithFaker;
-use Illuminate\Foundation\Testing\RefreshDatabase;
-
-class WaybillTest extends TestCase
-{
-    use RefreshDatabase;
-
-        protected $id;
-
-        public  function testAddWaybill(){
-            $waybill=new  Waybill([
-                'type'=>'直发车',
-                'waybill_number'=>'BSZX19112530861455654',
-                'owner_id'=>1,
-                'wms_bill_number'=>'201900202455165454',
-                'origination'=>'上海松江泗泾交科松江科创园',
-                'destination'=>'北京',
-                'recipient'=>'陈某',
-                'recipient_mobile'=>'12345628915',
-                'charge'=>285.2,
-                'ordering_remark'=>'测试',
-                'carrier_id'=>1,
-                'carrier_bill'=>'254785852154452',
-                'origination_city_id'=>1,
-                'destination_city_id'=>1,
-                'warehouse_weight'=>2058.2,
-                'warehouse_weight_unit_id'=>1,
-                'carrier_weight'=>2058,
-                'carrier_weight_unit_id'=>1,
-                'carType_id'=>1,
-                'fee'=>2223,
-                'pick_up_fee'=>44445,
-                'other_fee'=>200,
-                'collect_fee'=>558,
-                'dispatch_remark'=>'调度测试'
-            ]);
-            $waybill->save();
-            $this->assertNotEmpty($waybill);
-            $this->id=$waybill->id;
-            return $waybill;
-        }
-
-
-    public function tearDown(): void
-    {
-        $this->assertTrue(Waybill::find($this->id)->delete());
-        parent::tearDown(); // TODO: Change the autogenerated stub
-    }
-
-}

+ 43 - 0
tests/Services/CacheService/GetOrExecuteTest.php

@@ -0,0 +1,43 @@
+<?php
+
+namespace Tests\Unit;
+
+use App\Authority;
+use App\Carrier;
+use App\Role;
+use App\Services\CacheService;
+use App\User;
+use Illuminate\Support\Facades\Cache;
+use Illuminate\Support\Facades\DB;
+use Ramsey\Uuid\Uuid;
+use Tests\TestCase;
+use Illuminate\Foundation\Testing\WithFaker;
+use Illuminate\Foundation\Testing\RefreshDatabase;
+
+class GetOrExecuteTest extends TestCase
+{
+
+    /** @var CacheService $cacheService */
+    public $cacheService;
+    public function setUp(): void
+    {
+        parent::setUp();
+        $this->cacheService = app('CacheService');
+    }
+
+    public function testPutAndGet(){
+        $key = md5(microtime(true));
+        $result=$this->cacheService->getOrExecute($key,function (){
+            return 'yes';
+        });
+        $this->assertEquals('yes',$result);
+        $this->assertEquals('yes',Cache::pull($key));//销毁
+    }
+
+    public function testNullFunction(){
+        $this->expectException(\Exception::class);
+        $key = md5(microtime(true));
+        $this->cacheService->getOrExecute($key,null);
+    }
+
+}

+ 0 - 21
tests/Services/ExampleTest.php

@@ -1,21 +0,0 @@
-<?php
-
-namespace Tests\Unit;
-
-use App\Authority;
-use App\Carrier;
-use App\Role;
-use App\User;
-use Illuminate\Support\Facades\DB;
-use Tests\TestCase;
-use Illuminate\Foundation\Testing\WithFaker;
-use Illuminate\Foundation\Testing\RefreshDatabase;
-
-class ExampleTest extends TestCase
-{
-
-    public function testExample(){
-        $this->assertEquals(2,1+1);
-    }
-
-}