예제 #1
0
 public function testUnshiftRetainAssocKeys()
 {
     $expected = ['bar', 'foo' => 5];
     $xao = new XArray(['foo' => 5]);
     $xao->unshift('bar');
     $this->assertEquals($expected, (array) $xao);
 }
 public function testChangeKeyCaseMultibyteKeys()
 {
     $xao = new XArray(['cœur' => 1, 'Søren' => 2]);
     $expected = ['CŒUR' => 1, 'SØREN' => 2];
     $obj = $xao->changeKeyCase(\CASE_UPPER);
     $this->assertEquals($expected, (array) $obj);
 }
 public function testReplaceWithSimilarKeys()
 {
     $xao = new XArray([1, 2, 3]);
     $obj = $xao->replace(['1' => 'foo']);
     // string numbers (int) are automatically converted to integers
     $this->assertEquals([1, 'foo', 3], (array) $obj);
 }
 /**
  * @expectedException RuntimeException
  */
 public function testFilterFailsForInvalidCallback()
 {
     $xao = new XArray([1, 2]);
     // ArrayObject::count() is non-static and accepts no parameters
     // though the syntax for the callable is correct the execution will fail
     $xao->filter(['ArrayObject', 'count']);
 }
 public function testContainsOnEmptyArray()
 {
     $xao = new XArray([]);
     $this->assertSame(false, $xao->contains('foo', true));
     $this->assertSame(false, $xao->contains('foo', false));
     $this->assertSame(false, $xao->contains('foo'));
 }
 public function testReversePreserveNumericKeys()
 {
     $expected = [2 => 3, 1 => 2, 0 => 1];
     $xao = new XArray([1, 2, 3]);
     $obj = $xao->reverse(true);
     $this->assertEquals($expected, (array) $obj);
 }
 public function testValuesSuccess()
 {
     $expected = [0 => 'bar', 1 => 'y'];
     $xao = new XArray(['foo' => 'bar', 'x' => 'y']);
     $obj = $xao->values();
     $this->assertCount(2, $obj);
     $this->assertEquals($expected, (array) $obj);
 }
 public function testGetPreviousObject()
 {
     $xao = new XArray([1, 2, 3]);
     $obj = $xao->map(function ($value) {
         return 2 * $value;
     });
     $this->assertSame($xao, $obj->back());
 }
예제 #9
0
 public function testRandKeysAndValuesExist()
 {
     $xao = new XArray(['foo', 'bar']);
     $obj = $xao->rand();
     $keys = array_keys((array) $obj);
     $this->assertArrayHasKey($keys[0], $obj);
     $values = array_values((array) $obj);
     $this->assertContains($values[0], $obj);
 }
 public function testShuffleNotChangesArrayElements()
 {
     $expected = [1, 2, 3, 4, 5];
     $xao = new XArray($expected);
     $array = (array) $xao->shuffle();
     // undo shuffle
     sort($array, \SORT_NUMERIC);
     $this->assertEquals($expected, $array);
 }
예제 #11
0
 public function testSlicePreservingKeys()
 {
     $xao = new XArray([3 => 5, 6 => 2, 0 => 6, 7 => 3]);
     // without
     $this->assertEquals([2, 6], (array) $xao->slice(1, 2));
     $this->assertEquals([2, 6], (array) $xao->slice(1, 2, XAInterface::IGNORE_KEYS));
     // with
     $this->assertEquals([6 => 2, 0 => 6], (array) $xao->slice(1, 2, XAInterface::PRESERVE_KEYS));
     // without length
     $this->assertEquals([6 => 2, 0 => 6, 7 => 3], (array) $xao->slice(1, NULL, XAInterface::PRESERVE_KEYS));
 }
 /**
  * Create ArrayObject instance according to the native constructor 
  * optionally adding the object from the previous action.
  * 
  * @param mixed $input An array or object (with externally iterable properties)
  * @param integer $flags Flags to control the behaviour of the ArrayObject object.
  * @param string $iterator_class Specify the class that will be used for iteration of the ArrayObject object.
  * @param ArrayObject $previous ArrayObject of the operation that created this object.
  * @return self
  * @throws InvalidArgumentException $input is not an array or object
  * @throws InvalidArgumentException $flags is not an integer
  * @throws InvalidArgumentException $iterator_class is not an object that implements Iterator
  */
 public function __construct($input = [], $flags = 0, $iterator_class = 'ArrayIterator', \ArrayObject $previous = null)
 {
     parent::__construct($input, $flags, $iterator_class);
     if ($previous) {
         $this->previous = $previous;
     }
 }
