Kaynağa Gözat

海柔单元测试

LD 5 yıl önce
ebeveyn
işleme
e7836d8ce0

+ 3 - 2
app/Services/BatchService.php

@@ -8,6 +8,7 @@ use App\Order;
 use App\OrderCommodity;
 use App\Owner;
 use Exception;
+use Illuminate\Support\Collection;
 use Illuminate\Support\Facades\Http;
 
 Class BatchService
@@ -53,10 +54,10 @@ Class BatchService
 
     /**
      * 为波次附加任务,已附加的重复任务不影响
-     * @param Batch[] $batches
+     * @param Collection $batches
      * @throws Exception
      */
-    public function assignTasks(array $batches)
+    public function assignTasks(Collection $batches)
     {
 //        $this->directTemp($batches);
         $this->stationTaskBatchService=app('StationTaskBatchService');

+ 1 - 1
app/Services/StationRuleBatchService.php

@@ -12,7 +12,7 @@ use Illuminate\Support\Facades\Cache;
 
 class StationRuleBatchService
 {
-    function getByBatch(Batch $batch): StationRuleBatch
+    function getByBatch(Batch $batch): ?StationRuleBatch
     {
         $batchType = $batch['type'] ?? 'null';
         $ownerId = $batch['owner_id'] ?? 'null';

+ 3 - 2
app/StationRuleBatch.php

@@ -3,14 +3,15 @@
 namespace App;
 
 use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\Relations\BelongsTo;
 use Illuminate\Database\Eloquent\Relations\HasOne;
 
 class StationRuleBatch extends ModelExtended
 {
     protected $fillable=['name','station_type_id','batch_type','owner_id'];
 
-    public function stationType(): HasOne
+    public function stationType(): BelongsTo
     {
-        return $this->hasOne(StationType::class);
+        return $this->belongsTo(StationType::class);
     }
 }

+ 0 - 1
database/factories/StationRuleBatchFactory.php

@@ -8,6 +8,5 @@ use Faker\Generator as Faker;
 $factory->define(StationRuleBatch::class, function (Faker $faker) {
     return [
         'name' => $faker->uuid,
-        'batch_type' =>$faker->name,
     ];
 });

+ 30 - 0
database/migrations/2020_12_29_182249_change_station_rule_batch_column_default_batch_type.php

@@ -0,0 +1,30 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class ChangeStationRuleBatchColumnDefaultBatchType extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::table('station_rule_batches',function(Blueprint $table){
+            $table->string('batch_type')->default('无')->change();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        //
+    }
+}

+ 3 - 3
tests/Unit/StationRuleBatchService/GetBatches_shouldProcessTest.php → tests/Services/StationRuleBatchService/GetBatches_shouldProcessTest.php

@@ -1,6 +1,6 @@
 <?php
 
-namespace Tests\Unit\StationRuleBatchService;
+namespace Tests\Services\StationRuleBatchService;
 
 use App\Batch;
 use App\Owner;
@@ -25,7 +25,7 @@ class GetBatches_shouldProcessTest extends TestCase
         $this->batchService = app('BatchService');
         $this->data['owner_target'] = factory(Owner::class)->create();
         $this->data['owner_none_target'] = factory(Owner::class)->create();
-        $this->data['stationRuleBatches'] = factory(StationRuleBatch::class)
+        $this->data['stationRuleBatch'] = factory(StationRuleBatch::class)
             ->create([
                 'owner_id' => $this->data['owner_target']['id'],
             ]);
@@ -50,7 +50,7 @@ class GetBatches_shouldProcessTest extends TestCase
             $this->data['owner_target']['id'],
             $this->data['owner_none_target']['id'],
         ])->delete();
-        StationRuleBatch::query()->whereIn('id',data_get($this->data['stationRuleBatches'],'*.id'))->delete();
+        StationRuleBatch::query()->where('id',$this->data['stationRuleBatch']['id'])->delete();
         Batch::query()->whereIn('id',data_get($this->data['batches'],'*.id'))->delete();
         parent::tearDown(); // TODO: Change the autogenerated stub
     }

+ 57 - 0
tests/Services/StationTaskService/CreateTest.php

@@ -0,0 +1,57 @@
+<?php
+
+namespace Tests\Services\StationTaskService;
+
+use App\Batch;
+use App\Owner;
+use App\Services\BatchService;
+use App\Services\StationRuleBatchService;
+use App\StationRuleBatch;
+use Tests\TestCase;
+
+class CreateTest extends TestCase
+{
+    /** @var StationRuleBatchService $service */
+    private $service;
+    /** @var BatchService $batchService */
+    private $batchService;
+    private $data = [];
+    private $amountOfInRule = 3;
+
+    public function setUp(): void
+    {
+        parent::setUp();
+        $this->service = app('StationRuleBatchService');
+        $this->batchService = app('BatchService');
+        $this->data['owner_target'] = factory(Owner::class)->create();
+        $this->data['owner_none_target'] = factory(Owner::class)->create();
+        $this->data['stationRuleBatch'] = factory(StationRuleBatch::class)
+            ->create([
+                'owner_id' => $this->data['owner_target']['id'],
+            ]);
+        $this->data['batches'] = factory(Batch::class, $this->amountOfInRule)->create([
+            'owner_id' => $this->data['owner_target']['id'],
+        ]);
+        $this->data['batches'][]=factory(Batch::class)->create([
+            'owner_id' => $this->data['owner_none_target']['id'],
+        ]);
+    }
+
+    public function testCreateSuccess()
+    {
+        $batches=$this->service->getBatches_shouldProcess($this->data['batches']);
+        $this->assertEquals($this->amountOfInRule,$batches->count());
+    }
+
+    public function tearDown(): void
+    {
+        cache()->flush();
+        Owner::query()->whereIn('id',[
+            $this->data['owner_target']['id'],
+            $this->data['owner_none_target']['id'],
+        ])->delete();
+        StationRuleBatch::query()->where('id',$this->data['stationRuleBatch']['id'])->delete();
+        Batch::query()->whereIn('id',data_get($this->data['batches'],'*.id'))->delete();
+        parent::tearDown(); // TODO: Change the autogenerated stub
+    }
+}

+ 2 - 2
tests/Unit/EchoTest.php

@@ -9,8 +9,8 @@ class EchoTest extends TestCase
 
 
     public function testEcho(){
-        echo "\nabc3213123\n";
-        $this->assertEquals(3,2);
+//        echo "\nabc3213123\n";
+        $this->assertEquals(3,3);
     }
 
 }