You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hey fam, I am writing a trading bot which is using a polling system to get the latest price as the provider's webs-socket stream is not providing the latest price data.
Here's the PriceFetcher.php class:
<?php
namespace Merchant\TradingBot\Core\Utils\Cryptocurrency\MarketData;
use Merchant\TradingBot\Core\Utils\Logger;
use Psr\Http\Message\ResponseInterface;
use React\EventLoop\LoopInterface;
use React\Http\Browser;
class PriceFetcher
{
public string $marketPriceUrl = '';
public Browser $http;
public int|float|string $pollInterval = 5;
/**
* Instantiate the PriceFetcher class
*
* @param \React\EventLoop\LoopInterface $loop
* @param string $symbol
*/
public function __construct(
private LoopInterface $loop,
private string $symbol
) {
$this->boot();
}
/**
* Boot the PriceFetcher class
* @return void
*/
public function boot(): void
{
$this->marketPriceUrl = $_ENV['EX_API_URL'] . "/api/v3/ticker/price?symbol=" . $this->symbol;
$this->http = new Browser(loop: $this->loop);
$this->pollInterval = $_ENV['PRICE_FETCH_INTERVAL'];
}
/**
* Fetch the current price of the cryptocurrency
* pair from EX Rest API
*
* @param callable $callable
* @return void
*/
public function fetch(callable $callable): void
{
$this->loop->addPeriodicTimer($this->pollInterval, function () use ($callable) {
$this->http->get($this->marketPriceUrl)->then(function (ResponseInterface $response) use ($callable) {
$priceData = json_decode($response->getBody());
$priceDataArray = [
'ticker' => $priceData->symbol,
'price' => $priceData->price,
];
$callable($priceDataArray);
}, function (\Exception $exception) use ($callable) {
Logger::create()->info("Error fetching price: " . $exception->getMessage());
$this->fetch($callable);
});
});
}
}
The issue is when I am writing a unit test using PHPUnit, the callback inside the addPeriodicTimer is not triggered, hence the test fails.
Note: I am mocking the Loop, Browser, and Logger class inside the unit test
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hey fam, I am writing a trading bot which is using a polling system to get the latest price as the provider's webs-socket stream is not providing the latest price data.
Here's the PriceFetcher.php class:
The issue is when I am writing a unit test using PHPUnit, the callback inside the addPeriodicTimer is not triggered, hence the test fails.
Note: I am mocking the Loop, Browser, and Logger class inside the unit test
Any Help would be appreciated
Beta Was this translation helpful? Give feedback.
All reactions