ConfigTest.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. namespace think\tests;
  3. use org\bovigo\vfs\vfsStream;
  4. use PHPUnit\Framework\TestCase;
  5. use think\Config;
  6. class ConfigTest extends TestCase
  7. {
  8. public function testLoad()
  9. {
  10. $root = vfsStream::setup();
  11. $file = vfsStream::newFile('test.php')->setContent("<?php return ['key1'=> 'value1','key2'=>'value2'];");
  12. $root->addChild($file);
  13. $config = new Config();
  14. $config->load($file->url(), 'test');
  15. $this->assertEquals('value1', $config->get('test.key1'));
  16. $this->assertEquals('value2', $config->get('test.key2'));
  17. $this->assertSame(['key1' => 'value1', 'key2' => 'value2'], $config->get('test'));
  18. }
  19. public function testSetAndGet()
  20. {
  21. $config = new Config();
  22. $config->set([
  23. 'key1' => 'value1',
  24. 'key2' => [
  25. 'key3' => 'value3',
  26. ],
  27. ], 'test');
  28. $this->assertTrue($config->has('test.key1'));
  29. $this->assertEquals('value1', $config->get('test.key1'));
  30. $this->assertEquals('value3', $config->get('test.key2.key3'));
  31. $this->assertEquals(['key3' => 'value3'], $config->get('test.key2'));
  32. $this->assertFalse($config->has('test.key3'));
  33. $this->assertEquals('none', $config->get('test.key3', 'none'));
  34. }
  35. }