1<?php defined('PHPREDIS_TESTRUN') or die("Use TestRedis.php to run tests!\n");
2
3require_once(dirname($_SERVER['PHP_SELF'])."/TestSuite.php");
4
5class Redis_Sentinel_Test extends TestSuite
6{
7    const NAME = 'mymaster';
8
9    /**
10     * @var RedisSentinel
11     */
12    public $sentinel;
13
14    /**
15     * Common fields
16     */
17    protected $fields = [
18        'name',
19        'ip',
20        'port',
21        'runid',
22        'flags',
23        'link-pending-commands',
24        'link-refcount',
25        'last-ping-sent',
26        'last-ok-ping-reply',
27        'last-ping-reply',
28        'down-after-milliseconds',
29    ];
30
31    protected function newInstance()
32    {
33        return new RedisSentinel($this->getHost());
34    }
35
36    public function setUp()
37    {
38        $this->sentinel = $this->newInstance();
39    }
40
41    public function testCkquorum()
42    {
43        $this->assertTrue(is_bool($this->sentinel->ckquorum(self::NAME)));
44    }
45
46    public function testFailover()
47    {
48        $this->assertFalse($this->sentinel->failover(self::NAME));
49    }
50
51    public function testFlushconfig()
52    {
53        $this->assertTrue($this->sentinel->flushconfig());
54    }
55
56    public function testGetMasterAddrByName()
57    {
58        $result = $this->sentinel->getMasterAddrByName(self::NAME);
59        $this->assertTrue(is_array($result));
60        $this->assertEquals(2, count($result));
61    }
62
63    protected function checkFields(array $fields)
64    {
65        foreach ($this->fields as $k) {
66            $this->assertTrue(array_key_exists($k, $fields));
67        }
68    }
69
70    public function testMaster()
71    {
72        $result = $this->sentinel->master(self::NAME);
73        $this->assertTrue(is_array($result));
74        $this->checkFields($result);
75    }
76
77    public function testMasters()
78    {
79        $result = $this->sentinel->masters();
80        $this->assertTrue(is_array($result));
81        foreach ($result as $master) {
82            $this->checkFields($master);
83        }
84    }
85
86    public function testPing()
87    {
88        $this->assertTrue($this->sentinel->ping());
89    }
90
91    public function testReset()
92    {
93        $this->assertFalse($this->sentinel->reset('*'));
94    }
95
96    public function testSentinels()
97    {
98        $result = $this->sentinel->sentinels(self::NAME);
99        $this->assertTrue(is_array($result));
100        foreach ($result as $sentinel) {
101            $this->checkFields($sentinel);
102        }
103    }
104
105    public function testSlaves()
106    {
107        $result = $this->sentinel->slaves(self::NAME);
108        $this->assertTrue(is_array($result));
109        foreach ($result as $slave) {
110            $this->checkFields($slave);
111        }
112    }
113}
114