84 lines
3.0 KiB
PHP
84 lines
3.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Unit;
|
|
|
|
use App\Http\Resources\Api\Admin\ProductResource;
|
|
use App\Models\Product;
|
|
use App\Models\ProductPricings;
|
|
use Illuminate\Support\Collection;
|
|
use Tests\TestCase;
|
|
|
|
class AdminProductResourceTest extends TestCase
|
|
{
|
|
public function test_it_reads_legacy_pricing_cycle_names_without_returning_null(): void
|
|
{
|
|
$product = new Product([
|
|
'name' => 'Legacy product',
|
|
'is_active' => true,
|
|
'setup_fee' => 0,
|
|
]);
|
|
$product->id = 4;
|
|
$product->setRelation('pricings', new Collection([
|
|
new ProductPricings([
|
|
'billing_cycle' => 'semiannually',
|
|
'price' => 7200000,
|
|
'currency' => 'VND',
|
|
]),
|
|
new ProductPricings([
|
|
'billing_cycle' => 'one_time',
|
|
'price' => 15000000,
|
|
'currency' => 'VND',
|
|
]),
|
|
]));
|
|
|
|
$data = (new ProductResource($product))->resolve(request());
|
|
|
|
$this->assertSame('7200000.00', $data['price_semiannually']);
|
|
$this->assertSame('15000000.00', $data['price_onetime']);
|
|
$this->assertSame('recurring', $data['billing_type']);
|
|
}
|
|
|
|
public function test_legacy_one_time_only_product_uses_frontend_billing_value(): void
|
|
{
|
|
$product = new Product([
|
|
'name' => 'One-time product',
|
|
'is_active' => true,
|
|
'setup_fee' => 0,
|
|
]);
|
|
$product->id = 5;
|
|
$product->setRelation('pricings', new Collection([
|
|
new ProductPricings([
|
|
'billing_cycle' => 'one_time',
|
|
'price' => 15000000,
|
|
'currency' => 'VND',
|
|
]),
|
|
]));
|
|
|
|
$data = (new ProductResource($product))->resolve(request());
|
|
|
|
$this->assertSame('onetime', $data['billing_type']);
|
|
$this->assertSame('15000000.00', $data['price_onetime']);
|
|
}
|
|
|
|
public function test_custom_prices_serialize_as_json_object_even_when_empty(): void
|
|
{
|
|
$product = new Product(['name' => 'No custom cycles', 'is_active' => true, 'setup_fee' => 0]);
|
|
$product->id = 6;
|
|
$product->setRelation('pricings', new Collection([
|
|
new ProductPricings(['billing_cycle' => 'annually', 'price' => 1000000, 'currency' => 'VND']),
|
|
new ProductPricings(['billing_cycle' => 'months_60', 'price' => 5000000, 'currency' => 'VND']),
|
|
]));
|
|
|
|
$withCustom = json_decode((new ProductResource($product))->toResponse(request())->getContent());
|
|
$this->assertIsObject($withCustom->data->custom_prices ?? $withCustom->custom_prices);
|
|
|
|
$product->setRelation('pricings', new Collection([
|
|
new ProductPricings(['billing_cycle' => 'annually', 'price' => 1000000, 'currency' => 'VND']),
|
|
]));
|
|
$empty = json_decode((new ProductResource($product))->toResponse(request())->getContent());
|
|
$this->assertIsObject($empty->data->custom_prices ?? $empty->custom_prices);
|
|
}
|
|
}
|