2021-08-24 11:19:17 +00:00
|
|
|
<?php
|
2024-08-24 12:31:41 +00:00
|
|
|
|
|
|
|
// Copyright (C) 2010-2024, the Friendica project
|
|
|
|
// SPDX-FileCopyrightText: 2010-2024 the Friendica project
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
2021-08-24 11:19:17 +00:00
|
|
|
|
|
|
|
namespace Friendica\Test;
|
|
|
|
|
2021-08-24 22:13:50 +00:00
|
|
|
use Dice\Dice;
|
2021-08-24 11:19:17 +00:00
|
|
|
use Friendica\DI;
|
2021-10-23 10:50:31 +00:00
|
|
|
use Friendica\Network\HTTPClient\Factory\HttpClient;
|
2021-10-29 06:03:59 +00:00
|
|
|
use Friendica\Network\HTTPClient\Capability\ICanSendHttpRequests;
|
2021-08-24 11:19:17 +00:00
|
|
|
use GuzzleHttp\HandlerStack;
|
|
|
|
|
|
|
|
/**
|
2021-08-24 22:13:50 +00:00
|
|
|
* This class injects a mockable handler into the IHTTPClient dependency per Dice
|
2021-08-24 11:19:17 +00:00
|
|
|
*/
|
2021-08-24 22:13:50 +00:00
|
|
|
trait DiceHttpMockHandlerTrait
|
2021-08-24 11:19:17 +00:00
|
|
|
{
|
2023-02-13 19:52:24 +00:00
|
|
|
use FixtureTestTrait;
|
|
|
|
|
2021-08-24 11:19:17 +00:00
|
|
|
/**
|
|
|
|
* Handler for mocking requests anywhere for testing purpose
|
|
|
|
*
|
|
|
|
* @var HandlerStack
|
|
|
|
*/
|
2021-08-24 22:13:50 +00:00
|
|
|
protected $httpRequestHandler;
|
2021-08-24 11:19:17 +00:00
|
|
|
|
2021-08-24 22:13:50 +00:00
|
|
|
protected function setupHttpMockHandler(): void
|
2021-08-24 11:19:17 +00:00
|
|
|
{
|
2023-02-13 19:52:24 +00:00
|
|
|
$this->setUpFixtures();
|
2021-08-24 11:19:17 +00:00
|
|
|
|
2021-08-24 22:13:50 +00:00
|
|
|
$this->httpRequestHandler = HandlerStack::create();
|
2021-08-24 11:19:17 +00:00
|
|
|
|
2021-08-24 22:13:50 +00:00
|
|
|
$dice = DI::getDice();
|
|
|
|
// addRule() clones the current instance and returns a new one, so no concurrency problems :-)
|
2021-10-29 06:03:59 +00:00
|
|
|
$newDice = $dice->addRule(ICanSendHttpRequests::class, [
|
2021-10-23 10:50:31 +00:00
|
|
|
'instanceOf' => HttpClient::class,
|
2021-08-24 22:13:50 +00:00
|
|
|
'call' => [
|
|
|
|
['createClient', [$this->httpRequestHandler], Dice::CHAIN_CALL],
|
|
|
|
],
|
|
|
|
]);
|
2021-08-24 11:19:17 +00:00
|
|
|
|
|
|
|
DI::init($newDice);
|
|
|
|
}
|
|
|
|
|
2023-02-13 19:52:24 +00:00
|
|
|
protected function tearDownHandler(): void
|
2021-08-24 11:19:17 +00:00
|
|
|
{
|
2023-02-13 19:52:24 +00:00
|
|
|
$this->tearDownFixtures();
|
2021-08-24 11:19:17 +00:00
|
|
|
}
|
|
|
|
}
|