MergeVariablesTraitTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Utility;
  16. use Cake\TestSuite\TestCase;
  17. use Cake\Utility\MergeVariablesTrait;
  18. class Base
  19. {
  20. use MergeVariablesTrait;
  21. public $hasBoolean = false;
  22. public $listProperty = ['One'];
  23. public $assocProperty = ['Red'];
  24. public function mergeVars($properties, $options = [])
  25. {
  26. return $this->_mergeVars($properties, $options);
  27. }
  28. }
  29. class Child extends Base
  30. {
  31. public $hasBoolean = ['test'];
  32. public $listProperty = ['Two', 'Three'];
  33. public $assocProperty = [
  34. 'Green' => ['lime'],
  35. 'Orange',
  36. ];
  37. public $nestedProperty = [
  38. 'Red' => [
  39. 'apple' => 'gala',
  40. ],
  41. 'Green' => [
  42. 'citrus' => 'lime',
  43. ],
  44. ];
  45. }
  46. class Grandchild extends Child
  47. {
  48. public $listProperty = ['Four', 'Five'];
  49. public $assocProperty = [
  50. 'Green' => ['apple'],
  51. 'Yellow' => ['banana'],
  52. ];
  53. public $nestedProperty = [
  54. 'Red' => [
  55. 'citrus' => 'blood orange',
  56. ],
  57. 'Green' => [
  58. 'citrus' => 'key lime',
  59. ],
  60. ];
  61. }
  62. /**
  63. * MergeVariablesTrait test case
  64. */
  65. class MergeVariablesTraitTest extends TestCase
  66. {
  67. /**
  68. * Test merging vars as a list.
  69. *
  70. * @return void
  71. */
  72. public function testMergeVarsAsList()
  73. {
  74. $object = new Grandchild();
  75. $object->mergeVars(['listProperty']);
  76. $expected = ['One', 'Two', 'Three', 'Four', 'Five'];
  77. $this->assertSame($expected, $object->listProperty);
  78. }
  79. /**
  80. * Test merging vars as an associative list.
  81. *
  82. * @return void
  83. */
  84. public function testMergeVarsAsAssoc()
  85. {
  86. $object = new Grandchild();
  87. $object->mergeVars(['assocProperty'], ['associative' => ['assocProperty']]);
  88. $expected = [
  89. 'Red' => null,
  90. 'Orange' => null,
  91. 'Green' => ['apple'],
  92. 'Yellow' => ['banana'],
  93. ];
  94. $this->assertEquals($expected, $object->assocProperty);
  95. }
  96. /**
  97. * Test merging variable in associated properties that contain
  98. * additional keys.
  99. *
  100. * @return void
  101. */
  102. public function testMergeVarsAsAssocWithKeyValues()
  103. {
  104. $object = new Grandchild();
  105. $object->mergeVars(['nestedProperty'], ['associative' => ['nestedProperty']]);
  106. $expected = [
  107. 'Red' => [
  108. 'citrus' => 'blood orange',
  109. ],
  110. 'Green' => [
  111. 'citrus' => 'key lime',
  112. ],
  113. ];
  114. $this->assertEquals($expected, $object->nestedProperty);
  115. }
  116. /**
  117. * Test merging vars with mixed modes.
  118. *
  119. * @return void
  120. */
  121. public function testMergeVarsMixedModes()
  122. {
  123. $object = new Grandchild();
  124. $object->mergeVars(['assocProperty', 'listProperty'], ['associative' => ['assocProperty']]);
  125. $expected = [
  126. 'Red' => null,
  127. 'Orange' => null,
  128. 'Green' => ['apple'],
  129. 'Yellow' => ['banana'],
  130. ];
  131. $this->assertEquals($expected, $object->assocProperty);
  132. $expected = ['One', 'Two', 'Three', 'Four', 'Five'];
  133. $this->assertSame($expected, $object->listProperty);
  134. }
  135. /**
  136. * Test that merging variables with booleans in the class hierarchy
  137. * doesn't cause issues.
  138. *
  139. * @return void
  140. */
  141. public function testMergeVarsWithBoolean()
  142. {
  143. $object = new Child();
  144. $object->mergeVars(['hasBoolean']);
  145. $this->assertEquals(['test'], $object->hasBoolean);
  146. }
  147. }