|
4 | 4 |
|
5 | 5 | use Exception; |
6 | 6 | use pconfig\PConfig; |
| 7 | +use pconfig\provider\impl\FileProvider; |
| 8 | +use pconfig\serializer\impl\JSONSerializer; |
7 | 9 | use PHPUnit\Framework\TestCase; |
8 | 10 |
|
9 | 11 | /** |
@@ -34,38 +36,92 @@ public function testYAML() |
34 | 36 | $this->_testConfig('yaml'); |
35 | 37 | } |
36 | 38 |
|
37 | | - private function _testConfig($type) |
| 39 | + public function testNew() |
38 | 40 | { |
39 | | - $config = null; |
40 | 41 | try { |
41 | | - $config = new PConfig($this->basePath . 'config.' . $type); |
| 42 | + $config = new PConfig($this->basePath . 'config.json'); |
| 43 | + $this->_testPartial($config); |
42 | 44 | } catch (Exception $e) { |
43 | 45 | $this->fail($e->getMessage()); |
44 | 46 | } |
45 | | - $this->assertNotNull($config); |
46 | 47 |
|
47 | | - $this->assertArrayHasKey('language', $config); |
48 | | - $this->assertArrayHasKey("php", $config->get('language')); |
49 | | - $this->assertArrayHasKey("php", $config['language']); |
50 | | - $this->assertArrayHasKey("type", $config['language']['php']); |
51 | | - $this->assertCount(3, $config['language']['php']['type']); |
52 | | - $this->assertContains('object', $config['language']['php']['type']); |
| 48 | + try { |
| 49 | + $config = new PConfig([ |
| 50 | + PConfig::CONFIG_KEY_FILE => $this->basePath . 'config.json' |
| 51 | + ]); |
| 52 | + $this->_testPartial($config); |
| 53 | + } catch (Exception $e) { |
| 54 | + $this->fail($e->getMessage()); |
| 55 | + } |
53 | 56 |
|
54 | 57 | try { |
55 | 58 | $config = new PConfig([ |
56 | | - 'file' => $this->basePath . 'config.' . $type, |
| 59 | + PConfig::CONFIG_KEY_DATA => [ |
| 60 | + 'language' => [ |
| 61 | + 'php' => [ |
| 62 | + 'type' => ['array', 'string', 'object'] |
| 63 | + ] |
| 64 | + ] |
| 65 | + ] |
57 | 66 | ]); |
| 67 | + $this->_testPartial($config); |
58 | 68 | } catch (Exception $e) { |
59 | 69 | $this->fail($e->getMessage()); |
60 | 70 | } |
61 | | - $this->assertNotNull($config); |
62 | 71 |
|
| 72 | + try { |
| 73 | + $config = new PConfig([ |
| 74 | + PConfig::CONFIG_KEY_FILE => $this->basePath . 'config.json', |
| 75 | + PConfig::CONFIG_KEY_SERIALIZER => new JSONSerializer(), |
| 76 | + ]); |
| 77 | + $this->_testPartial($config); |
| 78 | + } catch (Exception $e) { |
| 79 | + $this->fail($e->getMessage()); |
| 80 | + } |
| 81 | + |
| 82 | + try { |
| 83 | + $config = new PConfig([ |
| 84 | + PConfig::CONFIG_KEY_PROVIDER => new FileProvider($this->basePath . 'config.json'), |
| 85 | + PConfig::CONFIG_KEY_SERIALIZER => new JSONSerializer(), |
| 86 | + ]); |
| 87 | + $this->_testPartial($config); |
| 88 | + } catch (Exception $e) { |
| 89 | + $this->fail($e->getMessage()); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @param PConfig $config |
| 95 | + */ |
| 96 | + private function _testPartial($config) |
| 97 | + { |
| 98 | + $this->assertNotNull($config); |
63 | 99 | $this->assertArrayHasKey('language', $config); |
64 | 100 | $this->assertArrayHasKey("php", $config->get('language')); |
65 | 101 | $this->assertArrayHasKey("php", $config['language']); |
66 | 102 | $this->assertArrayHasKey("type", $config['language']['php']); |
67 | 103 | $this->assertCount(3, $config['language']['php']['type']); |
68 | 104 | $this->assertContains('object', $config['language']['php']['type']); |
| 105 | + } |
| 106 | + |
| 107 | + private function _testConfig($type) |
| 108 | + { |
| 109 | + $config = null; |
| 110 | + try { |
| 111 | + $config = new PConfig($this->basePath . 'config.' . $type); |
| 112 | + } catch (Exception $e) { |
| 113 | + $this->fail($e->getMessage()); |
| 114 | + } |
| 115 | + $this->_testPartial($config); |
| 116 | + |
| 117 | + try { |
| 118 | + $config = new PConfig([ |
| 119 | + 'file' => $this->basePath . 'config.' . $type, |
| 120 | + ]); |
| 121 | + } catch (Exception $e) { |
| 122 | + $this->fail($e->getMessage()); |
| 123 | + } |
| 124 | + $this->_testPartial($config); |
69 | 125 |
|
70 | 126 | $this->assertTrue($config->set('hello.world', true)); |
71 | 127 |
|
|
0 commit comments