"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "tests/framework/helpers/ArrayHelperTest.php" between
yii2-2.0.35.tar.gz and yii2-2.0.36.tar.gz

About: Yii 2 is a high-performance component-based PHP framework for developing large-scale Web applications (source).

ArrayHelperTest.php  (yii2-2.0.35):ArrayHelperTest.php  (yii2-2.0.36)
<?php <?php
/** /**
* @link http://www.yiiframework.com/ * @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC * @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/ * @license http://www.yiiframework.com/license/
*/ */
namespace yiiunit\framework\helpers; namespace yiiunit\framework\helpers;
use ArrayAccess;
use Iterator;
use yii\base\BaseObject; use yii\base\BaseObject;
use yii\data\Sort; use yii\data\Sort;
use yii\helpers\ArrayHelper; use yii\helpers\ArrayHelper;
use yiiunit\TestCase; use yiiunit\TestCase;
class Post1 class Post1
{ {
public $id = 23; public $id = 23;
public $title = 'tt'; public $title = 'tt';
} }
skipping to change at line 44 skipping to change at line 46
public $id = 33; public $id = 33;
/** @var BaseObject */ /** @var BaseObject */
public $subObject; public $subObject;
public function init() public function init()
{ {
$this->subObject = new Post2(); $this->subObject = new Post2();
} }
} }
class ArrayAccessibleObject implements ArrayAccess
{
public $name = 'bar1';
protected $container = [];
public function __construct($container)
{
$this->container = $container;
}
public function offsetSet($offset, $value)
{
if (is_null($offset)) {
$this->container[] = $value;
} else {
$this->container[$offset] = $value;
}
}
public function offsetExists($offset)
{
return array_key_exists($offset, $this->container);
}
public function offsetUnset($offset)
{
unset($this->container[$offset]);
}
public function offsetGet($offset)
{
return $this->offsetExists($offset) ? $this->container[$offset] : null;
}
}
class TraversableArrayAccessibleObject extends ArrayAccessibleObject implements
Iterator
{
private $position = 0;
public function __construct($container)
{
$this->position = 0;
parent::__construct($container);
}
protected function getContainerKey($keyIndex)
{
$keys = array_keys($this->container);
return array_key_exists($keyIndex, $keys) ? $keys[$keyIndex] : false;
}
public function rewind()
{
$this->position = 0;
}
public function current()
{
return $this->offsetGet($this->getContainerKey($this->position));
}
public function key()
{
return $this->getContainerKey($this->position);
}
public function next()
{
++$this->position;
}
public function valid()
{
$key = $this->getContainerKey($this->position);
return !(!$key || !$this->offsetExists($key));
}
}
/** /**
* @group helpers * @group helpers
*/ */
class ArrayHelperTest extends TestCase class ArrayHelperTest extends TestCase
{ {
protected function setUp() protected function setUp()
{ {
parent::setUp(); parent::setUp();
// destroy application, Helper must work without Yii::$app // destroy application, Helper must work without Yii::$app
skipping to change at line 416 skipping to change at line 497
'mvc', 'mvc',
'gii', 'gii',
'debug', 'debug',
], ],
'foo', 'foo',
]; ];
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
public function testMergeWithNumericKeys()
{
$a = [10 => [1]];
$b = [10 => [2]];
$result = ArrayHelper::merge($a, $b);
$expected = [10 => [1], 11 => [2]];
$this->assertEquals($expected, $result);
}
public function testMergeWithUnset() public function testMergeWithUnset()
{ {
$a = [ $a = [
'name' => 'Yii', 'name' => 'Yii',
'version' => '1.0', 'version' => '1.0',
'options' => [ 'options' => [
'namespace' => false, 'namespace' => false,
'unittest' => false, 'unittest' => false,
], ],
'features' => [ 'features' => [
skipping to change at line 737 skipping to change at line 829
$this->assertFalse(ArrayHelper::keyExists('b', $array)); $this->assertFalse(ArrayHelper::keyExists('b', $array));
$this->assertTrue(ArrayHelper::keyExists('B', $array)); $this->assertTrue(ArrayHelper::keyExists('B', $array));
$this->assertFalse(ArrayHelper::keyExists('c', $array)); $this->assertFalse(ArrayHelper::keyExists('c', $array));
$this->assertTrue(ArrayHelper::keyExists('a', $array, false)); $this->assertTrue(ArrayHelper::keyExists('a', $array, false));
$this->assertTrue(ArrayHelper::keyExists('b', $array, false)); $this->assertTrue(ArrayHelper::keyExists('b', $array, false));
$this->assertTrue(ArrayHelper::keyExists('B', $array, false)); $this->assertTrue(ArrayHelper::keyExists('B', $array, false));
$this->assertFalse(ArrayHelper::keyExists('c', $array, false)); $this->assertFalse(ArrayHelper::keyExists('c', $array, false));
} }
public function testKeyExistsArrayAccess()
{
$array = new TraversableArrayAccessibleObject([
'a' => 1,
'B' => 2,
]);
$this->assertTrue(ArrayHelper::keyExists('a', $array));
$this->assertFalse(ArrayHelper::keyExists('b', $array));
$this->assertTrue(ArrayHelper::keyExists('B', $array));
$this->assertFalse(ArrayHelper::keyExists('c', $array));
}
/**
* @expectedException \yii\base\InvalidArgumentException
* @expectedExceptionMessage Second parameter($array) cannot be ArrayAccess
in case insensitive mode
*/
public function testKeyExistsArrayAccessCaseInsensitiveThrowsError()
{
$array = new TraversableArrayAccessibleObject([
'a' => 1,
'B' => 2,
]);
ArrayHelper::keyExists('a', $array, false);
}
public function valueProvider() public function valueProvider()
{ {
return [ return [
['name', 'test'], ['name', 'test'],
['noname', null], ['noname', null],
['noname', 'test', 'test'], ['noname', 'test', 'test'],
['post.id', 5], ['post.id', 5],
['post.id', 5, 'test'], ['post.id', 5, 'test'],
['nopost.id', null], ['nopost.id', null],
['nopost.id', 'test', 'test'], ['nopost.id', 'test', 'test'],
skipping to change at line 833 skipping to change at line 952
* This is expected to result in a PHP error. * This is expected to result in a PHP error.
* @requires PHPUnit 6.0 * @requires PHPUnit 6.0
*/ */
public function testGetValueNonexistingProperties2() public function testGetValueNonexistingProperties2()
{ {
$this->expectException('PHPUnit\Framework\Error\Notice'); $this->expectException('PHPUnit\Framework\Error\Notice');
$arrayObject = new \ArrayObject(['id' => 23], \ArrayObject::ARRAY_AS_PRO PS); $arrayObject = new \ArrayObject(['id' => 23], \ArrayObject::ARRAY_AS_PRO PS);
$this->assertEquals(23, ArrayHelper::getValue($arrayObject, 'nonExisting ')); $this->assertEquals(23, ArrayHelper::getValue($arrayObject, 'nonExisting '));
} }
public function testGetValueFromArrayAccess()
{
$arrayAccessibleObject = new ArrayAccessibleObject([
'one' => 1,
'two' => 2,
'three' => 3,
'key.with.dot' => 'dot',
'null' => null,
]);
$this->assertEquals(1, ArrayHelper::getValue($arrayAccessibleObject, 'on
e'));
}
public function testGetValueWithDotsFromArrayAccess()
{
$arrayAccessibleObject = new ArrayAccessibleObject([
'one' => 1,
'two' => 2,
'three' => 3,
'key.with.dot' => 'dot',
'null' => null,
]);
$this->assertEquals('dot', ArrayHelper::getValue($arrayAccessibleObject,
'key.with.dot'));
}
public function testGetValueNonexistingArrayAccess()
{
$arrayAccessibleObject = new ArrayAccessibleObject([
'one' => 1,
'two' => 2,
'three' => 3,
'key.with.dot' => 'dot',
'null' => null,
]);
$this->assertEquals(null, ArrayHelper::getValue($arrayAccessibleObject,
'four'));
}
/** /**
* Data provider for [[testSetValue()]]. * Data provider for [[testSetValue()]].
* @return array test data * @return array test data
*/ */
public function dataProviderSetValue() public function dataProviderSetValue()
{ {
return [ return [
[ [
[ [
'key1' => 'val1', 'key1' => 'val1',
skipping to change at line 1349 skipping to change at line 1507
$tmp = [ $tmp = [
'a' => 0, 'a' => 0,
'b' => '', 'b' => '',
'c' => false, 'c' => false,
'd' => null, 'd' => null,
'e' => true, 'e' => true,
]; ];
$this->assertEquals(ArrayHelper::filter($tmp, array_keys($tmp)), $tmp); $this->assertEquals(ArrayHelper::filter($tmp, array_keys($tmp)), $tmp);
} }
/**
* @see https://github.com/yiisoft/yii2/issues/18086
*/
public function testArrayAccessWithPublicProperty()
{
$data = new ArrayAccessibleObject(['value' => 123]);
$this->assertEquals(123, ArrayHelper::getValue($data, 'value'));
$this->assertEquals('bar1', ArrayHelper::getValue($data, 'name'));
}
} }
 End of changes. 6 change blocks. 
0 lines changed or deleted 174 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)