123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- <?php
- namespace Illuminate\Tests\Config;
- use Illuminate\Config\Repository;
- use PHPUnit\Framework\TestCase;
- class RepositoryTest extends TestCase
- {
- /**
- * @var \Illuminate\Config\Repository
- */
- protected $repository;
- /**
- * @var array
- */
- protected $config;
- protected function setUp(): void
- {
- $this->repository = new Repository($this->config = [
- 'foo' => 'bar',
- 'bar' => 'baz',
- 'baz' => 'bat',
- 'null' => null,
- 'associate' => [
- 'x' => 'xxx',
- 'y' => 'yyy',
- ],
- 'array' => [
- 'aaa',
- 'zzz',
- ],
- 'x' => [
- 'z' => 'zoo',
- ],
- ]);
- parent::setUp();
- }
- public function testConstruct()
- {
- $this->assertInstanceOf(Repository::class, $this->repository);
- }
- public function testHasIsTrue()
- {
- $this->assertTrue($this->repository->has('foo'));
- }
- public function testHasIsFalse()
- {
- $this->assertFalse($this->repository->has('not-exist'));
- }
- public function testGet()
- {
- $this->assertSame('bar', $this->repository->get('foo'));
- }
- public function testGetWithArrayOfKeys()
- {
- $this->assertSame([
- 'foo' => 'bar',
- 'bar' => 'baz',
- 'none' => null,
- ], $this->repository->get([
- 'foo',
- 'bar',
- 'none',
- ]));
- $this->assertSame([
- 'x.y' => 'default',
- 'x.z' => 'zoo',
- 'bar' => 'baz',
- 'baz' => 'bat',
- ], $this->repository->get([
- 'x.y' => 'default',
- 'x.z' => 'default',
- 'bar' => 'default',
- 'baz',
- ]));
- }
- public function testGetMany()
- {
- $this->assertSame([
- 'foo' => 'bar',
- 'bar' => 'baz',
- 'none' => null,
- ], $this->repository->getMany([
- 'foo',
- 'bar',
- 'none',
- ]));
- $this->assertSame([
- 'x.y' => 'default',
- 'x.z' => 'zoo',
- 'bar' => 'baz',
- 'baz' => 'bat',
- ], $this->repository->getMany([
- 'x.y' => 'default',
- 'x.z' => 'default',
- 'bar' => 'default',
- 'baz',
- ]));
- }
- public function testGetWithDefault()
- {
- $this->assertSame('default', $this->repository->get('not-exist', 'default'));
- }
- public function testSet()
- {
- $this->repository->set('key', 'value');
- $this->assertSame('value', $this->repository->get('key'));
- }
- public function testSetArray()
- {
- $this->repository->set([
- 'key1' => 'value1',
- 'key2' => 'value2',
- ]);
- $this->assertSame('value1', $this->repository->get('key1'));
- $this->assertSame('value2', $this->repository->get('key2'));
- }
- public function testPrepend()
- {
- $this->repository->prepend('array', 'xxx');
- $this->assertSame('xxx', $this->repository->get('array.0'));
- }
- public function testPush()
- {
- $this->repository->push('array', 'xxx');
- $this->assertSame('xxx', $this->repository->get('array.2'));
- }
- public function testPrependWithNewKey()
- {
- $this->repository->prepend('new_key', 'xxx');
- $this->assertSame(['xxx'], $this->repository->get('new_key'));
- }
- public function testPushWithNewKey()
- {
- $this->repository->push('new_key', 'xxx');
- $this->assertSame(['xxx'], $this->repository->get('new_key'));
- }
- public function testAll()
- {
- $this->assertSame($this->config, $this->repository->all());
- }
- public function testOffsetExists()
- {
- $this->assertTrue(isset($this->repository['foo']));
- $this->assertFalse(isset($this->repository['not-exist']));
- }
- public function testOffsetGet()
- {
- $this->assertNull($this->repository['not-exist']);
- $this->assertSame('bar', $this->repository['foo']);
- $this->assertSame([
- 'x' => 'xxx',
- 'y' => 'yyy',
- ], $this->repository['associate']);
- }
- public function testOffsetSet()
- {
- $this->assertNull($this->repository['key']);
- $this->repository['key'] = 'value';
- $this->assertSame('value', $this->repository['key']);
- }
- public function testOffsetUnset()
- {
- $this->assertArrayHasKey('associate', $this->repository->all());
- $this->assertSame($this->config['associate'], $this->repository->get('associate'));
- unset($this->repository['associate']);
- $this->assertArrayHasKey('associate', $this->repository->all());
- $this->assertNull($this->repository->get('associate'));
- }
- }
|