Skip to content

Commit

Permalink
pkp#9899 completing all pkp-libs job tests
Browse files Browse the repository at this point in the history
  • Loading branch information
touhidurabir committed Jun 13, 2024
1 parent 52564df commit b254f09
Show file tree
Hide file tree
Showing 20 changed files with 116 additions and 12 deletions.
2 changes: 2 additions & 0 deletions classes/core/PKPContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,14 @@ public function register($provider)
// will spin through them and register them with the application, which
// serves as a convenience layer while registering a lot of bindings.
if (property_exists($provider, 'bindings')) {
/** @disregard P1014 PHP Intelephense error suppression */
foreach ($provider->bindings as $key => $value) {
$this->bind($key, $value);
}
}

if (property_exists($provider, 'singletons')) {
/** @disregard P1014 PHP Intelephense error suppression */
foreach ($provider->singletons as $key => $value) {
$key = is_int($key) ? $value : $key;
$this->singleton($key, $value);
Expand Down
4 changes: 2 additions & 2 deletions jobs/statistics/CompileMonthlyMetrics.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ public function handle(): void
$currentMonth = date('Ym');
$lastMonth = date('Ym', strtotime('last month'));

$geoService = Services::get('geoStats');
$geoService = Services::get('geoStats'); /** @var \PKP\services\PKPStatsGeoService $geoService */
$geoService->deleteMonthlyMetrics($this->month);
$geoService->addMonthlyMetrics($this->month);
if (!$this->site->getData('keepDailyUsageStats') && $this->month != $currentMonth && $this->month != $lastMonth) {
$geoService->deleteDailyMetrics($this->month);
}

$counterService = Services::get('sushiStats');
$counterService = Services::get('sushiStats'); /** @var \PKP\services\PKPStatsSushiService $counterService */
$counterService->deleteMonthlyMetrics($this->month);
$counterService->addMonthlyMetrics($this->month);
if (!$this->site->getData('keepDailyUsageStats') && $this->month != $currentMonth && $this->month != $lastMonth) {
Expand Down
26 changes: 16 additions & 10 deletions tests/PKPTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
namespace PKP\tests;

use APP\core\Application;
use Illuminate\Support\Facades\Mail;

use APP\core\PageRouter;
use APP\core\Request;
use Mockery;
Expand Down Expand Up @@ -252,18 +254,21 @@ protected function localeToRegExp(string $translation): string
}

/**
* Swap the mail driver with something better for test
* Default will be to `log` driver so that no mail would be set
*
* We need to use this as we have override the default mail manager with some custom
* methods (e.g. `compileParams`, ...) which not available to core mail fake test class
* that is `Illuminate\Support\Testing\Fakes\MailFake` which cause test exception.
* Mock the mail facade
* @see https://laravel.com/docs/10.x/mocking
*/
protected function swapMailDriver(string $driver = 'log'): void
protected function mockMail(): void
{
$mailConfig = app()->get('config')['mail'];
$mailConfig['default'] = $driver;
FacadesConfig::set('mail', $mailConfig);
/**
* @disregard P1013 PHP Intelephense error suppression
* @see https://github.com/bmewburn/vscode-intelephense/issues/568
*/
Mail::shouldReceive('send')
->withAnyArgs()
->andReturn(null)
->shouldReceive('compileParams')
->withAnyArgs()
->andReturn('');
}

/**
Expand All @@ -278,6 +283,7 @@ protected function swapMailDriver(string $driver = 'log'): void
protected function mockGuzzleClient(bool $setToRegistry = true): MockInterface|LegacyMockInterface
{
$guzzleClientMock = Mockery::mock(\GuzzleHttp\Client::class)
->makePartial()
->shouldReceive('request')
->withAnyArgs()
->andReturn(new \GuzzleHttp\Psr7\Response)
Expand Down
Binary file modified tests/jobs/bulk/BulkEmailSenderTest.php
Binary file not shown.
Binary file modified tests/jobs/doi/DepositContextTest.php
Binary file not shown.
Binary file modified tests/jobs/doi/DepositSubmissionTest.php
Binary file not shown.
Binary file added tests/jobs/email/EditorialReminderTest.php
Binary file not shown.
96 changes: 96 additions & 0 deletions tests/jobs/metadata/BatchMetadataChangedJobTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

namespace PKP\tests\jobs\metadata;

use Mockery;
use PKP\db\DAORegistry;
use PKP\tests\PKPTestCase;
use PKP\jobs\metadata\BatchMetadataChangedJob;
use APP\submission\Repository as SubmissionRepository;

/**
* @runTestsInSeparateProcesses
*
* @see https://docs.phpunit.de/en/9.6/annotations.html#runtestsinseparateprocesses
*/
class BatchMetadataChangedJobTest extends PKPTestCase
{
/**
* Serializion from OJS 3.4.0
*/
protected string $serializedJobData = 'O:41:"PKP\jobs\metadata\BatchMetadataChangedJob":3:{s:13:"submissionIds";a:2:{i:0;i:1;i:1;i:2;}s:10:"connection";s:8:"database";s:5:"queue";s:5:"queue";}';

/**
* Test job is a proper instance
*/
public function testUnserializationGetJobInstance(): void
{
$this->assertInstanceOf(
BatchMetadataChangedJob::class,
unserialize($this->serializedJobData)
);
}

/**
* Ensure that a serialized job can be unserialized and executed
*/
public function testRunSerializedJob()
{
$this->mockRequest();

/** @var BatchMetadataChangedJob $batchMetadataChangedJob */
$batchMetadataChangedJob = unserialize($this->serializedJobData);

/**
* @disregard P1013 PHP Intelephense error suppression
* @see https://github.com/bmewburn/vscode-intelephense/issues/568
*/
$publicationMock = Mockery::mock(\APP\publication\Publication::class)
->makePartial()
->shouldReceive('getData')
->with('authors')
->andReturn(\Illuminate\Support\LazyCollection::make([new \PKP\author\Author()]))
->shouldReceive('getData')
->with('subject')
->andReturn([])
->shouldReceive('getData')
->with('subjects')
->andReturn([])
->shouldReceive('getData')
->with('keywords')
->andReturn([])
->shouldReceive('getData')
->with('disciplines')
->andReturn([])
->getMock();

$submissionMock = Mockery::mock(\APP\submission\Submission::class)
->makePartial()
->shouldReceive(['getCurrentPublication' => $publicationMock,])
->shouldReceive('getData')
->with('doiObject')
->andReturn(new \PKP\doi\Doi())
->getMock();

$submissionRepoMock = Mockery::mock(app(SubmissionRepository::class))
->makePartial()
->shouldReceive('get')
->withAnyArgs()
->andReturn($submissionMock)
->getMock();

app()->instance(SubmissionRepository::class, $submissionRepoMock);

$submissionSearchDAOMock = Mockery::mock(\PKP\search\SubmissionSearchDAO::class)
->makePartial()
->shouldReceive(['insertObject' => 0, 'insertObjectKeywords' => null,])
->withAnyArgs()
->getMock();

DAORegistry::registerDAO('ArticleSearchDAO', $submissionSearchDAOMock); // for OJS
DAORegistry::registerDAO('MonographSearchDAO', $submissionSearchDAOMock); // for OMP
DAORegistry::registerDAO('PreprintSearchDAO', $submissionSearchDAOMock); // for OPS

$this->assertNull($batchMetadataChangedJob->handle());
}
}
Binary file modified tests/jobs/metadata/MetadataChangedJobTest.php
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added tests/jobs/statistics/RemoveDoubleClicksTest.php
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified tests/jobs/submissions/UpdateSubmissionSearchJobTest.php
Binary file not shown.

0 comments on commit b254f09

Please sign in to comment.