xref: /dokuwiki/_test/tests/Remote/ApiCallTest.php (revision 04acbb6fa78aa0b48be52e98501d8a9d113043df)
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     * @param string[] $baz
17     * @something else
18     * @something other
19     * @another tag
20     * @return string  The return
21     */
22    public function dummyMethod1($foo, $bar, $baz, $boink = 'boink')
23    {
24        return $foo . $bar . implode('', $baz) . $boink;
25    }
26
27    public function testMethodDocBlock()
28    {
29        $call = new ApiCall([$this, 'dummyMethod1']);
30
31        $this->assertEquals('This is a test', $call->getSummary());
32        $this->assertEquals("With more information\nin several lines", $call->getDescription());
33
34        $this->assertEquals(
35            [
36                'foo' => [
37                    'type' => 'string',
38                    'description' => 'First variable',
39                ],
40                'bar' => [
41                    'type' => 'int',
42                    'description' => '',
43                ],
44                'baz' => [
45                    'type' => 'array',
46                    'description' => '',
47                ],
48            ],
49            $call->getArgs()
50        );
51
52        $this->assertEquals(
53            [
54                'type' => 'string',
55                'description' => 'The return'
56            ],
57            $call->getReturn()
58        );
59
60        // remove one parameter
61        $call->limitArgs(['foo']);
62        $this->assertEquals(
63            [
64                'foo' => [
65                    'type' => 'string',
66                    'description' => 'First variable',
67                ],
68            ],
69            $call->getArgs()
70        );
71    }
72
73    public function testFunctionDocBlock()
74    {
75        $call = new ApiCall('inlineSVG');
76        $call->setArgDescription('file', 'overwritten description');
77
78        $this->assertEquals(
79            [
80                'file' => [
81                    'type' => 'string',
82                    'description' => 'overwritten description',
83                ],
84                'maxsize' => [
85                    'type' => 'int',
86                    'description' => 'maximum allowed size for the SVG to be embedded',
87                ]
88            ],
89            $call->getArgs()
90        );
91    }
92
93    public function testExecution()
94    {
95        $call = new ApiCall([$this, 'dummyMethod1']);
96        $this->assertEquals(
97            'bar1molfhaha',
98            $call(['bar', 1, ['molf'], 'haha']),
99            'positional parameters'
100        );
101        $this->assertEquals(
102            'bar1molfhaha',
103            $call(['foo' => 'bar', 'bar' => 1, 'baz' => ['molf'], 'boink' => 'haha']),
104            'named parameters'
105        );
106
107        $this->assertEquals(
108            'bar1molfboink',
109            $call(['bar', 1, ['molf']]),
110            'positional parameters, missing optional'
111        );
112        $this->assertEquals(
113            'bar1molfboink',
114            $call(['foo' => 'bar', 'bar' => 1, 'baz' => ['molf']]),
115            'named parameters, missing optional'
116        );
117        $this->assertEquals(
118            'bar1molfboink',
119            $call(['foo' => 'bar', 'bar' => 1, 'baz' => ['molf'],'nope' => 'egal']),
120            'named parameters, missing optional, additional unknown'
121        );
122
123        $call = new ApiCall('date');
124        $this->assertEquals('2023-11-30', $call(['Y-m-d', 1701356591]), 'positional parameters');
125        $this->assertEquals('2023-11-30', $call(['format' => 'Y-m-d', 'timestamp' => 1701356591]), 'named parameters');
126    }
127
128    public function testCallMissingPositionalParameter()
129    {
130        $call = new ApiCall([$this, 'dummyMethod1']);
131        $this->expectException(\ArgumentCountError::class);
132        $call(['bar']);
133    }
134
135    public function testCallMissingNamedParameter()
136    {
137        $call = new ApiCall([$this, 'dummyMethod1']);
138        $this->expectException(\ArgumentCountError::class);
139        $call(['foo' => 'bar', 'baz'=> ['molf']]); // missing bar
140    }
141}
142