MergeVariablesTraitTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. public function testMergeVarsAsList(): void
  29. {
  30. $object = new Grandchild();
  31. $object->mergeVars(['listProperty']);
  32. $expected = ['One', 'Two', 'Three', 'Four', 'Five'];
  33. $this->assertSame($expected, $object->listProperty);
  34. }
  35. /**
  36. * Test merging vars as an associative list.
  37. */
  38. public function testMergeVarsAsAssoc(): void
  39. {
  40. $object = new Grandchild();
  41. $object->mergeVars(['assocProperty'], ['associative' => ['assocProperty']]);
  42. $expected = [
  43. 'Red' => null,
  44. 'Orange' => null,
  45. 'Green' => ['apple'],
  46. 'Yellow' => ['banana'],
  47. ];
  48. $this->assertEquals($expected, $object->assocProperty);
  49. }
  50. /**
  51. * Test merging variable in associated properties that contain
  52. * additional keys.
  53. */
  54. public function testMergeVarsAsAssocWithKeyValues(): void
  55. {
  56. $object = new Grandchild();
  57. $object->mergeVars(['nestedProperty'], ['associative' => ['nestedProperty']]);
  58. $expected = [
  59. 'Red' => [
  60. 'citrus' => 'blood orange',
  61. ],
  62. 'Green' => [
  63. 'citrus' => 'key lime',
  64. ],
  65. ];
  66. $this->assertSame($expected, $object->nestedProperty);
  67. }
  68. /**
  69. * Test merging vars with mixed modes.
  70. */
  71. public function testMergeVarsMixedModes(): void
  72. {
  73. $object = new Grandchild();
  74. $object->mergeVars(['assocProperty', 'listProperty'], ['associative' => ['assocProperty']]);
  75. $expected = [
  76. 'Red' => null,
  77. 'Orange' => null,
  78. 'Green' => ['apple'],
  79. 'Yellow' => ['banana'],
  80. ];
  81. $this->assertEquals($expected, $object->assocProperty);
  82. $expected = ['One', 'Two', 'Three', 'Four', 'Five'];
  83. $this->assertEquals($expected, $object->listProperty);
  84. }
  85. /**
  86. * Test that merging variables with booleans in the class hierarchy
  87. * doesn't cause issues.
  88. */
  89. public function testMergeVarsWithBoolean(): void
  90. {
  91. $object = new Child();
  92. $object->mergeVars(['hasBoolean']);
  93. $this->assertSame(['test'], $object->hasBoolean);
  94. }
  95. }