xref: /dokuwiki/_test/tests/Remote/ApiCallTest.php (revision 42e66c7a3a46f444860e00bbf95403ac2b5cbc8f)
1<?php
2
3namespace dokuwiki\test\Remote;
4
5use dokuwiki\Remote\ApiCall;
6
7class ApiCallTest extends \DokuWikiTest
8{
9    /**
10     * This is a test
11     *
12     * With more information
13     * in several lines
14     * @param string $foo First variable
15     * @param int $bar
16     * @something else
17     * @something other
18     * @another tag
19     * @return string  The return
20     */
21    public function dummyMethod1($foo, $bar)
22    {
23        return 'dummy';
24    }
25
26    public function testMethodDocBlock()
27    {
28        $call = new ApiCall([$this, 'dummyMethod1']);
29
30        $this->assertEquals('This is a test', $call->getSummary());
31        $this->assertEquals("With more information\nin several lines", $call->getDescription());
32
33        $this->assertEquals(
34            [
35                'foo' => [
36                    'type' => 'string',
37                    'description' => 'First variable',
38                ],
39                'bar' => [
40                    'type' => 'int',
41                    'description' => '',
42                ]
43            ],
44            $call->getArgs()
45        );
46
47        $this->assertEquals(
48            [
49                'type' => 'string',
50                'description' => 'The return'
51            ],
52            $call->getReturn()
53        );
54
55        // remove one parameter
56        $call->limitArgs(['foo']);
57        $this->assertEquals(
58            [
59                'foo' => [
60                    'type' => 'string',
61                    'description' => 'First variable',
62                ],
63            ],
64            $call->getArgs()
65        );
66    }
67
68    public function testFunctionDocBlock()
69    {
70        $call = new ApiCall('date');
71        $call->setArgDescription('format', 'The format');
72
73        $this->assertEquals(
74            [
75                'format' => [
76                    'type' => 'string',
77                    'description' => 'The format',
78                ],
79                'timestamp' => [
80                    'type' => 'int',
81                    'description' => '',
82                ]
83            ],
84            $call->getArgs()
85        );
86    }
87
88    public function testExecution()
89    {
90        $call = new ApiCall([$this, 'dummyMethod1']);
91        $this->assertEquals('dummy', $call(['bar', 1]), 'positional parameters');
92        $this->assertEquals('dummy', $call(['foo' => 'bar', 'bar' => 1]), 'named parameters');
93
94        $call = new ApiCall('date');
95        $this->assertEquals('2023-11-30', $call(['Y-m-d', 1701356591]), 'positional parameters');
96        $this->assertEquals('2023-11-30', $call(['format' => 'Y-m-d', 'timestamp' => 1701356591]), 'named parameters');
97    }
98}
99