Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fredden committed Dec 29, 2023
1 parent 23d500e commit a90dbd8
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 43 deletions.
4 changes: 2 additions & 2 deletions Api/ScheduleRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ public function getList(
/**
* Store the given model to the database
*
* @param \Magento\Cron\Model\Schedule $schedule
* @param \Magento\Cron\Model\Schedule|\EthanYehuda\CronjobManager\Model\Data\Schedule $schedule
*
* @return \Magento\Cron\Model\Schedule
* @throws CouldNotSaveException
*/
public function save(\Magento\Cron\Model\Schedule $schedule): \Magento\Cron\Model\Schedule;
public function save(\Magento\Cron\Model\Schedule|\EthanYehuda\CronjobManager\Model\Data\Schedule $schedule): \Magento\Cron\Model\Schedule;

/**
* Delete the given model from the database
Expand Down
10 changes: 2 additions & 8 deletions Console/Command/KillJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

namespace EthanYehuda\CronjobManager\Console\Command;

use EthanYehuda\CronjobManager\Api\Data\ScheduleInterface;
use Magento\Framework\App\State;
use Magento\Framework\App\Area;
use Magento\Framework\Console\Cli;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Api\Search\FilterGroupBuilder;
use Magento\Framework\Api\FilterBuilder;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\Api\SearchCriteria;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
Expand Down Expand Up @@ -119,7 +119,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
if ($optionProcKill) {
$killed = $this->processManagement->killPid($pid, $job->getHostname());
if ($killed) {
$job->setStatus(Schedule::STATUS_KILLED);
$job->setStatus(ScheduleInterface::STATUS_KILLED);
$this->scheduleRepository->save($job);
}
} else {
Expand Down Expand Up @@ -159,34 +159,28 @@ protected function execute(InputInterface $input, OutputInterface $output)
*/
private function loadRunningJobsByCode(string $jobCode): array
{
/** @var AbstractSimpleObject $jobCode */
$jobCodeFilter = $this->filterBuilder
->setField(Schedule::KEY_JOB_CODE)
->setConditionType('eq')
->setValue($jobCode)
->create();
/** @var AbstractSimpleObject $jobCodeFilterGroup */
$jobCodeFilterGroup = $this->filterGroupBuilder
->addFilter($jobCodeFilter)
->create();

/** @var AbstractSimpleObject $jobCode */
$statusFilter = $this->filterBuilder
->setField(Schedule::KEY_STATUS)
->setConditionType('eq')
->setValue(Schedule::STATUS_RUNNING)
->create();
/** @var AbstractSimpleObject $statusFilterGroup */
$statusFilterGroup = $this->filterGroupBuilder
->addFilter($statusFilter)
->create();

/** @var SearchCriteria $searchCriteria */
$searchCriteria = $this->searchCriteriaBuilder->setFilterGroups(
[$jobCodeFilterGroup, $statusFilterGroup]
)->create();

/** @var \Magento\Framework\Api\SearchResultsInterface $result */
$result = $this->scheduleRepository->getList($searchCriteria);
return $result->getItems();
}
Expand Down
2 changes: 1 addition & 1 deletion Model/ScheduleRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCr
/**
* @inheritDoc
*/
public function save(Schedule $schedule): Schedule
public function save(Schedule|Data\Schedule $schedule): Schedule
{
try {
$this->scheduleResource->save($schedule);
Expand Down
48 changes: 17 additions & 31 deletions Test/Unit/Console/Command/KillJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,34 +56,20 @@ protected function setUp(): void
State::MODE_PRODUCTION
])->getMock();

$this->mockScheduleRepository = $this->getMockBuilder(ScheduleRepositoryInterface::class)
->disableOriginalConstructor()
->setMethodsExcept([])
->getMock();

$this->mockScheduleManagement = $this->getMockBuilder(ScheduleManagementInterface::class)
->disableOriginalConstructor()
->setMethods(['kill'])
->getMock();

$this->mockSearchCriteriaBuilder = $this->getMockBuilder(SearchCriteriaBuilder::class)
->disableOriginalConstructor()
->setMethods(['create'])
->getMock();

$this->mockFilterBuilder = $this->getMockBuilder(FilterBuilder::class)
->disableOriginalConstructor()
->setMethods(['create'])
->getMock();

$this->mockFilterGroupBuilder = $this->getMockBuilder(FilterGroupBuilder::class)
->disableOriginalConstructor()
->setMethods(['create'])
->getMock();

$this->mockProcessManagement = $this->getMockBuilder(ProcessManagement::class)
->disableOriginalConstructor()
->getMock();
$this->mockScheduleRepository = $this->createMock(ScheduleRepositoryInterface::class);
$this->mockScheduleManagement = $this->createMock(ScheduleManagementInterface::class);
$this->mockProcessManagement = $this->createMock(ProcessManagement::class);

$this->mockFilterBuilder = $this->createMock(FilterBuilder::class);
$this->mockFilterBuilder->method('setField')->willReturnSelf();
$this->mockFilterBuilder->method('setConditionType')->willReturnSelf();
$this->mockFilterBuilder->method('setValue')->willReturnSelf();

$this->mockFilterGroupBuilder = $this->createMock(FilterGroupBuilder::class);
$this->mockFilterGroupBuilder->method('addFilter')->willReturnSelf();

$this->mockSearchCriteriaBuilder = $this->createMock(SearchCriteriaBuilder::class);
$this->mockSearchCriteriaBuilder->method('setFilterGroups')->willReturnSelf();

$this->command = new KillJob(
$this->mockState,
Expand Down Expand Up @@ -166,7 +152,6 @@ public function testExecuteWithProcKillFlag()
public function testExecuteWithKillFailures()
{
$numOfSchedules = 3;
/** @var Schedule[] $mockedSchedules */
$mockedSchedules = $this->mockMultipleSchedules($numOfSchedules);

$this->mockQueryResults($mockedSchedules);
Expand Down Expand Up @@ -203,7 +188,6 @@ public function testExecuteWithKillFailures()
public function testExecuteUsingProcKillWithKillFailures()
{
$numOfSchedules = 3;
/** @var Schedule[] $mockedSchedules */
$mockedSchedules = $this->mockMultipleSchedules($numOfSchedules);

$this->mockQueryResults($mockedSchedules);
Expand Down Expand Up @@ -242,7 +226,6 @@ public function testExecuteUsingProcKillWithKillFailures()
public function testExecuteWithPartialKillFailures()
{
$numOfSchedules = 2;
/** @var Schedule[] $mockedSchedules */
$mockedSchedules = $this->mockMultipleSchedules($numOfSchedules);

$this->mockQueryResults($mockedSchedules);
Expand Down Expand Up @@ -304,6 +287,9 @@ private function mockQueryResults($queryResults)
->willReturn($queryResults);
}

/**
* @return Schedule[]
*/
private function mockMultipleSchedules(int $numOfSchedules): array
{
$mockSchedules = [];
Expand Down
2 changes: 1 addition & 1 deletion Test/Util/FakeClock.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function setTimestamp(int $timestamp): void

public function advance(string $expression): void
{
$this->timestamp = strtotime("+$expression", $this->timestamp);
$this->timestamp = (int) strtotime("+$expression", $this->timestamp);
}

public function now(): int
Expand Down

0 comments on commit a90dbd8

Please sign in to comment.