Mercurial > hg > isophonics-drupal-site
comparison core/modules/rest/tests/src/Kernel/RequestHandlerTest.php @ 14:1fec387a4317
Update Drupal core to 8.5.2 via Composer
author | Chris Cannam |
---|---|
date | Mon, 23 Apr 2018 09:46:53 +0100 |
parents | 4c8ae668cc8c |
children | 129ea1e6d783 |
comparison
equal
deleted
inserted
replaced
13:5fb285c0d0e3 | 14:1fec387a4317 |
---|---|
1 <?php | 1 <?php |
2 | 2 |
3 namespace Drupal\Tests\rest\Kernel; | 3 namespace Drupal\Tests\rest\Kernel; |
4 | 4 |
5 use Drupal\Core\Entity\EntityStorageInterface; | 5 use Drupal\Core\Config\ConfigFactoryInterface; |
6 use Drupal\Core\Config\ImmutableConfig; | |
6 use Drupal\Core\Routing\RouteMatch; | 7 use Drupal\Core\Routing\RouteMatch; |
7 use Drupal\KernelTests\KernelTestBase; | 8 use Drupal\KernelTests\KernelTestBase; |
8 use Drupal\rest\Plugin\ResourceBase; | 9 use Drupal\rest\Plugin\ResourceBase; |
9 use Drupal\rest\RequestHandler; | 10 use Drupal\rest\RequestHandler; |
10 use Drupal\rest\ResourceResponse; | 11 use Drupal\rest\ResourceResponse; |
11 use Drupal\rest\RestResourceConfigInterface; | 12 use Drupal\rest\RestResourceConfigInterface; |
12 use Symfony\Component\HttpFoundation\Request; | 13 use Symfony\Component\HttpFoundation\Request; |
13 use Symfony\Component\Routing\Route; | 14 use Symfony\Component\Routing\Route; |
15 use Symfony\Component\Serializer\SerializerInterface; | |
14 | 16 |
15 /** | 17 /** |
16 * Test REST RequestHandler controller logic. | 18 * Test REST RequestHandler controller logic. |
17 * | 19 * |
18 * @group rest | 20 * @group rest |
37 /** | 39 /** |
38 * {@inheritdoc} | 40 * {@inheritdoc} |
39 */ | 41 */ |
40 public function setUp() { | 42 public function setUp() { |
41 parent::setUp(); | 43 parent::setUp(); |
42 $this->entityStorage = $this->prophesize(EntityStorageInterface::class); | 44 $config_factory = $this->prophesize(ConfigFactoryInterface::class); |
43 $this->requestHandler = new RequestHandler($this->entityStorage->reveal()); | 45 $config_factory->get('rest.settings') |
44 $this->requestHandler->setContainer($this->container); | 46 ->willReturn($this->prophesize(ImmutableConfig::class)->reveal()); |
47 $serializer = $this->prophesize(SerializerInterface::class); | |
48 $this->requestHandler = new RequestHandler($config_factory->reveal(), $serializer->reveal()); | |
45 } | 49 } |
46 | 50 |
47 /** | 51 /** |
48 * @covers ::handle | 52 * @covers ::handle |
49 */ | 53 */ |
59 $config = $this->prophesize(RestResourceConfigInterface::class); | 63 $config = $this->prophesize(RestResourceConfigInterface::class); |
60 $config->getResourcePlugin()->willReturn($resource->reveal()); | 64 $config->getResourcePlugin()->willReturn($resource->reveal()); |
61 $config->getCacheContexts()->willReturn([]); | 65 $config->getCacheContexts()->willReturn([]); |
62 $config->getCacheTags()->willReturn([]); | 66 $config->getCacheTags()->willReturn([]); |
63 $config->getCacheMaxAge()->willReturn(12); | 67 $config->getCacheMaxAge()->willReturn(12); |
64 $this->entityStorage->load('restplugin')->willReturn($config->reveal()); | |
65 | 68 |
66 // Response returns NULL this time because response from plugin is not | 69 // Response returns NULL this time because response from plugin is not |
67 // a ResourceResponse so it is passed through directly. | 70 // a ResourceResponse so it is passed through directly. |
68 $response = $this->requestHandler->handle($route_match, $request); | 71 $response = $this->requestHandler->handle($route_match, $request, $config->reveal()); |
69 $this->assertEquals(NULL, $response); | 72 $this->assertEquals(NULL, $response); |
70 | 73 |
71 // Response will return a ResourceResponse this time. | 74 // Response will return a ResourceResponse this time. |
72 $response = new ResourceResponse([]); | 75 $response = new ResourceResponse([]); |
73 $resource->get(NULL, $request) | 76 $resource->get(NULL, $request) |
74 ->willReturn($response); | 77 ->willReturn($response); |
75 $handler_response = $this->requestHandler->handle($route_match, $request); | 78 $handler_response = $this->requestHandler->handle($route_match, $request, $config->reveal()); |
76 $this->assertEquals($response, $handler_response); | 79 $this->assertEquals($response, $handler_response); |
77 | 80 |
78 // We will call the patch method this time. | 81 // We will call the patch method this time. |
79 $route_match = new RouteMatch('test', (new Route('/rest/test', ['_rest_resource_config' => 'restplugin'], ['_content_type_format' => 'json']))->setMethods(['PATCH'])); | 82 $route_match = new RouteMatch('test', (new Route('/rest/test', ['_rest_resource_config' => 'restplugin'], ['_content_type_format' => 'json']))->setMethods(['PATCH'])); |
80 $request->setMethod('PATCH'); | 83 $request->setMethod('PATCH'); |
81 $response = new ResourceResponse([]); | 84 $response = new ResourceResponse([]); |
82 $resource->patch(NULL, $request) | 85 $resource->patch(NULL, $request) |
83 ->shouldBeCalledTimes(1) | 86 ->shouldBeCalledTimes(1) |
84 ->willReturn($response); | 87 ->willReturn($response); |
85 $handler_response = $this->requestHandler->handle($route_match, $request); | 88 $handler_response = $this->requestHandler->handle($route_match, $request, $config->reveal()); |
86 $this->assertEquals($response, $handler_response); | 89 $this->assertEquals($response, $handler_response); |
87 } | 90 } |
88 | 91 |
89 } | 92 } |
90 | 93 |
91 /** | 94 /** |
92 * Stub class where we can prophesize methods. | 95 * Stub class where we can prophesize methods. |
93 */ | 96 */ |
94 class StubRequestHandlerResourcePlugin extends ResourceBase { | 97 class StubRequestHandlerResourcePlugin extends ResourceBase { |
95 | 98 |
96 public function get() {} | 99 public function get($example, Request $request) {} |
97 public function post() {} | 100 public function post() {} |
98 public function patch() {} | 101 public function patch($example_original, Request $request) {} |
99 public function delete() {} | 102 public function delete() {} |
100 | 103 |
101 } | 104 } |