예제 #13
0
 /**
  * @expectedException RuntimeException
  */
 public function testJoinWithNonscalarArray()
 {
     $xao = new XArray([1, ['foo'], 7]);
     $xao->join('-');
 }
예제 #14
0
 /**
  * @depends testWalkAcceptsClosure
  */
 public function testWalkModifiesValuesWithUserdata()
 {
     $xao = new XArray([1, 2, 3]);
     $expected = [2, 4, 6];
     $xao->walk(function (&$value, $key, $number) {
         $value = $value * $number;
     }, 2);
     $this->assertEquals($expected, (array) $xao);
 }
예제 #15
0
 /**
  * @expectedException RuntimeException
  */
 public function testFlipWithInvalidArray()
 {
     $array = [true, null, ['foo' => 'bar']];
     $xao = new XArray($array);
     $obj = $xao->flip();
 }
예제 #16
0
 /**
  * @depends testMapAcceptsFunction
  */
 public function testMapPreserveKeys()
 {
     $array = ['x' => 28, 'y' => 68, 'z' => 4];
     $xao = new XArray($array);
     $this->assertEquals($array, (array) $xao->map('test_map', true));
 }
 public function testXAIintersectWithKeyValueCallback()
 {
     $xao = new XArray(['x' => 1, 'ab' => 2, 'z' => 3]);
     $obj = $xao->xaintersect(['xy' => 5, 'z' => 11], 'length_compare_func', 'length_compare_func');
     $this->assertEquals(['xy' => 5], (array) $obj);
 }
예제 #18
0
 public function testSearchStringLookupIsCaseSensitive()
 {
     $xao = new XArray(['foo', 'bar']);
     $this->assertFalse($xao->search('Foo'));
 }
 /**
  * @expectedException RuntimeException
  */
 public function testCountValuesWithNonScalarValues()
 {
     $xao = new XArray([1, ['foo']]);
     $obj = $xao->countValues();
 }
예제 #20
0
 public function testXADiffWithKeyValueCallback()
 {
     $xao = new XArray(['ab' => 2, 'z' => 1]);
     $obj = $xao->xadiff(['x' => 1, 'ab' => 22, 'z' => 3], 'length_compare_func', 'length_compare_func');
     $this->assertEquals(['ab' => 22], (array) $obj);
 }
예제 #21
0
 public function testSpliceModifiesObject()
 {
     $xao = new XArray([1, 2, 3]);
     $obj = $xao->splice(2);
     $this->assertEquals([1, 2], (array) $xao);
 }
예제 #22
0
 /**
  * @expectedException RuntimeException
  */
 public function testUniqueFailsOnConversionProblems()
 {
     $xao = new XArray(['foo', ['bar'], 'bar']);
     $obj = $xao->unique();
 }
 public function testMergeAssocArrayWithArrayObject()
 {
     $expected = ['x' => 1, 'y' => 2, 'z' => 2, 'a' => 1];
     $xao1 = new XArray(['x' => 1, 'y' => 2, 'z' => 3]);
     $xao2 = new XArray(['a' => 1, 'z' => 2]);
     $obj = $xao1->merge($xao2);
     $this->assertEquals($expected, (array) $obj);
 }
예제 #24
0
 /**
  * @depends testReduceSkipsInitialValueIfNoneGiven
  */
 public function testReduceCanUseArrayKeysInCallback()
 {
     $xao = new XArray(['a' => 1, 'b' => 2, 'c' => 3]);
     // this would return 0 if the default initial value of NULL gets paased
     $string = $xao->reduce(function ($carry, $value, $key) {
         return $carry .= $key . ':' . $value . '|';
     }, '|');
     $this->assertSame('|a:1|b:2|c:3|', $string);
 }
 public function testPushReturnsSameInstance()
 {
     $xao = new XArray(['foo']);
     $obj = $xao->push('bar');
     $this->assertSame($xao, $obj);
 }
 public function testCreateObjectFromnStaticMethod()
 {
     $xao = XArray::from([1, 2, 3]);
     $this->assertInstanceOf('\\Dormilich\\Core\\ArrayInterface', $xao);
     $test = new XArray([1, 2, 3]);
     $this->assertEquals($test, $xao);
 }