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