MergeVariablesTraitTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.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. */
  66. class MergeVariablesTraitTest extends TestCase
  67. {
  68. /**
  69. * Test merging vars as a list.
  70. *
  71. * @return void
  72. */
  73. public function testMergeVarsAsList()
  74. {
  75. $object = new Grandchild();
  76. $object->mergeVars(['listProperty']);
  77. $expected = ['One', 'Two', 'Three', 'Four', 'Five'];
  78. $this->assertSame($expected, $object->listProperty);
  79. }
  80. /**
  81. * Test merging vars as an associative list.
  82. *
  83. * @return void
  84. */
  85. public function testMergeVarsAsAssoc()
  86. {
  87. $object = new Grandchild();
  88. $object->mergeVars(['assocProperty'], ['associative' => ['assocProperty']]);
  89. $expected = [
  90. 'Red' => null,
  91. 'Orange' => null,
  92. 'Green' => ['apple'],
  93. 'Yellow' => ['banana'],
  94. ];
  95. $this->assertEquals($expected, $object->assocProperty);
  96. }
  97. /**
  98. * Test merging variable in associated properties that contain
  99. * additional keys.
  100. *
  101. * @return void
  102. */
  103. public function testMergeVarsAsAssocWithKeyValues()
  104. {
  105. $object = new Grandchild();
  106. $object->mergeVars(['nestedProperty'], ['associative' => ['nestedProperty']]);
  107. $expected = [
  108. 'Red' => [
  109. 'citrus' => 'blood orange',
  110. ],
  111. 'Green' => [
  112. 'citrus' => 'key lime',
  113. ],
  114. ];
  115. $this->assertEquals($expected, $object->nestedProperty);
  116. }
  117. /**
  118. * Test merging vars with mixed modes.
  119. *
  120. * @return void
  121. */
  122. public function testMergeVarsMixedModes()
  123. {
  124. $object = new Grandchild();
  125. $object->mergeVars(['assocProperty', 'listProperty'], ['associative' => ['assocProperty']]);
  126. $expected = [
  127. 'Red' => null,
  128. 'Orange' => null,
  129. 'Green' => ['apple'],
  130. 'Yellow' => ['banana'],
  131. ];
  132. $this->assertEquals($expected, $object->assocProperty);
  133. $expected = ['One', 'Two', 'Three', 'Four', 'Five'];
  134. $this->assertSame($expected, $object->listProperty);
  135. }
  136. /**
  137. * Test that merging variables with booleans in the class hierarchy
  138. * doesn't cause issues.
  139. *
  140. * @return void
  141. */
  142. public function testMergeVarsWithBoolean()
  143. {
  144. $object = new Child();
  145. $object->mergeVars(['hasBoolean']);
  146. $this->assertEquals(['test'], $object->hasBoolean);
  147. }
  148. }