Simple problems require simple solutions
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

156 lines
4.8 KiB

<?php
namespace Tests\Feature\Http;
use Illuminate\Testing\Fluent\AssertableJson;
use Tests\TestCase;
class CalculateDurationTest extends TestCase
{
public function testCanCalulateDurationViaApi(): void
{
$this
->postJson('/calculate-duration', [
'start' => [
'date' => '2024-08-10',
],
'end' => [
'date' => '2024-08-21'
],
])
->assertSuccessful()
->assertJson(fn (AssertableJson $json) => $json
->has('data', fn (AssertableJson $json) => $json
->where('days', 12)
->where('weeks', 1)
->where('week_days', 8)));
}
public function testCanCalulateDurationViaApiWithSecondsModifier(): void
{
$this
->postJson('/calculate-duration', [
'start' => [
'date' => '2024-08-10',
],
'end' => [
'date' => '2024-08-21'
],
'convert_to' => 'second',
])
->assertSuccessful()
->assertJson(fn (AssertableJson $json) => $json
->has('data', fn (AssertableJson $json) => $json
->where('days', 1036800)
->where('weeks', 604800)
->where('week_days', 691200)));
}
public function testCanCalulateDurationViaApiWithHoursModifier(): void
{
$this
->postJson('/calculate-duration', [
'start' => [
'date' => '2024-08-10',
],
'end' => [
'date' => '2024-08-21'
],
'convert_to' => 'hour',
])
->assertSuccessful()
->assertJson(fn (AssertableJson $json) => $json
->has('data', fn (AssertableJson $json) => $json
->where('days', 288)
->where('weeks', 168)
->where('week_days', 192)));
}
public function testCanCalulateDurationViaApiWithMinutesModifier(): void
{
$this
->postJson('/calculate-duration', [
'start' => [
'date' => '2024-08-10',
],
'end' => [
'date' => '2024-08-21'
],
'convert_to' => 'minute',
])
->assertSuccessful()
->assertJson(fn (AssertableJson $json) => $json
->has('data', fn (AssertableJson $json) => $json
->where('days', 17280)
->where('weeks', 10080)
->where('week_days', 11520)));
}
public function testCanCalulateDurationViaApiWithYearsModifier(): void
{
$this
->postJson('/calculate-duration', [
'start' => [
'date' => '2024-08-10',
],
'end' => [
'date' => '2024-08-21'
],
'convert_to' => 'year',
])
->assertSuccessful()
->assertJson(fn (AssertableJson $json) => $json
->has('data', fn (AssertableJson $json) => $json
->where('days', 0.03288)
->where('weeks', 0.01918)
->where('week_days', 0.02192)));
}
public function testCalculateDurationValidatesTimeFormat(): void
{
$this
->postJson('/calculate-duration', [
'start' => [
'date' => 'not valid',
],
'end' => [
'date' => '10-09-1998',
],
])
->assertStatus(422)
->assertJsonValidationErrors([
'start.date' => 'match the format',
'end.date' => 'match the format',
]);
}
public function testCalculateDurationValidatesTimeZones(): void
{
$this
->postJson('/calculate-duration', [
'start' => [
'timezone' => 'not valid',
],
'end' => [
'timezone' => 'Australia/adelaide-but-misspelled-or-something',
],
])
->assertStatus(422)
->assertJsonValidationErrors([
'start.timezone' => 'valid timezone',
'end.timezone' => 'valid timezone',
]);
}
public function testCalculateDurationValidatesConvertToValid(): void
{
$this
->postJson('/calculate-duration', [
'convert_to' => 'invalid value',
])
->assertStatus(422)
->assertJsonValidationErrors([
'convert_to' => 'invalid',
]);
}
}