Esempio n. 1
0
 function testSet()
 {
     $scope = __FUNCTION__;
     Redis::config(array('format' => $scope));
     // simplest call
     $expected = array('bar' => 'baz');
     $this->assertEqual($expected, Stats::set('foo', array('bar' => 'baz')));
     $this->assertEqual($expected, $this->redis->hGetAll("{$scope}:stats:global:foo"));
     // simple call, adding second field
     $expected = array('bar' => 'baz', 'baz' => 'bak');
     $this->assertEqual($expected, Stats::set('foo', array('baz' => 'bak')));
     $this->assertEqual($expected, $this->redis->hGetAll("{$scope}:stats:global:foo"));
     // simple call, overwrite second value
     $expected = array('bar' => 'baz', 'baz' => 'bam');
     $this->assertEqual($expected, Stats::set('foo', array('baz' => 'bam')));
     $this->assertEqual($expected, $this->redis->hGetAll("{$scope}:stats:global:foo"));
     // multiple at once
     $expected = array('field1' => 1, 'field2' => 1);
     $this->assertEqual($expected, Stats::set('multi', array('field1' => 1, 'field2' => 1)));
     $this->assertEqual($expected, $this->redis->hGetAll("{$scope}:stats:global:multi"));
     // overwriting multiple at once
     $expected = array('field1' => 1, 'field2' => 2);
     $this->assertEqual($expected, Stats::set('multi', array('field1' => 1, 'field2' => 2)));
     $this->assertEqual($expected, $this->redis->hGetAll("{$scope}:stats:global:multi"));
     // multiple at once, with plain bucket
     $expected = array('field1' => 1, 'field2' => 1);
     $this->assertEqual($expected, Stats::set('withBucket', $expected, 'prefix'));
     $this->assertEqual(array(), $this->redis->hGetAll("{$scope}:stats:global:withBucket"));
     $this->assertEqual($expected, $this->redis->hGetAll("{$scope}:stats:prefix:withBucket"));
     // multiple at once, with plain bucket, again (no inc?)
     $expected = array('field1' => 1, 'field2' => 1);
     $this->assertEqual($expected, Stats::set('withBucket', $expected, 'prefix'));
     $this->assertEqual(array(), $this->redis->hGetAll("{$scope}:stats:global:withBucket"));
     $this->assertEqual($expected, $this->redis->hGetAll("{$scope}:stats:prefix:withBucket"));
     // multiple buckets at once
     $data = array('field1' => 1, 'field2' => 1);
     $expected = array('prefix1' => $data, 'prefix2' => $data);
     $this->assertEqual($expected, Stats::set('multiBucket', $data, array('prefix1', 'prefix2')));
     $this->assertEqual(array(), $this->redis->hGetAll("{$scope}:stats:global:multiBucket"));
     $this->assertEqual($data, $this->redis->hGetAll("{$scope}:stats:prefix1:multiBucket"));
     $this->assertEqual($data, $this->redis->hGetAll("{$scope}:stats:prefix2:multiBucket"));
     // multiple at once, with one bucket in array
     $expected = array('field1' => 2, 'field2' => 2);
     $this->assertEqual($expected, Stats::set('withBucketArray', $expected, array('user' => 'foo')));
     $this->assertEqual(array(), $this->redis->hGetAll("{$scope}:stats:global:withBucketArray"));
     $this->assertEqual($expected, $this->redis->hGetAll("{$scope}:stats:user:foo:withBucketArray"));
     // multiple buckets as associated array
     $data = array('field1' => 5, 'field2' => 'bla');
     $expected = array('user:foo' => $data, 'year:2013' => $data);
     $this->assertEqual($expected, Stats::set('multiBucketArray', $data, array('user' => 'foo', 'year' => '2013')));
     $this->assertEqual(array(), $this->redis->hGetAll("{$scope}:stats:global:multiBucketArray"));
     $this->assertEqual($data, $this->redis->hGetAll("{$scope}:stats:user:foo:multiBucketArray"));
     $this->assertEqual($data, $this->redis->hGetAll("{$scope}:stats:year:2013:multiBucketArray"));
     // multiple buckets as associated array with global
     $data = array('field1' => 'foo', 'field2' => 'foo');
     $expected = array('global' => $data, 'prefix1' => $data, 'prefix2' => $data);
     $this->assertEqual($expected, Stats::set('multiBucket', $data, array('global', 'prefix1', 'prefix2')));
     $this->assertEqual($data, $this->redis->hGetAll("{$scope}:stats:global:multiBucket"));
     $this->assertEqual($data, $this->redis->hGetAll("{$scope}:stats:prefix1:multiBucket"));
     $this->assertEqual($data, $this->redis->hGetAll("{$scope}:stats:prefix2:multiBucket"));
     // multiple buckets as associated array with global
     $data = array('field1' => 'foobar', 'field2' => 'foobar');
     $expected = array('global' => $data, 'user:foo' => $data, 'year:2013' => $data);
     $result = Stats::set('multiBucket', array('field1' => 'foobar', 'field2' => 'foobar'), array('global', 'user' => 'foo', 'year' => '2013'));
     $this->assertEqual($expected, $result);
     $this->assertEqual($data, $this->redis->hGetAll("{$scope}:stats:global:multiBucket"));
     $this->assertEqual($data, $this->redis->hGetAll("{$scope}:stats:user:foo:multiBucket"));
     $this->assertEqual($data, $this->redis->hGetAll("{$scope}:stats:year:2013:multiBucket"));
 }