| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- <?php
- /**
- * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice.
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- * @link http://cakephp.org CakePHP(tm) Project
- * @since 3.0.0
- * @license http://www.opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Test\TestCase\Utility;
- use Cake\TestSuite\TestCase;
- use Cake\Utility\MergeVariablesTrait;
- class Base
- {
- use MergeVariablesTrait;
- public $hasBoolean = false;
- public $listProperty = ['One'];
- public $assocProperty = ['Red'];
- public function mergeVars($properties, $options = [])
- {
- return $this->_mergeVars($properties, $options);
- }
- }
- class Child extends Base
- {
- public $hasBoolean = ['test'];
- public $listProperty = ['Two', 'Three'];
- public $assocProperty = [
- 'Green' => ['lime'],
- 'Orange'
- ];
- public $nestedProperty = [
- 'Red' => [
- 'apple' => 'gala',
- ],
- 'Green' => [
- 'citrus' => 'lime'
- ],
- ];
- }
- class Grandchild extends Child
- {
- public $listProperty = ['Four', 'Five'];
- public $assocProperty = [
- 'Green' => ['apple'],
- 'Yellow' => ['banana']
- ];
- public $nestedProperty = [
- 'Red' => [
- 'citrus' => 'blood orange',
- ],
- 'Green' => [
- 'citrus' => 'key lime'
- ],
- ];
- }
- /**
- * MergeVariablesTrait test case
- *
- */
- class MergeVariablesTraitTest extends TestCase
- {
- /**
- * Test merging vars as a list.
- *
- * @return void
- */
- public function testMergeVarsAsList()
- {
- $object = new Grandchild();
- $object->mergeVars(['listProperty']);
- $expected = ['One', 'Two', 'Three', 'Four', 'Five'];
- $this->assertSame($expected, $object->listProperty);
- }
- /**
- * Test merging vars as an associative list.
- *
- * @return void
- */
- public function testMergeVarsAsAssoc()
- {
- $object = new Grandchild();
- $object->mergeVars(['assocProperty'], ['associative' => ['assocProperty']]);
- $expected = [
- 'Red' => null,
- 'Orange' => null,
- 'Green' => ['apple'],
- 'Yellow' => ['banana'],
- ];
- $this->assertEquals($expected, $object->assocProperty);
- }
- /**
- * Test merging variable in associated properties that contain
- * additional keys.
- *
- * @return void
- */
- public function testMergeVarsAsAssocWithKeyValues()
- {
- $object = new Grandchild();
- $object->mergeVars(['nestedProperty'], ['associative' => ['nestedProperty']]);
- $expected = [
- 'Red' => [
- 'citrus' => 'blood orange',
- ],
- 'Green' => [
- 'citrus' => 'key lime',
- ],
- ];
- $this->assertEquals($expected, $object->nestedProperty);
- }
- /**
- * Test merging vars with mixed modes.
- *
- * @return void
- */
- public function testMergeVarsMixedModes()
- {
- $object = new Grandchild();
- $object->mergeVars(['assocProperty', 'listProperty'], ['associative' => ['assocProperty']]);
- $expected = [
- 'Red' => null,
- 'Orange' => null,
- 'Green' => ['apple'],
- 'Yellow' => ['banana'],
- ];
- $this->assertEquals($expected, $object->assocProperty);
- $expected = ['One', 'Two', 'Three', 'Four', 'Five'];
- $this->assertSame($expected, $object->listProperty);
- }
- /**
- * Test that merging variables with booleans in the class hierarchy
- * doesn't cause issues.
- *
- * @return void
- */
- public function testMergeVarsWithBoolean()
- {
- $object = new Child();
- $object->mergeVars(['hasBoolean']);
- $this->assertEquals(['test'], $object->hasBoolean);
- }
- }
|