Skip to content

Commit f73710b

Browse files
Upgraded the library to require at least PHP8.1 and illuminate/queue 10.0 (digbang#3)
* Upgraded the library to require at least PHP8.1 and illuminate/queue 10.0 - Upgraded the laravel-doctrine/orm package to version 2.0 - Require phpunit version 10 and migrated the phpunit.xml.dist to the newest schema - Merged the Throwable catch branch with the Exception catch branch in the Worker.php as it used an exception class that is no longer available - Added the MockeryPHPUnitIntegration trait to the testcases to allow Mockery to influence the expectation count - Changed the namespace of the test cases to Digbang\SafeQueue\Tests - DoctrineQueueProvider now implements DeferrableProvider instead of using the $defer property - Added some return type (non-exhaustive) * Ran phpcsfixer with default configuration
1 parent d225c9d commit f73710b

File tree

8 files changed

+53
-53
lines changed

8 files changed

+53
-53
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
.DS_Store
44
.php_cs.cache
55
composer.lock
6+
/.phpunit.cache/

composer.json

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,21 @@
1010
{
1111
"name": "Dario Govergun",
1212
"email": "dgovergun@digbang.com"
13+
},
14+
{
15+
"name": "DIJ Digital",
16+
"email": "support@dij.digital"
1317
}
1418
],
1519
"require": {
16-
"php": "^7.4 || ^8.0.2",
17-
"illuminate/queue": "^8.0 || ^9.0",
18-
"laravel-doctrine/orm": "^1.7"
20+
"php": "^8.1",
21+
"illuminate/queue": "^10.0",
22+
"laravel-doctrine/orm": "^2.0"
1923
},
2024
"require-dev": {
21-
"mockery/mockery": "^1.5",
22-
"phpunit/phpunit": "^8.5 || ^9.5",
23-
"friendsofphp/php-cs-fixer": "^3.6"
25+
"mockery/mockery": "^1.6",
26+
"phpunit/phpunit": "^10.0",
27+
"friendsofphp/php-cs-fixer": "^3.40"
2428
},
2529
"autoload": {
2630
"psr-4": {
@@ -29,7 +33,7 @@
2933
},
3034
"autoload-dev": {
3135
"psr-4": {
32-
"test\\Digbang\\SafeQueue\\": "tests/"
36+
"Digbang\\SafeQueue\\Tests\\": "tests/"
3337
}
3438
}
3539
}

phpunit.xml.dist

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false" bootstrap="vendor/autoload.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
3-
<coverage>
4-
<include>
5-
<directory suffix=".php">src/</directory>
6-
</include>
7-
</coverage>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="vendor/autoload.php" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
3+
<coverage/>
84
<testsuites>
95
<testsuite name="Full Test Suite">
106
<directory>./tests/</directory>
117
</testsuite>
128
</testsuites>
139
<php>
1410
</php>
11+
<source>
12+
<include>
13+
<directory suffix=".php">src/</directory>
14+
</include>
15+
</source>
1516
</phpunit>

