From: Tim Otten Date: Sat, 29 Jan 2022 06:35:30 +0000 (-0800) Subject: Civi::queue(string $name) - Add facade to load queue by name. Add test. X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=8b5f2c50270427b32b7e368310f7fc6c8d853c2e;p=civicrm-core.git Civi::queue(string $name) - Add facade to load queue by name. Add test. The test includes coverage for a couple flows - eg creating the queue via traditional `CRM_Queue_Service` or with the newer Civi\Api4\Queue. --- diff --git a/Civi.php b/Civi.php index 9e4b8dcbfd..d081572f99 100644 --- a/Civi.php +++ b/Civi.php @@ -105,6 +105,23 @@ class Civi { return \Civi\Core\Container::getBootService('paths'); } + /** + * Fetch a queue object. + * + * @param string $name + * Name of the queue. + * The queue must be persistent (stored in `civicrm_queue`). + * @return \CRM_Queue_Queue + * @see \CRM_Queue_Service + */ + public static function queue(string $name): CRM_Queue_Queue { + return CRM_Queue_Service::singleton()->create([ + 'name' => $name, + 'reset' => FALSE, + 'is_persistent' => TRUE, + ]); + } + /** * Obtain the formatting object. * diff --git a/tests/phpunit/CRM/Queue/QueueTest.php b/tests/phpunit/CRM/Queue/QueueTest.php index 8a9f25dcb9..c4b37644b3 100644 --- a/tests/phpunit/CRM/Queue/QueueTest.php +++ b/tests/phpunit/CRM/Queue/QueueTest.php @@ -207,6 +207,54 @@ class CRM_Queue_QueueTest extends CiviUnitTestCase { $this->assertEquals(0, $this->queue->numberOfItems()); } + /** + * Create a persistent queue via CRM_Queue_Service. Get a queue object with Civi::queue(). + * + * @dataProvider getQueueSpecs + * @param $queueSpec + */ + public function testPersistentUsage_service($queueSpec) { + $this->assertTrue(!empty($queueSpec['name'])); + $this->assertTrue(!empty($queueSpec['type'])); + + $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_queue'); + $q1 = CRM_Queue_Service::singleton()->create($queueSpec + [ + 'is_persistent' => TRUE, + ]); + $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_queue'); + + $q2 = Civi::queue($queueSpec['name']); + $this->assertInstanceOf('CRM_Queue_Queue_' . $queueSpec['type'], $q2); + $this->assertTrue($q1 === $q2); + } + + /** + * Create a persistent queue via APIv4. Get a queue object with Civi::queue(). + * + * @dataProvider getQueueSpecs + * @param $queueSpec + */ + public function testPersistentUsage_api4($queueSpec) { + $this->assertTrue(!empty($queueSpec['name'])); + $this->assertTrue(!empty($queueSpec['type'])); + + \Civi\Api4\Queue::create(0) + ->setValues($queueSpec) + ->execute(); + + $q1 = Civi::queue($queueSpec['name']); + $this->assertInstanceOf('CRM_Queue_Queue_' . $queueSpec['type'], $q1); + + if ($queueSpec['type'] !== 'Memory') { + CRM_Queue_Service::singleton(TRUE); + $q2 = CRM_Queue_Service::singleton()->load([ + 'name' => $queueSpec['name'], + 'is_persistent' => TRUE, + ]); + $this->assertInstanceOf('CRM_Queue_Queue_' . $queueSpec['type'], $q1); + } + } + /** * Test that queue content is reset when reset=>TRUE *