1<?php
2
3declare(strict_types = 1);
4
5namespace Elasticsearch\Tests;
6
7use Elasticsearch;
8use Elasticsearch\ClientBuilder;
9use Elasticsearch\Endpoints\AbstractEndpoint;
10use Elasticsearch\Serializers\SerializerInterface;
11use Elasticsearch\Transport;
12use Mockery as m;
13
14/**
15 * Class RegisteredNamespaceTest
16 *
17 * @category   Tests
18 * @package    Elasticsearch
19 * @subpackage Tests
20 * @author     Zachary Tong <zachary.tong@elasticsearch.com>
21 * @license    http://www.apache.org/licenses/LICENSE-2.0 Apache2
22 * @link       http://elasticsearch.org
23 */
24class RegisteredNamespaceTest extends \PHPUnit\Framework\TestCase
25{
26    public function tearDown()
27    {
28        m::close();
29    }
30
31    public function testRegisteringNamespace()
32    {
33        $builder = new FooNamespaceBuilder();
34        $client = ClientBuilder::create()->registerNamespace($builder)->build();
35        $this->assertSame("123", $client->foo()->fooMethod());
36    }
37
38    public function testNonExistingNamespace()
39    {
40        $builder = new FooNamespaceBuilder();
41        $client = ClientBuilder::create()->registerNamespace($builder)->build();
42
43        $this->expectException(\Elasticsearch\Common\Exceptions\BadMethodCallException::class);
44        $this->expectExceptionMessage('Namespace [bar] not found');
45
46        $client->bar()->fooMethod();
47    }
48}
49
50// @codingStandardsIgnoreStart "Each class must be in a file by itself" - not worth the extra work here
51class FooNamespaceBuilder implements Elasticsearch\Namespaces\NamespaceBuilderInterface
52{
53    public function getName(): string
54    {
55        return "foo";
56    }
57
58    public function getObject(Transport $transport, SerializerInterface $serializer)
59    {
60        return new FooNamespace();
61    }
62}
63
64class FooNamespace
65{
66    public function fooMethod()
67    {
68        return "123";
69    }
70}
71// @codingStandardsIgnoreEnd
72