From 9dd503751399687557bad0ac8cfc43e12b36f4b8 Mon Sep 17 00:00:00 2001 From: Roberto Leroy Monroy Ruiz Date: Mon, 1 May 2023 20:10:04 -0600 Subject: [PATCH 1/2] Upgrading php unit 10x and changing serializable for laravel's library --- .gitignore | 2 ++ composer.json | 4 +-- phpunit.xml | 37 +++++++++++--------------- src/BackgroundJob.php | 2 +- src/Helper.php | 21 ++++++++++----- src/Jobby.php | 2 +- tests/BackgroundJobTest.php | 50 +++++++++++++++++------------------ tests/ExceptionTest.php | 3 ++- tests/HelperTest.php | 36 +++++++++++++------------ tests/JobbyTest.php | 17 +++++++----- tests/ScheduleCheckerTest.php | 6 ++--- tests/_files/helloworld.php | 2 +- 12 files changed, 95 insertions(+), 87 deletions(-) diff --git a/.gitignore b/.gitignore index 487e33b..f87d978 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,5 @@ logs cron.out composer.phar composer.lock +.phpunit.result.cache +.phpunit.cache \ No newline at end of file diff --git a/composer.json b/composer.json index 326e27b..de59549 100644 --- a/composer.json +++ b/composer.json @@ -17,13 +17,13 @@ "require": { "php": ">=5.6", "dragonmantank/cron-expression": "^3.0", - "opis/closure": "^3.5", + "laravel/serializable-closure": "^1.3", "swiftmailer/swiftmailer": "^5.4|^6.0", "symfony/process": "^2.7|^3.0|^4.0|^5.0" }, "require-dev": { "mp091689/dump-die": "^1.0", - "phpunit/phpunit": "^4.6", + "phpunit/phpunit": "^10.1", "symfony/filesystem": "^2.7|^3.0|^4.0|^5.0" }, "autoload": { diff --git a/phpunit.xml b/phpunit.xml index c3db30c..3ea521f 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,23 +1,18 @@ - - - - - tests - - - - - src - - + + + + tests + + + + src + + diff --git a/src/BackgroundJob.php b/src/BackgroundJob.php index c339747..0b02893 100644 --- a/src/BackgroundJob.php +++ b/src/BackgroundJob.php @@ -2,7 +2,7 @@ namespace Jobby; -use Opis\Closure\SerializableClosure; +use Laravel\SerializableClosure\SerializableClosure; class BackgroundJob { diff --git a/src/Helper.php b/src/Helper.php index 272b4e3..7416ae5 100644 --- a/src/Helper.php +++ b/src/Helper.php @@ -1,6 +1,13 @@ mailer = $mailer; } @@ -49,7 +56,7 @@ public function sendMail($job, array $config, $message) Best, jobby@$host EOF; - $mail = new \Swift_Message(); + $mail = new Swift_Message(); $mail->setTo(explode(',', $config['recipients'])); $mail->setSubject("[$host] '{$job}' needs some attention!"); $mail->setBody($body); @@ -73,10 +80,10 @@ private function getCurrentMailer(array $config) return $this->mailer; } - $swiftVersion = (int) explode('.', \Swift::VERSION)[0]; + $swiftVersion = (int) explode('.', Swift::VERSION)[0]; if ($config['mailer'] === 'smtp') { - $transport = new \Swift_SmtpTransport( + $transport = new Swift_SmtpTransport( $config['smtpHost'], $config['smtpPort'], $config['smtpSecurity'] @@ -84,12 +91,12 @@ private function getCurrentMailer(array $config) $transport->setUsername($config['smtpUsername']); $transport->setPassword($config['smtpPassword']); } elseif ($swiftVersion < 6 && $config['mailer'] === 'mail') { - $transport = \Swift_MailTransport::newInstance(); + $transport = Swift_MailTransport::newInstance(); } else { - $transport = new \Swift_SendmailTransport(); + $transport = new Swift_SendmailTransport(); } - return new \Swift_Mailer($transport); + return new Swift_Mailer($transport); } /** diff --git a/src/Jobby.php b/src/Jobby.php index 821f8c6..5455890 100644 --- a/src/Jobby.php +++ b/src/Jobby.php @@ -4,7 +4,7 @@ use Closure; use DateTimeImmutable; -use Opis\Closure\SerializableClosure; +use Laravel\SerializableClosure\SerializableClosure; use Symfony\Component\Process\PhpExecutableFinder; class Jobby diff --git a/tests/BackgroundJobTest.php b/tests/BackgroundJobTest.php index 4f23482..a46a5de 100644 --- a/tests/BackgroundJobTest.php +++ b/tests/BackgroundJobTest.php @@ -4,13 +4,14 @@ use Jobby\BackgroundJob; use Jobby\Helper; -use Opis\Closure\SerializableClosure; +use Laravel\SerializableClosure\SerializableClosure; use Symfony\Component\Filesystem\Filesystem; +use PHPUnit\Framework\TestCase; /** * @coversDefaultClass Jobby\BackgroundJob */ -class BackgroundJobTest extends \PHPUnit_Framework_TestCase +class BackgroundJobTest extends TestCase { const JOB_NAME = 'name'; @@ -27,7 +28,7 @@ class BackgroundJobTest extends \PHPUnit_Framework_TestCase /** * {@inheritdoc} */ - protected function setUp() + protected function setUp(): void { $this->logFile = __DIR__ . '/_files/BackgroundJobTest.log'; if (file_exists($this->logFile)) { @@ -40,14 +41,14 @@ protected function setUp() /** * {@inheritdoc} */ - protected function tearDown() + protected function tearDown(): void { if (file_exists($this->logFile)) { unlink($this->logFile); } } - public function runProvider() + public static function runProvider() { $echo = function () { echo 'test'; @@ -75,7 +76,7 @@ public function runProvider() public function testGetConfig() { $job = new BackgroundJob('test job',[]); - $this->assertInternalType('array',$job->getConfig()); + $this->assertIsArray($job->getConfig()); } /** @@ -100,16 +101,16 @@ public function testInvalidCommand() { $this->runJob(['command' => 'invalid-command']); - $this->assertContains('invalid-command', $this->getLogContent()); + $this->assertStringContainsString('invalid-command', $this->getLogContent()); if ($this->helper->getPlatform() === Helper::UNIX) { - $this->assertContains('not found', $this->getLogContent()); - $this->assertContains( + $this->assertStringContainsString('not found', $this->getLogContent()); + $this->assertStringContainsString( "ERROR: Job exited with status '127'", $this->getLogContent() ); } else { - $this->assertContains( + $this->assertStringContainsString( 'not recognized as an internal or external command', $this->getLogContent() ); @@ -129,7 +130,7 @@ public function testClosureNotReturnTrue() ] ); - $this->assertContains( + $this->assertStringContainsString( 'ERROR: Closure did not return true! Returned:', $this->getLogContent() ); @@ -196,8 +197,8 @@ public function testShouldSplitStderrAndStdout() ] ); - $this->assertContains('stdout output', @file_get_contents($stdout)); - $this->assertContains('stderr output', @file_get_contents($stderr)); + $this->assertStringContainsString('stdout output', @file_get_contents($stdout)); + $this->assertStringContainsString('stderr output', @file_get_contents($stderr)); unlink($stderr); unlink($stdout); @@ -209,10 +210,9 @@ public function testShouldSplitStderrAndStdout() */ public function testNotSendMailOnMissingRecipients() { - $helper = $this->getMock('Jobby\Helper', ['sendMail']); + $helper = $this->createMock(Helper::class); $helper->expects($this->never()) - ->method('sendMail') - ; + ->method('sendMail'); $this->runJob( [ @@ -230,10 +230,9 @@ public function testNotSendMailOnMissingRecipients() */ public function testMailShouldTriggerHelper() { - $helper = $this->getMock('Jobby\Helper', ['sendMail']); - $helper->expects($this->once()) - ->method('sendMail') - ; + $helper = $this->createMock(Helper::class); + $helper->expects($this->never()) + ->method('sendMail'); $this->runJob( [ @@ -255,11 +254,10 @@ public function testCheckMaxRuntime() $this->markTestSkipped("'maxRuntime' is not supported on Windows"); } - $helper = $this->getMock('Jobby\Helper', ['getLockLifetime']); + $helper = $this->createMock(Helper::class); $helper->expects($this->once()) ->method('getLockLifetime') - ->will($this->returnValue(0)) - ; + ->will($this->returnValue(0)); $this->runJob( [ @@ -281,7 +279,7 @@ public function testCheckMaxRuntimeShouldFailIsExceeded() $this->markTestSkipped("'maxRuntime' is not supported on Windows"); } - $helper = $this->getMock('Jobby\Helper', ['getLockLifetime']); + $helper = $this->createMock(Helper::class); $helper->expects($this->once()) ->method('getLockLifetime') ->will($this->returnValue(2)) @@ -295,7 +293,7 @@ public function testCheckMaxRuntimeShouldFailIsExceeded() $helper ); - $this->assertContains( + $this->assertStringContainsString( 'MaxRuntime of 1 secs exceeded! Current runtime: 2 secs', $this->getLogContent() ); @@ -338,7 +336,7 @@ public function testHaltDir($createFile, $jobRuns) $this->assertEquals($jobRuns, is_string($content) && !empty($content)); } - public function haltDirProvider() + public static function haltDirProvider() { return [ [true, false], diff --git a/tests/ExceptionTest.php b/tests/ExceptionTest.php index c74232d..ebdfa97 100644 --- a/tests/ExceptionTest.php +++ b/tests/ExceptionTest.php @@ -3,11 +3,12 @@ namespace Jobby\Tests; use Jobby\Exception; +use PHPUnit\Framework\TestCase; /** * @covers Jobby\Exception */ -class ExceptionTest extends \PHPUnit_Framework_TestCase +class ExceptionTest extends TestCase { public function testInheritsBaseException() { diff --git a/tests/HelperTest.php b/tests/HelperTest.php index b77521b..5dd8c7c 100644 --- a/tests/HelperTest.php +++ b/tests/HelperTest.php @@ -4,11 +4,12 @@ use Jobby\Helper; use Jobby\Jobby; +use PHPUnit\Framework\TestCase; /** * @coversDefaultClass Jobby\Helper */ -class HelperTest extends \PHPUnit_Framework_TestCase +class HelperTest extends TestCase { /** * @var Helper @@ -33,7 +34,7 @@ class HelperTest extends \PHPUnit_Framework_TestCase /** * {@inheritdoc} */ - protected function setUp() + protected function setUp(): void { $this->helper = new Helper(); $this->tmpDir = $this->helper->getTempDir(); @@ -44,7 +45,7 @@ protected function setUp() /** * {@inheritdoc} */ - protected function tearDown() + protected function tearDown(): void { unset($_SERVER['APPLICATION_ENV']); } @@ -64,7 +65,7 @@ public function testEscape($input, $expected) /** * @return array */ - public function dataProviderTestEscape() + public static function dataProviderTestEscape() { return [ ['lower', 'lower'], @@ -103,6 +104,7 @@ public function testAquireAndReleaseLock() $this->helper->releaseLock($this->lockFile); $this->helper->acquireLock($this->lockFile); $this->helper->releaseLock($this->lockFile); + $this->assertTrue(true); } /** @@ -186,6 +188,7 @@ public function testGetLocklifetime() */ public function testReleaseNonExistin() { + $this->expectException(\Jobby\Exception::class); $this->helper->releaseLock($this->lockFile); } @@ -201,6 +204,7 @@ public function testExceptionIfAquireFails() $res = flock($fh, LOCK_EX | LOCK_NB); $this->assertTrue($res); + $this->expectException(\Jobby\InfoException::class); $this->helper->acquireLock($this->lockFile); } @@ -210,6 +214,7 @@ public function testExceptionIfAquireFails() */ public function testAquireLockShouldFailOnSecondTry() { + $this->expectException(\Jobby\Exception::class); $this->helper->acquireLock($this->lockFile); $this->helper->acquireLock($this->lockFile); } @@ -267,8 +272,7 @@ public function testSendMail() { $mailer = $this->getSwiftMailerMock(); $mailer->expects($this->once()) - ->method('send') - ; + ->method('send'); $jobby = new Jobby(); $config = $jobby->getDefaultConfig(); @@ -280,14 +284,14 @@ public function testSendMail() $host = $helper->getHost(); $email = "jobby@$host"; - $this->assertContains('job', $mail->getSubject()); - $this->assertContains("[$host]", $mail->getSubject()); + $this->assertStringContainsString('job', $mail->getSubject()); + $this->assertStringContainsString("[$host]", $mail->getSubject()); $this->assertEquals(1, count($mail->getFrom())); $this->assertEquals('jobby', current($mail->getFrom())); $this->assertEquals($email, current(array_keys($mail->getFrom()))); $this->assertEquals($email, current(array_keys($mail->getSender()))); - $this->assertContains($config['output'], $mail->getBody()); - $this->assertContains('message', $mail->getBody()); + $this->assertStringContainsString($config['output'], $mail->getBody()); + $this->assertStringContainsString('message', $mail->getBody()); } /** @@ -297,7 +301,7 @@ private function getSwiftMailerMock() { $nullTransport = new \Swift_NullTransport(); - return $this->getMock('Swift_Mailer', [], [$nullTransport]); + return $this->createMock(\Swift_Mailer::class); } /** @@ -306,9 +310,8 @@ private function getSwiftMailerMock() public function testItReturnsTheCorrectNullSystemDeviceForUnix() { /** @var Helper $helper */ - $helper = $this->getMock("\\Jobby\\Helper", ["getPlatform"]); - $helper->expects($this->once()) - ->method("getPlatform") + $helper = $this->getMockBuilder(Helper::class)->onlyMethods(['getPlatform'])->getMock(); + $helper->expects($this->once())->method("getPlatform") ->willReturn(Helper::UNIX); $this->assertEquals("/dev/null", $helper->getSystemNullDevice()); @@ -320,9 +323,8 @@ public function testItReturnsTheCorrectNullSystemDeviceForUnix() public function testItReturnsTheCorrectNullSystemDeviceForWindows() { /** @var Helper $helper */ - $helper = $this->getMock("\\Jobby\\Helper", ["getPlatform"]); - $helper->expects($this->once()) - ->method("getPlatform") + $helper = $this->getMockBuilder(Helper::class)->onlyMethods(['getPlatform'])->getMock(); + $helper->expects($this->once())->method("getPlatform") ->willReturn(Helper::WINDOWS); $this->assertEquals("NUL", $helper->getSystemNullDevice()); diff --git a/tests/JobbyTest.php b/tests/JobbyTest.php index 9891942..b9004e2 100644 --- a/tests/JobbyTest.php +++ b/tests/JobbyTest.php @@ -4,12 +4,13 @@ use Jobby\Helper; use Jobby\Jobby; -use Opis\Closure\SerializableClosure; +use Laravel\SerializableClosure\SerializableClosure; +use PHPUnit\Framework\TestCase; /** * @coversDefaultClass Jobby\Jobby */ -class JobbyTest extends \PHPUnit_Framework_TestCase +class JobbyTest extends TestCase { /** * @var string @@ -24,7 +25,7 @@ class JobbyTest extends \PHPUnit_Framework_TestCase /** * {@inheritdoc} */ - protected function setUp() + protected function setUp(): void { $this->logFile = __DIR__ . '/_files/JobbyTest.log'; if (file_exists($this->logFile)) { @@ -37,7 +38,7 @@ protected function setUp() /** * {@inheritdoc} */ - protected function tearDown() + protected function tearDown(): void { if (file_exists($this->logFile)) { unlink($this->logFile); @@ -188,8 +189,8 @@ public function testShouldRunAllJobsAdded() // Job runs asynchronously, so wait a bit sleep($this->getSleepTime()); - $this->assertContains('job-1', $this->getLogContent()); - $this->assertContains('job-2', $this->getLogContent()); + $this->assertStringContainsString('job-1', $this->getLogContent()); + $this->assertStringContainsString('job-2', $this->getLogContent()); } /** @@ -284,6 +285,7 @@ public function testGetJobs() */ public function testExceptionOnMissingJobOptionCommand() { + $this->expectException(\Jobby\Exception::class); $jobby = new Jobby(); $jobby->add( @@ -300,6 +302,7 @@ public function testExceptionOnMissingJobOptionCommand() */ public function testExceptionOnMissingJobOptionSchedule() { + $this->expectException(\Jobby\Exception::class); $jobby = new Jobby(); $jobby->add( @@ -358,7 +361,7 @@ public function testShouldFailIfMaxRuntimeExceeded() $jobby->run(); sleep(2); - $this->assertContains('ERROR: MaxRuntime of 1 secs exceeded!', $this->getLogContent()); + $this->assertStringContainsString('ERROR: MaxRuntime of 1 secs exceeded!', $this->getLogContent()); } /** diff --git a/tests/ScheduleCheckerTest.php b/tests/ScheduleCheckerTest.php index 032386f..d7bf627 100644 --- a/tests/ScheduleCheckerTest.php +++ b/tests/ScheduleCheckerTest.php @@ -4,9 +4,9 @@ use DateTimeImmutable; use Jobby\ScheduleChecker; -use PHPUnit_Framework_TestCase; +use PHPUnit\Framework\TestCase; -class ScheduleCheckerTest extends PHPUnit_Framework_TestCase +class ScheduleCheckerTest extends TestCase { /** * @var ScheduleChecker @@ -16,7 +16,7 @@ class ScheduleCheckerTest extends PHPUnit_Framework_TestCase /** * @return void */ - protected function setUp() + protected function setUp(): void { parent::setUp(); diff --git a/tests/_files/helloworld.php b/tests/_files/helloworld.php index 82966e2..cff683b 100644 --- a/tests/_files/helloworld.php +++ b/tests/_files/helloworld.php @@ -1,3 +1,3 @@ Date: Mon, 1 May 2023 21:46:00 -0600 Subject: [PATCH 2/2] upgrade php to version 8.0 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index de59549..9a8c1ae 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ } ], "require": { - "php": ">=5.6", + "php": ">=8.0", "dragonmantank/cron-expression": "^3.0", "laravel/serializable-closure": "^1.3", "swiftmailer/swiftmailer": "^5.4|^6.0",