Merge pull request #17524 from mattwire/memberbaocreate
[civicrm-core.git] / tests / phpunit / CRM / Queue / Queue / SqlTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * Ensure that the extended interface for SQL-backed queues
14 * work. For example, the createItem() interface supports
15 * priority-queueing.
16 * @group headless
17 */
18 class CRM_Queue_Queue_SqlTest extends CiviUnitTestCase {
19
20 /* ----------------------- Queue providers ----------------------- */
21
22 /* Define a list of queue providers which should be tested */
23
24 /**
25 * Return a list of persistent and transient queue providers.
26 */
27 public function getQueueSpecs() {
28 $queueSpecs = [];
29 $queueSpecs[] = [
30 [
31 'type' => 'Sql',
32 'name' => 'test-queue',
33 ],
34 ];
35 return $queueSpecs;
36 }
37
38 /**
39 * Per-provider tests
40 *
41 */
42 public function setUp() {
43 parent::setUp();
44 $this->queueService = CRM_Queue_Service::singleton(TRUE);
45 }
46
47 public function tearDown() {
48 CRM_Utils_Time::resetTime();
49
50 $tablesToTruncate = ['civicrm_queue_item'];
51 $this->quickCleanup($tablesToTruncate);
52 }
53
54 /**
55 * Create a few queue items; alternately enqueue and dequeue various
56 *
57 * @dataProvider getQueueSpecs
58 * @param $queueSpec
59 */
60 public function testPriorities($queueSpec) {
61 $this->queue = $this->queueService->create($queueSpec);
62 $this->assertTrue($this->queue instanceof CRM_Queue_Queue);
63
64 $this->queue->createItem([
65 'test-key' => 'a',
66 ]);
67 $this->queue->createItem([
68 'test-key' => 'b',
69 ]);
70 $this->queue->createItem([
71 'test-key' => 'c',
72 ]);
73
74 $this->assertEquals(3, $this->queue->numberOfItems());
75 $item = $this->queue->claimItem();
76 $this->assertEquals('a', $item->data['test-key']);
77 $this->queue->deleteItem($item);
78
79 $this->assertEquals(2, $this->queue->numberOfItems());
80 $item = $this->queue->claimItem();
81 $this->assertEquals('b', $item->data['test-key']);
82 $this->queue->deleteItem($item);
83
84 $this->queue->createItem(
85 [
86 'test-key' => 'start',
87 ],
88 [
89 'weight' => -1,
90 ]
91 );
92 $this->queue->createItem(
93 [
94 'test-key' => 'end',
95 ],
96 [
97 'weight' => 1,
98 ]
99 );
100 $this->queue->createItem([
101 'test-key' => 'd',
102 ]);
103
104 $this->assertEquals(4, $this->queue->numberOfItems());
105 $item = $this->queue->claimItem();
106 $this->assertEquals('start', $item->data['test-key']);
107 $this->queue->deleteItem($item);
108
109 $this->assertEquals(3, $this->queue->numberOfItems());
110 $item = $this->queue->claimItem();
111 $this->assertEquals('c', $item->data['test-key']);
112 $this->queue->deleteItem($item);
113
114 $this->assertEquals(2, $this->queue->numberOfItems());
115 $item = $this->queue->claimItem();
116 $this->assertEquals('d', $item->data['test-key']);
117 $this->queue->deleteItem($item);
118
119 $this->assertEquals(1, $this->queue->numberOfItems());
120 $item = $this->queue->claimItem();
121 $this->assertEquals('end', $item->data['test-key']);
122 $this->queue->deleteItem($item);
123
124 $this->assertEquals(0, $this->queue->numberOfItems());
125 }
126
127 }