From 60260d3d7fcb5f0ae9679120daa02d60c7babf5f Mon Sep 17 00:00:00 2001 From: Heinz Wiesinger Date: Thu, 30 Dec 2021 12:43:01 +0100 Subject: [PATCH 1/3] Clean up unit test output - Use a mocked logger to remove output spam - Fix phpunit deprecation warnings --- test/Resque/Tests/EventTest.php | 15 +++++++++- test/Resque/Tests/JobPIDTest.php | 5 +++- test/Resque/Tests/JobStatusTest.php | 4 +-- test/Resque/Tests/JobTest.php | 22 ++++++++------- test/Resque/Tests/TestCase.php | 4 +++ test/Resque/Tests/WorkerTest.php | 44 ++++++++++++++--------------- 6 files changed, 58 insertions(+), 36 deletions(-) diff --git a/test/Resque/Tests/EventTest.php b/test/Resque/Tests/EventTest.php index 6e102cf..31c1041 100644 --- a/test/Resque/Tests/EventTest.php +++ b/test/Resque/Tests/EventTest.php @@ -14,9 +14,12 @@ public function setUp() { Test_Job::$called = false; + $this->logger = $this->getMockBuilder('Psr\Log\LoggerInterface') + ->getMock(); + // Register a worker to test with $this->worker = new Resque_Worker('jobs'); - $this->worker->setLogger(new Resque_Log()); + $this->worker->setLogger($this->logger); $this->worker->registerWorker(); } @@ -56,6 +59,15 @@ public function testEventCallbacksFire($event, $callback) Resque_Event::listen($event, array($this, $callback)); $job = $this->getEventTestJob(); + + $this->logger->expects($this->exactly(3)) + ->method('log') + ->withConsecutive( + [ 'notice', '{job} has finished', [ 'job' => $job ] ], + [ 'debug', 'Registered signals', [] ], + [ 'info', 'Checking {queue} for jobs', [ 'queue' => 'jobs' ] ] + ); + $this->worker->perform($job); $this->worker->work(0); @@ -72,6 +84,7 @@ public function testBeforeForkEventCallbackFires() 'somevar' )); $job = $this->getEventTestJob(); + $this->worker->work(0); $this->assertContains($callback, $this->callbacksHit, $event . ' callback (' . $callback .') was not called'); } diff --git a/test/Resque/Tests/JobPIDTest.php b/test/Resque/Tests/JobPIDTest.php index 2d4a93d..0213ed2 100644 --- a/test/Resque/Tests/JobPIDTest.php +++ b/test/Resque/Tests/JobPIDTest.php @@ -19,11 +19,14 @@ public function setUp() // Register a worker to test with $this->worker = new Resque_Worker('jobs'); - $this->worker->setLogger(new Resque_Log()); + $this->worker->setLogger($this->logger); } public function testQueuedJobDoesNotReturnPID() { + $this->logger->expects($this->never()) + ->method('log'); + $token = Resque::enqueue('jobs', 'Test_Job', null, true); $this->assertEquals(0, Resque_Job_PID::get($token)); } diff --git a/test/Resque/Tests/JobStatusTest.php b/test/Resque/Tests/JobStatusTest.php index 8be7753..aafc9b1 100644 --- a/test/Resque/Tests/JobStatusTest.php +++ b/test/Resque/Tests/JobStatusTest.php @@ -19,7 +19,7 @@ public function setUp() // Register a worker to test with $this->worker = new Resque_Worker('jobs'); - $this->worker->setLogger(new Resque_Log()); + $this->worker->setLogger($this->logger); } public function testJobStatusCanBeTracked() @@ -103,4 +103,4 @@ public function testRecreatedJobWithTrackingStillTracksStatus() $newJob = Resque_Job::reserve('jobs'); $this->assertEquals(Resque_Job_Status::STATUS_WAITING, $newJob->getStatus()); } -} \ No newline at end of file +} diff --git a/test/Resque/Tests/JobTest.php b/test/Resque/Tests/JobTest.php index 7ff6d8d..bf22150 100644 --- a/test/Resque/Tests/JobTest.php +++ b/test/Resque/Tests/JobTest.php @@ -17,7 +17,7 @@ public function setUp() // Register a worker to test with $this->worker = new Resque_Worker('jobs'); - $this->worker->setLogger(new Resque_Log()); + $this->worker->setLogger($this->logger); $this->worker->registerWorker(); } @@ -153,7 +153,7 @@ public function testInvalidJobThrowsException() $job->worker = $this->worker; $job->perform(); } - + public function testJobWithSetUpCallbackFiresSetUp() { $payload = array( @@ -165,10 +165,10 @@ public function testJobWithSetUpCallbackFiresSetUp() ); $job = new Resque_Job('jobs', $payload); $job->perform(); - + $this->assertTrue(Test_Job_With_SetUp::$called); } - + public function testJobWithTearDownCallbackFiresTearDown() { $payload = array( @@ -180,7 +180,7 @@ public function testJobWithTearDownCallbackFiresTearDown() ); $job = new Resque_Job('jobs', $payload); $job->perform(); - + $this->assertTrue(Test_Job_With_TearDown::$called); } @@ -329,7 +329,7 @@ public function testDequeueItemWithArg() $this->assertEquals(Resque::dequeue($queue, $test), 1); #$this->assertEquals(Resque::size($queue), 1); } - + public function testDequeueSeveralItemsWithArgs() { // GIVEN @@ -340,11 +340,11 @@ public function testDequeueSeveralItemsWithArgs() Resque::enqueue($queue, 'Test_Job_Dequeue9', $removeArgs); Resque::enqueue($queue, 'Test_Job_Dequeue9', $removeArgs); $this->assertEquals(Resque::size($queue), 3); - + // WHEN $test = array('Test_Job_Dequeue9' => $removeArgs); $removedItems = Resque::dequeue($queue, $test); - + // THEN $this->assertEquals($removedItems, 2); $this->assertEquals(Resque::size($queue), 1); @@ -410,8 +410,10 @@ public function testDoNotUseFactoryToGetInstance() 'args' => array(array()) ); $job = new Resque_Job('jobs', $payload); - $factory = $this->getMock('Resque_Job_FactoryInterface'); - $testJob = $this->getMock('Resque_JobInterface'); + $factory = $this->getMockBuilder('Resque_Job_FactoryInterface') + ->getMock(); + $testJob = $this->getMockBuilder('Resque_JobInterface') + ->getMock(); $factory->expects(self::never())->method('create')->will(self::returnValue($testJob)); $instance = $job->getInstance(); $this->assertInstanceOf('Resque_JobInterface', $instance); diff --git a/test/Resque/Tests/TestCase.php b/test/Resque/Tests/TestCase.php index a97f64b..1acc1b1 100644 --- a/test/Resque/Tests/TestCase.php +++ b/test/Resque/Tests/TestCase.php @@ -10,6 +10,7 @@ class Resque_Tests_TestCase extends PHPUnit_Framework_TestCase { protected $resque; protected $redis; + protected $logger; public static function setUpBeforeClass() { @@ -22,6 +23,9 @@ public function setUp() preg_match('#^\s*port\s+([0-9]+)#m', $config, $matches); $this->redis = new Credis_Client('localhost', $matches[1]); + $this->logger = $this->getMockBuilder('Psr\Log\LoggerInterface') + ->getMock(); + Resque::setBackend('redis://localhost:' . $matches[1]); // Flush redis diff --git a/test/Resque/Tests/WorkerTest.php b/test/Resque/Tests/WorkerTest.php index 938ee24..24378a0 100644 --- a/test/Resque/Tests/WorkerTest.php +++ b/test/Resque/Tests/WorkerTest.php @@ -11,7 +11,7 @@ class Resque_Tests_WorkerTest extends Resque_Tests_TestCase public function testWorkerRegistersInList() { $worker = new Resque_Worker('*'); - $worker->setLogger(new Resque_Log()); + $worker->setLogger($this->logger); $worker->registerWorker(); // Make sure the worker is in the list @@ -24,7 +24,7 @@ public function testGetAllWorkers() // Register a few workers for($i = 0; $i < $num; ++$i) { $worker = new Resque_Worker('queue_' . $i); - $worker->setLogger(new Resque_Log()); + $worker->setLogger($this->logger); $worker->registerWorker(); } @@ -35,7 +35,7 @@ public function testGetAllWorkers() public function testGetWorkerById() { $worker = new Resque_Worker('*'); - $worker->setLogger(new Resque_Log()); + $worker->setLogger($this->logger); $worker->registerWorker(); $newWorker = Resque_Worker::find((string)$worker); @@ -50,7 +50,7 @@ public function testInvalidWorkerDoesNotExist() public function testWorkerCanUnregister() { $worker = new Resque_Worker('*'); - $worker->setLogger(new Resque_Log()); + $worker->setLogger($this->logger); $worker->registerWorker(); $worker->unregisterWorker(); @@ -62,7 +62,7 @@ public function testWorkerCanUnregister() public function testPausedWorkerDoesNotPickUpJobs() { $worker = new Resque_Worker('*'); - $worker->setLogger(new Resque_Log()); + $worker->setLogger($this->logger); $worker->pauseProcessing(); Resque::enqueue('jobs', 'Test_Job'); $worker->work(0); @@ -73,7 +73,7 @@ public function testPausedWorkerDoesNotPickUpJobs() public function testResumedWorkerPicksUpJobs() { $worker = new Resque_Worker('*'); - $worker->setLogger(new Resque_Log()); + $worker->setLogger($this->logger); $worker->pauseProcessing(); Resque::enqueue('jobs', 'Test_Job'); $worker->work(0); @@ -89,7 +89,7 @@ public function testWorkerCanWorkOverMultipleQueues() 'queue1', 'queue2' )); - $worker->setLogger(new Resque_Log()); + $worker->setLogger($this->logger); $worker->registerWorker(); Resque::enqueue('queue1', 'Test_Job_1'); Resque::enqueue('queue2', 'Test_Job_2'); @@ -108,7 +108,7 @@ public function testWorkerWorksQueuesInSpecifiedOrder() 'medium', 'low' )); - $worker->setLogger(new Resque_Log()); + $worker->setLogger($this->logger); $worker->registerWorker(); // Queue the jobs in a different order @@ -130,7 +130,7 @@ public function testWorkerWorksQueuesInSpecifiedOrder() public function testWildcardQueueWorkerWorksAllQueues() { $worker = new Resque_Worker('*'); - $worker->setLogger(new Resque_Log()); + $worker->setLogger($this->logger); $worker->registerWorker(); Resque::enqueue('queue1', 'Test_Job_1'); @@ -146,7 +146,7 @@ public function testWildcardQueueWorkerWorksAllQueues() public function testWorkerDoesNotWorkOnUnknownQueues() { $worker = new Resque_Worker('queue1'); - $worker->setLogger(new Resque_Log()); + $worker->setLogger($this->logger); $worker->registerWorker(); Resque::enqueue('queue2', 'Test_Job'); @@ -157,7 +157,7 @@ public function testWorkerClearsItsStatusWhenNotWorking() { Resque::enqueue('jobs', 'Test_Job'); $worker = new Resque_Worker('jobs'); - $worker->setLogger(new Resque_Log()); + $worker->setLogger($this->logger); $job = $worker->reserve(); $worker->workingOn($job); $worker->doneWorking(); @@ -167,7 +167,7 @@ public function testWorkerClearsItsStatusWhenNotWorking() public function testWorkerRecordsWhatItIsWorkingOn() { $worker = new Resque_Worker('jobs'); - $worker->setLogger(new Resque_Log()); + $worker->setLogger($this->logger); $worker->registerWorker(); $payload = array( @@ -190,7 +190,7 @@ public function testWorkerErasesItsStatsWhenShutdown() Resque::enqueue('jobs', 'Invalid_Job'); $worker = new Resque_Worker('jobs'); - $worker->setLogger(new Resque_Log()); + $worker->setLogger($this->logger); $worker->work(0); $worker->work(0); @@ -202,18 +202,18 @@ public function testWorkerCleansUpDeadWorkersOnStartup() { // Register a good worker $goodWorker = new Resque_Worker('jobs'); - $goodWorker->setLogger(new Resque_Log()); + $goodWorker->setLogger($this->logger); $goodWorker->registerWorker(); $workerId = explode(':', $goodWorker); // Register some bad workers $worker = new Resque_Worker('jobs'); - $worker->setLogger(new Resque_Log()); + $worker->setLogger($this->logger); $worker->setId($workerId[0].':1:jobs'); $worker->registerWorker(); $worker = new Resque_Worker(array('high', 'low')); - $worker->setLogger(new Resque_Log()); + $worker->setLogger($this->logger); $worker->setId($workerId[0].':2:high,low'); $worker->registerWorker(); @@ -229,14 +229,14 @@ public function testDeadWorkerCleanUpDoesNotCleanUnknownWorkers() { // Register a bad worker on this machine $worker = new Resque_Worker('jobs'); - $worker->setLogger(new Resque_Log()); + $worker->setLogger($this->logger); $workerId = explode(':', $worker); $worker->setId($workerId[0].':1:jobs'); $worker->registerWorker(); // Register some other false workers $worker = new Resque_Worker('jobs'); - $worker->setLogger(new Resque_Log()); + $worker->setLogger($this->logger); $worker->setId('my.other.host:1:jobs'); $worker->registerWorker(); @@ -253,7 +253,7 @@ public function testDeadWorkerCleanUpDoesNotCleanUnknownWorkers() public function testWorkerFailsUncompletedJobsOnExit() { $worker = new Resque_Worker('jobs'); - $worker->setLogger(new Resque_Log()); + $worker->setLogger($this->logger); $worker->registerWorker(); $payload = array( @@ -270,7 +270,7 @@ public function testWorkerFailsUncompletedJobsOnExit() public function testBlockingListPop() { $worker = new Resque_Worker('jobs'); - $worker->setLogger(new Resque_Log()); + $worker->setLogger($this->logger); $worker->registerWorker(); Resque::enqueue('jobs', 'Test_Job_1'); @@ -296,9 +296,9 @@ public function testWorkerFailsSegmentationFaultJob() Resque::enqueue('jobs', 'Test_Infinite_Recursion_Job'); $worker = new Resque_Worker('jobs'); - $worker->setLogger(new Resque_Log()); + $worker->setLogger($this->logger); $worker->work(0); $this->assertEquals(1, Resque_Stat::get('failed')); } -} \ No newline at end of file +} From 3113a0ce8459f83869f85cea25a30433e37c1a1d Mon Sep 17 00:00:00 2001 From: Heinz Wiesinger Date: Thu, 30 Dec 2021 13:32:07 +0100 Subject: [PATCH 2/3] Migrate phpunit config file to newer format --- phpunit.xml.dist | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 61d2d7b..f030264 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -8,17 +8,17 @@ convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" - syntaxCheck="false" > + ./test/Resque/ - - + + ./lib/Resque/ - - - \ No newline at end of file + + + From 3bc9e6f354babb8134d0a11ff6cd4d8d79b92097 Mon Sep 17 00:00:00 2001 From: Heinz Wiesinger Date: Thu, 30 Dec 2021 13:33:26 +0100 Subject: [PATCH 3/3] Use phpunit 5.x for github actions The default is phpunit 9.x and that doesn't find any tests. --- .github/workflows/php-test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/php-test.yml b/.github/workflows/php-test.yml index d1f6f0d..f28503c 100644 --- a/.github/workflows/php-test.yml +++ b/.github/workflows/php-test.yml @@ -21,7 +21,7 @@ jobs: uses: shivammathur/setup-php@v2 with: php-version: ${{ matrix.php-versions }} - tools: phpunit + tools: phpunit:5 extensions: redis - name: Setup problem matchers for PHP