211 lines
7.0 KiB
PHP
211 lines
7.0 KiB
PHP
<?php
|
||
|
||
namespace Tests\Unit;
|
||
|
||
use Tests\TestCase;
|
||
use Illuminate\Support\Facades\Config;
|
||
use Illuminate\Support\Facades\DB;
|
||
use Illuminate\Support\Facades\Auth;
|
||
|
||
class BatchExcelSaveTest extends TestCase
|
||
{
|
||
protected function setUp(): void
|
||
{
|
||
parent::setUp();
|
||
|
||
Config::set('database.default', 'mysql');
|
||
Config::set('database.connections.mysql.host', 'localhost');
|
||
Config::set('database.connections.mysql.port', '3307');
|
||
Config::set('database.connections.mysql.database', 'stellar_dev');
|
||
Config::set('database.connections.mysql.username', 'stellar_test');
|
||
Config::set('database.connections.mysql.password', 'pGyPXtEmRK1c');
|
||
|
||
// Root kullanıcısı olarak giriş yap
|
||
Auth::loginUsingId(1);
|
||
|
||
// Middleware'leri devre dışı bırak
|
||
$this->withoutMiddleware();
|
||
}
|
||
|
||
public function testBatchExcelSaveEndpoint()
|
||
{
|
||
// Test verisi hazırla
|
||
$columns = [
|
||
'unit',
|
||
'plant',
|
||
'engineering',
|
||
'line_no',
|
||
'sheet',
|
||
'p_id',
|
||
'line_specification',
|
||
'painting_cycle',
|
||
'external_finish_type',
|
||
'rev',
|
||
'fluid_code',
|
||
'fluid_ru',
|
||
'fluid_en',
|
||
'from',
|
||
'to',
|
||
'pipe_material_class',
|
||
'dn',
|
||
'design_pressure_mpa',
|
||
'working_pressure_mpa',
|
||
'working_temperature',
|
||
'design_temperature',
|
||
'density',
|
||
'ndt',
|
||
'pwht',
|
||
'asme_fluid_service',
|
||
'category',
|
||
'fluid_group',
|
||
'test_media',
|
||
'test_pressure_mpa',
|
||
'pneumatic_test_pressure',
|
||
'remarks',
|
||
'circuit_number'
|
||
];
|
||
|
||
// Grid verisi hazırla
|
||
$gridData = [
|
||
// Test verisi 1 - Tüm alanlar dolu
|
||
[
|
||
['value' => '1'],
|
||
['value' => 'Plant A'],
|
||
['value' => 'Engineering A'],
|
||
['value' => 'LINE-001'],
|
||
['value' => 'Sheet-1'],
|
||
['value' => 'PID-001'],
|
||
['value' => 'SPEC-001'],
|
||
['value' => 'PC-001'],
|
||
['value' => 'Type A'],
|
||
['value' => 'Rev.1'],
|
||
['value' => 'FC-001'],
|
||
['value' => 'Fluid RU'],
|
||
['value' => 'Fluid EN'],
|
||
['value' => 'From A'],
|
||
['value' => 'To B'],
|
||
['value' => 'Class A'],
|
||
['value' => 'DN50'],
|
||
['value' => '2.5'],
|
||
['value' => '2.0'],
|
||
['value' => '80'],
|
||
['value' => '100'],
|
||
['value' => '1.5'],
|
||
['value' => 'Yes'],
|
||
['value' => 'Yes'],
|
||
['value' => 'Service A'],
|
||
['value' => 'Cat A'],
|
||
['value' => 'Group 1'],
|
||
['value' => 'Water'],
|
||
['value' => '3.0'],
|
||
['value' => '2.0'],
|
||
['value' => 'Test remarks'],
|
||
['value' => 'CIR-001']
|
||
],
|
||
|
||
];
|
||
|
||
// POST isteği gönder
|
||
$requestData = [
|
||
'table' => 'line_lists',
|
||
'columns' => $columns,
|
||
'data' => json_encode($gridData),
|
||
'run_trigger' => 'false'
|
||
];
|
||
|
||
$response = $this->post('/admin-ajax/batch-excel-save', $requestData);
|
||
|
||
// Response kontrolü
|
||
$response->assertStatus(200);
|
||
|
||
// JSON response'ı decode et
|
||
$responseData = json_decode($response->getContent(), true);
|
||
|
||
// Temel kontroller
|
||
$this->assertArrayHasKey('storedData', $responseData);
|
||
$this->assertArrayHasKey('updateOrInsert', $responseData);
|
||
$this->assertArrayHasKey('update', $responseData);
|
||
$this->assertArrayHasKey('insert', $responseData);
|
||
$this->assertArrayHasKey('error', $responseData);
|
||
$this->assertArrayHasKey('triggerInfo', $responseData);
|
||
$this->assertArrayHasKey('ids', $responseData);
|
||
$this->assertArrayHasKey('runTrigger', $responseData);
|
||
$this->assertArrayHasKey('errorRows', $responseData);
|
||
$this->assertArrayHasKey('totalErrors', $responseData);
|
||
|
||
// Veri kayıt kontrolü
|
||
// $this->assertEquals(2, $responseData['storedData'], 'Beklenen sayıda veri kaydedilmedi');
|
||
$this->assertFalse($responseData['error']);
|
||
|
||
// Veritabanı kontrolü
|
||
$savedRecords = DB::table('line_lists')
|
||
->whereIn('line_no', ['LINE-001'])
|
||
->get();
|
||
|
||
$this->assertCount(1, $savedRecords, 'Veritabanında beklenen sayıda kayıt bulunamadı');
|
||
|
||
// İlk kaydın detaylı kontrolü
|
||
$firstRecord = $savedRecords->where('line_no', 'LINE-001')->first();
|
||
$this->assertEquals('Plant A', $firstRecord->plant);
|
||
$this->assertEquals('LINE-001', $firstRecord->line_no);
|
||
$this->assertEquals('FC-001', $firstRecord->fluid_code);
|
||
$this->assertEquals('2.5', $firstRecord->design_pressure_mpa);
|
||
|
||
// runTrigger kontrolü ekle
|
||
$this->assertFalse($responseData['runTrigger'], 'runTrigger false olarak ayarlandığında false dönmeli');
|
||
}
|
||
|
||
public function testBatchExcelSaveWithInvalidData()
|
||
{
|
||
// Geçersiz veri ile test
|
||
$response = $this->post('/admin-ajax/batch-excel-save', [
|
||
'table' => 'line_lists',
|
||
'columns' => [],
|
||
'data' => '[]'
|
||
]);
|
||
|
||
// Response kontrolü
|
||
$response->assertStatus(200);
|
||
|
||
$responseData = json_decode($response->getContent(), true);
|
||
$this->assertEquals(0, $responseData['storedData'], 'Geçersiz veri ile kayıt yapılmamalıydı');
|
||
}
|
||
|
||
public function testBatchExcelSaveWithRunTrigger()
|
||
{
|
||
// Test verisi hazırla
|
||
$columns = [
|
||
'unit',
|
||
'line_no',
|
||
'plant'
|
||
];
|
||
|
||
$gridData = [
|
||
[
|
||
['value' => '1'],
|
||
['value' => 'LINE-001'],
|
||
['value' => 'Plant A']
|
||
]
|
||
];
|
||
|
||
// runTrigger true ile test
|
||
$requestData = [
|
||
'table' => 'line_lists',
|
||
'columns' => $columns,
|
||
'data' => json_encode($gridData),
|
||
'run_trigger' => 'true'
|
||
];
|
||
|
||
$response = $this->post('/admin-ajax/batch-excel-save', $requestData);
|
||
$responseData = json_decode($response->getContent(), true);
|
||
|
||
$this->assertTrue($responseData['runTrigger'], 'runTrigger true olarak ayarlandığında true dönmeli');
|
||
|
||
// runTrigger false ile test
|
||
$requestData['run_trigger'] = 'false';
|
||
$response = $this->post('/admin-ajax/batch-excel-save', $requestData);
|
||
$responseData = json_decode($response->getContent(), true);
|
||
|
||
$this->assertFalse($responseData['runTrigger'], 'runTrigger false olarak ayarlandığında false dönmeli');
|
||
}
|
||
}
|