Ejemplo n.º 1
0
 public function tests()
 {
     PHPUsableTest::$current_test = $this;
     describe('Arrays', function ($test) {
         describe('Merge', function ($test) {
             it('should not convert scalars to arrays', function ($test) {
                 $a = array('level 1' => array('level 2' => 2));
                 $b = array('level 1' => array('level 2' => 5));
                 $test->expect(array_replace_recursive($a, $b))->to->eql(array('level 1' => array('level 2' => 5)));
             });
             it('should unionize arrays', function ($test) {
                 $a = array('level 1' => array('level 2' => array(2, 3)));
                 $b = array('level 1' => array('level 2' => array(5)));
                 $test->expect(array_replace_recursive($a, $b))->to->eql(array('level 1' => array('level 2' => array(5, 3))));
             });
         });
         describe('Get', function ($test) {
             it('should get a single level value', function ($test) {
                 $arr = array('planets' => 'pluto');
                 $test->expect(Arrays::get($arr, 'planets'))->to->eql('pluto');
             });
             it('should get a multi level value', function ($test) {
                 $arr = array('milky_way' => array('planets' => array('earth')));
                 $test->expect(Arrays::get($arr, 'milky_way.planets'))->to->eql(array('earth'));
             });
             it('should return null for an undefined key', function ($test) {
                 $arr = array('milky_way' => array('planets' => array('earth')));
                 $test->expect(Arrays::get($arr, 'andromeda.planets'))->to->eql(null);
             });
         });
     });
 }
Ejemplo n.º 2
0
 public static function run($fn, $options = array())
 {
     $memory_before = memory_get_usage();
     $time_before = microtime(true);
     $elapsed = function () use($time_before) {
         return microtime(true) - $time_before;
     };
     $marks = array();
     $markFunc = function ($label = "mark") use(&$marks, $elapsed) {
         $memory = Profile::humanizeMemory();
         $formatted_time_delta = $elapsed();
         $marks[] = "{$label}: {$formatted_time_delta} - {$memory}";
     };
     if (Arrays::get($options, 'debug', false)) {
         Trace::traceOffset(1)->debug('Staring profile.');
     }
     // The callback provided to the profile closure will trigger
     // a measurement.
     $fn($markFunc);
     $memory_after = memory_get_usage();
     $memory_peak = memory_get_peak_usage(true);
     $data = array('elapsed' => $elapsed(), 'memory_before' => $memory_before, 'memory_after' => $memory_after, 'memory_peak' => $memory_peak, 'marks' => $marks);
     if (Arrays::get($options, 'debug', false)) {
         Trace::traceDepth(2)->traceOffset(1)->debug($data);
     }
     return $data;
 }