mirror of
				https://github.com/yiisoft/yii2.git
				synced 2025-10-31 02:28:35 +08:00 
			
		
		
		
	Fix #19914: Fixed ArrayHelper::keyExists() and  ::remove() functions when the key is a float and the value is null
				
					
				
			This commit is contained in:
		| @ -135,6 +135,29 @@ class ArrayHelperTest extends TestCase | ||||
|         $this->assertEquals('defaultValue', $default); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * @return void | ||||
|      */ | ||||
|     public function testRemoveWithFloat() | ||||
|     { | ||||
|         if (version_compare(PHP_VERSION, '8.1.0', '>=')) { | ||||
|             $this->markTestSkipped('Using floats as array key is deprecated.'); | ||||
|         } | ||||
|  | ||||
|         $array = ['name' => 'b', 'age' => 3, 1.1 => null]; | ||||
|  | ||||
|         $name = ArrayHelper::remove($array, 'name'); | ||||
|         $this->assertEquals($name, 'b'); | ||||
|         $this->assertEquals($array, ['age' => 3, 1.1 => null]); | ||||
|  | ||||
|         $floatVal = ArrayHelper::remove($array, 1.1); | ||||
|         $this->assertNull($floatVal); | ||||
|         $this->assertEquals($array, ['age' => 3]); | ||||
|  | ||||
|         $default = ArrayHelper::remove($array, 'nonExisting', 'defaultValue'); | ||||
|         $this->assertEquals('defaultValue', $default); | ||||
|     } | ||||
|  | ||||
|     public function testRemoveValueMultiple() | ||||
|     { | ||||
|         $array = [ | ||||
| @ -506,14 +529,21 @@ class ArrayHelperTest extends TestCase | ||||
|     /** | ||||
|      * @see https://github.com/yiisoft/yii2/pull/11549 | ||||
|      */ | ||||
|     public function test() | ||||
|     public function testGetValueWithFloatKeys() | ||||
|     { | ||||
|         if (version_compare(PHP_VERSION, '8.1.0', '>=')) { | ||||
|             $this->markTestSkipped('Using floats as array key is deprecated.'); | ||||
|         } | ||||
|  | ||||
|         $array = []; | ||||
|         $array[1.0] = 'some value'; | ||||
|  | ||||
|         $result = ArrayHelper::getValue($array, 1.0); | ||||
|         $array[1.1] = 'some value'; | ||||
|         $array[2.1] = null; | ||||
|  | ||||
|         $result = ArrayHelper::getValue($array, 1.2); | ||||
|         $this->assertEquals('some value', $result); | ||||
|  | ||||
|         $result = ArrayHelper::getValue($array, 2.2); | ||||
|         $this->assertNull($result); | ||||
|     } | ||||
|  | ||||
|     public function testIndex() | ||||
| @ -712,6 +742,7 @@ class ArrayHelperTest extends TestCase | ||||
|             'a' => 1, | ||||
|             'B' => 2, | ||||
|         ]; | ||||
|  | ||||
|         $this->assertTrue(ArrayHelper::keyExists('a', $array)); | ||||
|         $this->assertFalse(ArrayHelper::keyExists('b', $array)); | ||||
|         $this->assertTrue(ArrayHelper::keyExists('B', $array)); | ||||
| @ -723,6 +754,27 @@ class ArrayHelperTest extends TestCase | ||||
|         $this->assertFalse(ArrayHelper::keyExists('c', $array, false)); | ||||
|     } | ||||
|  | ||||
|     public function testKeyExistsWithFloat() | ||||
|     { | ||||
|         if (version_compare(PHP_VERSION, '8.1.0', '>=')) { | ||||
|             $this->markTestSkipped('Using floats as array key is deprecated.'); | ||||
|         } | ||||
|  | ||||
|         $array = [ | ||||
|             1 => 3, | ||||
|             2.2 => 4, // Note: Floats are cast to ints, which means that the fractional part will be truncated. | ||||
|             3.3 => null, | ||||
|         ]; | ||||
|  | ||||
|         $this->assertTrue(ArrayHelper::keyExists(1, $array)); | ||||
|         $this->assertTrue(ArrayHelper::keyExists(1.1, $array)); | ||||
|         $this->assertTrue(ArrayHelper::keyExists(2, $array)); | ||||
|         $this->assertTrue(ArrayHelper::keyExists('2', $array)); | ||||
|         $this->assertTrue(ArrayHelper::keyExists(2.2, $array)); | ||||
|         $this->assertTrue(ArrayHelper::keyExists(3, $array)); | ||||
|         $this->assertTrue(ArrayHelper::keyExists(3.3, $array)); | ||||
|     } | ||||
|  | ||||
|     public function testKeyExistsArrayAccess() | ||||
|     { | ||||
|         $array = new TraversableArrayAccessibleObject([ | ||||
|  | ||||
		Reference in New Issue
	
	Block a user
	 rhertogh
					rhertogh