mirror of
https://github.com/friendica/friendica
synced 2024-12-23 02:40:17 +00:00
52 lines
1.2 KiB
PHP
52 lines
1.2 KiB
PHP
<?php
|
|
|
|
// Copyright (C) 2010-2024, the Friendica project
|
|
// SPDX-FileCopyrightText: 2010-2024 the Friendica project
|
|
//
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
namespace Friendica\Test;
|
|
|
|
use Dice\Dice;
|
|
use Friendica\DI;
|
|
use Friendica\Network\HTTPClient\Factory\HttpClient;
|
|
use Friendica\Network\HTTPClient\Capability\ICanSendHttpRequests;
|
|
use GuzzleHttp\HandlerStack;
|
|
|
|
/**
|
|
* This class injects a mockable handler into the IHTTPClient dependency per Dice
|
|
*/
|
|
trait DiceHttpMockHandlerTrait
|
|
{
|
|
use FixtureTestTrait;
|
|
|
|
/**
|
|
* Handler for mocking requests anywhere for testing purpose
|
|
*
|
|
* @var HandlerStack
|
|
*/
|
|
protected $httpRequestHandler;
|
|
|
|
protected function setupHttpMockHandler(): void
|
|
{
|
|
$this->setUpFixtures();
|
|
|
|
$this->httpRequestHandler = HandlerStack::create();
|
|
|
|
$dice = DI::getDice();
|
|
// addRule() clones the current instance and returns a new one, so no concurrency problems :-)
|
|
$newDice = $dice->addRule(ICanSendHttpRequests::class, [
|
|
'instanceOf' => HttpClient::class,
|
|
'call' => [
|
|
['createClient', [$this->httpRequestHandler], Dice::CHAIN_CALL],
|
|
],
|
|
]);
|
|
|
|
DI::init($newDice);
|
|
}
|
|
|
|
protected function tearDownHandler(): void
|
|
{
|
|
$this->tearDownFixtures();
|
|
}
|
|
}
|