xref: /dokuwiki/_test/tests/Remote/ApiCallTest.php (revision 04acbb6fa78aa0b48be52e98501d8a9d113043df)
142e66c7aSAndreas Gohr<?php
242e66c7aSAndreas Gohr
342e66c7aSAndreas Gohrnamespace dokuwiki\test\Remote;
442e66c7aSAndreas Gohr
542e66c7aSAndreas Gohruse dokuwiki\Remote\ApiCall;
642e66c7aSAndreas Gohr
742e66c7aSAndreas Gohrclass ApiCallTest extends \DokuWikiTest
842e66c7aSAndreas Gohr{
942e66c7aSAndreas Gohr    /**
1042e66c7aSAndreas Gohr     * This is a test
1142e66c7aSAndreas Gohr     *
1242e66c7aSAndreas Gohr     * With more information
1342e66c7aSAndreas Gohr     * in several lines
1442e66c7aSAndreas Gohr     * @param string $foo First variable
1542e66c7aSAndreas Gohr     * @param int $bar
16b05603abSAndreas Gohr     * @param string[] $baz
1742e66c7aSAndreas Gohr     * @something else
1842e66c7aSAndreas Gohr     * @something other
1942e66c7aSAndreas Gohr     * @another tag
2042e66c7aSAndreas Gohr     * @return string  The return
2142e66c7aSAndreas Gohr     */
22*04acbb6fSAndreas Gohr    public function dummyMethod1($foo, $bar, $baz, $boink = 'boink')
2342e66c7aSAndreas Gohr    {
24*04acbb6fSAndreas Gohr        return $foo . $bar . implode('', $baz) . $boink;
2542e66c7aSAndreas Gohr    }
2642e66c7aSAndreas Gohr
2742e66c7aSAndreas Gohr    public function testMethodDocBlock()
2842e66c7aSAndreas Gohr    {
2942e66c7aSAndreas Gohr        $call = new ApiCall([$this, 'dummyMethod1']);
3042e66c7aSAndreas Gohr
3142e66c7aSAndreas Gohr        $this->assertEquals('This is a test', $call->getSummary());
3242e66c7aSAndreas Gohr        $this->assertEquals("With more information\nin several lines", $call->getDescription());
3342e66c7aSAndreas Gohr
3442e66c7aSAndreas Gohr        $this->assertEquals(
3542e66c7aSAndreas Gohr            [
3642e66c7aSAndreas Gohr                'foo' => [
3742e66c7aSAndreas Gohr                    'type' => 'string',
3842e66c7aSAndreas Gohr                    'description' => 'First variable',
3942e66c7aSAndreas Gohr                ],
4042e66c7aSAndreas Gohr                'bar' => [
4142e66c7aSAndreas Gohr                    'type' => 'int',
4242e66c7aSAndreas Gohr                    'description' => '',
43b05603abSAndreas Gohr                ],
44b05603abSAndreas Gohr                'baz' => [
45b05603abSAndreas Gohr                    'type' => 'array',
46b05603abSAndreas Gohr                    'description' => '',
47b05603abSAndreas Gohr                ],
4842e66c7aSAndreas Gohr            ],
4942e66c7aSAndreas Gohr            $call->getArgs()
5042e66c7aSAndreas Gohr        );
5142e66c7aSAndreas Gohr
5242e66c7aSAndreas Gohr        $this->assertEquals(
5342e66c7aSAndreas Gohr            [
5442e66c7aSAndreas Gohr                'type' => 'string',
5542e66c7aSAndreas Gohr                'description' => 'The return'
5642e66c7aSAndreas Gohr            ],
5742e66c7aSAndreas Gohr            $call->getReturn()
5842e66c7aSAndreas Gohr        );
5942e66c7aSAndreas Gohr
6042e66c7aSAndreas Gohr        // remove one parameter
6142e66c7aSAndreas Gohr        $call->limitArgs(['foo']);
6242e66c7aSAndreas Gohr        $this->assertEquals(
6342e66c7aSAndreas Gohr            [
6442e66c7aSAndreas Gohr                'foo' => [
6542e66c7aSAndreas Gohr                    'type' => 'string',
6642e66c7aSAndreas Gohr                    'description' => 'First variable',
6742e66c7aSAndreas Gohr                ],
6842e66c7aSAndreas Gohr            ],
6942e66c7aSAndreas Gohr            $call->getArgs()
7042e66c7aSAndreas Gohr        );
7142e66c7aSAndreas Gohr    }
7242e66c7aSAndreas Gohr
7342e66c7aSAndreas Gohr    public function testFunctionDocBlock()
7442e66c7aSAndreas Gohr    {
755b379b50SAndreas Gohr        $call = new ApiCall('inlineSVG');
765b379b50SAndreas Gohr        $call->setArgDescription('file', 'overwritten description');
7742e66c7aSAndreas Gohr
7842e66c7aSAndreas Gohr        $this->assertEquals(
7942e66c7aSAndreas Gohr            [
805b379b50SAndreas Gohr                'file' => [
8142e66c7aSAndreas Gohr                    'type' => 'string',
825b379b50SAndreas Gohr                    'description' => 'overwritten description',
8342e66c7aSAndreas Gohr                ],
845b379b50SAndreas Gohr                'maxsize' => [
8542e66c7aSAndreas Gohr                    'type' => 'int',
865b379b50SAndreas Gohr                    'description' => 'maximum allowed size for the SVG to be embedded',
8742e66c7aSAndreas Gohr                ]
8842e66c7aSAndreas Gohr            ],
8942e66c7aSAndreas Gohr            $call->getArgs()
9042e66c7aSAndreas Gohr        );
9142e66c7aSAndreas Gohr    }
9242e66c7aSAndreas Gohr
9342e66c7aSAndreas Gohr    public function testExecution()
9442e66c7aSAndreas Gohr    {
9542e66c7aSAndreas Gohr        $call = new ApiCall([$this, 'dummyMethod1']);
96*04acbb6fSAndreas Gohr        $this->assertEquals(
97*04acbb6fSAndreas Gohr            'bar1molfhaha',
98*04acbb6fSAndreas Gohr            $call(['bar', 1, ['molf'], 'haha']),
99*04acbb6fSAndreas Gohr            'positional parameters'
100*04acbb6fSAndreas Gohr        );
101*04acbb6fSAndreas Gohr        $this->assertEquals(
102*04acbb6fSAndreas Gohr            'bar1molfhaha',
103*04acbb6fSAndreas Gohr            $call(['foo' => 'bar', 'bar' => 1, 'baz' => ['molf'], 'boink' => 'haha']),
104*04acbb6fSAndreas Gohr            'named parameters'
105*04acbb6fSAndreas Gohr        );
106*04acbb6fSAndreas Gohr
107*04acbb6fSAndreas Gohr        $this->assertEquals(
108*04acbb6fSAndreas Gohr            'bar1molfboink',
109*04acbb6fSAndreas Gohr            $call(['bar', 1, ['molf']]),
110*04acbb6fSAndreas Gohr            'positional parameters, missing optional'
111*04acbb6fSAndreas Gohr        );
112*04acbb6fSAndreas Gohr        $this->assertEquals(
113*04acbb6fSAndreas Gohr            'bar1molfboink',
114*04acbb6fSAndreas Gohr            $call(['foo' => 'bar', 'bar' => 1, 'baz' => ['molf']]),
115*04acbb6fSAndreas Gohr            'named parameters, missing optional'
116*04acbb6fSAndreas Gohr        );
117*04acbb6fSAndreas Gohr        $this->assertEquals(
118*04acbb6fSAndreas Gohr            'bar1molfboink',
119*04acbb6fSAndreas Gohr            $call(['foo' => 'bar', 'bar' => 1, 'baz' => ['molf'],'nope' => 'egal']),
120*04acbb6fSAndreas Gohr            'named parameters, missing optional, additional unknown'
121*04acbb6fSAndreas Gohr        );
12242e66c7aSAndreas Gohr
12342e66c7aSAndreas Gohr        $call = new ApiCall('date');
12442e66c7aSAndreas Gohr        $this->assertEquals('2023-11-30', $call(['Y-m-d', 1701356591]), 'positional parameters');
12542e66c7aSAndreas Gohr        $this->assertEquals('2023-11-30', $call(['format' => 'Y-m-d', 'timestamp' => 1701356591]), 'named parameters');
12642e66c7aSAndreas Gohr    }
127*04acbb6fSAndreas Gohr
128*04acbb6fSAndreas Gohr    public function testCallMissingPositionalParameter()
129*04acbb6fSAndreas Gohr    {
130*04acbb6fSAndreas Gohr        $call = new ApiCall([$this, 'dummyMethod1']);
131*04acbb6fSAndreas Gohr        $this->expectException(\ArgumentCountError::class);
132*04acbb6fSAndreas Gohr        $call(['bar']);
133*04acbb6fSAndreas Gohr    }
134*04acbb6fSAndreas Gohr
135*04acbb6fSAndreas Gohr    public function testCallMissingNamedParameter()
136*04acbb6fSAndreas Gohr    {
137*04acbb6fSAndreas Gohr        $call = new ApiCall([$this, 'dummyMethod1']);
138*04acbb6fSAndreas Gohr        $this->expectException(\ArgumentCountError::class);
139*04acbb6fSAndreas Gohr        $call(['foo' => 'bar', 'baz'=> ['molf']]); // missing bar
140*04acbb6fSAndreas Gohr    }
14142e66c7aSAndreas Gohr}
142