Skip to content

Commit

Permalink
pkp#9899 added guzzle client mocking feature
Browse files Browse the repository at this point in the history
  • Loading branch information
touhidurabir committed Jun 9, 2024
1 parent 3ac127e commit 52564df
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 2 deletions.
7 changes: 7 additions & 0 deletions classes/core/PKPApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,13 @@ public function getUUID(): string
*/
public function getHttpClient()
{
if (PKPContainer::getInstance()->runningUnitTests()) {
$client = Registry::get(\PKP\tests\PKPTestCase::MOCKED_GUZZLE_CLIENT_NAME);
if ($client) {
return $client;
}
}

$application = Application::get();
$userAgent = $application->getName() . '/';
if (static::isInstalled() && !static::isUpgrading()) {
Expand Down
25 changes: 23 additions & 2 deletions classes/core/PKPContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@

class PKPContainer extends Container
{
/**
* Define if the app currently runing the unit test
*/
private bool $isRunningUnitTest = false;

/**
* @var string
*
Expand Down Expand Up @@ -369,9 +374,25 @@ protected function settingProxyForStreamContext(): void
*
* @return bool
*/
public function runningUnitTests()
public function runningUnitTests(): bool
{
return $this->isRunningUnitTest;
}

/**
* Set the app running unit test
*/
public function setRunningUnitTests(): void
{
$this->isRunningUnitTest = true;
}

/**
* Unset the app running unit test
*/
public function unsetRunningUnitTests(): void
{
return false;
$this->isRunningUnitTest = false;
}

/**
Expand Down
34 changes: 34 additions & 0 deletions tests/PKPTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@
use APP\core\PageRouter;
use APP\core\Request;
use Mockery;
use Mockery\MockInterface;
use Mockery\LegacyMockInterface;
use PHPUnit\Framework\TestCase;
use PKP\config\Config;
use PKP\core\PKPContainer;
use PKP\core\Core;
use PKP\core\Dispatcher;
use PKP\core\Registry;
Expand All @@ -34,6 +37,8 @@

abstract class PKPTestCase extends TestCase
{
public const MOCKED_GUZZLE_CLIENT_NAME = 'GuzzleClient';

private array $daoBackup = [];
private array $registryBackup = [];
private array $containerBackup = [];
Expand Down Expand Up @@ -79,6 +84,7 @@ protected function setUp(): void
{
parent::setUp();
$this->setBackupGlobals(true);
PKPContainer::getInstance()->setRunningUnitTests();

// Rather than using "include_once()", ADOdb uses
// a global variable to maintain the information
Expand Down Expand Up @@ -128,6 +134,10 @@ protected function tearDown(): void
DAORegistry::registerDAO($mockedDao, $this->daoBackup[$mockedDao]);
}

Registry::delete(self::MOCKED_GUZZLE_CLIENT_NAME);

PKPContainer::getInstance()->unsetRunningUnitTests();

Mockery::close();
parent::tearDown();
}
Expand Down Expand Up @@ -255,6 +265,30 @@ protected function swapMailDriver(string $driver = 'log'): void
$mailConfig['default'] = $driver;
FacadesConfig::set('mail', $mailConfig);
}

/**
* Create mockable guzzle client
* @see https://docs.guzzlephp.org/en/stable/testing.html
*
* @param bool $setToRegistry Should store it in app registry to be used by call
* as `Application::get()->getHttpClient()`
*
* @return \Mockery\MockInterface|\Mockery\LegacyMockInterface
*/
protected function mockGuzzleClient(bool $setToRegistry = true): MockInterface|LegacyMockInterface
{
$guzzleClientMock = Mockery::mock(\GuzzleHttp\Client::class)
->shouldReceive('request')
->withAnyArgs()
->andReturn(new \GuzzleHttp\Psr7\Response)
->getMock();

if ($setToRegistry) {
Registry::set(self::MOCKED_GUZZLE_CLIENT_NAME, $guzzleClientMock);
}

return $guzzleClientMock;
}
}

if (!PKP_STRICT_MODE) {
Expand Down
Binary file modified tests/jobs/doi/DepositSubmissionTest.php
Binary file not shown.

0 comments on commit 52564df

Please sign in to comment.