AppSettingsRepositoryTest.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Tests\Repositories;
  4. use App\Repositories\AppSettingsRepository;
  5. use App\Tests\TestCase;
  6. /**
  7. * Phase 18: app_settings KV store. The migration seeds
  8. * task_status_enabled='0', so the boolean reader has a known starting point.
  9. */
  10. final class AppSettingsRepositoryTest extends TestCase
  11. {
  12. public function testSeededFlagDefaultsToOff(): void
  13. {
  14. $pdo = $this->makeDb();
  15. $repo = new AppSettingsRepository($pdo);
  16. $this->assertSame('0', $repo->get('task_status_enabled'));
  17. $this->assertFalse($repo->getBool('task_status_enabled', false));
  18. }
  19. public function testSetReturnsBeforeAndAfter(): void
  20. {
  21. $pdo = $this->makeDb();
  22. $repo = new AppSettingsRepository($pdo);
  23. $r = $repo->set('task_status_enabled', '1');
  24. $this->assertSame('0', $r['before']);
  25. $this->assertSame('1', $r['after']);
  26. $this->assertTrue($repo->getBool('task_status_enabled'));
  27. }
  28. public function testSetWithSameValueIsRecognisedAsNoop(): void
  29. {
  30. $pdo = $this->makeDb();
  31. $repo = new AppSettingsRepository($pdo);
  32. $r = $repo->set('task_status_enabled', '0');
  33. $this->assertSame('0', $r['before']);
  34. $this->assertSame('0', $r['after']);
  35. }
  36. public function testGetReturnsDefaultForUnknownKey(): void
  37. {
  38. $pdo = $this->makeDb();
  39. $repo = new AppSettingsRepository($pdo);
  40. $this->assertNull($repo->get('does_not_exist'));
  41. $this->assertSame('fallback', $repo->get('does_not_exist', 'fallback'));
  42. $this->assertFalse($repo->getBool('does_not_exist'));
  43. $this->assertTrue($repo->getBool('does_not_exist', true));
  44. }
  45. public function testInsertNewKeyOnFirstWrite(): void
  46. {
  47. $pdo = $this->makeDb();
  48. $repo = new AppSettingsRepository($pdo);
  49. $r = $repo->set('new_flag', '1');
  50. $this->assertNull($r['before']);
  51. $this->assertSame('1', $r['after']);
  52. $this->assertSame('1', $repo->get('new_flag'));
  53. }
  54. }