Clean up APICall tests
[dokuwiki.git] / _test / tests / Remote / ApiCallTest.php
blob13258f4b2d0f2b34f36c38b0f733f5acc086ec0f
1 <?php
3 namespace dokuwiki\test\Remote;
5 use dokuwiki\Remote\ApiCall;
6 use dokuwiki\Remote\OpenApiDoc\DocBlockMethod;
8 class ApiCallTest extends \DokuWikiTest
10 /**
11 * This is a test
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
23 public function dummyMethod1($foo, $bar, $baz, $boink = 'boink')
25 return $foo . $bar . implode('', $baz) . $boink;
28 public function testMethodDocBlock()
30 $call = new ApiCall([$this, 'dummyMethod1'], 'cat1');
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);
41 // test public access
42 $this->assertFalse($call->isPublic());
43 $call->setPublic();
44 $this->assertTrue($call->isPublic());
46 // check category
47 $this->assertEquals('cat1', $call->getCategory());
50 public function testFunctionDocBlock()
52 $call = new ApiCall('inlineSVG');
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);
61 // check category (not set)
62 $this->assertEquals('', $call->getCategory());
65 public function testExecution()
67 $call = new ApiCall([$this, 'dummyMethod1']);
68 $this->assertEquals(
69 'bar1molfhaha',
70 $call(['bar', 1, ['molf'], 'haha']),
71 'positional parameters'
73 $this->assertEquals(
74 'bar1molfhaha',
75 $call(['foo' => 'bar', 'bar' => 1, 'baz' => ['molf'], 'boink' => 'haha']),
76 'named parameters'
79 $this->assertEquals(
80 'bar1molfboink',
81 $call(['bar', 1, ['molf']]),
82 'positional parameters, missing optional'
84 $this->assertEquals(
85 'bar1molfboink',
86 $call(['foo' => 'bar', 'bar' => 1, 'baz' => ['molf']]),
87 'named parameters, missing optional'
89 $this->assertEquals(
90 'bar1molfboink',
91 $call(['foo' => 'bar', 'bar' => 1, 'baz' => ['molf'],'nope' => 'egal']),
92 'named parameters, missing optional, additional unknown'
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');
100 public function testCallMissingPositionalParameter()
102 $call = new ApiCall([$this, 'dummyMethod1']);
103 $this->expectException(\ArgumentCountError::class);
104 $call(['bar']);
107 public function testCallMissingNamedParameter()
109 $call = new ApiCall([$this, 'dummyMethod1']);
110 $this->expectException(\ArgumentCountError::class);
111 $call(['foo' => 'bar', 'baz'=> ['molf']]); // missing bar