GroupTest.php 5.0 KB

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