WorldCupScheduleUpdateServiceTest.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. namespace Tests\Unit;
  3. use App\Services\WorldCup\Repositories\WorldCupScheduleRepositoryInterface;
  4. use App\Services\WorldCup\WorldCupScheduleUpdateService;
  5. use Tests\TestCase;
  6. class WorldCupScheduleUpdateServiceTest extends TestCase
  7. {
  8. public function test_lists_all_matches_for_admin_form()
  9. {
  10. $repository = new InMemoryWorldCupScheduleRepository();
  11. $service = new WorldCupScheduleUpdateService($repository);
  12. $matches = $service->allMatches();
  13. $this->assertCount(3, $matches);
  14. $this->assertSame(72, $matches[0]['match_no']);
  15. $this->assertSame(104, $matches[2]['match_no']);
  16. }
  17. public function test_updates_checked_form_rows_across_all_schedule()
  18. {
  19. $repository = new InMemoryWorldCupScheduleRepository();
  20. $service = new WorldCupScheduleUpdateService($repository);
  21. $result = $service->updateMatches([
  22. [
  23. 'enabled' => '1',
  24. 'match_no' => 72,
  25. 'home_team' => 'Croatia Updated',
  26. 'away_team' => 'Ghana',
  27. 'venue' => 'Philadelphia',
  28. 'kickoff_at' => '2026-06-27 21:00:00',
  29. 'status' => 'scheduled',
  30. ],
  31. [
  32. 'enabled' => '0',
  33. 'match_no' => 104,
  34. 'home_team' => 'France',
  35. 'away_team' => 'Brazil',
  36. 'status' => 'closed',
  37. ],
  38. ], 'admin-01');
  39. $this->assertTrue($result['success']);
  40. $this->assertSame(1, $result['data']['updated']);
  41. $this->assertSame(1, $result['data']['skipped']);
  42. $this->assertSame('Croatia Updated', $repository->matches[72]['home_team']);
  43. $this->assertSame('Winner 104 A', $repository->matches[104]['home_team']);
  44. }
  45. public function test_updates_only_unresolved_matches_for_selected_date()
  46. {
  47. $repository = new InMemoryWorldCupScheduleRepository();
  48. $service = new WorldCupScheduleUpdateService($repository);
  49. $result = $service->updateUnresolvedMatches('2026-07-19', [
  50. [
  51. 'match_no' => 104,
  52. 'home_team' => 'France',
  53. 'away_team' => 'Brazil',
  54. 'venue' => 'East Rutherford',
  55. ],
  56. [
  57. 'match_no' => 103,
  58. 'home_team' => 'Argentina',
  59. 'away_team' => 'England',
  60. ],
  61. [
  62. 'match_no' => 72,
  63. 'home_team' => 'Croatia',
  64. 'away_team' => 'Ghana',
  65. ],
  66. ], 'admin-01');
  67. $this->assertTrue($result['success']);
  68. $this->assertSame(1, $result['data']['updated']);
  69. $this->assertSame(2, $result['data']['skipped']);
  70. $this->assertSame('France', $repository->matches[104]['home_team']);
  71. $this->assertSame('Brazil', $repository->matches[104]['away_team']);
  72. $this->assertSame('closed', $repository->matches[104]['status']);
  73. $this->assertSame('Winner 103 A', $repository->matches[103]['home_team']);
  74. $this->assertSame('Croatia', $repository->matches[72]['home_team']);
  75. $this->assertSame('schedule_update', $repository->audits[0]['action']);
  76. }
  77. public function test_allows_operator_to_open_match_when_status_is_scheduled()
  78. {
  79. $repository = new InMemoryWorldCupScheduleRepository();
  80. $service = new WorldCupScheduleUpdateService($repository);
  81. $result = $service->updateUnresolvedMatches('2026-07-19', [
  82. [
  83. 'match_no' => 104,
  84. 'home_team' => 'France',
  85. 'away_team' => 'Brazil',
  86. 'status' => 'scheduled',
  87. ],
  88. ], 'admin-01');
  89. $this->assertTrue($result['success']);
  90. $this->assertSame('scheduled', $repository->matches[104]['status']);
  91. }
  92. public function test_rejects_invalid_payload_rows()
  93. {
  94. $repository = new InMemoryWorldCupScheduleRepository();
  95. $service = new WorldCupScheduleUpdateService($repository);
  96. $result = $service->updateUnresolvedMatches('2026-07-19', [
  97. ['match_no' => '', 'home_team' => 'France', 'away_team' => 'Brazil'],
  98. ['match_no' => 104, 'home_team' => '', 'away_team' => 'Brazil'],
  99. ['match_no' => 104, 'home_team' => 'France', 'away_team' => 'Brazil', 'status' => 'open'],
  100. ], 'admin-01');
  101. $this->assertFalse($result['success']);
  102. $this->assertSame(0, $result['data']['updated']);
  103. $this->assertCount(3, $result['data']['errors']);
  104. }
  105. public function test_rejects_invalid_schedule_date()
  106. {
  107. $repository = new InMemoryWorldCupScheduleRepository();
  108. $service = new WorldCupScheduleUpdateService($repository);
  109. $result = $service->updateUnresolvedMatches('2026/07/19', [], 'admin-01');
  110. $this->assertFalse($result['success']);
  111. $this->assertSame('Invalid schedule date', $result['message']);
  112. }
  113. public function test_imports_existing_matches_from_csv_rows_by_match_id()
  114. {
  115. $repository = new InMemoryWorldCupScheduleRepository();
  116. $service = new WorldCupScheduleUpdateService($repository);
  117. $result = $service->importExistingMatches([
  118. [
  119. 'match_id' => '104',
  120. 'home_name' => 'France',
  121. 'away_name' => 'Brazil',
  122. ],
  123. [
  124. 'match_id' => '103',
  125. 'home_name' => 'Argentina',
  126. 'away_name' => 'England',
  127. ],
  128. [
  129. 'match_id' => '',
  130. 'home_name' => 'Spain',
  131. 'away_name' => 'Portugal',
  132. ],
  133. ], 'admin-01');
  134. $this->assertTrue($result['success']);
  135. $this->assertSame(2, $result['data']['updated']);
  136. $this->assertSame(0, $result['data']['skipped']);
  137. $this->assertCount(1, $result['data']['errors']);
  138. $this->assertSame('France', $repository->matches[104]['home_team']);
  139. $this->assertSame('Brazil', $repository->matches[104]['away_team']);
  140. $this->assertSame('schedule_import', $repository->audits[0]['action']);
  141. }
  142. }
  143. class InMemoryWorldCupScheduleRepository implements WorldCupScheduleRepositoryInterface
  144. {
  145. public $matches = [
  146. 72 => [
  147. 'match_no' => 72,
  148. 'home_team' => 'Croatia',
  149. 'away_team' => 'Ghana',
  150. 'venue' => 'Philadelphia',
  151. 'kickoff_at' => '2026-06-27 21:00:00',
  152. 'status' => 'scheduled',
  153. ],
  154. 103 => [
  155. 'match_no' => 103,
  156. 'home_team' => 'Winner 103 A',
  157. 'away_team' => 'Winner 103 B',
  158. 'venue' => 'Miami Gardens',
  159. 'kickoff_at' => '2026-07-18 21:00:00',
  160. 'status' => 'closed',
  161. ],
  162. 104 => [
  163. 'match_no' => 104,
  164. 'home_team' => 'Winner 104 A',
  165. 'away_team' => 'Winner 104 B',
  166. 'venue' => 'East Rutherford',
  167. 'kickoff_at' => '2026-07-19 19:00:00',
  168. 'status' => 'closed',
  169. ],
  170. ];
  171. public $audits = [];
  172. public function unresolvedMatchesByDate(string $scheduleDate): array
  173. {
  174. return array_values(array_filter($this->matches, function (array $match) use ($scheduleDate) {
  175. return substr($match['kickoff_at'], 0, 10) === $scheduleDate
  176. && $this->isUnresolved($match);
  177. }));
  178. }
  179. public function allMatches(): array
  180. {
  181. ksort($this->matches);
  182. return array_values($this->matches);
  183. }
  184. public function matchesByDate(string $scheduleDate): array
  185. {
  186. return array_values(array_filter($this->matches, function (array $match) use ($scheduleDate) {
  187. return substr($match['kickoff_at'], 0, 10) === $scheduleDate;
  188. }));
  189. }
  190. public function updateMatchByNoAndDate(
  191. int $matchNo,
  192. string $scheduleDate,
  193. array $attributes
  194. ): bool {
  195. if (!isset($this->matches[$matchNo])) {
  196. return false;
  197. }
  198. if (substr($this->matches[$matchNo]['kickoff_at'], 0, 10) !== $scheduleDate) {
  199. return false;
  200. }
  201. if (!$this->isUnresolved($this->matches[$matchNo])) {
  202. return false;
  203. }
  204. $this->matches[$matchNo] = array_merge($this->matches[$matchNo], $attributes);
  205. return true;
  206. }
  207. public function updateMatchByNo(int $matchNo, array $attributes): bool
  208. {
  209. if (!isset($this->matches[$matchNo])) {
  210. return false;
  211. }
  212. $this->matches[$matchNo] = array_merge($this->matches[$matchNo], $attributes);
  213. return true;
  214. }
  215. public function updateMatchById(int $matchId, array $attributes): bool
  216. {
  217. return $this->updateMatchByNo($matchId, $attributes);
  218. }
  219. public function writeScheduleAudit(string $actor, string $action, array $payload): void
  220. {
  221. $this->audits[] = compact('actor', 'action', 'payload');
  222. }
  223. private function isUnresolved(array $match): bool
  224. {
  225. return strpos($match['home_team'], 'Winner ') === 0
  226. || strpos($match['away_team'], 'Winner ') === 0;
  227. }
  228. }