summaryrefslogtreecommitdiff
path: root/vendor/ruflin/elastica/test/lib/Elastica/Test/StatusTest.php
blob: 14d7950a2fd2252d0abcc8350bfda1d4a2fcdadd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php

namespace Elastica\Test;
use Elastica\Client;
use Elastica\Exception\ResponseException;
use Elastica\Status;
use Elastica\Test\Base as BaseTest;

class StatusTest extends BaseTest
{
    public function testGetResponse()
    {
        $index = $this->_createIndex();
        $status = new Status($index->getClient());
        $this->assertInstanceOf('Elastica\Response', $status->getResponse());
    }

    public function testGetIndexStatuses()
    {
        $index = $this->_createIndex();

        $status = new Status($index->getClient());
        $statuses = $status->getIndexStatuses();

        $this->assertInternalType('array', $statuses);

        foreach ($statuses as $indexStatus) {
            $this->assertInstanceOf('Elastica\Index\Status', $indexStatus);
        }
    }

    public function testGetIndexNames()
    {
        $indexName = 'test';
        $client = $this->_getClient();
        $index = $client->getIndex($indexName);
        $index->create(array(), true);
        $index = $this->_createIndex();
		$index->refresh();
		$index->optimize();

        $status = new Status($index->getClient());
        $names = $status->getIndexNames();

        $this->assertInternalType('array', $names);
        $this->assertContains($index->getName(), $names);

        foreach ($names as $name) {
            $this->assertInternalType('string', $name);
        }
    }

    public function testIndexExists()
    {
        $indexName = 'elastica_test';
        $aliasName = 'elastica_test-alias';

        $client = $this->_getClient();
        $index = $client->getIndex($indexName);

        try {
            // Make sure index is deleted first
            $index->delete();
        } catch (ResponseException $e) {
        }

        $status = new Status($client);
        $this->assertFalse($status->indexExists($indexName));
        $index->create();

        $status->refresh();
        $this->assertTrue($status->indexExists($indexName));
    }

    public function testAliasExists()
    {
        $indexName = 'test';
        $aliasName = 'elastica_test-alias';

        $index1 = $this->_createIndex();

        $status = new Status($index1->getClient());

        foreach ($status->getIndicesWithAlias($aliasName) as $tmpIndex) {
            $tmpIndex->removeAlias($aliasName);
        }

        $this->assertFalse($status->aliasExists($aliasName));

        $index1->addAlias($aliasName);
        $status->refresh();
        $this->assertTrue($status->aliasExists($aliasName));

        $indicesWithAlias = $status->getIndicesWithAlias($aliasName);
        $this->assertEquals(array("elastica_$indexName"), array_map(
            function($index) {
                return $index->getName();
            }, $indicesWithAlias));
    }

    public function testServerStatus()
    {
        $client = $this->_getClient();
        $status = $client->getStatus();
        $serverStatus = $status->getServerStatus();

        $this->assertTrue(!empty($serverStatus) );
        $this->assertTrue('array' == gettype($serverStatus));
        $this->assertArrayHasKey('status', $serverStatus);
        $this->assertTrue($serverStatus['status'] == 200);
        $this->assertArrayHasKey('version', $serverStatus);

        $versionInfo = $serverStatus['version'];
        $this->assertArrayHasKey('number', $versionInfo);
    }
}