Example #1
0
 public function test3_Appending()
 {
     $collection = new Collection(new Yaml());
     $collection_of_values = new Collection(new Yaml());
     # appending
     static::$collection->append('part1.item2', ['name' => 'Janice']);
     $this->assertEquals('Janice', static::$collection->get('part1.item2.name'));
     static::$collection->forget('part1');
     static::$collection->append('part1', ['item2' => ['name' => 'Janice']]);
     static::$collection->with('with', 'with test');
     $this->assertEquals('with test', static::$collection->get('with'));
     $this->assertTrue(static::$collection->count() === 2);
     static::$collection->forget('with');
     $this->assertTrue(static::$collection->count() === 1);
     # setup
     $test_array = ['merge_with' => ['merge' => 'this']];
     $collection_of_values->with($test_array);
     # test with an array
     $collection->merge($test_array);
     $this->assertEquals($collection->get('merge_with.merge'), 'this');
     $collection->forget('merge_with');
     # test with a collection as a value object
     $collection->merge($collection_of_values);
     $this->assertEquals($collection->get('merge_with.merge'), 'this');
     $collection->forget('merge_with');
     # test with value object
     $value_object = new \stdClass();
     $value_object->merge_with = $test_array['merge_with'];
     $collection->merge($value_object);
     $this->assertEquals($collection->get('merge_with.merge'), 'this');
     # test with flat array
     $collection->merge(Util::array_from_str('merge with'));
     $this->assertEquals($collection->get('merge'), NULL);
     $this->assertEquals($collection->get('with'), NULL);
     $this->setExpectedException('InvalidArgumentException', "Cannot append an already existing key: 'part1'");
     static::$collection->append('part1', ['item2' => ['name' => 'Mary']]);
 }