[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

/_test/tests/Remote/ -> ApiCallTest.php (source)

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