Files
shipping-portal/tests/Feature/Segments/ControllersLogic/UpdateSegmentPriceLogicTest.php
T
2026-04-06 11:26:16 +08:00

130 lines
3.6 KiB
PHP

<?php
/**
* Original controller logic file:
* app/Classes/Modules/Segments/ControllersLogic/UpdateSegmentPriceLogic.php
*/
namespace Tests\Feature\Segments\ControllersLogic;
use App\Classes\ValueObjects\Constants\RoleTypes;
use App\Models\Segment;
use App\Models\SegmentConstant;
use App\Models\User;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\TestCaseChild;
use PHPUnit\Framework\Attributes\Group;
#[Group('feature')]
#[Group('segments')]
#[Group('controllers_logic')]
#[Group('ok_to_run')]
class UpdateSegmentPriceLogicTest extends TestCaseChild
{
// use DatabaseTransactions;
/** @var User */
private $admin;
/** @var Segment */
private $segment;
protected function setUp(): void
{
parent::setUp();
// A R R A N G E
// Create an admin user for authentication
$testName = substr(str_replace('test_it_', '', $this->name()), 0, 20);
$this->admin = new User();
$this->admin->name = $testName . '_admin_user';
$this->admin->email = $testName . '_admin_' . uniqid() . '@example.com';
$this->admin->password = bcrypt('password');
$this->admin->type = RoleTypes::ADMIN;
$this->admin->status = 1;
$this->admin->save();
// Create a test segment
$this->segment = new Segment();
$this->segment->name = substr($testName . '_segment', 0, 45);
$this->segment->company_module_id = 1;
$this->segment->save();
}
/**
* Test updating an existing segment price constant.
*/
public function test_it_can_update_segment_price_constant()
{
// A R R A N G E
$reference = 'update_segment_price_constant_ref';
$value = 100.50;
$constant = new SegmentConstant();
$constant->segment_id = $this->segment->id;
$constant->reference = $reference;
$constant->value = [50.00];
$constant->save();
$payload = [
'reference' => $reference,
'value' => $value
];
// A C T
$response = $this->actingAs($this->admin, 'api')
->putJson("/api/v1/segment/price/update/{$this->segment->id}", $payload);
// A S S E R T
$response->assertStatus(200);
$response->assertJsonStructure([
'title',
'message',
'payload' => [
'data' => [
'id',
'reference',
'value'
]
]
]);
$constant = SegmentConstant::query()
->where('segment_id', $this->segment->id)
->where('reference', $reference)
->first();
$this->assertNotNull($constant);
$this->assertEquals([$value], $constant->value);
}
/**
* Test creating a constant if it doesn't exist and then updating it.
*/
public function test_it_creates_constant_if_not_exists_then_updates_it()
{
// A R R A N G E
$reference = 'create_segment_price_constant_ref';
$value = 75.25;
$payload = [
'reference' => $reference,
'value' => $value
];
// A C T
$response = $this->actingAs($this->admin, 'api')
->putJson("/api/v1/segment/price/update/{$this->segment->id}", $payload);
// A S S E R T
$response->assertStatus(200);
$constant = SegmentConstant::query()
->where('segment_id', $this->segment->id)
->where('reference', $reference)
->first();
$this->assertNotNull($constant);
$this->assertEquals([$value], $constant->value);
}
}