/** * @dataProvider commandProvider */ public function testValidCommandsAreReturned($command, $factoryMethod, Cli\Options $arguments = null) { $factory = $this->prophesize(Factory::class); if ($arguments != null) { $method = new MethodProphecy($factory, $factoryMethod, [$arguments]); } else { $method = new MethodProphecy($factory, $factoryMethod, []); } $method->willReturn($this->prophesize(Cli\Command::class)->reveal()); $factory->addMethodProphecy($method); $locator = new CommandLocator($factory->reveal()); $request = $this->prophesize(Cli\Request::class); $request->getCommand()->willReturn($command)->shouldBeCalled(); $request->getCommandOptions()->willReturn($arguments); $result = $locator->getCommandForRequest($request->reveal()); $this->assertInstanceOf(CLI\Command::class, $result); }
/** * @covers ::getResponseFormat * * Note this does *not* need to test formats being requested that are not * accepted by the server, because the routing system would have already * prevented those from reaching RequestHandler. * * @param string[] $methods * The HTTP methods to test. * @param string[] $supported_formats * The supported formats for the REST route to be tested. * @param string|false $request_format * The value for the ?_format URL query argument, if any. * @param string[] $request_headers * The request headers to send, if any. * @param string|null $request_body * The request body to send, if any. * @param string|null $expected_response_content_type * The expected MIME type of the response, if any. * @param string $expected_response_content * The expected content of the response. * * @dataProvider providerTestResponseFormat */ public function testResponseFormat($methods, array $supported_formats, $request_format, array $request_headers, $request_body, $expected_response_content_type, $expected_response_content) { $rest_config_name = $this->randomMachineName(); $parameters = []; if ($request_format !== FALSE) { $parameters['_format'] = $request_format; } foreach ($request_headers as $key => $value) { unset($request_headers[$key]); $key = strtoupper(str_replace('-', '_', $key)); $request_headers[$key] = $value; } foreach ($methods as $method) { $request = Request::create('/rest/test', $method, $parameters, [], [], $request_headers, $request_body); $route_requirement_key_format = $request->isMethodSafe() ? '_format' : '_content_type_format'; $route_match = new RouteMatch('test', new Route('/rest/test', ['_rest_resource_config' => $rest_config_name], [$route_requirement_key_format => implode('|', $supported_formats)])); $resource = $this->prophesize(StubRequestHandlerResourcePlugin::class); // Mock the configuration. $config = $this->prophesize(RestResourceConfigInterface::class); $config->getFormats($method)->willReturn($supported_formats); $config->getResourcePlugin()->willReturn($resource->reveal()); $config->getCacheContexts()->willReturn([]); $config->getCacheTags()->willReturn([]); $config->getCacheMaxAge()->willReturn(12); $this->entityStorage->load($rest_config_name)->willReturn($config->reveal()); // Mock the resource plugin. $response = new ResourceResponse($method !== 'DELETE' ? ['REST' => 'Drupal'] : NULL); $resource->getPluginDefinition()->willReturn([]); $method_prophecy = new MethodProphecy($resource, strtolower($method), [Argument::any(), $request]); $method_prophecy->willReturn($response); $resource->addMethodProphecy($method_prophecy); // Test the request handler. $handler_response = $this->requestHandler->handle($route_match, $request); $this->assertSame($expected_response_content_type, $handler_response->headers->get('Content-Type')); $this->assertEquals($expected_response_content, $handler_response->getContent()); } }