xref: /dokuwiki/_test/tests/Remote/ApiCallTest.php (revision 7de5ac5588c9296c42d9e9b42e57467e6a8b816f)
142e66c7aSAndreas Gohr<?php
242e66c7aSAndreas Gohr
342e66c7aSAndreas Gohrnamespace dokuwiki\test\Remote;
442e66c7aSAndreas Gohr
5*7de5ac55SAndreas Gohruse ArgumentCountError;
642e66c7aSAndreas Gohruse dokuwiki\Remote\ApiCall;
7b209907bSAndreas Gohruse dokuwiki\Remote\OpenApiDoc\DocBlockMethod;
8*7de5ac55SAndreas Gohruse InvalidArgumentException;
942e66c7aSAndreas Gohr
1042e66c7aSAndreas Gohrclass ApiCallTest extends \DokuWikiTest
1142e66c7aSAndreas Gohr{
1242e66c7aSAndreas Gohr    /**
1342e66c7aSAndreas Gohr     * This is a test
1442e66c7aSAndreas Gohr     *
1542e66c7aSAndreas Gohr     * With more information
1642e66c7aSAndreas Gohr     * in several lines
1742e66c7aSAndreas Gohr     * @param string $foo First variable
1842e66c7aSAndreas Gohr     * @param int $bar
19b05603abSAndreas Gohr     * @param string[] $baz
20*7de5ac55SAndreas Gohr     * @param string $boink
21*7de5ac55SAndreas Gohr     * @param string $bonk
22*7de5ac55SAndreas Gohr     * @return string  The return
2342e66c7aSAndreas Gohr     * @something else
2442e66c7aSAndreas Gohr     * @something other
2542e66c7aSAndreas Gohr     * @another tag
2642e66c7aSAndreas Gohr     */
27*7de5ac55SAndreas Gohr    public function dummyMethod1($foo, $bar, $baz, $boink = 'boink', $bonk = 'bonk')
2842e66c7aSAndreas Gohr    {
29*7de5ac55SAndreas Gohr        return $foo . $bar . implode('', $baz) . $boink . $bonk;
3042e66c7aSAndreas Gohr    }
3142e66c7aSAndreas Gohr
3242e66c7aSAndreas Gohr    public function testMethodDocBlock()
3342e66c7aSAndreas Gohr    {
34b209907bSAndreas Gohr        $call = new ApiCall([$this, 'dummyMethod1'], 'cat1');
3542e66c7aSAndreas Gohr
36b209907bSAndreas Gohr        // basic doc block tests. More tests are done in the docblock parser class tests
3742e66c7aSAndreas Gohr        $this->assertEquals('This is a test', $call->getSummary());
3842e66c7aSAndreas Gohr        $this->assertEquals("With more information\nin several lines", $call->getDescription());
39b209907bSAndreas Gohr        $args = $call->getArgs();
40b209907bSAndreas Gohr        $this->assertIsArray($args);
41b209907bSAndreas Gohr        $this->assertArrayHasKey('foo', $args);
42b209907bSAndreas Gohr        $docs = $call->getDocs();
43b209907bSAndreas Gohr        $this->assertInstanceOf(DocBlockMethod::class, $docs);
4442e66c7aSAndreas Gohr
45b209907bSAndreas Gohr        // test public access
46b209907bSAndreas Gohr        $this->assertFalse($call->isPublic());
47b209907bSAndreas Gohr        $call->setPublic();
48b209907bSAndreas Gohr        $this->assertTrue($call->isPublic());
4942e66c7aSAndreas Gohr
50b209907bSAndreas Gohr        // check category
51b209907bSAndreas Gohr        $this->assertEquals('cat1', $call->getCategory());
5242e66c7aSAndreas Gohr    }
5342e66c7aSAndreas Gohr
5442e66c7aSAndreas Gohr    public function testFunctionDocBlock()
5542e66c7aSAndreas Gohr    {
565b379b50SAndreas Gohr        $call = new ApiCall('inlineSVG');
5742e66c7aSAndreas Gohr
58b209907bSAndreas Gohr        // basic doc block tests. More tests are done in the docblock parser class tests
59b209907bSAndreas Gohr        $args = $call->getArgs();
60b209907bSAndreas Gohr        $this->assertIsArray($args);
61b209907bSAndreas Gohr        $this->assertArrayHasKey('file', $args);
62b209907bSAndreas Gohr        $docs = $call->getDocs();
63b209907bSAndreas Gohr        $this->assertInstanceOf(DocBlockMethod::class, $docs);
64b209907bSAndreas Gohr
65b209907bSAndreas Gohr        // check category (not set)
66b209907bSAndreas Gohr        $this->assertEquals('', $call->getCategory());
6742e66c7aSAndreas Gohr    }
6842e66c7aSAndreas Gohr
6942e66c7aSAndreas Gohr    public function testExecution()
7042e66c7aSAndreas Gohr    {
7142e66c7aSAndreas Gohr        $call = new ApiCall([$this, 'dummyMethod1']);
7204acbb6fSAndreas Gohr        $this->assertEquals(
73*7de5ac55SAndreas Gohr            'bar1molfhahahuhu',
74*7de5ac55SAndreas Gohr            $call(['bar', 1, ['molf'], 'haha', 'huhu']),
7504acbb6fSAndreas Gohr            'positional parameters'
7604acbb6fSAndreas Gohr        );
7704acbb6fSAndreas Gohr        $this->assertEquals(
78*7de5ac55SAndreas Gohr            'bar1molfhahahuhu',
79*7de5ac55SAndreas Gohr            $call(['foo' => 'bar', 'bar' => 1, 'baz' => ['molf'], 'boink' => 'haha', 'bonk' => 'huhu']),
8004acbb6fSAndreas Gohr            'named parameters'
8104acbb6fSAndreas Gohr        );
8204acbb6fSAndreas Gohr
8304acbb6fSAndreas Gohr        $this->assertEquals(
84*7de5ac55SAndreas Gohr            'bar1molfboinkbonk',
8504acbb6fSAndreas Gohr            $call(['bar', 1, ['molf']]),
8604acbb6fSAndreas Gohr            'positional parameters, missing optional'
8704acbb6fSAndreas Gohr        );
8804acbb6fSAndreas Gohr        $this->assertEquals(
89*7de5ac55SAndreas Gohr            'bar1molfboinkbonk',
9004acbb6fSAndreas Gohr            $call(['foo' => 'bar', 'bar' => 1, 'baz' => ['molf']]),
9104acbb6fSAndreas Gohr            'named parameters, missing optional'
9204acbb6fSAndreas Gohr        );
9304acbb6fSAndreas Gohr        $this->assertEquals(
94*7de5ac55SAndreas Gohr            'bar1molfboinkbonk',
9504acbb6fSAndreas Gohr            $call(['foo' => 'bar', 'bar' => 1, 'baz' => ['molf'], 'nope' => 'egal']),
9604acbb6fSAndreas Gohr            'named parameters, missing optional, additional unknown'
9704acbb6fSAndreas Gohr        );
98*7de5ac55SAndreas Gohr        $this->assertEquals(
99*7de5ac55SAndreas Gohr            'bar1molfboinkhuhu',
100*7de5ac55SAndreas Gohr            $call(['foo' => 'bar', 'bar' => 1, 'baz' => ['molf'], 'bonk' => 'huhu']),
101*7de5ac55SAndreas Gohr            'named parameters, missing optional inbetween'
102*7de5ac55SAndreas Gohr        );
10342e66c7aSAndreas Gohr
10442e66c7aSAndreas Gohr        $call = new ApiCall('date');
10542e66c7aSAndreas Gohr        $this->assertEquals('2023-11-30', $call(['Y-m-d', 1701356591]), 'positional parameters');
10642e66c7aSAndreas Gohr        $this->assertEquals('2023-11-30', $call(['format' => 'Y-m-d', 'timestamp' => 1701356591]), 'named parameters');
10742e66c7aSAndreas Gohr    }
10804acbb6fSAndreas Gohr
10904acbb6fSAndreas Gohr    public function testCallMissingPositionalParameter()
11004acbb6fSAndreas Gohr    {
11104acbb6fSAndreas Gohr        $call = new ApiCall([$this, 'dummyMethod1']);
112*7de5ac55SAndreas Gohr        $this->expectException(ArgumentCountError::class);
11304acbb6fSAndreas Gohr        $call(['bar']);
11404acbb6fSAndreas Gohr    }
11504acbb6fSAndreas Gohr
11604acbb6fSAndreas Gohr    public function testCallMissingNamedParameter()
11704acbb6fSAndreas Gohr    {
11804acbb6fSAndreas Gohr        $call = new ApiCall([$this, 'dummyMethod1']);
119*7de5ac55SAndreas Gohr        $this->expectException(InvalidArgumentException::class);
12004acbb6fSAndreas Gohr        $call(['foo' => 'bar', 'baz' => ['molf']]); // missing bar
12104acbb6fSAndreas Gohr    }
12242e66c7aSAndreas Gohr}
123