1
0

AppSettingsRepositoryTest.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /*
  3. * Copyright 2026 Alessandro Chiapparini <sprint_planer_web@chiapparini.org>
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * See the LICENSE file in the project root for the full license text.
  9. */
  10. declare(strict_types=1);
  11. namespace App\Tests\Repositories;
  12. use App\Repositories\AppSettingsRepository;
  13. use App\Tests\TestCase;
  14. /**
  15. * Phase 18: app_settings KV store. The migration seeds
  16. * task_status_enabled='0', so the boolean reader has a known starting point.
  17. */
  18. final class AppSettingsRepositoryTest extends TestCase
  19. {
  20. public function testSeededFlagDefaultsToOff(): void
  21. {
  22. $pdo = $this->makeDb();
  23. $repo = new AppSettingsRepository($pdo);
  24. $this->assertSame('0', $repo->get('task_status_enabled'));
  25. $this->assertFalse($repo->getBool('task_status_enabled', false));
  26. }
  27. public function testSetReturnsBeforeAndAfter(): void
  28. {
  29. $pdo = $this->makeDb();
  30. $repo = new AppSettingsRepository($pdo);
  31. $r = $repo->set('task_status_enabled', '1');
  32. $this->assertSame('0', $r['before']);
  33. $this->assertSame('1', $r['after']);
  34. $this->assertTrue($repo->getBool('task_status_enabled'));
  35. }
  36. public function testSetWithSameValueIsRecognisedAsNoop(): void
  37. {
  38. $pdo = $this->makeDb();
  39. $repo = new AppSettingsRepository($pdo);
  40. $r = $repo->set('task_status_enabled', '0');
  41. $this->assertSame('0', $r['before']);
  42. $this->assertSame('0', $r['after']);
  43. }
  44. public function testGetReturnsDefaultForUnknownKey(): void
  45. {
  46. $pdo = $this->makeDb();
  47. $repo = new AppSettingsRepository($pdo);
  48. $this->assertNull($repo->get('does_not_exist'));
  49. $this->assertSame('fallback', $repo->get('does_not_exist', 'fallback'));
  50. $this->assertFalse($repo->getBool('does_not_exist'));
  51. $this->assertTrue($repo->getBool('does_not_exist', true));
  52. }
  53. public function testInsertNewKeyOnFirstWrite(): void
  54. {
  55. $pdo = $this->makeDb();
  56. $repo = new AppSettingsRepository($pdo);
  57. $r = $repo->set('new_flag', '1');
  58. $this->assertNull($r['before']);
  59. $this->assertSame('1', $r['after']);
  60. $this->assertSame('1', $repo->get('new_flag'));
  61. }
  62. public function testGetIntReadsNumericStringsAndFallsBackOnMissing(): void
  63. {
  64. $pdo = $this->makeDb();
  65. $repo = new AppSettingsRepository($pdo);
  66. $this->assertSame(10, $repo->getInt('assignment_slider_max', 10));
  67. $repo->set('assignment_slider_max', '20');
  68. $this->assertSame(20, $repo->getInt('assignment_slider_max', 10));
  69. $repo->set('garbage_value', 'not-a-number');
  70. $this->assertSame(7, $repo->getInt('garbage_value', 7));
  71. }
  72. }