MergeVariablesTraitTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. namespace Cake\Test\TestCase\Utility;
  16. use Cake\TestSuite\TestCase;
  17. use Cake\Utility\MergeVariablesTrait;
  18. class Base {
  19. use MergeVariablesTrait;
  20. public $hasBoolean = false;
  21. public $listProperty = ['One'];
  22. public $assocProperty = ['Red'];
  23. public function mergeVars($properties, $options = []) {
  24. return $this->_mergeVars($properties, $options);
  25. }
  26. }
  27. class Child extends Base {
  28. public $hasBoolean = ['test'];
  29. public $listProperty = ['Two', 'Three'];
  30. public $assocProperty = [
  31. 'Green' => ['lime'],
  32. 'Orange'
  33. ];
  34. public $nestedProperty = [
  35. 'Red' => [
  36. 'apple' => 'gala',
  37. ],
  38. 'Green' => [
  39. 'citrus' => 'lime'
  40. ],
  41. ];
  42. }
  43. class Grandchild extends Child {
  44. public $listProperty = ['Four', 'Five'];
  45. public $assocProperty = [
  46. 'Green' => ['apple'],
  47. 'Yellow' => ['banana']
  48. ];
  49. public $nestedProperty = [
  50. 'Red' => [
  51. 'citrus' => 'blood orange',
  52. ],
  53. 'Green' => [
  54. 'citrus' => 'key lime'
  55. ],
  56. ];
  57. }
  58. /**
  59. * MergeVariablesTrait test case
  60. *
  61. */
  62. class MergeVariablesTraitTest extends TestCase {
  63. /**
  64. * Test merging vars as a list.
  65. *
  66. * @return void
  67. */
  68. public function testMergeVarsAsList() {
  69. $object = new Grandchild();
  70. $object->mergeVars(['listProperty']);
  71. $expected = ['One', 'Two', 'Three', 'Four', 'Five'];
  72. $this->assertSame($expected, $object->listProperty);
  73. }
  74. /**
  75. * Test merging vars as an associative list.
  76. *
  77. * @return void
  78. */
  79. public function testMergeVarsAsAssoc() {
  80. $object = new Grandchild();
  81. $object->mergeVars(['assocProperty'], ['associative' => ['assocProperty']]);
  82. $expected = [
  83. 'Red' => null,
  84. 'Orange' => null,
  85. 'Green' => ['apple'],
  86. 'Yellow' => ['banana'],
  87. ];
  88. $this->assertEquals($expected, $object->assocProperty);
  89. }
  90. /**
  91. * Test merging variable in associated properties that contain
  92. * additional keys.
  93. *
  94. * @return void
  95. */
  96. public function testMergeVarsAsAssocWithKeyValues() {
  97. $object = new Grandchild();
  98. $object->mergeVars(['nestedProperty'], ['associative' => ['nestedProperty']]);
  99. $expected = [
  100. 'Red' => [
  101. 'citrus' => 'blood orange',
  102. ],
  103. 'Green' => [
  104. 'citrus' => 'key lime',
  105. ],
  106. ];
  107. $this->assertEquals($expected, $object->nestedProperty);
  108. }
  109. /**
  110. * Test merging vars with mixed modes.
  111. *
  112. * @return void
  113. */
  114. public function testMergeVarsMixedModes() {
  115. $object = new Grandchild();
  116. $object->mergeVars(['assocProperty', 'listProperty'], ['associative' => ['assocProperty']]);
  117. $expected = [
  118. 'Red' => null,
  119. 'Orange' => null,
  120. 'Green' => ['apple'],
  121. 'Yellow' => ['banana'],
  122. ];
  123. $this->assertEquals($expected, $object->assocProperty);
  124. $expected = ['One', 'Two', 'Three', 'Four', 'Five'];
  125. $this->assertSame($expected, $object->listProperty);
  126. }
  127. /**
  128. * Test that merging variables with booleans in the class hierarchy
  129. * doesn't cause issues.
  130. *
  131. * @return void
  132. */
  133. public function testMergeVarsWithBoolean() {
  134. $object = new Child();
  135. $object->mergeVars(['hasBoolean']);
  136. $this->assertEquals(['test'], $object->hasBoolean);
  137. }
  138. }