InitialStateServiceTest.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. namespace Test;
  24. use OC\AppFramework\Bootstrap\Coordinator;
  25. use OCP\IServerContainer;
  26. use function json_encode;
  27. use JsonSerializable;
  28. use OC\InitialStateService;
  29. use OCP\ILogger;
  30. use stdClass;
  31. class InitialStateServiceTest extends TestCase {
  32. /** @var InitialStateService */
  33. private $service;
  34. protected function setUp(): void {
  35. parent::setUp();
  36. $this->service = new InitialStateService(
  37. $this->createMock(ILogger::class),
  38. $this->createMock(Coordinator::class),
  39. $this->createMock(IServerContainer::class)
  40. );
  41. }
  42. public function staticData() {
  43. return [
  44. ['string'],
  45. [23],
  46. [2.3],
  47. [new class implements JsonSerializable {
  48. public function jsonSerialize() {
  49. return 3;
  50. }
  51. }],
  52. ];
  53. }
  54. /**
  55. * @dataProvider staticData
  56. */
  57. public function testStaticData($value) {
  58. $this->service->provideInitialState('test', 'key', $value);
  59. $data = $this->service->getInitialStates();
  60. $this->assertEquals(
  61. ['test-key' => json_encode($value)],
  62. $data
  63. );
  64. }
  65. public function testStaticButInvalidData() {
  66. $this->service->provideInitialState('test', 'key', new stdClass());
  67. $data = $this->service->getInitialStates();
  68. $this->assertEquals(
  69. [],
  70. $data
  71. );
  72. }
  73. /**
  74. * @dataProvider staticData
  75. */
  76. public function testLazyData($value) {
  77. $this->service->provideLazyInitialState('test', 'key', function () use ($value) {
  78. return $value;
  79. });
  80. $data = $this->service->getInitialStates();
  81. $this->assertEquals(
  82. ['test-key' => json_encode($value)],
  83. $data
  84. );
  85. }
  86. }