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