Open Data Catalog v2.0.0
ApiTest.php
Go to the documentation of this file.
1 <?php
2 
3 use MockChain\Options;
4 use Drupal\Component\DependencyInjection\Container;
5 use MockChain\Chain;
6 use Procrastinator\Result;
7 use Symfony\Component\HttpFoundation\Request;
8 use Symfony\Component\HttpFoundation\JsonResponse;
9 use Symfony\Component\HttpFoundation\RequestStack;
10 use Contracts\Mock\Storage\MemoryFactory;
12 use PHPUnit\Framework\TestCase;
14 use Symfony\Component\DependencyInjection\ContainerInterface;
15 
19 class ApiTest extends TestCase {
20 
21  private $request;
22 
26  public function getContainer() {
27  // TODO: Change the autogenerated stub.
28  parent::setUp();
29 
30  $container = $this->getMockBuilder(ContainerInterface::class)
31  ->setMethods(['get'])
32  ->disableOriginalConstructor()
33  ->getMockForAbstractClass();
34 
35  $container->method('get')
36  ->with(
37  $this->logicalOr(
38  $this->equalTo('dkan_harvest.service'),
39  $this->equalTo('dkan_harvest.logger_channel'),
40  $this->equalTo('request_stack')
41  )
42  )
43  ->will($this->returnCallback([$this, 'containerGet']));
44 
45  return $container;
46  }
47 
51  public function containerGet($input) {
52  switch ($input) {
53  case 'dkan_harvest.service':
54  return new Harvester(new MemoryFactory());
55 
56  break;
57  case 'request_stack':
58  $stack = $this->getMockBuilder(RequestStack::class)
59  ->disableOriginalConstructor()
60  ->setMethods(['getCurrentRequest'])
61  ->getMock();
62 
63  $stack->method("getCurrentRequest")->willReturn($this->request);
64 
65  return $stack;
66 
67  break;
68  }
69  }
70 
74  public function testEmptyIndex() {
75  $controller = new Api($this->getContainer());
76  $response = $controller->index();
77  $this->assertEquals(JsonResponse::class, get_class($response));
78  $this->assertEquals($response->getContent(), json_encode([]));
79  }
80 
84  public function testBadPlan() {
85  $this->request = new Request();
86  $controller = new Api($this->getContainer());
87  $response = $controller->register();
88  $this->assertEquals(JsonResponse::class, get_class($response));
89  $this->assertEquals($response->getContent(), json_encode(["message" => "Harvest plan must be a php object."]));
90  }
91 
95  public function testRegisterAndIndex() {
96  $request = $this->getMockBuilder(Request::class)
97  ->setMethods(['getContent'])
98  ->disableOriginalConstructor()
99  ->getMock();
100 
101  $plan = [
102  'identifier' => 'test',
103  'extract' => ['type' => "blah", "uri" => "http://blah"],
104  'load' => ['type' => 'blah'],
105  ];
106 
107  $request->method('getContent')->willReturn(json_encode($plan));
108 
109  $this->request = $request;
110 
111  $controller = new Api($this->getContainer());
112  $response = $controller->register();
113  $this->assertEquals(JsonResponse::class, get_class($response));
114  $this->assertEquals($response->getContent(), json_encode(["identifier" => "test"]));
115 
116  $response = $controller->index();
117  $this->assertEquals(JsonResponse::class, get_class($response));
118  }
119 
123  public function testRun() {
124  $options = (new Options())
125  ->add("request_stack", RequestStack::class)
126  ->add("dkan_harvest.service", Harvester::class)
127  ->index(0);
128 
129  $container = (new Chain($this))
130  ->add(Container::class, "get", $options)
131  ->add(RequestStack::class, 'getCurrentRequest', Request::class)
132  ->add(Request::class, 'getContent', json_encode((object) ['plan_id' => 'test']))
133  ->add(Harvester::class, "runHarvest", Result::class)
134  ->getMock();
135 
136  $controller = Api::create($container);
137  $response = $controller->run();
138  $this->assertEquals(JsonResponse::class, get_class($response));
139  }
140 
141 }
ApiTest\containerGet
containerGet($input)
Definition: ApiTest.php:51
ApiTest\testRegisterAndIndex
testRegisterAndIndex()
Definition: ApiTest.php:95
ApiTest\testRun
testRun()
Definition: ApiTest.php:123
ApiTest\getContainer
getContainer()
Definition: ApiTest.php:26
ApiTest\testEmptyIndex
testEmptyIndex()
Definition: ApiTest.php:74
Drupal\dkan_harvest\Harvester
Definition: Harvester.php:14
ApiTest\testBadPlan
testBadPlan()
Definition: ApiTest.php:84
Drupal\dkan_harvest\Controller\Api
Definition: Api.php:16
ApiTest
Definition: ApiTest.php:19