GroupTest.php 4.9 KB

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