MergeVariablesTraitTest.php 3.1 KB

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