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

  1. <?php
  2. namespace Tests\Feature\Http;
  3. use Illuminate\Testing\Fluent\AssertableJson;
  4. use Tests\TestCase;
  5. class CalculateDurationTest extends TestCase
  6. {
  7. public function testCanCalulateDurationViaApi(): void
  8. {
  9. $this
  10. ->postJson('/calculate-duration', [
  11. 'start' => [
  12. 'date' => '2024-08-10',
  13. ],
  14. 'end' => [
  15. 'date' => '2024-08-21'
  16. ],
  17. ])
  18. ->assertSuccessful()
  19. ->assertJson(fn (AssertableJson $json) => $json
  20. ->has('data', fn (AssertableJson $json) => $json
  21. ->where('days', 12)
  22. ->where('weeks', 1)
  23. ->where('week_days', 8)));
  24. }
  25. public function testCanCalulateDurationViaApiWithSecondsModifier(): void
  26. {
  27. $this
  28. ->postJson('/calculate-duration', [
  29. 'start' => [
  30. 'date' => '2024-08-10',
  31. ],
  32. 'end' => [
  33. 'date' => '2024-08-21'
  34. ],
  35. 'convert_to' => 'second',
  36. ])
  37. ->assertSuccessful()
  38. ->assertJson(fn (AssertableJson $json) => $json
  39. ->has('data', fn (AssertableJson $json) => $json
  40. ->where('days', 1036800)
  41. ->where('weeks', 604800)
  42. ->where('week_days', 691200)));
  43. }
  44. public function testCanCalulateDurationViaApiWithHoursModifier(): void
  45. {
  46. $this
  47. ->postJson('/calculate-duration', [
  48. 'start' => [
  49. 'date' => '2024-08-10',
  50. ],
  51. 'end' => [
  52. 'date' => '2024-08-21'
  53. ],
  54. 'convert_to' => 'hour',
  55. ])
  56. ->assertSuccessful()
  57. ->assertJson(fn (AssertableJson $json) => $json
  58. ->has('data', fn (AssertableJson $json) => $json
  59. ->where('days', 288)
  60. ->where('weeks', 168)
  61. ->where('week_days', 192)));
  62. }
  63. public function testCanCalulateDurationViaApiWithMinutesModifier(): void
  64. {
  65. $this
  66. ->postJson('/calculate-duration', [
  67. 'start' => [
  68. 'date' => '2024-08-10',
  69. ],
  70. 'end' => [
  71. 'date' => '2024-08-21'
  72. ],
  73. 'convert_to' => 'minute',
  74. ])
  75. ->assertSuccessful()
  76. ->assertJson(fn (AssertableJson $json) => $json
  77. ->has('data', fn (AssertableJson $json) => $json
  78. ->where('days', 17280)
  79. ->where('weeks', 10080)
  80. ->where('week_days', 11520)));
  81. }
  82. public function testCanCalulateDurationViaApiWithYearsModifier(): void
  83. {
  84. $this
  85. ->postJson('/calculate-duration', [
  86. 'start' => [
  87. 'date' => '2024-08-10',
  88. ],
  89. 'end' => [
  90. 'date' => '2024-08-21'
  91. ],
  92. 'convert_to' => 'year',
  93. ])
  94. ->assertSuccessful()
  95. ->assertJson(fn (AssertableJson $json) => $json
  96. ->has('data', fn (AssertableJson $json) => $json
  97. ->where('days', 0.03288)
  98. ->where('weeks', 0.01918)
  99. ->where('week_days', 0.02192)));
  100. }
  101. public function testCalculateDurationValidatesTimeFormat(): void
  102. {
  103. $this
  104. ->postJson('/calculate-duration', [
  105. 'start' => [
  106. 'date' => 'not valid',
  107. ],
  108. 'end' => [
  109. 'date' => '10-09-1998',
  110. ],
  111. ])
  112. ->assertStatus(422)
  113. ->assertJsonValidationErrors([
  114. 'start.date' => 'match the format',
  115. 'end.date' => 'match the format',
  116. ]);
  117. }
  118. public function testCalculateDurationValidatesTimeZones(): void
  119. {
  120. $this
  121. ->postJson('/calculate-duration', [
  122. 'start' => [
  123. 'timezone' => 'not valid',
  124. ],
  125. 'end' => [
  126. 'timezone' => 'Australia/adelaide-but-misspelled-or-something',
  127. ],
  128. ])
  129. ->assertStatus(422)
  130. ->assertJsonValidationErrors([
  131. 'start.timezone' => 'valid timezone',
  132. 'end.timezone' => 'valid timezone',
  133. ]);
  134. }
  135. public function testCalculateDurationValidatesConvertToValid(): void
  136. {
  137. $this
  138. ->postJson('/calculate-duration', [
  139. 'convert_to' => 'invalid value',
  140. ])
  141. ->assertStatus(422)
  142. ->assertJsonValidationErrors([
  143. 'convert_to' => 'invalid',
  144. ]);
  145. }
  146. }