xref: /dokuwiki/_test/tests/Remote/ApiCallTest.php (revision b209907b0d11e7ba66212349b498fb1476b64c03)
142e66c7aSAndreas Gohr<?php
242e66c7aSAndreas Gohr
342e66c7aSAndreas Gohrnamespace dokuwiki\test\Remote;
442e66c7aSAndreas Gohr
542e66c7aSAndreas Gohruse dokuwiki\Remote\ApiCall;
6*b209907bSAndreas Gohruse dokuwiki\Remote\OpenApiDoc\DocBlockMethod;
742e66c7aSAndreas Gohr
842e66c7aSAndreas Gohrclass ApiCallTest extends \DokuWikiTest
942e66c7aSAndreas Gohr{
1042e66c7aSAndreas Gohr    /**
1142e66c7aSAndreas Gohr     * This is a test
1242e66c7aSAndreas Gohr     *
1342e66c7aSAndreas Gohr     * With more information
1442e66c7aSAndreas Gohr     * in several lines
1542e66c7aSAndreas Gohr     * @param string $foo First variable
1642e66c7aSAndreas Gohr     * @param int $bar
17b05603abSAndreas Gohr     * @param string[] $baz
1842e66c7aSAndreas Gohr     * @something else
1942e66c7aSAndreas Gohr     * @something other
2042e66c7aSAndreas Gohr     * @another tag
2142e66c7aSAndreas Gohr     * @return string  The return
2242e66c7aSAndreas Gohr     */
2304acbb6fSAndreas Gohr    public function dummyMethod1($foo, $bar, $baz, $boink = 'boink')
2442e66c7aSAndreas Gohr    {
2504acbb6fSAndreas Gohr        return $foo . $bar . implode('', $baz) . $boink;
2642e66c7aSAndreas Gohr    }
2742e66c7aSAndreas Gohr
2842e66c7aSAndreas Gohr    public function testMethodDocBlock()
2942e66c7aSAndreas Gohr    {
30*b209907bSAndreas Gohr        $call = new ApiCall([$this, 'dummyMethod1'], 'cat1');
3142e66c7aSAndreas Gohr
32*b209907bSAndreas Gohr        // basic doc block tests. More tests are done in the docblock parser class tests
3342e66c7aSAndreas Gohr        $this->assertEquals('This is a test', $call->getSummary());
3442e66c7aSAndreas Gohr        $this->assertEquals("With more information\nin several lines", $call->getDescription());
35*b209907bSAndreas Gohr        $args = $call->getArgs();
36*b209907bSAndreas Gohr        $this->assertIsArray($args);
37*b209907bSAndreas Gohr        $this->assertArrayHasKey('foo', $args);
38*b209907bSAndreas Gohr        $docs = $call->getDocs();
39*b209907bSAndreas Gohr        $this->assertInstanceOf(DocBlockMethod::class, $docs);
4042e66c7aSAndreas Gohr
41*b209907bSAndreas Gohr        // test public access
42*b209907bSAndreas Gohr        $this->assertFalse($call->isPublic());
43*b209907bSAndreas Gohr        $call->setPublic();
44*b209907bSAndreas Gohr        $this->assertTrue($call->isPublic());
4542e66c7aSAndreas Gohr
46*b209907bSAndreas Gohr        // check category
47*b209907bSAndreas Gohr        $this->assertEquals('cat1', $call->getCategory());
4842e66c7aSAndreas Gohr    }
4942e66c7aSAndreas Gohr
5042e66c7aSAndreas Gohr    public function testFunctionDocBlock()
5142e66c7aSAndreas Gohr    {
525b379b50SAndreas Gohr        $call = new ApiCall('inlineSVG');
5342e66c7aSAndreas Gohr
54*b209907bSAndreas Gohr        // basic doc block tests. More tests are done in the docblock parser class tests
55*b209907bSAndreas Gohr        $args = $call->getArgs();
56*b209907bSAndreas Gohr        $this->assertIsArray($args);
57*b209907bSAndreas Gohr        $this->assertArrayHasKey('file', $args);
58*b209907bSAndreas Gohr        $docs = $call->getDocs();
59*b209907bSAndreas Gohr        $this->assertInstanceOf(DocBlockMethod::class, $docs);
60*b209907bSAndreas Gohr
61*b209907bSAndreas Gohr        // check category (not set)
62*b209907bSAndreas Gohr        $this->assertEquals('', $call->getCategory());
6342e66c7aSAndreas Gohr    }
6442e66c7aSAndreas Gohr
6542e66c7aSAndreas Gohr    public function testExecution()
6642e66c7aSAndreas Gohr    {
6742e66c7aSAndreas Gohr        $call = new ApiCall([$this, 'dummyMethod1']);
6804acbb6fSAndreas Gohr        $this->assertEquals(
6904acbb6fSAndreas Gohr            'bar1molfhaha',
7004acbb6fSAndreas Gohr            $call(['bar', 1, ['molf'], 'haha']),
7104acbb6fSAndreas Gohr            'positional parameters'
7204acbb6fSAndreas Gohr        );
7304acbb6fSAndreas Gohr        $this->assertEquals(
7404acbb6fSAndreas Gohr            'bar1molfhaha',
7504acbb6fSAndreas Gohr            $call(['foo' => 'bar', 'bar' => 1, 'baz' => ['molf'], 'boink' => 'haha']),
7604acbb6fSAndreas Gohr            'named parameters'
7704acbb6fSAndreas Gohr        );
7804acbb6fSAndreas Gohr
7904acbb6fSAndreas Gohr        $this->assertEquals(
8004acbb6fSAndreas Gohr            'bar1molfboink',
8104acbb6fSAndreas Gohr            $call(['bar', 1, ['molf']]),
8204acbb6fSAndreas Gohr            'positional parameters, missing optional'
8304acbb6fSAndreas Gohr        );
8404acbb6fSAndreas Gohr        $this->assertEquals(
8504acbb6fSAndreas Gohr            'bar1molfboink',
8604acbb6fSAndreas Gohr            $call(['foo' => 'bar', 'bar' => 1, 'baz' => ['molf']]),
8704acbb6fSAndreas Gohr            'named parameters, missing optional'
8804acbb6fSAndreas Gohr        );
8904acbb6fSAndreas Gohr        $this->assertEquals(
9004acbb6fSAndreas Gohr            'bar1molfboink',
9104acbb6fSAndreas Gohr            $call(['foo' => 'bar', 'bar' => 1, 'baz' => ['molf'],'nope' => 'egal']),
9204acbb6fSAndreas Gohr            'named parameters, missing optional, additional unknown'
9304acbb6fSAndreas Gohr        );
9442e66c7aSAndreas Gohr
9542e66c7aSAndreas Gohr        $call = new ApiCall('date');
9642e66c7aSAndreas Gohr        $this->assertEquals('2023-11-30', $call(['Y-m-d', 1701356591]), 'positional parameters');
9742e66c7aSAndreas Gohr        $this->assertEquals('2023-11-30', $call(['format' => 'Y-m-d', 'timestamp' => 1701356591]), 'named parameters');
9842e66c7aSAndreas Gohr    }
9904acbb6fSAndreas Gohr
10004acbb6fSAndreas Gohr    public function testCallMissingPositionalParameter()
10104acbb6fSAndreas Gohr    {
10204acbb6fSAndreas Gohr        $call = new ApiCall([$this, 'dummyMethod1']);
10304acbb6fSAndreas Gohr        $this->expectException(\ArgumentCountError::class);
10404acbb6fSAndreas Gohr        $call(['bar']);
10504acbb6fSAndreas Gohr    }
10604acbb6fSAndreas Gohr
10704acbb6fSAndreas Gohr    public function testCallMissingNamedParameter()
10804acbb6fSAndreas Gohr    {
10904acbb6fSAndreas Gohr        $call = new ApiCall([$this, 'dummyMethod1']);
11004acbb6fSAndreas Gohr        $this->expectException(\ArgumentCountError::class);
11104acbb6fSAndreas Gohr        $call(['foo' => 'bar', 'baz'=> ['molf']]); // missing bar
11204acbb6fSAndreas Gohr    }
11342e66c7aSAndreas Gohr}
114