Skip to content
This repository has been archived by the owner on Dec 9, 2022. It is now read-only.

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bupy7 committed Aug 18, 2017
1 parent 2bceb0d commit 12fff8e
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 49 deletions.
46 changes: 46 additions & 0 deletions src/Entity/TaskInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Bupy7\Queue\Entity;

use DateTime;

/**
* @author Vasily Belosludcev <https://github.com/bupy7>
*/
interface TaskInterface
{
public const STATUS_WAIT = 10;
public const STATUS_ERROR = 20;
public const STATUS_OK = 30;
public const STATUS_IMPOSSIBLE = 40;

public function setId(int $id): TaskInterface;

public function getId(): int;

public function setName(string $name): TaskInterface;

public function getName(): string;

public function setStatusId(int $statusId): TaskInterface;

public function getStatusId(): int;

public function setCreatedAt(DateTime $createdAt): TaskInterface;

public function getCreatedAt(): DateTime;

public function setRunAt(DateTime $runAt): TaskInterface;

public function getRunAt(): ?DateTime;

public function setStopAt(DateTime $stopAt): TaskInterface;

public function getStopAt(): ?DateTime;

public function incNumberErrors(): TaskInterface;

public function setNumberErrors(int $numberErrors): TaskInterface;

public function getNumberErrors(): int;
}
12 changes: 0 additions & 12 deletions src/Exception/InvalidArgumentException.php

This file was deleted.

14 changes: 7 additions & 7 deletions src/Service/QueueService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Bupy7\Queue\Service;

use Bupy7\Queue\Repository\TaskRepositoryInterface;
use Bupy7\Queue\Entity\Task as TaskEntity;
use Bupy7\Queue\Entity\TaskInterface as TaskEntityInterface;
use Bupy7\Queue\Manager\EntityManagerInterface;
use Bupy7\Queue\Manager\QueueManager;
use DateTime;
Expand Down Expand Up @@ -47,16 +47,16 @@ public function run(): void
$entities = $this->taskRepository->findForRun($this->config->getOneTimeLimit() ?: null);
foreach ($entities as $entity) {
if (in_array($entity->getStatusId(), [
TaskEntity::STATUS_WAIT,
TaskEntity::STATUS_ERROR,
TaskEntityInterface::STATUS_WAIT,
TaskEntityInterface::STATUS_ERROR,
])) {
$this->executeTask($entity);
}
}
$this->entityManager->flush();
}

