Commit a8c964ee authored by Fares's avatar Fares

test(core): add unit tests for global helper functions

Tests cover e(), money(), percentage(), arabic_date(), age_from_dob(), now(), and today() helpers.
Co-Authored-By: 's avatarClaude Opus 4.6 <noreply@anthropic.com>
parent ee47e057
<?php
declare(strict_types=1);
namespace Tests\Unit\Core;
use Tests\TestCase;
class HelpersTest extends TestCase
{
public function testEscapesHtml(): void
{
$this->assertSame('&lt;script&gt;alert(&quot;xss&quot;)&lt;/script&gt;', e('<script>alert("xss")</script>'));
}
public function testEscapesQuotes(): void
{
$this->assertSame('it&#039;s &amp; &quot;quoted&quot;', e('it\'s & "quoted"'));
}
public function testEscapesNull(): void
{
$this->assertSame('', e(null));
}
public function testMoneyFormatsCorrectly(): void
{
$this->assertSame('1,000.50 ج.م', money(1000.5));
}
public function testMoneyFormatsZero(): void
{
$this->assertSame('0.00 ج.م', money(0));
}
public function testMoneyFormatsNegative(): void
{
$this->assertSame('-500.00 ج.م', money(-500));
}
public function testMoneyWithCustomCurrency(): void
{
$this->assertSame('100.00 $', money(100, '$'));
}
public function testMoneyHandlesStringInput(): void
{
$this->assertSame('250.75 ج.م', money('250.75'));
}
public function testMoneyHandlesNonNumericInput(): void
{
$this->assertSame('0.00 ج.م', money('abc'));
}
public function testPercentageFormats(): void
{
$this->assertSame('75.50%', percentage(75.5));
}
public function testPercentageZero(): void
{
$this->assertSame('0.00%', percentage(0));
}
public function testPercentageFromString(): void
{
$this->assertSame('33.33%', percentage('33.33'));
}
public function testArabicDateFormatsCorrectly(): void
{
$result = arabic_date('2024-01-15');
$this->assertStringContainsString('يناير', $result);
$this->assertStringContainsString('2024', $result);
$this->assertStringContainsString('15', $result);
}
public function testArabicDateDecemberFormat(): void
{
$result = arabic_date('2024-12-25');
$this->assertStringContainsString('ديسمبر', $result);
$this->assertStringContainsString('25', $result);
}
public function testArabicDateReturnsOriginalOnInvalid(): void
{
$this->assertSame('not-a-date', arabic_date('not-a-date'));
}
public function testAgeFromDobReturnsArray(): void
{
$result = age_from_dob('2000-01-01');
$this->assertArrayHasKey('years', $result);
$this->assertArrayHasKey('months', $result);
$this->assertArrayHasKey('days', $result);
$this->assertArrayHasKey('total_months', $result);
$this->assertGreaterThan(20, $result['years']);
}
public function testAgeFromDobInvalidDate(): void
{
$result = age_from_dob('invalid');
$this->assertSame(0, $result['years']);
$this->assertSame(0, $result['months']);
$this->assertSame(0, $result['days']);
$this->assertSame(0, $result['total_months']);
}
public function testNowReturnsDateTimeFormat(): void
{
$result = now();
$this->assertMatchesRegularExpression('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/', $result);
}
public function testTodayReturnsDateFormat(): void
{
$result = today();
$this->assertMatchesRegularExpression('/^\d{4}-\d{2}-\d{2}$/', $result);
$this->assertSame(date('Y-m-d'), $result);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment