PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/share/test/application/configuration/configuration-test.php

https://github.com/php-tox/tox
PHP | 357 lines | 240 code | 45 blank | 72 comment | 1 complexity | 599f2cac443a72e4ba99a8f71c728982 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /**
  3. * Defines the test case for Tox\Application\Controller\Controller.
  4. *
  5. * This file is part of Tox.
  6. *
  7. * Tox is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * Tox is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with Tox. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * @copyright Š 2012-2013 PHP-Tox.org
  21. * @license GNU General Public License, version 3
  22. */
  23. namespace Tox\Application\Configuration;
  24. use PHPUnit_Framework_TestCase;
  25. use org\bovigo\vfs\vfsStream;
  26. if (!defined('DIR_VFSSTREAM')) {
  27. define('DIR_VFSSTREAM', __DIR__ . '/../../../../include/mikey179/vfsStream/src/main/php/org/bovigo/vfs');
  28. }
  29. require_once DIR_VFSSTREAM . '/vfsStream.php';
  30. require_once DIR_VFSSTREAM . '/vfsStreamWrapper.php';
  31. require_once DIR_VFSSTREAM . '/Quota.php';
  32. require_once DIR_VFSSTREAM . '/vfsStreamContent.php';
  33. require_once DIR_VFSSTREAM . '/vfsStreamAbstractContent.php';
  34. require_once DIR_VFSSTREAM . '/vfsStreamContainer.php';
  35. require_once DIR_VFSSTREAM . '/vfsStreamDirectory.php';
  36. require_once DIR_VFSSTREAM . '/vfsStreamFile.php';
  37. require_once __DIR__ . '/../../../../src/core/assembly.php';
  38. require_once __DIR__ . '/../../../../src/core/exception.php';
  39. require_once __DIR__ . '/../../../../src/application/iconfiguration.php';
  40. require_once __DIR__ . '/../../../../src/application/configuration/configuration.php';
  41. require_once __DIR__ . '/../../../../src/application/configuration/invalidconfigurationfileexception.php';
  42. require_once __DIR__ . '/../../../../src/application/configuration/invalidconfigurationitemsexception.php';
  43. require_once __DIR__ . '/../../../../src/application/application.php';
  44. use Tox;
  45. /**
  46. * Tests Tox\Application\Configuration\Configuration.
  47. *
  48. * @internal
  49. *
  50. * @package tox.application.configuration
  51. * @author Trainxy Ho <trainxy@gmail.com>
  52. */
  53. class ConfigurationTest extends PHPUnit_Framework_TestCase
  54. {
  55. /**
  56. * Stores the virtual file system.
  57. *
  58. * @var vfsStream
  59. */
  60. protected $vfs;
  61. /**
  62. * Stores the root folder name.
  63. *
  64. * WARNING: Randomize names are used to ignore the `require_once()`
  65. * machenism.
  66. *
  67. * @var string
  68. */
  69. protected $root;
  70. protected $cwd;
  71. public function setUp()
  72. {
  73. $this->cwd = getcwd();
  74. $this->root = md5(microtime());
  75. $this->vfs = vfsStream::setUp(
  76. $this->root,
  77. 0755,
  78. array(
  79. 'etc' => array(
  80. 'essential.conf.php' => '<?php $a_array = array("domain" => "9apps.mobi",' .
  81. ' "package-name" => "com.nineapps.android");',
  82. 'import.conf.php' => '<?php $a_array = array("domain" => "google.com",' .
  83. ' "package-name" => "com.google.android");',
  84. 'constant.conf.php' => '<?php $a_array = array("page-size" => "20", "suffix" => "apk");',
  85. 'db.conf.php' => '<?php $array = array("abc" => "value abc", "bcd" => "value bcd");',
  86. 'memcache.conf.php' => '<?php $a_array = array("memcache" => array("host" => "xxx",' .
  87. ' "port" => "11211"));',
  88. 'default.conf.php' => '<?php $a_array = array("aaa" => "value aaa", "bbb" => "value bbb");',
  89. 'var.conf.php' => '<?php $a_array = "abc";',
  90. 'export.conf.php' => '<?php $a_array = array("abw9j" => "ababsdf2", "cd" => "cdcd",' .
  91. ' "ab2ee" => "ab2ee", "ee" => "eeee");',
  92. )
  93. )
  94. );
  95. }
  96. public function tearDown()
  97. {
  98. chdir($this->cwd);
  99. }
  100. /**
  101. * @dataProvider legalConfigurationFile
  102. */
  103. public function testInitConfigurationWouldBeFine($path, $configs)
  104. {
  105. $o_configuration = $this->getMockBuilder('Tox\\Application\\Configuration\\Configuration')
  106. ->setMethods(array('getPath'))
  107. ->disableOriginalConstructor()
  108. ->getMock();
  109. $o_configuration->expects($this->once())
  110. ->method('getPath')
  111. ->with($this->equalTo($path))
  112. ->will($this->returnValue(vfsStream::url($this->root . $path)));
  113. $o_configuration->__construct($path);
  114. $o_expected_config = json_decode($configs, true);
  115. /**
  116. * @XXX Can not assertEqual an object (implemented ArrayAccess) and an array, use loop to instead.
  117. */
  118. foreach ($o_expected_config as $k => $v) {
  119. $this->assertEquals($o_expected_config[$k], $o_configuration[$k]);
  120. }
  121. }
  122. /**
  123. * @dataProvider legalConfigurationFile
  124. */
  125. public function testImportConfigurationWouldBeFine($path, $configs)
  126. {
  127. $o_configuration = $this->getMockBuilder('Tox\\Application\\Configuration\\Configuration')
  128. ->setMethods(array('getPath'))
  129. ->disableOriginalConstructor()
  130. ->getMock();
  131. $o_configuration->expects($this->once())
  132. ->method('getPath')
  133. ->with($this->equalTo($path))
  134. ->will($this->returnValue(vfsStream::url($this->root . $path)));
  135. $o_configuration->import($path);
  136. $o_expected_config = json_decode($configs, true);
  137. /**
  138. * @XXX Can not assertEqual an object (implemented ArrayAccess) and an array, use loop to instead.
  139. */
  140. foreach ($o_expected_config as $k => $v) {
  141. $this->assertEquals($o_expected_config[$k], $o_configuration[$k]);
  142. }
  143. }
  144. /**
  145. * @depends testInitConfigurationWouldBeFine
  146. * @dataProvider invalidConfigurationFile
  147. * @expectedException Tox\Application\Configuration\InvalidConfigurationFileException
  148. */
  149. public function testInvalidConfiguratoinFileWouldNotBeInited($path)
  150. {
  151. $o_configuration = $this->getMockBuilder('Tox\\Application\\Configuration\\Configuration')
  152. ->setMethods(array('getPath'))
  153. ->disableOriginalConstructor()
  154. ->getMock();
  155. $o_configuration->expects($this->once())
  156. ->method('getPath')
  157. ->with($this->equalTo($path))
  158. ->will($this->returnValue(vfsStream::url($this->root . $path)));
  159. $o_configuration->__construct($path);
  160. }
  161. /**
  162. * @depends testInitConfigurationWouldBeFine
  163. * @dataProvider invalidConfigurationFile
  164. * @expectedException Tox\Application\Configuration\InvalidConfigurationFileException
  165. */
  166. public function testInvalidConfiguratoinFileWouldNotBeImported($path)
  167. {
  168. $o_configuration = $this->getMockBuilder('Tox\\Application\\Configuration\\Configuration')
  169. ->setMethods(array('getPath'))
  170. ->disableOriginalConstructor()
  171. ->getMock();
  172. $o_configuration->expects($this->once())
  173. ->method('getPath')
  174. ->with($this->equalTo($path))
  175. ->will($this->returnValue(vfsStream::url($this->root . $path)));
  176. $o_configuration->import($path);
  177. }
  178. /**
  179. * @depends testInitConfigurationWouldBeFine
  180. */
  181. public function testLoadedOrSettedItemsWouldOverWriteInitedItems()
  182. {
  183. $path = '/etc/essential.conf.php';
  184. $o_configuration = $this->getMockBuilder('Tox\\Application\\Configuration\\Configuration')
  185. ->setMethods(array('getPath'))
  186. ->disableOriginalConstructor()
  187. ->getMock();
  188. $o_configuration->expects($this->once())
  189. ->method('getPath')
  190. ->with($this->equalTo($path))
  191. ->will($this->returnValue(vfsStream::url($this->root . $path)));
  192. $o_configuration->__construct($path);
  193. $o_configuration->load(array('domain' => 'abc', 'package-name' => 'bcd'));
  194. $this->assertEquals('abc', $o_configuration['domain']);
  195. $this->assertEquals('bcd', $o_configuration['package-name']);
  196. $o_configuration->set('domain', 'google.com');
  197. $o_configuration->set('package-name', 'com.google.android');
  198. $this->assertTrue(isset($o_configuration['domain']));
  199. $this->assertEquals('google.com', $o_configuration['domain']);
  200. $this->assertEquals('com.google.android', $o_configuration['package-name']);
  201. }
  202. /**
  203. * @depends testInitConfigurationWouldBeFine
  204. * @depends testImportConfigurationWouldBeFine
  205. */
  206. public function testImportedItemsWouldNotOverWriteInitedItems()
  207. {
  208. $path = '/etc/essential.conf.php';
  209. $import_path = '/etc/import.conf.php';
  210. $o_configuration = $this->getMockBuilder('Tox\\Application\\Configuration\\Configuration')
  211. ->setMethods(array('getPath'))
  212. ->disableOriginalConstructor()
  213. ->getMock();
  214. $o_configuration->expects($this->at(0))
  215. ->method('getPath')
  216. ->with($this->equalTo($path))
  217. ->will($this->returnValue(vfsStream::url($this->root . $path)));
  218. $o_configuration->expects($this->at(1))
  219. ->method('getPath')
  220. ->with($this->equalTo($import_path))
  221. ->will($this->returnValue(vfsStream::url($this->root . $import_path)));
  222. $o_configuration->__construct($path);
  223. $this->assertEquals('9apps.mobi', $o_configuration['domain']);
  224. $this->assertEquals('com.nineapps.android', $o_configuration['package-name']);
  225. $o_configuration->import($import_path);
  226. $this->assertEquals('9apps.mobi', $o_configuration['domain']);
  227. $this->assertEquals('com.nineapps.android', $o_configuration['package-name']);
  228. $o_configuration['domain'] = 'offsetset-domain';
  229. $o_configuration['package-name'] = 'offsetset-package-name';
  230. $this->assertEquals('offsetset-domain', $o_configuration['domain']);
  231. $this->assertEquals('offsetset-package-name', $o_configuration['package-name']);
  232. unset($o_configuration['domain']);
  233. $this->assertNull($o_configuration['domain']);
  234. $this->assertNull($o_configuration['confignotseted']);
  235. }
  236. public function testExportItemsWouldBeFine()
  237. {
  238. $path = '/etc/export.conf.php';
  239. $o_configuration = $this->getMockBuilder('Tox\\Application\\Configuration\\Configuration')
  240. ->setMethods(array('getPath'))
  241. ->disableOriginalConstructor()
  242. ->getMock();
  243. $o_configuration->expects($this->once())
  244. ->method('getPath')
  245. ->with($this->equalTo($path))
  246. ->will($this->returnValue(vfsStream::url($this->root . $path)));
  247. $o_configuration->import($path);
  248. $a_export = $o_configuration->export('ab*');
  249. $this->assertEquals($a_export, array('abw9j' => 'ababsdf2', 'ab2ee' => 'ab2ee'));
  250. $a_export = $o_configuration->export('sab*', array('default' => 'default'));
  251. $this->assertEquals($a_export, array('default' => 'default'));
  252. }
  253. public function testDumpWouldBeFine()
  254. {
  255. $path = '/etc/essential.conf.php';
  256. $import_path = '/etc/import.conf.php';
  257. $import_path_2 = '/etc/memcache.conf.php';
  258. $o_configuration = $this->getMockBuilder('Tox\\Application\\Configuration\\Configuration')
  259. ->setMethods(array('getPath'))
  260. ->disableOriginalConstructor()
  261. ->getMock();
  262. $o_configuration->expects($this->at(0))
  263. ->method('getPath')
  264. ->with($this->equalTo($path))
  265. ->will($this->returnValue(vfsStream::url($this->root . $path)));
  266. $o_configuration->expects($this->at(1))
  267. ->method('getPath')
  268. ->with($this->equalTo($import_path))
  269. ->will($this->returnValue(vfsStream::url($this->root . $import_path)));
  270. $o_configuration->expects($this->at(2))
  271. ->method('getPath')
  272. ->with($this->equalTo($import_path_2))
  273. ->will($this->returnValue(vfsStream::url($this->root . $import_path_2)));
  274. $o_configuration->__construct($path);
  275. $o_configuration->import($import_path);
  276. $o_configuration->import($import_path_2);
  277. $o_configuration->load(array('load-item' => 'loaded', 'load-item2' => 'loaded2'));
  278. $o_configuration->set('seted-item', 'seted');
  279. $o_dump = $o_configuration->dump();
  280. $this->assertTrue(array_key_exists('global', $o_dump));
  281. $this->assertTrue(array_key_exists('imported', $o_dump));
  282. $this->assertTrue(array_key_exists('loaded', $o_dump));
  283. $this->assertTrue(array_key_exists('seted', $o_dump));
  284. }
  285. public function testInvalidFileCanNotBeAssembled()
  286. {
  287. $o_configuration = $this->getMockBuilder('Tox\\Application\\Configuration\\Configuration')
  288. ->setMethods(array('abc'))
  289. ->disableOriginalConstructor()
  290. ->getMock();
  291. $this->assertFalse($o_configuration->getPath('/etc/conf.php'));
  292. }
  293. public function invalidConfigurationFile()
  294. {
  295. return array(
  296. array('/etc/db.conf.php'),
  297. array('/etc/var.conf.php'),
  298. );
  299. }
  300. public function legalConfigurationFile()
  301. {
  302. return array(
  303. array('/etc/constant.conf.php', json_encode(array('page-size' => 20, 'suffix' => 'apk'))),
  304. array('/etc/default.conf.php', json_encode(array('aaa' => 'value aaa', 'bbb' => 'value bbb'))),
  305. array(
  306. '/etc/essential.conf.php',
  307. json_encode(array('domain' => '9apps.mobi', 'package-name' => 'com.nineapps.android'))
  308. )
  309. );
  310. }
  311. }
  312. // vi:ft=php fenc=utf-8 ff=unix ts=4 sts=4 et sw=4 fen fdm=indent fdl=1 tw=120