RepositoryTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. namespace Illuminate\Tests\Config;
  3. use Illuminate\Config\Repository;
  4. use PHPUnit\Framework\TestCase;
  5. class RepositoryTest extends TestCase
  6. {
  7. /**
  8. * @var \Illuminate\Config\Repository
  9. */
  10. protected $repository;
  11. /**
  12. * @var array
  13. */
  14. protected $config;
  15. protected function setUp(): void
  16. {
  17. $this->repository = new Repository($this->config = [
  18. 'foo' => 'bar',
  19. 'bar' => 'baz',
  20. 'baz' => 'bat',
  21. 'null' => null,
  22. 'associate' => [
  23. 'x' => 'xxx',
  24. 'y' => 'yyy',
  25. ],
  26. 'array' => [
  27. 'aaa',
  28. 'zzz',
  29. ],
  30. 'x' => [
  31. 'z' => 'zoo',
  32. ],
  33. ]);
  34. parent::setUp();
  35. }
  36. public function testConstruct()
  37. {
  38. $this->assertInstanceOf(Repository::class, $this->repository);
  39. }
  40. public function testHasIsTrue()
  41. {
  42. $this->assertTrue($this->repository->has('foo'));
  43. }
  44. public function testHasIsFalse()
  45. {
  46. $this->assertFalse($this->repository->has('not-exist'));
  47. }
  48. public function testGet()
  49. {
  50. $this->assertSame('bar', $this->repository->get('foo'));
  51. }
  52. public function testGetWithArrayOfKeys()
  53. {
  54. $this->assertSame([
  55. 'foo' => 'bar',
  56. 'bar' => 'baz',
  57. 'none' => null,
  58. ], $this->repository->get([
  59. 'foo',
  60. 'bar',
  61. 'none',
  62. ]));
  63. $this->assertSame([
  64. 'x.y' => 'default',
  65. 'x.z' => 'zoo',
  66. 'bar' => 'baz',
  67. 'baz' => 'bat',
  68. ], $this->repository->get([
  69. 'x.y' => 'default',
  70. 'x.z' => 'default',
  71. 'bar' => 'default',
  72. 'baz',
  73. ]));
  74. }
  75. public function testGetMany()
  76. {
  77. $this->assertSame([
  78. 'foo' => 'bar',
  79. 'bar' => 'baz',
  80. 'none' => null,
  81. ], $this->repository->getMany([
  82. 'foo',
  83. 'bar',
  84. 'none',
  85. ]));
  86. $this->assertSame([
  87. 'x.y' => 'default',
  88. 'x.z' => 'zoo',
  89. 'bar' => 'baz',
  90. 'baz' => 'bat',
  91. ], $this->repository->getMany([
  92. 'x.y' => 'default',
  93. 'x.z' => 'default',
  94. 'bar' => 'default',
  95. 'baz',
  96. ]));
  97. }
  98. public function testGetWithDefault()
  99. {
  100. $this->assertSame('default', $this->repository->get('not-exist', 'default'));
  101. }
  102. public function testSet()
  103. {
  104. $this->repository->set('key', 'value');
  105. $this->assertSame('value', $this->repository->get('key'));
  106. }
  107. public function testSetArray()
  108. {
  109. $this->repository->set([
  110. 'key1' => 'value1',
  111. 'key2' => 'value2',
  112. ]);
  113. $this->assertSame('value1', $this->repository->get('key1'));
  114. $this->assertSame('value2', $this->repository->get('key2'));
  115. }
  116. public function testPrepend()
  117. {
  118. $this->repository->prepend('array', 'xxx');
  119. $this->assertSame('xxx', $this->repository->get('array.0'));
  120. }
  121. public function testPush()
  122. {
  123. $this->repository->push('array', 'xxx');
  124. $this->assertSame('xxx', $this->repository->get('array.2'));
  125. }
  126. public function testPrependWithNewKey()
  127. {
  128. $this->repository->prepend('new_key', 'xxx');
  129. $this->assertSame(['xxx'], $this->repository->get('new_key'));
  130. }
  131. public function testPushWithNewKey()
  132. {
  133. $this->repository->push('new_key', 'xxx');
  134. $this->assertSame(['xxx'], $this->repository->get('new_key'));
  135. }
  136. public function testAll()
  137. {
  138. $this->assertSame($this->config, $this->repository->all());
  139. }
  140. public function testOffsetExists()
  141. {
  142. $this->assertTrue(isset($this->repository['foo']));
  143. $this->assertFalse(isset($this->repository['not-exist']));
  144. }
  145. public function testOffsetGet()
  146. {
  147. $this->assertNull($this->repository['not-exist']);
  148. $this->assertSame('bar', $this->repository['foo']);
  149. $this->assertSame([
  150. 'x' => 'xxx',
  151. 'y' => 'yyy',
  152. ], $this->repository['associate']);
  153. }
  154. public function testOffsetSet()
  155. {
  156. $this->assertNull($this->repository['key']);
  157. $this->repository['key'] = 'value';
  158. $this->assertSame('value', $this->repository['key']);
  159. }
  160. public function testOffsetUnset()
  161. {
  162. $this->assertArrayHasKey('associate', $this->repository->all());
  163. $this->assertSame($this->config['associate'], $this->repository->get('associate'));
  164. unset($this->repository['associate']);
  165. $this->assertArrayHasKey('associate', $this->repository->all());
  166. $this->assertNull($this->repository->get('associate'));
  167. }
  168. }