From 5e335a8604920ebac6927c7d3d8d1e6aa55d6130 Mon Sep 17 00:00:00 2001 From: Eugene Ritter Date: Sat, 24 Oct 2020 13:43:53 -0500 Subject: [PATCH 1/2] Added end to end test using enqueue/fs. Test that queue will start and job will go through. Added enqueue/fs composer file. --- composer.json | 3 +- tests/TestCase/Command/WorkerCommandTest.php | 37 ++++++++++++++++++++ tests/app/TestApp/WelcomeMailer.php | 4 ++- 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 657ec47..1f7353f 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,8 @@ "require-dev": { "cakephp/cakephp-codesniffer": "^4.0", "phpunit/phpunit": "^8.5 || ^9.3", - "cakephp/bake": "^2.0" + "cakephp/bake": "^2.0", + "enqueue/fs": "^0.9" }, "autoload": { "psr-4": { diff --git a/tests/TestCase/Command/WorkerCommandTest.php b/tests/TestCase/Command/WorkerCommandTest.php index 008fce0..b5bfb35 100644 --- a/tests/TestCase/Command/WorkerCommandTest.php +++ b/tests/TestCase/Command/WorkerCommandTest.php @@ -18,8 +18,10 @@ use Cake\Core\Configure; use Cake\Log\Log; +use Cake\Queue\QueueManager; use Cake\TestSuite\ConsoleIntegrationTestTrait; use Cake\TestSuite\TestCase; +use TestApp\WelcomeMailer; use TestApp\WelcomeMailerListener; /** @@ -149,4 +151,39 @@ public function testQueueProcessesWithLogger() $this->assertNotEmpty($log->read()); $this->assertEquals($log->read()[0], 'debug Max Iterations: 0'); } + + /** + * Start up the worker queue, push a job, and see that it processes + * + * @runInSeparateProcess + */ + public function testQueueProcessesJob() + { + Configure::write([ + 'Queue' => [ + 'default' => [ + 'queue' => 'default', + 'url' => 'file:///' . TMP . DS . 'queue', + ], + ], + ]); + + Log::setConfig('debug', [ + 'className' => 'Array', + 'levels' => ['notice', 'info', 'debug'], + ]); + + $this->exec('worker --max-runtime=3 --logger=debug --verbose'); + + $callable = [WelcomeMailer::class, 'welcome']; + $arguments = []; + $options = ['config' => 'default']; + + QueueManager::push($callable, $arguments, $options); + + $log = Log::engine('debug'); + $this->assertIsArray($log->read()); + $this->assertNotEmpty($log->read()); + $this->assertContains('info Welcome mail sent', $log->read()); + } } diff --git a/tests/app/TestApp/WelcomeMailer.php b/tests/app/TestApp/WelcomeMailer.php index 740fae6..0169e07 100644 --- a/tests/app/TestApp/WelcomeMailer.php +++ b/tests/app/TestApp/WelcomeMailer.php @@ -3,6 +3,7 @@ namespace TestApp; +use Cake\Log\Log; use Cake\Mailer\Mailer; use Cake\Queue\Mailer\QueueTrait; @@ -16,6 +17,7 @@ public function getName() public function welcome() { - // Do nothing. + $debug = Log::engine('debug'); + $debug->info('Welcome mail sent'); } } From 7b22e24ff185ea0c7302ea47ac079b3925a795d7 Mon Sep 17 00:00:00 2001 From: Eugene Ritter Date: Sat, 24 Oct 2020 14:04:29 -0500 Subject: [PATCH 2/2] Update assertion for end to end test. --- tests/TestCase/Command/WorkerCommandTest.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/TestCase/Command/WorkerCommandTest.php b/tests/TestCase/Command/WorkerCommandTest.php index b5bfb35..92032dc 100644 --- a/tests/TestCase/Command/WorkerCommandTest.php +++ b/tests/TestCase/Command/WorkerCommandTest.php @@ -184,6 +184,10 @@ public function testQueueProcessesJob() $log = Log::engine('debug'); $this->assertIsArray($log->read()); $this->assertNotEmpty($log->read()); - $this->assertContains('info Welcome mail sent', $log->read()); + foreach ($log->read() as $line) { + if (stripos($line, 'Welcome mail sent') !== false) { + $this->assertTrue(true); + } + } } }