GroupTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php defined('SYSPATH') OR die('Kohana bootstrap needs to be included before tests run');
  2. /**
  3. * Tests the Config group lib
  4. *
  5. * @group kohana
  6. * @group kohana.config
  7. *
  8. * @package Unittest
  9. * @author Kohana Team
  10. * @author Jeremy Bush <[email protected]>
  11. * @author Matt Button <[email protected]>
  12. * @copyright (c) 2008-2012 Kohana Team
  13. * @license http://kohanaphp.com/license
  14. */
  15. class Kohana_Config_GroupTest extends Kohana_Unittest_TestCase
  16. {
  17. /**
  18. * Create a mock Kohana_Config instance
  19. *
  20. * @return Kohana_Config
  21. */
  22. public function get_mock_config()
  23. {
  24. return new Kohana_Config;
  25. }
  26. /**
  27. * Gets a fresh instance of Kohana_Config_Group
  28. *
  29. * @param string $group Config Group name
  30. * @param array $config Configuration
  31. * @param Kohana_Config $instance Instance of Kohana_Config
  32. * @return Kohana_Config_Group
  33. */
  34. public function get_mock_group($group, $config = array(), $instance = NULL)
  35. {
  36. if ($instance === NULL)
  37. {
  38. $instance = $this->get_mock_config();
  39. }
  40. return new Kohana_Config_Group($instance, $group, $config);
  41. }
  42. /**
  43. * The group name and group's config values should be loaded into the object
  44. * by the constructor
  45. *
  46. * @test
  47. * @covers Kohana_Config_Group
  48. */
  49. public function test_loads_group_name_and_values_in_constructor()
  50. {
  51. $group_name = 'information';
  52. $group_values = array('var' => 'value');
  53. $group = $this->get_mock_group($group_name, $group_values);
  54. // Now usually we'd just use assertAttributeSame, but that tries to get at protected properties
  55. // by casting the object in question into an array. This usually works fine, but as Kohana_Config_Group
  56. // is a subclass of ArrayObject, casting to an array returns the config items!
  57. // Therefore we have to use this little workaround
  58. $this->assertSame($group_name, $group->group_name());
  59. $this->assertSame($group_values, $group->getArrayCopy());
  60. }
  61. /**
  62. * A config group may not exist (or may not have any values) when it is loaded.
  63. * The config group should allow for this situation and not complain
  64. *
  65. * @test
  66. * @covers Kohana_Config_Group
  67. */
  68. public function test_allows_empty_group_values()
  69. {
  70. $group = $this->get_mock_group('informatica');
  71. $this->assertSame(array(), $group->getArrayCopy());
  72. }
  73. /**
  74. * When get() is called it should fetch the config value specified
  75. *
  76. * @test
  77. * @covers Kohana_Config_Group::get
  78. */
  79. public function test_get_fetches_config_value()
  80. {
  81. $group = $this->get_mock_group('kohana', array('status' => 'awesome'));
  82. $this->assertSame('awesome', $group->get('status'));
  83. }
  84. /**
  85. * If a config option does not exist then get() should return the default value, which is
  86. * NULL by default
  87. *
  88. * @test
  89. * @covers Kohana_Config_Group::get
  90. */
  91. public function test_get_returns_default_value_if_config_option_dnx()
  92. {
  93. $group = $this->get_mock_group('kohana');
  94. $this->assertSame(NULL, $group->get('problems', NULL));
  95. $this->assertSame('nada', $group->get('problems', 'nada'));
  96. }
  97. /**
  98. * We should be able to modify existing configuration items using set()
  99. *
  100. * @test
  101. * @covers Kohana_Config_Group::set
  102. */
  103. public function test_set_modifies_existing_config()
  104. {
  105. $group = $this->get_mock_group('kohana', array('status' => 'pre-awesome'));
  106. $group->set('status', 'awesome');
  107. $this->assertSame('awesome', $group->get('status'));
  108. }
  109. /**
  110. * If we modify the config via set() [$var] or ->$var then the change should be passed to
  111. * the parent config instance so that the config writers can be notified.
  112. *
  113. * The modification to the config should also stick
  114. *
  115. * @test
  116. * @covers Kohana_Config_Group::offsetSet
  117. */
  118. public function test_writes_changes_to_config()
  119. {
  120. $mock = $this->getMock('Kohana_Config', array('_write_config'));
  121. $mock
  122. ->expects($this->exactly(3))
  123. ->method('_write_config')
  124. ->with('kohana', 'status', $this->LogicalOr('totally', 'maybe', 'not'));
  125. $group = $this->get_mock_group('kohana', array('status' => 'kool'), $mock);
  126. $group['status'] = 'totally';
  127. $group->status = 'maybe';
  128. $group->set('status', 'not');
  129. }
  130. /**
  131. * Calling as_array() should return the full array, inc. any modifications
  132. *
  133. * @test
  134. * @covers Kohana_Config_Group::as_array
  135. */
  136. public function test_as_array_returns_full_array()
  137. {
  138. $config = $this->get_mock_group('something', array('var' => 'value'));
  139. $this->assertSame(array('var' => 'value'), $config->as_array());
  140. // Now change some vars **ahem**
  141. $config->var = 'LOLCAT';
  142. $config->lolcat = 'IN UR CODE';
  143. $this->assertSame(
  144. array('var' => 'LOLCAT', 'lolcat' => 'IN UR CODE'),
  145. $config->as_array()
  146. );
  147. // And if we remove an item it should be removed from the exported array
  148. unset($config['lolcat']);
  149. $this->assertSame(array('var' => 'LOLCAT'), $config->as_array());
  150. }
  151. /**
  152. * Casting the object to a string should serialize the output of as_array
  153. *
  154. * @test
  155. * @covers Kohana_Config_Group::__toString
  156. */
  157. public function test_to_string_serializes_array_output()
  158. {
  159. $vars = array('kohana' => 'cool', 'unit_tests' => 'boring');
  160. $config = $this->get_mock_group('hehehe', $vars);
  161. $this->assertSame(serialize($vars), (string) $config);
  162. }
  163. }