protected function executeTask(TaskEntity $entity): void
protected function executeTask(TaskEntityInterface $entity): void
{
if (!$this->queueManager->has($entity->getName())) {
throw new UnknownTaskException(sprintf('"%s task is unknown."', $entity->getName()));
Expand All @@ -65,16 +65,16 @@ protected function executeTask(TaskEntity $entity): void
$task = $this->queueManager->get($entity->getName());
$entity->setRunAt(new DateTime);
if ($task->execute()) {
$entity->setStatusId(TaskEntity::STATUS_OK);
$entity->setStatusId(TaskEntityInterface::STATUS_OK);
} else {
$entity->incNumberErrors();
if (
$this->config->getErrorLimit() !== 0
&& $entity->getNumberErrors() >= $this->config->getErrorLimit()
) {
$entity->setStatusId(TaskEntity::STATUS_IMPOSSIBLE);
$entity->setStatusId(TaskEntityInterface::STATUS_IMPOSSIBLE);
} else {
$entity->setStatusId(TaskEntity::STATUS_ERROR);
$entity->setStatusId(TaskEntityInterface::STATUS_ERROR);
}
}
$entity->setStopAt(new DateTime);
Expand Down
35 changes: 35 additions & 0 deletions src/Service/TaskService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Bupy7\Queue\Service;

use Bupy7\Queue\Entity\TaskInterface;
use DateTime;
use Bupy7\Queue\Manager\EntityManagerInterface;

class TaskService
{
private const DEFAULT_NUMBER_ERRORS = 0;

/**
* @var EntityManagerInterface
*/
protected $entityManager;

public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}

public function create(array $data, TaskInterface $task): bool
{
if (!isset($data['name'])) {
return false;
}
$task->setStatusId(TaskInterface::STATUS_WAIT)
->setCreatedAt(new DateTime)
->setNumberErrors(self::DEFAULT_NUMBER_ERRORS)
->setName($data['name']);
$this->entityManager->persist($task);
$this->entityManager->flush();
}
}
32 changes: 17 additions & 15 deletions src/Entity/Task.php → test/Assert/Entity/Task.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
<?php

namespace Bupy7\Queue\Entity;
namespace Bupy7\Queue\Test\Assert\Entity;

use DateTime;
use Bupy7\Queue\Exception\InvalidArgumentException;
use Bupy7\Queue\Entity\TaskInterface;

/**
* @author Vasily Belosludcev <https://github.com/bupy7>
*/
class Task
class Task implements TaskInterface
{
public const STATUS_WAIT = 10;
public const STATUS_ERROR = 20;
public const STATUS_OK = 30;
public const STATUS_IMPOSSIBLE = 40;

/**
* @var int
*/
Expand Down Expand Up @@ -49,7 +45,7 @@ public function __construct()
$this->createdAt = new DateTime;
}

public function setId(int $id): Task
public function setId(int $id): TaskInterface
{
$this->id = $id;
return $this;
Expand All @@ -60,7 +56,7 @@ public function getId(): int
return $this->id;
}

public function setName(string $name): Task
public function setName(string $name): TaskInterface
{
$this->name = $name;
return $this;
Expand All @@ -71,7 +67,7 @@ public function getName(): string
return $this->name;
}

public function setStatusId(int $statusId): Task
public function setStatusId(int $statusId): TaskInterface
{
if (!in_array($statusId, [
self::STATUS_WAIT,
Expand All @@ -90,7 +86,7 @@ public function getStatusId(): int
return $this->statusId;
}

public function setCreatedAt(DateTime $createdAt): Task
public function setCreatedAt(DateTime $createdAt): TaskInterface
{
$this->createdAt = $createdAt;
return $this;
Expand All @@ -101,7 +97,7 @@ public function getCreatedAt(): DateTime
return $this->createdAt;
}

public function setRunAt(DateTime $runAt): Task
public function setRunAt(DateTime $runAt): TaskInterface
{
$this->runAt = $runAt;
return $this;
Expand All @@ -112,7 +108,7 @@ public function getRunAt(): ?DateTime
return $this->runAt;
}

public function setStopAt(DateTime $stopAt): Task
public function setStopAt(DateTime $stopAt): TaskInterface
{
$this->stopAt = $stopAt;
return $this;
Expand All @@ -123,9 +119,15 @@ public function getStopAt(): ?DateTime
return $this->stopAt;
}

public function incNumberErrors(int $inc = 1): Task
public function incNumberErrors(): TaskInterface
{
++$this->numberErrors;
return $this;
}

public function setNumberErrors(int $numberErrors): TaskInterface
{
$this->numberErrors += $inc;
$this->numberErrors = $numberErrors;
return $this;
}

Expand Down
4 changes: 2 additions & 2 deletions test/Assert/Repository/MemoryTaskRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Bupy7\Queue\Test\Assert\Repository;

use Bupy7\Queue\Repository\TaskRepositoryInterface;
use Bupy7\Queue\Entity\Task;
use Bupy7\Queue\Test\Assert\Entity\Task;

class MemoryTaskRepository implements TaskRepositoryInterface
{
Expand All @@ -18,7 +18,7 @@ public function __construct()
(new Task)->setId(1)->setName('Bupy7\Queue\Test\Assert\Task\ErrorHelloTask'),
(new Task)->setId(2)->setName('Bupy7\Queue\Test\Assert\Task\SuccessHelloTask'),
(new Task)->setId(3)
->incNumberErrors(4)
->setNumberErrors(4)
->setStatusId(Task::STATUS_ERROR)
->setName('Bupy7\Queue\Test\Assert\Task\ErrorHelloTask'),
];
Expand Down
5 changes: 2 additions & 3 deletions test/AppTrait.php → test/Functional/AppTrait.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Bupy7\Queue\Test;
namespace Bupy7\Queue\Test\Functional;

use Zend\ServiceManager\ServiceManager;
use Zend\Test\Util\ModuleLoader;
Expand All @@ -19,8 +19,7 @@ protected function getSm(array $config = []): ServiceManager
],
'module_listener_options' => [
'config_glob_paths' => [
__DIR__ . '/../config/module.config.php',
__DIR__ . '/config/module.config.php',
__DIR__ . '/_file/config/module.config.php',
],
'extra_config' => $config,
'module_paths' => [],
Expand Down
2 changes: 1 addition & 1 deletion test/ModuleTest.php → test/Functional/ModuleTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Bupy7\Queue\Test;
namespace Bupy7\Queue\Test\Functional;

use PHPUnit\Framework\TestCase;
use Zend\Test\Util\ModuleLoader;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<?php

namespace Bupy7\Queue\Test\Options;
namespace Bupy7\Queue\Test\Functional\Options;

use PHPUnit\Framework\TestCase;
use Bupy7\Queue\Options\ModuleOptions;
use Bupy7\Queue\Test\AppTrait;
use Queue\Hydrator\ArrayHydrator;
use Bupy7\Queue\Test\Functional\AppTrait;

/**
* @author Vasily Belosludcev <https://github.com/bupy7>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

namespace Bupy7\Queue\Test\Service;
namespace Bupy7\Queue\Test\Functional\Service;

use PHPUnit\Framework\TestCase;
use Bupy7\Queue\Test\AppTrait;
use Bupy7\Queue\Entity\Task;
use Bupy7\Queue\Test\Functional\AppTrait;
use Bupy7\Queue\Entity\TaskInterface;

/**
* @author Belosludcev Vasily <https://github.com/bupy7>
Expand All @@ -20,13 +20,13 @@ public function testRun()
$queueService = $sm->get('MemoryQueueService');
$queueService->run();

$tasks = $sm->get('MemoryTaskRepository')->findByStatusId(Task::STATUS_ERROR);
$tasks = $sm->get('MemoryTaskRepository')->findByStatusId(TaskInterface::STATUS_ERROR);
$this->assertEquals(1, count($tasks));

$tasks = $sm->get('MemoryTaskRepository')->findByStatusId(Task::STATUS_OK);
$tasks = $sm->get('MemoryTaskRepository')->findByStatusId(TaskInterface::STATUS_OK);
$this->assertEquals(1, count($tasks));

$tasks = $sm->get('MemoryTaskRepository')->findByStatusId(Task::STATUS_IMPOSSIBLE);
$tasks = $sm->get('MemoryTaskRepository')->findByStatusId(TaskInterface::STATUS_IMPOSSIBLE);
$this->assertEquals(1, count($tasks));
}
}

0 comments on commit 12fff8e

Please sign in to comment.