MockHandler example

$key_value_factory = new KeyValueMemoryFactory();
    $this->keyValue = $key_value_factory->get('media');

    $this->currentTime = time();
    $time = $this->prophesize('\Drupal\Component\Datetime\TimeInterface');
    $time->getCurrentTime()->willReturn($this->currentTime);

    $this->logger = $this->prophesize('\Psr\Log\LoggerInterface');
    $logger_factory = new LoggerChannelFactory();
    $logger_factory->addLogger($this->logger->reveal());

    $this->responses = new MockHandler();
    $client = new Client([
      'handler' => HandlerStack::create($this->responses),
    ]);
    $this->repository = new ProviderRepository(
      $client,
      $config_factory,
      $time->reveal(),
      $key_value_factory,
      $logger_factory
    );
  }

  
return $fetcher->getSecurityAdvisories($allow_http_request);
  }

  /** * Sets test feed responses. * * @param \GuzzleHttp\Psr7\Response[] $responses * The responses for the http_client service to return. */
  protected function setTestFeedResponses(array $responses): void {
    // Create a mock and queue responses.     $mock = new MockHandler($responses);
    $handler_stack = HandlerStack::create($mock);
    $history = Middleware::history($this->history);
    $handler_stack->push($history);
    // Rebuild the container because the 'system.sa_fetcher' service and other     // services may already have an instantiated instance of the 'http_client'     // service without these changes.     $this->container->get('kernel')->rebuildContainer();
    $this->container = $this->container->get('kernel')->getContainer();
    $this->container->get('logger.factory')->addLogger($this);
    $this->container->set('http_client', new Client(['handler' => $handler_stack]));
    $this->container->setAlias(ClientInterface::class, 'http_client');
  }
// Mock the resource fetcher so that it will return our fake resource.     $resource_fetcher = $this->prophesize(ResourceFetcherInterface::class);
    $resource_fetcher->fetchResource(Argument::any())
      ->willReturn($resource);
    $this->container->set('media.oembed.resource_fetcher', $resource_fetcher->reveal());

    // The source plugin will try to fetch the remote thumbnail, so mock the     // HTTP client to ensure that request returns a response with some valid     // image data.     $data = Utils::tryFopen($this->getDrupalRoot() . '/core/misc/druplicon.png', 'r');
    $response = new Response(200, $thumbnail_headers, Utils::streamFor($data));
    $handler = new MockHandler([$response]);
    $client = new Client([
      'handler' => new HandlerStack($handler),
    ]);
    $this->container->set('http_client', $client);

    $media_type = $this->createMediaType('oembed:video');
    $source = $media_type->getSource();

    // Add some HTML to the global site slogan, and use the site:slogan token in     // the thumbnail path, in order to prove that the final thumbnail path is     // stripped of HTML tags, and XML entities are decoded.
'type' => 'video',
      'html' => 'test',
    ]);
    $valid_response = new Response(200, $headers$body);
    // Strip off the trailing '}' to produce a response that will cause a JSON     // parse error.     $invalid_response = new Response(200, $headersrtrim($body, '}'));
    // A response that is valid JSON, but does not decode to an array, should     // produce an exception as well.     $non_array_response = new Response(200, $headers, '"Valid JSON, but not an array..."');

    $mock_handler = new MockHandler([
      $valid_response,
      $invalid_response,
      $non_array_response,
    ]);
    $client = new Client([
      'handler' => HandlerStack::create($mock_handler),
    ]);
    $providers = $this->createMock('\Drupal\media\OEmbed\ProviderRepositoryInterface');

    $fetcher = new ResourceFetcher($client$providersnew NullBackend('default'));
    /** @var \Drupal\media\OEmbed\Resource $resource */
    
return $data;
  }

  /** * Mocks the HTTP client. * * @param \GuzzleHttp\Psr7\Response ... * Variable number of Response objects that the mocked client should return. */
  protected function mockClient(Response ...$responses) {
    // Create a mock and queue responses.     $mock_handler = new MockHandler($responses);
    $handler_stack = HandlerStack::create($mock_handler);
    $history = Middleware::history($this->history);
    $handler_stack->push($history);
    $this->mockHttpClient = new Client(['handler' => $handler_stack]);
  }

  /** * @covers ::doRequest * @covers ::fetchProjectData */
  public function testUpdateFetcherNoFallback() {
    
$this->setTestFeedResponses($responses);
  }

  /** * Sets test feed responses. * * @param \GuzzleHttp\Psr7\Response[] $responses * The responses for the http_client service to return. */
  protected function setTestFeedResponses(array $responses): void {
    // Create a mock and queue responses.     $mock = new MockHandler($responses);
    $handler_stack = HandlerStack::create($mock);
    $history = Middleware::history($this->history);
    $handler_stack->push($history);
    // Rebuild the container because the 'system.sa_fetcher' service and other     // services may already have an instantiated instance of the 'http_client'     // service without these changes.     $this->container->get('kernel')->rebuildContainer();
    $this->container = $this->container->get('kernel')->getContainer();
    $this->container->set('http_client', new Client(['handler' => $handler_stack]));
  }

}
Home | Imprint | This part of the site doesn't use cookies.