MergeVariablesTraitTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 CakePHP(tm) v 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. }
  35. class Grandchild extends Child {
  36. public $listProperty = ['Four', 'Five'];
  37. public $assocProperty = [
  38. 'Green' => ['apple'],
  39. 'Yellow' => ['banana']
  40. ];
  41. }
  42. /**
  43. * MergeVariablesTrait test case
  44. *
  45. */
  46. class MergeVariablesTraitTest extends TestCase {
  47. /**
  48. * Test merging vars as a list.
  49. *
  50. * @return void
  51. */
  52. public function testMergeVarsAsList() {
  53. $object = new Grandchild();
  54. $object->mergeVars(['listProperty']);
  55. $expected = ['One', 'Two', 'Three', 'Four', 'Five'];
  56. $this->assertSame($expected, $object->listProperty);
  57. }
  58. /**
  59. * Test merging vars as an assoc list.
  60. *
  61. * @return void
  62. */
  63. public function testMergeVarsAsAssoc() {
  64. $object = new Grandchild();
  65. $object->mergeVars(['assocProperty'], ['associative' => ['assocProperty']]);
  66. $expected = [
  67. 'Red' => null,
  68. 'Orange' => null,
  69. 'Green' => ['lime', 'apple'],
  70. 'Yellow' => ['banana'],
  71. ];
  72. $this->assertEquals($expected, $object->assocProperty);
  73. }
  74. /**
  75. * Test merging vars with mixed modes.
  76. */
  77. public function testMergeVarsMixedModes() {
  78. $object = new Grandchild();
  79. $object->mergeVars(['assocProperty', 'listProperty'], ['associative' => ['assocProperty']]);
  80. $expected = [
  81. 'Red' => null,
  82. 'Orange' => null,
  83. 'Green' => ['lime', 'apple'],
  84. 'Yellow' => ['banana'],
  85. ];
  86. $this->assertEquals($expected, $object->assocProperty);
  87. $expected = ['One', 'Two', 'Three', 'Four', 'Five'];
  88. $this->assertSame($expected, $object->listProperty);
  89. }
  90. /**
  91. * Test that merging variables with booleans in the class hierarchy
  92. * doesn't cause issues.
  93. *
  94. * @return void
  95. */
  96. public function testMergeVarsWithBoolean() {
  97. $object = new Child();
  98. $object->mergeVars(['hasBoolean']);
  99. $this->assertEquals(['test'], $object->hasBoolean);
  100. }
  101. }