Files
shipping-portal/tests/Feature/Segments/ControllersLogic/UpdateSegmentPriceTest.php
T

123 lines
3.2 KiB
PHP

<?php
/**
* Test case for updating segment price through the logic layer.
*/
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')]
class UpdateSegmentPriceTest extends TestCaseChild
{
// use DatabaseTransactions;
/** @var User */
private $admin;
/** @var Segment */
private $segment;
protected function setUp(): void
{
parent::setUp();
// Arrange
// Create an admin user for authentication
$this->admin = new User();
$this->admin->name = 'Admin User';
$this->admin->email = 'admin@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 = 'Test Segment';
$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()
{
// Arrange
$reference = 'test_price_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
];
// Act
$response = $this->actingAs($this->admin, 'api')
->putJson("/v1/api/segment/price/update/{$this->segment->id}", $payload);
// Assert
$response->assertStatus(200);
$response->assertJsonStructure([
'data' => [
'id',
'reference',
'value'
],
'notification' => [
'title',
'message'
]
]);
$this->assertDatabaseHas('segment_constants', [
'segment_id' => $this->segment->id,
'reference' => $reference,
'value' => json_encode([$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()
{
// Arrange
$reference = 'new_price_ref';
$value = 75.25;
$payload = [
'reference' => $reference,
'value' => $value
];
// Act
$response = $this->actingAs($this->admin, 'api')
->putJson("/v1/api/segment/price/update/{$this->segment->id}", $payload);
// Assert
$response->assertStatus(200);
$this->assertDatabaseHas('segment_constants', [
'segment_id' => $this->segment->id,
'reference' => $reference,
'value' => json_encode([$value])
]);
}
}