芝麻web文件管理V1.00
编辑当前文件:/home/forge/stage.herta-bht.smartcon-survey.com/tests/Feature/UsageTest.php
countryId = 1; $this->brands = Brand::factory()->createMany([ ['sid' => 1, 'name' => 'Wagner', 'color' => '#ff0000'], ['sid' => 2, 'name' => 'Oatker', 'color' => '#fff000'], ]); $dataUpload = DataUpload::factory()->createMany([ ['wave' => 1, 'country_id' => $this->countryId, 'id' => 1, 'interviews_count' => 5, 'quarter' => 4, 'year' => 2024], ['wave' => 2, 'country_id' => $this->countryId, 'id' => 2, 'interviews_count' => 10, 'quarter' => 1, 'year' => 2025], ]); $interviews = Interview::factory()->createMany([ ['wave' => 1, 'country_id' => $this->countryId, 'userid' => 1, 'created_at' => Carbon::create(today()->year - 1, rand(10, 12), rand(1, 31))->format('Y-m-d H:i:s')], ['wave' => 1, 'country_id' => $this->countryId, 'userid' => 2, 'created_at' => Carbon::create(today()->year - 1, rand(10, 12), rand(1, 31))->format('Y-m-d H:i:s')], ['wave' => 1, 'country_id' => $this->countryId, 'userid' => 3, 'created_at' => Carbon::create(today()->year - 1, rand(10, 12), rand(1, 31))->format('Y-m-d H:i:s')], ['wave' => 1, 'country_id' => $this->countryId, 'userid' => 4, 'created_at' => Carbon::create(today()->year - 1, rand(10, 12), rand(1, 31))->format('Y-m-d H:i:s')], ['wave' => 1, 'country_id' => $this->countryId, 'userid' => 5, 'created_at' => Carbon::create(today()->year - 1, rand(10, 12), rand(1, 31))->format('Y-m-d H:i:s')], ['wave' => 2, 'country_id' => $this->countryId, 'userid' => 6], ['wave' => 2, 'country_id' => $this->countryId, 'userid' => 7], ['wave' => 2, 'country_id' => $this->countryId, 'userid' => 9], ['wave' => 2, 'country_id' => $this->countryId, 'userid' => 8], ['wave' => 2, 'country_id' => $this->countryId, 'userid' => 10], ['wave' => 2, 'country_id' => $this->countryId, 'userid' => 11], ['wave' => 2, 'country_id' => $this->countryId, 'userid' => 12], ['wave' => 2, 'country_id' => $this->countryId, 'userid' => 13], ['wave' => 2, 'country_id' => $this->countryId, 'userid' => 14], ['wave' => 2, 'country_id' => $this->countryId, 'userid' => 15], ]); DataAggregation::factory()->createMany([ ['country_id' => $this->countryId, 'model_id' => 1, 'wave' => 1, 'question_name' => 'Q6', 'value' => '1', 'aggregate' => 5, 'key' => 'Q6_'.'1', 'quarter' => 4, 'year' => 2024], ['country_id' => $this->countryId, 'model_id' => 1, 'wave' => 2, 'question_name' => 'Q6', 'value' => '1', 'aggregate' => 5, 'key' => 'Q6_'.'1', 'quarter' => 1, 'year' => 2025], ['country_id' => $this->countryId, 'model_id' => 2, 'wave' => 1, 'question_name' => 'Q6', 'value' => '1', 'aggregate' => 3, 'key' => 'Q6_'.'2', 'quarter' => 4, 'year' => 2024], ['country_id' => $this->countryId, 'model_id' => 2, 'wave' => 2, 'question_name' => 'Q6', 'value' => '1', 'aggregate' => 5, 'key' => 'Q6_'.'2', 'quarter' => 1, 'year' => 2025], ]); $this->service = new UsageChartService('title', $this->countryId, $this->brands); }); afterEach(function () { // Rollback or refresh the database after each test DB::rollBack(); DB::beginTransaction(); }); test('calculate values for first brand correctly', function () { $result = $this->service->getvalues($this->brands->get(0)); expect($result) ->toBeArray() ->and($result['id'])->toBe(1) ->and($result['label'])->toBe('Wagner') ->and($result['borderColor'])->toBe('#ff0000') ->and($result['data'])->toBeArray(); expectUsageData($result['data'][0], 'Q4 2024', 100.0, 5); expectUsageData($result['data'][1], 'Q1 2025', 50.0, 5); }); test('calculate values for second brand correctly', function () { $result = $this->service->getvalues($this->brands->get(1)); expect($result) ->toBeArray() ->and($result['id'])->toBe(2) ->and($result['label'])->toBe('Oatker') ->and($result['borderColor'])->toBe('#fff000') ->and($result['data'])->toBeArray(); expectUsageData($result['data'][0], 'Q4 2024', 60.0, 3); expectUsageData($result['data'][1], 'Q1 2025', 50.0, 5); }); // Helper function for common assertions function expectUsageData(array $data, string $x, float $y, int $n) { expect($data) ->toBeArray() ->and($data['x'])->toBe($x) ->and($data['y'])->toBe($y) ->and($data['n'])->toBe($n); }