src/Console/WorkCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
class WorkCommand extends IlluminateWorkCommand
1010
{
11-
const SIGNATURE_REGEX_PATTERN = '/([\w:-]+)(?=\s|\{)/i';
11+
public const SIGNATURE_REGEX_PATTERN = '/([\w:-]+)(?=\s|\{)/i';
1212

1313
/**
1414
* The console command description.
@@ -31,7 +31,7 @@ public function __construct(Worker $worker, Cache $cache, $config)
3131
parent::__construct($worker, $cache);
3232
}
3333

34-
public function renameCommandInSignature($commandName)
34+
public function renameCommandInSignature($commandName): void
3535
{
3636
if ($commandName) {
3737
/**

src/DoctrineQueueProvider.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,22 @@
22

33
namespace Digbang\SafeQueue;
44

5+
use Illuminate\Contracts\Support\DeferrableProvider;
56
use Illuminate\Support\ServiceProvider;
67
use Digbang\SafeQueue\Console\WorkCommand;
78
use Illuminate\Contracts\Cache\Repository as Cache;
8-
use Illuminate\Contracts\Debug\ExceptionHandler;
99

1010
/**
1111
* @codeCoverageIgnore
1212
*/
13-
class DoctrineQueueProvider extends ServiceProvider
13+
class DoctrineQueueProvider extends ServiceProvider implements DeferrableProvider
1414
{
15-
protected $defer = true;
16-
1715
/**
1816
* Register the service provider.
1917
*
2018
* @return void
2119
*/
22-
public function register()
20+
public function register(): void
2321
{
2422
if (!$this->isLumen()) {
2523
$this->publishes([
@@ -35,15 +33,15 @@ public function register()
3533
$this->registerWorker();
3634
}
3735

38-
public function boot()
36+
public function boot(): void
3937
{
4038
$this->commands('command.safeQueue.work');
4139
}
4240

4341
/**
4442
* @return void
4543
*/
46-
protected function registerWorker()
44+
protected function registerWorker(): void
4745
{
4846
$this->registerWorkCommand();
4947

@@ -65,7 +63,7 @@ protected function registerWorker()
6563
/**
6664
* @return void
6765
*/
68-
protected function registerWorkCommand()
66+
protected function registerWorkCommand(): void
6967
{
7068
$this->app->singleton('command.safeQueue.work', function ($app) {
7169
return new WorkCommand(
@@ -81,7 +79,7 @@ protected function registerWorkCommand()
8179
*
8280
* @return array
8381
*/
84-
public function provides()
82+
public function provides(): array
8583
{
8684
return [
8785
'safeQueue.worker',
@@ -92,8 +90,8 @@ public function provides()
9290
/**
9391
* @return bool
9492
*/
95-
protected function isLumen()
93+
protected function isLumen(): bool
9694
{
97-
return strpos($this->app->version(), 'Lumen') !== false;
95+
return str_contains($this->app->version(), 'Lumen');
9896
}
9997
}

src/Worker.php

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,17 @@
22

33
namespace Digbang\SafeQueue;
44

5-
use Doctrine\ORM\EntityManagerInterface;
65
use Doctrine\Persistence\ManagerRegistry;
76
use Exception;
87
use Illuminate\Contracts\Debug\ExceptionHandler;
98
use Illuminate\Contracts\Events\Dispatcher;
10-
use Illuminate\Contracts\Container\Container;
119
use Illuminate\Queue\QueueManager;
1210
use Illuminate\Queue\Worker as IlluminateWorker;
13-
use Illuminate\Queue\WorkerOptions;
14-
use Symfony\Component\Debug\Exception\FatalThrowableError;
1511
use Throwable;
1612

1713
class Worker extends IlluminateWorker
1814
{
19-
/**
20-
* @var ManagerRegistry
21-
*/
22-
protected $managerRegistry;
15+
protected ManagerRegistry $managerRegistry;
2316

2417
/**
2518
* Worker constructor.
@@ -28,7 +21,7 @@ class Worker extends IlluminateWorker
2821
* @param Dispatcher $events
2922
* @param ManagerRegistry $managerRegistry
3023
* @param ExceptionHandler $exceptions
31-
* @param \callable $isDownForMaintenance
24+
* @param callable $isDownForMaintenance
3225
*/
3326
public function __construct(
3427
QueueManager $manager,
@@ -62,10 +55,8 @@ protected function getNextJob($connection, $queue)
6255
$this->assertGoodDatabaseConnection();
6356
} catch (EntityManagerClosedException $e) {
6457
$exception = $e;
65-
} catch (Exception $e) {
58+
} catch (Exception|Throwable $e) {
6659
$exception = new QueueSetupException("Error in queue setup while getting next job", 0, $e);
67-
} catch (Throwable $e) {
68-
$exception = new QueueSetupException("Error in queue setup while getting next job", 0, new FatalThrowableError($e));
6960
}
7061

7162
if ($exception) {
@@ -85,7 +76,7 @@ private function assertEntityManagerOpen()
8576
{
8677
foreach ($this->managerRegistry->getManagers() as $entityManager) {
8778
if (!$entityManager->isOpen()) {
88-
throw new EntityManagerClosedException;
79+
throw new EntityManagerClosedException();
8980
}
9081
}
9182
}

tests/Console/WorkCommandTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<?php
22

3+
declare(strict_types=1);
34

4-
namespace tests\Digbang\SafeQueue\Console;
5+
namespace Digbang\SafeQueue\Tests\Console;
56

67
use Mockery as m;
78
use ReflectionClass;
@@ -12,6 +13,8 @@
1213

1314
class WorkCommandTest extends TestCase
1415
{
16+
use m\Adapter\Phpunit\MockeryPHPUnitIntegration;
17+
1518
/**
1619
* @var Worker|m\MockInterface
1720
*/

tests/WorkerTest.php

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<?php
22

3+
declare(strict_types=1);
34

4-
namespace tests\Digbang\SafeQueue;
5+
namespace Digbang\SafeQueue\Tests;
56

67
use Doctrine\DBAL\Connection;
78
use Doctrine\ORM\EntityManager;
@@ -21,6 +22,8 @@
2122

2223
class WorkerTest extends TestCase
2324
{
25+
use m\Adapter\Phpunit\MockeryPHPUnitIntegration;
26+
2427
/**
2528
* @var QueueManager|m\MockInterface
2629
*/
@@ -81,7 +84,7 @@ protected function setUp(): void
8184
$this->cache = m::mock(Repository::class);
8285
$this->exceptions = m::mock(Handler::class);
8386
$this->managerRegistry = m::mock(ManagerRegistry::class);
84-
$isDownForMaintenance= function () {
87+
$isDownForMaintenance = function () {
8588
return false;
8689
};
8790

@@ -96,12 +99,7 @@ protected function setUp(): void
9699
$this->entityManager->shouldReceive('getConnection')->andReturn($this->dbConnection);
97100
}
98101

99-
protected function tearDown(): void
100-
{
101-
m::close();
102-
}
103-
104-
protected function prepareToRunJob($job)
102+
protected function prepareToRunJob($job): void
105103
{
106104
if ($job instanceof Job) {
107105
$jobs = [$job];
@@ -114,6 +112,7 @@ protected function prepareToRunJob($job)
114112
$this->queueManager->shouldReceive('getName')->andReturn('test');
115113

116114
$this->queue->shouldReceive('pop')->andReturn(...$jobs);
115+
$this->queue->shouldReceive('getConnectionName')->andReturn('test');
117116
}
118117

119118
public function testExtendsLaravelWorker()
@@ -123,8 +122,7 @@ public function testExtendsLaravelWorker()
123122

124123
public function testChecksEmState()
125124
{
126-
$job = m::mock(Job::class);
127-
$job->shouldReceive('fire')->once();
125+
$job = m::spy(Job::class);
128126
$job->shouldIgnoreMissing();
129127

130128
$this->prepareToRunJob($job);
@@ -143,11 +141,13 @@ public function testChecksEmState()
143141
$this->managerRegistry->shouldReceive('getManagers')->andReturn(array($this->entityManager));
144142

145143
$this->worker->runNextJob('connection', 'queue', $this->options);
144+
145+
$job->shouldHaveReceived('fire')->once();
146146
}
147147

148-
public function testMultipleEntityManagers() {
149-
$job = m::mock(Job::class);
150-
$job->shouldReceive('fire')->once();
148+
public function testMultipleEntityManagers()
149+
{
150+
$job = m::spy(Job::class);
151151
$job->shouldIgnoreMissing();
152152

153153
$this->prepareToRunJob($job);
@@ -172,5 +172,7 @@ public function testMultipleEntityManagers() {
172172
$this->managerRegistry->shouldReceive('getManagers')->andReturn(array($this->entityManager, $secondManager));
173173

174174
$this->worker->runNextJob('connection', 'queue', $this->options);
175+
176+
$job->shouldHaveReceived('fire')->once();
175177
}
176178
}

0 commit comments

Comments
 (0)