1<?php
2
3namespace MatrixPhp\Tests;
4
5use MatrixPhp\Exceptions\MatrixException;
6use MatrixPhp\Exceptions\MatrixHttpLibException;
7use MatrixPhp\Exceptions\ValidationException;
8use GuzzleHttp\Client;
9use GuzzleHttp\Psr7\Response;
10use GuzzleHttp\Psr7\Request;
11
12class UserTest extends BaseTestCase {
13    const HOSTNAME = "http://localhost";
14    protected $userId = "@test:localhost";
15    protected $roomId = '!test:localhost';
16    /**
17     * @var MatrixClient
18     */
19    protected $client;
20    /**
21     * @var User
22     */
23    protected $user;
24    /**
25     * @var Room
26     */
27    protected $room;
28
29    protected function setUp(): void
30    {
31        parent::setUp();
32        $this->client = new MatrixClient(self::HOSTNAME);
33        $this->user = new User($this->client->api(), $this->userId);
34        $this->room = $this->invokePrivateMethod($this->client, 'mkRoom', [$this->roomId]);
35    }
36
37    public function testDisplayName() {
38        // No displayname
39        $displayname = 'test';
40        $this->assertEquals($this->user->userId(), $this->user->getDisplayName($this->room));
41        $container = [];
42        $handler = $this->getMockClientHandler([new Response(200, [], '{}')], $container);
43        $this->client->api()->setClient(new Client(['handler' => $handler]));
44        $this->assertEquals($this->user->userId(), $this->user->getDisplayName());
45        $this->assertEquals(1, count($container));
46
47
48//        $mapi->whoami();
49//        /** @var Request $req */
50//        $req = array_get($container, '0.request');
51    }
52
53    public function testDisplayNameGlobal() {
54        $displayname = 'test';
55
56        // Get global displayname
57        $container = [];
58        $str = sprintf('{"displayname": "%s"}', $displayname);
59        $handler = $this->getMockClientHandler([new Response(200, [], $str)], $container);
60        $this->client->api()->setClient(new Client(['handler' => $handler]));
61        $this->assertEquals($displayname, $this->user->getDisplayName());
62        $this->assertEquals(1, count($container));
63
64    }
65}
66