Merge pull request #13158 from elisseck/dev/core/544
[civicrm-core.git] / tests / phpunit / CRM / Queue / Queue / SqlTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * Ensure that the extended interface for SQL-backed queues
30 * work. For example, the createItem() interface supports
31 * priority-queueing.
32 * @group headless
33 */
34 class CRM_Queue_Queue_SqlTest extends CiviUnitTestCase {
35
36 /* ----------------------- Queue providers ----------------------- */
37
38 /* Define a list of queue providers which should be tested */
39
40 /**
41 * Return a list of persistent and transient queue providers.
42 */
43 public function getQueueSpecs() {
44 $queueSpecs = array();
45 $queueSpecs[] = array(
46 array(
47 'type' => 'Sql',
48 'name' => 'test-queue',
49 ),
50 );
51 return $queueSpecs;
52 }
53
54 /**
55 * Per-provider tests
56 *
57 */
58 public function setUp() {
59 parent::setUp();
60 $this->queueService = CRM_Queue_Service::singleton(TRUE);
61 }
62
63 public function tearDown() {
64 CRM_Utils_Time::resetTime();
65
66 $tablesToTruncate = array('civicrm_queue_item');
67 $this->quickCleanup($tablesToTruncate);
68 }
69
70 /**
71 * Create a few queue items; alternately enqueue and dequeue various
72 *
73 * @dataProvider getQueueSpecs
74 * @param $queueSpec
75 */
76 public function testPriorities($queueSpec) {
77 $this->queue = $this->queueService->create($queueSpec);
78 $this->assertTrue($this->queue instanceof CRM_Queue_Queue);
79
80 $this->queue->createItem(array(
81 'test-key' => 'a',
82 ));
83 $this->queue->createItem(array(
84 'test-key' => 'b',
85 ));
86 $this->queue->createItem(array(
87 'test-key' => 'c',
88 ));
89
90 $this->assertEquals(3, $this->queue->numberOfItems());
91 $item = $this->queue->claimItem();
92 $this->assertEquals('a', $item->data['test-key']);
93 $this->queue->deleteItem($item);
94
95 $this->assertEquals(2, $this->queue->numberOfItems());
96 $item = $this->queue->claimItem();
97 $this->assertEquals('b', $item->data['test-key']);
98 $this->queue->deleteItem($item);
99
100 $this->queue->createItem(
101 array(
102 'test-key' => 'start',
103 ),
104 array(
105 'weight' => -1,
106 )
107 );
108 $this->queue->createItem(
109 array(
110 'test-key' => 'end',
111 ),
112 array(
113 'weight' => 1,
114 )
115 );
116 $this->queue->createItem(array(
117 'test-key' => 'd',
118 ));
119
120 $this->assertEquals(4, $this->queue->numberOfItems());
121 $item = $this->queue->claimItem();
122 $this->assertEquals('start', $item->data['test-key']);
123 $this->queue->deleteItem($item);
124
125 $this->assertEquals(3, $this->queue->numberOfItems());
126 $item = $this->queue->claimItem();
127 $this->assertEquals('c', $item->data['test-key']);
128 $this->queue->deleteItem($item);
129
130 $this->assertEquals(2, $this->queue->numberOfItems());
131 $item = $this->queue->claimItem();
132 $this->assertEquals('d', $item->data['test-key']);
133 $this->queue->deleteItem($item);
134
135 $this->assertEquals(1, $this->queue->numberOfItems());
136 $item = $this->queue->claimItem();
137 $this->assertEquals('end', $item->data['test-key']);
138 $this->queue->deleteItem($item);
139
140 $this->assertEquals(0, $this->queue->numberOfItems());
141 }
142
143 }