xref: /dokuwiki/_test/tests/Remote/ApiCallTest.php (revision b209907b0d11e7ba66212349b498fb1476b64c03)
1<?php
2
3namespace dokuwiki\test\Remote;
4
5use dokuwiki\Remote\ApiCall;
6use dokuwiki\Remote\OpenApiDoc\DocBlockMethod;
7
8class ApiCallTest extends \DokuWikiTest
9{
10    /**
11     * This is a test
12     *
13     * With more information
14     * in several lines
15     * @param string $foo First variable
16     * @param int $bar
17     * @param string[] $baz
18     * @something else
19     * @something other
20     * @another tag
21     * @return string  The return
22     */
23    public function dummyMethod1($foo, $bar, $baz, $boink = 'boink')
24    {
25        return $foo . $bar . implode('', $baz) . $boink;
26    }
27
28    public function testMethodDocBlock()
29    {
30        $call = new ApiCall([$this, 'dummyMethod1'], 'cat1');
31
32        // basic doc block tests. More tests are done in the docblock parser class tests
33        $this->assertEquals('This is a test', $call->getSummary());
34        $this->assertEquals("With more information\nin several lines", $call->getDescription());
35        $args = $call->getArgs();
36        $this->assertIsArray($args);
37        $this->assertArrayHasKey('foo', $args);
38        $docs = $call->getDocs();
39        $this->assertInstanceOf(DocBlockMethod::class, $docs);
40
41        // test public access
42        $this->assertFalse($call->isPublic());
43        $call->setPublic();
44        $this->assertTrue($call->isPublic());
45
46        // check category
47        $this->assertEquals('cat1', $call->getCategory());
48    }
49
50    public function testFunctionDocBlock()
51    {
52        $call = new ApiCall('inlineSVG');
53
54        // basic doc block tests. More tests are done in the docblock parser class tests
55        $args = $call->getArgs();
56        $this->assertIsArray($args);
57        $this->assertArrayHasKey('file', $args);
58        $docs = $call->getDocs();
59        $this->assertInstanceOf(DocBlockMethod::class, $docs);
60
61        // check category (not set)
62        $this->assertEquals('', $call->getCategory());
63    }
64
65    public function testExecution()
66    {
67        $call = new ApiCall([$this, 'dummyMethod1']);
68        $this->assertEquals(
69            'bar1molfhaha',
70            $call(['bar', 1, ['molf'], 'haha']),
71            'positional parameters'
72        );
73        $this->assertEquals(
74            'bar1molfhaha',
75            $call(['foo' => 'bar', 'bar' => 1, 'baz' => ['molf'], 'boink' => 'haha']),
76            'named parameters'
77        );
78
79        $this->assertEquals(
80            'bar1molfboink',
81            $call(['bar', 1, ['molf']]),
82            'positional parameters, missing optional'
83        );
84        $this->assertEquals(
85            'bar1molfboink',
86            $call(['foo' => 'bar', 'bar' => 1, 'baz' => ['molf']]),
87            'named parameters, missing optional'
88        );
89        $this->assertEquals(
90            'bar1molfboink',
91            $call(['foo' => 'bar', 'bar' => 1, 'baz' => ['molf'],'nope' => 'egal']),
92            'named parameters, missing optional, additional unknown'
93        );
94
95        $call = new ApiCall('date');
96        $this->assertEquals('2023-11-30', $call(['Y-m-d', 1701356591]), 'positional parameters');
97        $this->assertEquals('2023-11-30', $call(['format' => 'Y-m-d', 'timestamp' => 1701356591]), 'named parameters');
98    }
99
100    public function testCallMissingPositionalParameter()
101    {
102        $call = new ApiCall([$this, 'dummyMethod1']);
103        $this->expectException(\ArgumentCountError::class);
104        $call(['bar']);
105    }
106
107    public function testCallMissingNamedParameter()
108    {
109        $call = new ApiCall([$this, 'dummyMethod1']);
110        $this->expectException(\ArgumentCountError::class);
111        $call(['foo' => 'bar', 'baz'=> ['molf']]); // missing bar
112    }
113}
114