Open Data Catalog v2.0.0
DocsTest.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use Drupal\Core\Extension\Extension;
7 use Drupal\Core\Extension\ModuleHandlerInterface;
8 use PHPUnit\Framework\TestCase;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
10 use Symfony\Component\HttpFoundation\Request;
11 use Symfony\Component\HttpFoundation\RequestStack;
12 use MockChain\Chain;
13 use MockChain\Options;
14 
18 class DocsTest extends TestCase {
19 
23  public function testGetVersions() {
24  $mockChain = $this->getCommonMockChain();
25  $controller = Docs::create($mockChain->getMock());
26  $response = $controller->getVersions();
27 
28  $spec = '{"version":1,"url":"\/api\/1"}';
29 
30  $this->assertEquals($spec, $response->getContent());
31  }
32 
36  public function testGetComplete() {
37  $mock = $this->getCommonMockChain()
38  ->add(RequestStack::class, 'getCurrentRequest', Request::class)
39  ->add(Request::class, 'get', NULL);
40 
41  $spec = '{"openapi":"3.0.1","info":{"title":"API Documentation","version":"Alpha"},"components":{"securitySchemes":{"basicAuth":{"type":"http","scheme":"basic"}}},"paths":{"\/api\/1\/metastore\/schemas\/dataset\/items\/{identifier}":{"get":{"summary":"Get this dataset","tags":["Dataset"],"parameters":[{"name":"identifier","in":"path","description":"Dataset uuid","required":true,"schema":{"type":"string"}}],"responses":{"200":{"description":"Ok"}}},"delete":{"summary":"This operation should not be present in dataset-specific docs.","security":[{"basicAuth":[]}],"responses":{"200":{"description":"Ok"}}},"post":null},"\/api\/1\/some\/other\/path":{"patch":{"summary":"This path and operation should not be present in dataset-specific docs.","security":[{"basicAuth":[]}],"responses":{"200":{"description":"Ok"}}}}}}';
42 
43  $controller = Docs::create($mock->getMock());
44  $response = $controller->getComplete();
45 
46  $this->assertEquals($spec, $response->getContent());
47  }
48 
52  public function testGetPublic() {
53  $mock = $this->getCommonMockChain()
54  ->add(RequestStack::class, 'getCurrentRequest', Request::class)
55  ->add(Request::class, 'get', 'false');
56 
57  $spec = '{"openapi":"3.0.1","info":{"title":"API Documentation","version":"Alpha"},"components":[],"paths":{"\/api\/1\/metastore\/schemas\/dataset\/items\/{identifier}":{"get":{"summary":"Get this dataset","tags":["Dataset"],"parameters":[{"name":"identifier","in":"path","description":"Dataset uuid","required":true,"schema":{"type":"string"}}],"responses":{"200":{"description":"Ok"}}},"post":null}}}';
58 
59  $controller = Docs::create($mock->getMock());
60  $response = $controller->getComplete();
61 
62  $this->assertEquals($spec, $response->getContent());
63  }
64 
68  public function getCommonMockChain() {
69  $options = (new Options())
70  ->add('module_handler', ModuleHandlerInterface::class)
71  ->add('request_stack', RequestStack::class)
72  ->index(0);
73 
74  $mockChain = (new Chain($this))
75  ->add(ContainerInterface::class, 'get', $options)
76  ->add(ModuleHandlerInterface::class, 'getModule', Extension::class)
77  ->add(Extension::class, 'getPath', __DIR__);
78 
79  return $mockChain;
80  }
81 
82 }
Drupal\Tests\dkan_api\Unit\Controller\DocsTest\getCommonMockChain
getCommonMockChain()
Definition: DocsTest.php:68
Drupal\dkan_api\Controller\Docs\create
static create(ContainerInterface $container)
Definition: Docs.php:52
Drupal\Tests\dkan_api\Unit\Controller\DocsTest\testGetComplete
testGetComplete()
Definition: DocsTest.php:36
Drupal\dkan_api\Controller\Docs
Definition: Docs.php:17
Drupal\Tests\dkan_api\Unit\Controller\DocsTest
Definition: DocsTest.php:18
Drupal\Tests\dkan_api\Unit\Controller\DocsTest\testGetPublic
testGetPublic()
Definition: DocsTest.php:52
Drupal\Tests\dkan_api\Unit\Controller
Definition: DocsTest.php:3
Drupal\Tests\dkan_api\Unit\Controller\DocsTest\testGetVersions
testGetVersions()
Definition: DocsTest.php:23